/*
* Name: InsertSectionDirective
* 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.Template;
import org.jostraca.TemplateActionHandler;
import org.jostraca.Property;
import org.jostraca.unit.BasicUnit;
import org.jostraca.unit.BasicUnitList;
import org.jostraca.unit.BasicUnitOrigin;
import org.jostraca.section.Section;
import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.transform.InsertSectionTransform;
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;
import java.util.Hashtable;
/** Insert the contents of a section into the <code>CodeWriter</code> source code.
* Usage: <% @insert-section section-name %>
*/
public class InsertSectionDirective implements Directive {
// private static
private static final String NAME = "insert-section";
private static final String[] ALIASES = new String[] { NAME };
private static final String REGEXP_MatchArguments = "^\\s*([0-9a-zA-Z_:-]*)\\s*$";
private String iRegExpText_MatchArguments = Standard.EMPTY;
private RegExp iRegExpMatchArguments = null;
// public methods
public InsertSectionDirective() throws DirectiveException {
setupRegExps();
}
// interface Directive
public String perform(
String pDirectiveName,
String pArguments,
BasicUnitList pBasicUnitList,
TemplateActionHandler pTemplateActionHandler,
PropertySet pPropertySet,
TextualTransformManagerTable pTextualTransformManagerTable,
Template pTemplate
)
throws DirectiveException
{
if( ! Standard.EMPTY.equals( pArguments.trim() ) ) {
String match = doRegExpMatchArguments( pArguments );
if( null != match ) {
String sectionName = match;
Section section = pTemplateActionHandler.getSection( sectionName );
if( pPropertySet.isYes( Property.jostraca_old ) ) {
pTemplateActionHandler.append( section.getContent() );
}
// backwards compatibility support
Hashtable isattr = new Hashtable();
isattr.put( InsertSectionTransform.ATTR_unit_source_section, sectionName );
BasicUnit bu = new BasicUnit( NAME, pTemplateActionHandler.getDefaultSection(), section.getContent(),
new BasicUnitOrigin(), isattr );
pBasicUnitList.add( bu );
}
else {
// REVIEW: better to have the full directive text or line numbers
throw new DirectiveException( DirectiveException.CODE_bad_args,
new ValueSet( ValueCode.DIRECTIVE, pDirectiveName,
ValueCode.ARGUMENTS, pArguments ) );
}
}
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( Exception 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( RegExpException e ) {
throw new DirectiveException
( DirectiveException.CODE_regexp_mismatch, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchArguments,
ValueCode.CONTENT, pContent ) );
}
}
}