/*
* Name: BasicUnitProcessor
* Author: Richard Rodger
*
* Copyright (c) 2002-2005 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.unit;
// import
import org.jostraca.section.SectionSet;
import org.jostraca.transform.TextualTransform;
import org.jostraca.transform.TextualTransformManager;
import org.jostraca.transform.TransformException;
import org.jostraca.util.ErrorUtil;
import org.jostraca.util.Internal;
import org.jostraca.util.PropertySet;
import java.util.Hashtable;
/** Basic template unit processor.
* @see org.jostraca.unit.UnitList
*/
public class BasicUnitProcessor implements UnitProcessor {
// public static
public static final String CN = BasicUnitProcessor.class.getName();
public static final String PREFIX_main_CodeWriterTransform = "main.CodeWriterTransform.";
// REVIEW: maybe move these to a BasicUnitTypes interface?
public static final String TYPE_text = "text";
public static final String TYPE_script = "script";
public static final String TYPE_expr = "expr";
// private instance
private String iTransformPrefix = PREFIX_main_CodeWriterTransform; //default
private SectionSet iSectionSet = null;
private PropertySet iPropertySet = null;
private Hashtable iTransformManagers = new Hashtable();
// interface UnitProcessor
public SectionSet process( UnitList pUnitList ) throws UnitException {
return process( pUnitList, new SectionSet() );
}
public SectionSet process( UnitList pUnitList, SectionSet pSectionSet ) throws UnitException {
SectionSet ss = pSectionSet;
if( ErrorUtil.is_null( pSectionSet, "pSectionSet" ) ) {
ss = new SectionSet();
}
return processImpl( pUnitList, ss );
}
// public methods
public void setPropertySet( PropertySet pPropertySet ) {
iPropertySet = (PropertySet) Internal.null_arg( pPropertySet );
}
public void setTransformPrefix( String pTransformPrefix ) {
iTransformPrefix = (String) Internal.null_arg( pTransformPrefix );
}
// private methods
private SectionSet processImpl( UnitList pUnitList, SectionSet pSectionSet ) throws UnitException {
clearTransforms();
Hashtable attr = new Hashtable();
attr.put( TextualTransform.ATTR_unit_set, pUnitList );
attr.put( TextualTransform.ATTR_sectionset, pSectionSet );
while( pUnitList.nextUnit() ) {
Unit u = pUnitList.getUnit();
String type = u.getType();
String content = u.getContent();
String section = u.getSection();
attr.put( TextualTransform.ATTR_unit_origin, u.getOrigin() );
attr.put( TextualTransform.ATTR_unit_attr, u.getAttributes() );
try {
TextualTransform transform = getTransform( type );
transform.setParameters( getPropertySet(), attr );
content = transform.transform( content );
}
catch( TransformException te ) {
throw new UnitException( UnitException.CODE_transform, te );
}
pSectionSet.appendToSection( section, content );
}
return pSectionSet;
}
private TextualTransform getTransform( String pType ) throws TransformException {
if( iTransformManagers.containsKey( pType ) ) {
return (TextualTransform) iTransformManagers.get( pType );
}
else {
PropertySet ps = getPropertySet();
String classList = ps.get( iTransformPrefix+pType );
TextualTransformManager ttm = new TextualTransformManager( pType );
ttm.loadClasses( classList );
iTransformManagers.put( pType, ttm );
return ttm;
}
}
private void clearTransforms() {
iTransformManagers.clear();
}
private PropertySet getPropertySet() {
if( null == iPropertySet ) {
iPropertySet = new PropertySet();
}
return iPropertySet;
}
}