/*
* Name: BasicDirectiveElementProcessor
* Authors: 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;
// import
import org.jostraca.directive.DirectiveManager;
import org.jostraca.unit.UnitList;
import org.jostraca.unit.BasicUnitList;
import org.jostraca.transform.TextualTransform;
import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.StandardException;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;
import org.jostraca.util.RegExp;
import org.jostraca.util.RegExpException;
/** Process a template directive element.
*/
public class BasicDirectiveElementProcessor implements BasicGenericElementProcessor, Standard {
// private instance
private BasicUnitList iUnitList;
private TemplateActionHandler iTemplateActionHandler;
private PropertySet iPropertySet;
private TextualTransform iTextualTransform;
private TextualTransformManagerTable iTextualTransformManagerTable;
private DirectiveManager iDirectiveManager;
private String iDirectiveMarker = Standard.EMPTY;
private String iRegExpText_MatchDirective = Standard.EMPTY;
private RegExp iRegExpMatchDirective = null;
private boolean iPerformMatchDirective = false;
private String iRegExpText_IsDirective = Standard.EMPTY;
private RegExp iRegExpIsDirective = null;
private boolean iPerformIsDirective = false;
// constructors
/* Constructor.
* @param pUnitList List of template units
* @param pTemplateActionHandler Send actions based on content here
* @param pPropertySet PropertySet to use
* @param pTextualTransformManagerTable Transforms to apply to content
*/
public BasicDirectiveElementProcessor( BasicUnitList pUnitList,
TemplateActionHandler pTemplateActionHandler,
PropertySet pPropertySet,
TextualTransformManagerTable pTextualTransformManagerTable )
{
iUnitList = pUnitList;
iTemplateActionHandler = pTemplateActionHandler;
iPropertySet = pPropertySet;
iTextualTransformManagerTable = pTextualTransformManagerTable;
iTextualTransform = pTextualTransformManagerTable.getDirectiveTTM();
iDirectiveManager = new DirectiveManager();
iDirectiveManager.loadClasses( pPropertySet.get( Property.parse_Directives ) );
iDirectiveMarker = iPropertySet.get( Property.parse_DirectiveMarker );
setupRegExps( pPropertySet );
}
// interface BasicGenericElementProcessor
public boolean isMatch( Block pBlock ) throws TemplateElementProcessorException {
try {
return ( pBlock.isScript()
&& ( !EMPTY.equals( iPerformIsDirective
? iRegExpIsDirective.match( pBlock.getContent() )
: EMPTY ) ) );
}
catch( RegExpException ree ) {
throw new TemplateElementProcessorException( TemplateElementProcessorException.CODE_regexp_failed,
new ValueSet( ValueCode.REGEXP, iRegExpIsDirective ), ree );
}
}
public boolean process( Block pBlock ) throws StandardException {
iTemplateActionHandler.setLastElementType( ELEMENT_TYPE_directive );
try {
String content = pBlock.getContent();
String directiveName = iPerformMatchDirective ? iRegExpMatchDirective.matchFirstSub( content ) : EMPTY;
String arguments = iPerformMatchDirective ? iRegExpMatchDirective.matchSecondSub( content ) : EMPTY;
iDirectiveManager.perform( directiveName,
arguments,
iUnitList,
iTemplateActionHandler,
iPropertySet,
iTextualTransformManagerTable
);
return true;
}
catch( StandardException se ) {
throw se;
}
catch( Exception e ) {
throw new TemplateElementProcessorException
( TemplateElementProcessorException.CODE_process_error, e.getMessage() );
}
}
public String getContent() {
return Standard.EMPTY;
}
public UnitList getUnitList() {
return iUnitList;
}
// private methods
/** Create regular scripts from properties. */
private void setupRegExps( PropertySet pPropertySet ) throws TemplateElementProcessorException {
// IsDirective
try {
if( ! Standard.EMPTY.equals( iDirectiveMarker ) ) {
iRegExpText_IsDirective = pPropertySet.get( Property.jostraca_regexp_IsDirective );
iRegExpIsDirective = RegExp.make( iRegExpText_IsDirective, new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
iPerformIsDirective = true;
}
}
catch( Exception e ) {
throw new TemplateElementProcessorException
( TemplateElementProcessorException.CODE_bad_regexp, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_IsDirective ) );
}
// MatchDirective
try {
if( ! Standard.EMPTY.equals( iDirectiveMarker ) ) {
iRegExpText_MatchDirective = pPropertySet.get( Property.jostraca_regexp_MatchDirective );
iRegExpMatchDirective = RegExp.make( iRegExpText_MatchDirective, new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
iPerformMatchDirective = true;
}
}
catch( Exception e ) {
throw new TemplateElementProcessorException
( TemplateElementProcessorException.CODE_bad_regexp, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchDirective ) );
}
}
}