/*
* Name: InitDirective
* 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.Template;
import org.jostraca.TemplateActionHandler;
import org.jostraca.Property;
import org.jostraca.unit.BasicUnit;
import org.jostraca.unit.BasicUnitList;
import org.jostraca.unit.BasicUnitProcessor;
import org.jostraca.section.Section;
import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;
import org.jostraca.util.Standard;
import org.jostraca.util.PropertySet;
import org.jostraca.util.RegExp;
import org.jostraca.util.RegExpException;
/** For backwards compatibility with version 0.1.0.
* This directive is deprecated.
*/
public class InitDirective implements Directive {
// private static
private static final String NAME = "init";
private static final String[] ALIASES = new String[] { NAME };
private static final String REGEXP_MatchArguments = "^(.*)$";
private String iRegExpText_MatchArguments = Standard.EMPTY;
private RegExp iRegExpMatchArguments = null;
// public methods
public InitDirective() throws DirectiveException {
setupRegExps();
}
// interface Directive
public String perform(
String pDirectiveName,
String pArguments,
BasicUnitList pBasicUnitList,
TemplateActionHandler pTemplateActionHandler,
PropertySet pPropertySet,
TextualTransformManagerTable pTextualTransformManagerTable,
Template pTemplate
)
throws DirectiveException
{
String match = doRegExpMatchArguments( pArguments );
if( !Standard.EMPTY.equals( match ) ) {
if( pPropertySet.isYes( Property.jostraca_old ) ) {
pTemplateActionHandler.append( Section.NAME_init, match );
}
BasicUnit bu = new BasicUnit( BasicUnitProcessor.TYPE_script, Section.NAME_init, match );
pBasicUnitList.add( bu );
}
return null;
}
public String getName() {
return NAME;
}
/** Should include value of getName() */
public String[] getAliases() {
return ALIASES;
}
// private methods
/** Create regular expressions from properties. */
private void setupRegExps() throws DirectiveException {
// MatchArguments
try {
iRegExpText_MatchArguments = REGEXP_MatchArguments;
iRegExpMatchArguments = RegExp.make( iRegExpText_MatchArguments, RegExp.ModeSet.DotMatchesNewline );
}
catch( RegExpException e ) {
throw new DirectiveException
( DirectiveException.CODE_bad_regexp, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchArguments ) );
}
}
/** perform reg exp */
private String doRegExpMatchArguments( String pContent ) throws DirectiveException {
try {
String match = iRegExpMatchArguments.matchFirstSub( pContent );
return match;
}
catch( Exception e ) {
throw new DirectiveException
( DirectiveException.CODE_regexp_mismatch, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchArguments,
ValueCode.CONTENT, pContent ) );
}
}
}