Package org.jostraca.transform

Source Code of org.jostraca.transform.RemoveExtraWhiteSpace

/*
* Name:    RemoveExtraWhiteSpace
* Author:  Richard Rodger
* Copyright (C) 2000-2005 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/


// package
package org.jostraca.transform;


// import
import org.jostraca.util.ErrorUtil;
import org.jostraca.util.Standard;
import org.jostraca.util.PropertySet;
import org.jostraca.util.RegExp;
import org.jostraca.util.RegExpMatch;
import org.jostraca.util.RegExpException;


/** remove surrounding whitespace from template text
*
*  WARNING: does not work, should be renamed to RemoveExtraWhiteSpaceTransform
*/
public class RemoveExtraWhiteSpace extends TextualTransformSupport {

  // private static
  private static final String REGEXP_RemoveExtraWhiteSpacePrefix        = "^\\n[ \\t]*(.*?)$";
  private static final int    REGEXP_RemoveExtraWhiteSpacePrefix_remain = 1;
  private static final String REGEXP_RemoveExtraWhiteSpaceSuffix        = "^(.*?)\\n[ \\t]*$";
  private static final int    REGEXP_RemoveExtraWhiteSpaceSuffix_remain = 1;
  private static RegExp sRemoveExtraWhiteSpacePrefixRegExp = null;
  private static RegExp sRemoveExtraWhiteSpaceSuffixRegExp = null;


  // constructors

  static {
    try {
      sRemoveExtraWhiteSpacePrefixRegExp    = RegExp.make(REGEXP_RemoveExtraWhiteSpacePrefix, RegExp.ModeSet.DotMatchesNewline);
    }
    catch(Exception e) {
      sRemoveExtraWhiteSpacePrefixRegExp    = null; // cause failure
    }

    try {
      sRemoveExtraWhiteSpaceSuffixRegExp    = RegExp.make(REGEXP_RemoveExtraWhiteSpaceSuffix, RegExp.ModeSet.DotMatchesNewline);
    }
    catch(Exception e) {
      sRemoveExtraWhiteSpaceSuffixRegExp    = null; // cause failure
    }
  }



  // interface TextualTransform

  /** Transform the supplied String.
   *  @param pToTransform String to transform.
   */
  public String transform( String pToTransform ) {
    if( ErrorUtil.is_null( pToTransform, "pToTransform" ) ) {
      return Standard.EMPTY;
    }

    try {
      return removeExtraWhiteSpace( pToTransform );
    }
    catch( RegExpException e ) {
      ErrorUtil.nonFatalException( e );
      return pToTransform;
    }
  }


  /** @see org.jostraca.transform.TextualTransform */
  public void setParameters( PropertySet pPropertySet ) {
    // not used
  }



  // private methods

  /** Remove extra white space
   */
  private String removeExtraWhiteSpace( String pText ) throws RegExpException {
    String newText = pText;

    RegExpMatch prefix = sRemoveExtraWhiteSpacePrefixRegExp.matchFirst( newText );
    if( prefix.hasMatch() ) {
      newText = prefix.matchFirstSub();
    }

    RegExpMatch suffix = sRemoveExtraWhiteSpaceSuffixRegExp.matchFirst( newText );
    if( suffix.hasMatch() ) {
      newText = suffix.matchFirstSub();
    }

    return newText;
  }

}
TOP

Related Classes of org.jostraca.transform.RemoveExtraWhiteSpace

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.