Package org.jostraca

Source Code of org.jostraca.BasicTextElementProcessor

/*
* Name:    BasicTextElementProcessor
* Authors: Richard Rodger
* Release: 0.3
*
* Copyright (c) 2001-2003 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.BasicUnitList;
import org.jostraca.unit.BasicUnit;
import org.jostraca.unit.BasicUnitProcessor;

import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.transform.TextualTransformManager;
import org.jostraca.transform.TransformException;
import org.jostraca.transform.RemoveFirstBlankLineTransform;

import org.jostraca.util.PropertySet;
/* Import >> */


/** <b>Description:</b><br>
*  Process a template text element.
*/
public class BasicTextElementProcessor implements BasicGenericElementProcessor {   

  /* Private Instance << */
  private BasicUnitList                iUnitList;
  private TemplateActionHandler        iTemplateActionHandler;
  private PropertySet                  iPropertySet;         
  private TextualTransformManagerTable iTextualTransformManagerTable;    
  private TextualTransformManager      iInsertTransformManager;    
  private TextualTransformManager      iTextTransformManager;    
  private String                       iContent;
  /* Private Instance >> */




  /* Public Methods << */
 
  /* Constructor.
   *  @param pTemplateActionHandler Send actions based on content here.
   *  @param pPropertySet           PropertySet to use.
   *  @param pTextualTransform      Transform to apply to content.
   */
  public BasicTextElementProcessor( BasicUnitList                pUnitList,
                                    TemplateActionHandler        pTemplateActionHandler,
                                    PropertySet                  pPropertySet,
                                    TextualTransformManagerTable pTextualTransformManagerTable )
  {
    iUnitList                     = pUnitList;
    iTemplateActionHandler        = pTemplateActionHandler;
    iPropertySet                  = pPropertySet;         
    iTextualTransformManagerTable = pTextualTransformManagerTable;    
    iTextTransformManager         = pTextualTransformManagerTable.getTextTTM();
    iInsertTransformManager       = pTextualTransformManagerTable.getInsertTTM();
  }



  /** @see BasicGenericElementProcessor */
  public boolean isMatch( Block pBlock ) throws TemplateElementProcessorException {    
    return pBlock.isText();
  }
 

  /** @see BasicGenericElementProcessor
   *  A text block is translated to a call to the _insert method of the code writer.
   */
  public boolean process( Block pBlock ) throws TemplateElementProcessorException {
    boolean finished = true;
    String  content  = pBlock.getContent();

    try {
      String text_content = iTextTransformManager.transform( content );
      iContent = text_content;

      // REVIEW: do we need to do this?
      String openScriptElement = iPropertySet.get( "o" );
      if( -1 == iContent.indexOf( openScriptElement ) ) {

        if( TemplateElementProcessor.ELEMENT_TYPE_expression == iTemplateActionHandler.getLastElementType() ) {
          iInsertTransformManager.deactivate( RemoveFirstBlankLineTransform.NAME );
        }

        iContent = iInsertTransformManager.transform( iContent );

        iInsertTransformManager.activate( RemoveFirstBlankLineTransform.NAME );

        if( iInsertTransformManager.needsReparse() ) {
          finished = false;
        }
        else {
          iTemplateActionHandler.append( iContent );

          String    section = iTemplateActionHandler.getDefaultSection();
          BasicUnit bu      = new BasicUnit( BasicUnitProcessor.TYPE_text, section, text_content );
          iUnitList.add( bu );
        }
      }
      else {
        finished = false;
      }

      iTemplateActionHandler.setLastElementType( ELEMENT_TYPE_text );
      return finished;
    }
    catch( TransformException te ) {
      throw new TemplateElementProcessorException( TemplateElementProcessorException.CODE_transform_error, te.getMessage() );
    }
  }


  public String getContent() {
    return iContent;
  }


  public UnitList getUnitList() {
    return iUnitList;
  }

  /* Public Methods >> */

TOP

Related Classes of org.jostraca.BasicTextElementProcessor

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.