Package org.jostraca.directive

Source Code of org.jostraca.directive.ReplaceDirective

/*
* Name:    ReplaceDirective
* Author:  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.directive;


// import
import org.jostraca.TemplateActionHandler;

import org.jostraca.transform.ReplaceTransform;
import org.jostraca.transform.TransformException;

import org.jostraca.unit.BasicUnitList;

import org.jostraca.util.Standard;
import org.jostraca.util.PropertySet;
import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.transform.TextualTransformManager;

import java.util.Vector;
import java.util.Hashtable;


/** Replace text in template with substitute text.
*  Usage:<br />
*  &nbsp;&nbsp;&lt;% @replace from-text to-text %&gt;
*/
public class ReplaceDirective extends DirectiveSupport {

  // public static
  public static final String ARG_from = "from";
  public static final String ARG_to   = "to";



 
  // private static
  private static final String    NAME    = "replace";
  private static final String[]  ALIASES = new String[] { NAME, "rep" };

  private static final Hashtable sCanonicalArgs  = new Hashtable();
  private static final Vector    sCanonicalOrder = new Vector();




  // private instance
  private ReplaceTransform iReplaceTransform = new ReplaceTransform();



  // static init
  static {
    sCanonicalOrder.addElement( ARG_from );
    sCanonicalOrder.addElement( ARG_to   );

    sCanonicalArgs.put( ARG_from, Standard.EMPTY );
    sCanonicalArgs.put( ARG_to,   Standard.EMPTY );
  }




  // public methods

  public ReplaceDirective() throws DirectiveException {
    // do nothing
  }



  // interface Directive

  public void perform( String                       pDirectiveName,
                       String                       pArguments,

                       BasicUnitList                pBasicUnitList,
                       TemplateActionHandler        pTemplateActionHandler,

                       PropertySet                  pPropertySet,
                       TextualTransformManagerTable pTextualTransformManagerTable
                       )
    throws DirectiveException
  {

    try {
      String[] nameSpec = iDirectiveParser.getNameElements( pDirectiveName );
      String   modifier = ( 1 < nameSpec.length ? nameSpec[1] : Standard.EMPTY );

      iDirectiveParser.parse( pArguments );
      Hashtable args = iDirectiveParser.getCanonicalArgs( sCanonicalOrder, sCanonicalArgs );

      String from = (String) args.get( ARG_from );
      String to   = (String) args.get( ARG_to   );

      // REVIEW: find a better way to do this
      pPropertySet.set( "directive.replace.from.tmp", from );
      from = pPropertySet.get( "directive.replace.from.tmp" );

      pPropertySet.set( "directive.replace.to.tmp", to );
      to = pPropertySet.get( "directive.replace.to.tmp" );

      TextualTransformManager textTTM = pTextualTransformManagerTable.getTextTTM();
      if( ! textTTM.contains( iReplaceTransform ) ) {
        textTTM.append( iReplaceTransform );
      }

      // REVIEW: should we do this once or every time?
      iReplaceTransform.setParameters( pPropertySet );
      iReplaceTransform.addReplacement( from, to, modifier );

    }
    catch( TransformException te ) {
      throw new DirectiveException( DirectiveException.CODE_transform, te.getMessage() );
    }
    catch( DirectiveException de ) {
      throw de;
    }
    catch( Exception e ) {
      throw new DirectiveException( e.getMessage() );
    }

  }


  public String getName() {
    return NAME;
  }

  /** Should include value of getName() */
  public String[] getAliases() {
    return ALIASES;
  }

}
TOP

Related Classes of org.jostraca.directive.ReplaceDirective

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.