/*
* Name: BasicScriptElementProcessor
* Authors: Richard Rodger
*
* Copyright (c) 2001-2006 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.section.Section;
import org.jostraca.unit.UnitList;
import org.jostraca.unit.BasicUnit;
import org.jostraca.unit.BasicUnitList;
import org.jostraca.unit.BasicUnitProcessor;
import org.jostraca.transform.TextualTransform;
import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;
import org.jostraca.util.RegExp;
/** Process a template script element.
*/
public class BasicScriptElementProcessor implements BasicGenericElementProcessor {
// private instance
private BasicUnitList iUnitList;
private TemplateActionHandler iTemplateActionHandler;
private PropertySet iPropertySet;
private TextualTransform iTextualTransform;
private TextualTransformManagerTable iTextualTransformManagerTable;
private String iSectionMarker = Standard.EMPTY;
private String iDirectiveMarker = Standard.EMPTY;
private String iDeclarationMarker = Standard.EMPTY;
private String iRegExpText_MatchSectionName = Standard.EMPTY;
private RegExp iRegExpMatchSectionName = null;
private boolean iPerformMatchSectionName = true;
private String iRegExpText_MatchDeclaration = Standard.EMPTY;
private RegExp iRegExpMatchDeclaration = null;
private boolean iPerformMatchDeclaration = true;
// 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 BasicScriptElementProcessor( BasicUnitList pUnitList,
TemplateActionHandler pTemplateActionHandler,
PropertySet pPropertySet,
TextualTransformManagerTable pTextualTransformManagerTable )
{
iUnitList = pUnitList;
iTemplateActionHandler = pTemplateActionHandler;
iPropertySet = pPropertySet;
iTextualTransformManagerTable = pTextualTransformManagerTable;
iTextualTransform = pTextualTransformManagerTable.getScriptTTM();
iSectionMarker = iPropertySet.get( Property.parse_SectionMarker );
iDeclarationMarker = iPropertySet.get( Property.parse_DeclarationMarker );
setupRegExps( pPropertySet );
}
// instance BasicGenericElementProcessor
public boolean isMatch( Block pBlock ) throws TemplateElementProcessorException {
return pBlock.isScript();
}
public String process( Block pBlock ) throws TemplateElementProcessorException {
iTemplateActionHandler.setLastElementType( ELEMENT_TYPE_script );
try {
String content = pBlock.getContent();
// section name specified
ScriptMatch match = doRegExpMatchSectionName( content );
// declare section
if( !match.hasSectionName() ) {
match = doRegExpMatchDeclaration( content );
}
// default section
if( !match.hasSectionName() ) {
match.sectionName = iTemplateActionHandler.getDefaultSection();
match.content = content;
}
if( iPropertySet.isYes( Property.jostraca_old ) ) {
String script = iTextualTransform.transform( match.content );
iTemplateActionHandler.append( match.sectionName, script );
}
BasicUnit bu = new BasicUnit( BasicUnitProcessor.TYPE_script, match.sectionName, match.content );
iUnitList.add( bu );
return null;
}
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 {
try {
if( Standard.EMPTY.equals( iSectionMarker ) ) {
iPerformMatchSectionName = false;
}
else {
iRegExpText_MatchSectionName = pPropertySet.get( Property.jostraca_regexp_MatchSectionName );
System.out.println("iRegExpText_MatchSectionName="+iRegExpText_MatchSectionName);
iRegExpMatchSectionName = RegExp.make( iRegExpText_MatchSectionName, new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
iPerformMatchSectionName = true;
}
}
catch( Exception e ) {
throw new TemplateElementProcessorException
( TemplateElementProcessorException.CODE_bad_regexp, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchSectionName,
ValueCode.NAME, Property.jostraca_regexp_MatchSectionName ) );
}
try {
if( Standard.EMPTY.equals( iDeclarationMarker ) ) {
iPerformMatchDeclaration = false;
}
else {
iRegExpText_MatchDeclaration = pPropertySet.get( Property.jostraca_regexp_MatchDeclaration );
iRegExpMatchDeclaration = RegExp.make( iRegExpText_MatchDeclaration, new RegExp.ModeSet( RegExp.Mode.DotMatchesNewline ) );
iPerformMatchDeclaration = true;
}
}
catch( Exception e ) {
throw new TemplateElementProcessorException
( TemplateElementProcessorException.CODE_bad_regexp, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchDeclaration,
ValueCode.NAME, Property.jostraca_regexp_MatchDeclaration ) );
}
}
/** perform reg exp */
private ScriptMatch doRegExpMatchSectionName( String pContent ) throws TemplateElementProcessorException {
try {
ScriptMatch match = new ScriptMatch();
if( iPerformMatchSectionName ) {
match.sectionName = iRegExpMatchSectionName.matchFirstSub( pContent );
match.content = iRegExpMatchSectionName.matchSecondSub( pContent );
}
System.out.println("SECTION:"+match+" ON "+pContent);
return match;
}
catch( Exception e ) {
throw new TemplateElementProcessorException
( TemplateElementProcessorException.CODE_unexpected_regexp_mismatch, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchSectionName,
ValueCode.NAME, Property.jostraca_regexp_MatchSectionName,
ValueCode.CONTENT, pContent ) );
}
}
/** perform reg exp */
private ScriptMatch doRegExpMatchDeclaration( String pContent ) throws TemplateElementProcessorException {
try {
ScriptMatch match = new ScriptMatch();
if( iPerformMatchDeclaration ) {
match.content = iRegExpMatchDeclaration.matchFirstSub( pContent );
if( match.hasContent() ) {
match.sectionName = Section.NAME_declare;
}
}
return match;
}
catch( Exception e ) {
throw new TemplateElementProcessorException
( TemplateElementProcessorException.CODE_unexpected_regexp_mismatch, e.getMessage(),
new ValueSet( ValueCode.REGEXP, iRegExpText_MatchDeclaration,
ValueCode.NAME, Property.jostraca_regexp_MatchDeclaration,
ValueCode.CONTENT, pContent ) );
}
}
// private classes
private class ScriptMatch {
public String sectionName = Standard.EMPTY;
public String content = Standard.EMPTY;
public boolean hasSectionName() {
return !Standard.EMPTY.equals( sectionName );
}
public boolean hasContent() {
return !Standard.EMPTY.equals( content );
}
public String toString() {
return "sn:["+sectionName+"],hc:"+hasContent();
}
}
}