Package org.jostraca

Source Code of org.jostraca.BasicExpressionElementProcessor

/*
* Name:    BasicExpressionElementProcessor
* Authors: Richard Rodger
*
* Copyright (c) 2001-2004 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;


// import
import org.jostraca.unit.UnitList;
import org.jostraca.unit.BasicUnit;
import org.jostraca.unit.BasicUnitList;
import org.jostraca.unit.BasicUnitProcessor;

import org.jostraca.transform.TextualTransform;
import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.transform.TransformException;

import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;
import org.jostraca.util.RegExp;


/** Process a template expression element.
*/
public class BasicExpressionElementProcessor implements BasicGenericElementProcessor {   

  // private instance
  private BasicUnitList                iUnitList;
  private TemplateActionHandler        iTemplateActionHandler;
  private PropertySet                  iPropertySet;         
  private TextualTransformManagerTable iTextualTransformManagerTable;    
  private TextualTransform             iTextualTransform;    

  private String iExpressionMarker  = Standard.EMPTY;

  private String  iRegExpText_IsExpression    = Standard.EMPTY;
  private RegExp  iRegExpIsExpression         = null;
  private boolean iPerformIsExpression        = false;
  private String  iRegExpText_MatchExpression = Standard.EMPTY;
  private RegExp  iRegExpMatchExpression      = null;
  private boolean iPerformMatchExpression     = false;




  // constructors
 
  /* Constructor.
   *  @param pUnitList                      list of template units
   *  @param pTemplateActionHandler         send actions based on content here
   *  @param pPropertySet                   PropertySet to use
   *  @param pTextualTransformManagerTable  transforms to apply to content
   */
  public BasicExpressionElementProcessor( BasicUnitList                pUnitList,
                                          TemplateActionHandler        pTemplateActionHandler,
                                          PropertySet                  pPropertySet,
                                          TextualTransformManagerTable pTextualTransformManagerTable )
  {
    iUnitList                     = pUnitList;
    iTemplateActionHandler        = pTemplateActionHandler;
    iPropertySet                  = pPropertySet;         
    iTextualTransformManagerTable = pTextualTransformManagerTable;
    iTextualTransform             = pTextualTransformManagerTable.getExpressionTTM();    

    iExpressionMarker = iPropertySet.get( Property.parse_ExpressionMarker );

    setupRegExps( pPropertySet );
  }



  // interface BasicGenericElementProcessor

  public boolean isMatch( Block pBlock ) throws TemplateElementProcessorException {
    return ( pBlock.isScript() && isExpression( pBlock.getContent() ) );
  }
 


  public boolean process( Block pBlock ) throws TemplateElementProcessorException {
    String content = pBlock.getContent();
    String match   = Standard.EMPTY;

    // parse expression element using regular expression
    try {
      match = doRegExpMatchExpression( content );
      String    section = iTemplateActionHandler.getDefaultSection();
      BasicUnit bu      = new BasicUnit( BasicUnitProcessor.TYPE_expr, section, match );
      iUnitList.add( bu );
    }
    catch( Exception e ) {
      throw new TemplateElementProcessorException
        ( TemplateElementProcessorException.CODE_unexpected_regexp_mismatch, e.getMessage(),
          new ValueSet( ValueCode.REGEXP, iRegExpText_MatchExpression,
                        ValueCode.CONTENT, content ) );
    }

    iTemplateActionHandler.setLastElementType( ELEMENT_TYPE_expression );
   
    // old parser
    if( iPropertySet.isYes( Property.jostraca_old ) ) {
      try {
        match = iTextualTransform.transform( match );
        iTemplateActionHandler.append( match );
      }
      catch( TransformException te ) {
        throw new TemplateElementProcessorException( TemplateElementProcessorException.CODE_transform_error, te.getMessage() );
      }
    }

    return true;
  }



  public String getContent() {
    return Standard.EMPTY;
  }

  public UnitList getUnitList() {
    return iUnitList;
  }




  // private methods

  /** Create regular expressions from properties. */
  private void setupRegExps( PropertySet pPropertySet ) throws TemplateElementProcessorException {

    try {
      if( ! Standard.EMPTY.equals( iExpressionMarker ) ) {
        iRegExpText_IsExpression = pPropertySet.get( Property.jostraca_regexp_IsExpression );
        iRegExpIsExpression      = RegExp.make( iRegExpText_IsExpression, new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
        iPerformIsExpression     = true;
      }
    }
    catch( Exception e ) {
      throw new TemplateElementProcessorException
        ( TemplateElementProcessorException.CODE_bad_regexp, e.getMessage(),
          new ValueSet( ValueCode.REGEXP, iRegExpText_IsExpression ) );
    }

    try {
      if( ! Standard.EMPTY.equals( iExpressionMarker ) ) {
        iRegExpText_MatchExpression = pPropertySet.get( Property.jostraca_regexp_MatchExpression );
        iRegExpMatchExpression      = RegExp.make( iRegExpText_MatchExpression, new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
        iPerformMatchExpression     = true;
      }
    }
    catch( Exception e ) {
      throw new TemplateElementProcessorException
        ( TemplateElementProcessorException.CODE_bad_regexp, e.getMessage(),
          new ValueSet( ValueCode.REGEXP, iRegExpText_MatchExpression ) );
    }
  }



  /** match expression */
  private boolean isExpression( String pContent ) throws TemplateElementProcessorException {
    return ! Standard.EMPTY.equals( doRegExpIsExpression( pContent ) );
  }



  /** perform reg exp */
  private String doRegExpIsExpression( String pContent ) throws TemplateElementProcessorException {
    try {
      String match = Standard.EMPTY;

      if( iPerformIsExpression ) {
        match = iRegExpIsExpression.match( pContent );
      }

      return match;
    }
    catch( Exception e ) {
      throw new TemplateElementProcessorException
        ( TemplateElementProcessorException.CODE_unexpected_regexp_mismatch, e.getMessage(),
          new ValueSet( ValueCode.REGEXP, iRegExpText_IsExpression,
                        ValueCode.CONTENT, pContent ) );
    }
  }


  /** perform reg exp */
  private String doRegExpMatchExpression( String pContent ) throws TemplateElementProcessorException {
    try {
      String match = Standard.EMPTY;

      if( iPerformMatchExpression ) {
        match = iRegExpMatchExpression.matchFirstSub( pContent );
      }

      return match;
    }
    catch( Exception e ) {
      throw new TemplateElementProcessorException
        ( TemplateElementProcessorException.CODE_unexpected_regexp_mismatch, e.getMessage(),
          new ValueSet( ValueCode.REGEXP, iRegExpText_MatchExpression,
                        ValueCode.CONTENT, pContent ) );
    }
  }

}
TOP

Related Classes of org.jostraca.BasicExpressionElementProcessor

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.