/*
* Name: SectionDirective
* 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.section.Section;
import org.jostraca.unit.BasicUnitList;
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.RegExpMatch;
import org.jostraca.util.RegExpException;
import org.jostraca.transform.TextualTransformManagerTable;
/** Set the current insert section for template text and script elements.
* Usage: <% @section section-name %>
*/
public class SectionDirective implements Directive {
private static final String NAME = "section";
private static final String[] ALIASES = new String[] { NAME };
private static final String REGEXP_MatchArguments = "^\\s*([0-9a-zA-Z_:-]+)(.*)$";
private String iRegExpText_MatchArguments = Standard.EMPTY;
private RegExp iRegExpMatchArguments = null;
// public methods
public SectionDirective() throws DirectiveException {
setupRegExps();
}
// interface Directive
public void perform( String pDirectiveName,
String pArguments,
BasicUnitList pBasicUnitList,
TemplateActionHandler pTemplateActionHandler,
PropertySet pPropertySet,
TextualTransformManagerTable pTextualTransformManagerTable
) throws DirectiveException
{
// set default section to "body"
if( Standard.EMPTY.equals( pArguments.trim() ) ) {
pTemplateActionHandler.setDefaultSection( Section.NAME_body );
}
// set default section to first argument, and put everything else into that section
else {
RegExpMatch match = doRegExpMatchArguments( pArguments );
if( match.hasMatch() ) {
String sectionName = match.matchFirstSub();
String content = match.matchSecondSub();
pTemplateActionHandler.setDefaultSection( sectionName );
// REVIEW: this is deprecated
pTemplateActionHandler.append( content );
}
else {
throw new DirectiveException( DirectiveException.CODE_bad_args,
new ValueSet( ValueCode.DIRECTIVE, pDirectiveName,
ValueCode.ARGUMENTS, pArguments ) );
}
}
}
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 RegExpMatch doRegExpMatchArguments( String pContent ) throws DirectiveException {
try {
RegExpMatch match = iRegExpMatchArguments.matchFirst( pContent );
return match;
}
catch( RegExpException e ) {
throw new DirectiveException
( DirectiveException.CODE_regexp_mismatch, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchArguments,
ValueCode.CONTENT, pContent ) );
}
}
}