/*
* Name: ReplaceRegExpDirective
* Author: Richard Rodger
*
* Copyright (c) 2002-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.ReplaceRegExpTransform;
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 using regular expressions.
* Usage:<br />
* <% @replace from-regexp to-regexp %>
* <p>The regular expression syntax is determined by the <code>jostraca.RegExpProvider</code> setting</p>
*/
public class ReplaceRegExpDirective 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-regexp";
private static final String[] ALIASES = new String[] { NAME, "replace-re", "repre" };
private static final Hashtable sCanonicalArgs = new Hashtable();
private static final Vector sCanonicalOrder = new Vector();
// private instance
private ReplaceRegExpTransform iReplaceRegExpTransform = new ReplaceRegExpTransform();
static {
sCanonicalOrder.addElement( ARG_from );
sCanonicalOrder.addElement( ARG_to );
sCanonicalArgs.put( ARG_from, Standard.EMPTY );
sCanonicalArgs.put( ARG_to, Standard.EMPTY );
}
// public methods
public ReplaceRegExpDirective() throws DirectiveException {
// do nothing
}
// interface Directive
public void perform( String pDirectiveName,
String pArguments,
BasicUnitList pBasicUnitList,
TemplateActionHandler pTemplateActionHandler,
PropertySet pPropertySet,
TextualTransformManagerTable pTextualTransformManagerTable
)
throws DirectiveException
{
try {
//REVIEW: this should not be done here!
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( iReplaceRegExpTransform ) ) {
textTTM.append( iReplaceRegExpTransform );
}
// REVIEW: should we do this once or every time?
iReplaceRegExpTransform.setParameters( pPropertySet );
iReplaceRegExpTransform.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;
}
}