/*
* Name: BasicTemplateScript
* Authors: Richard Rodger
*
* Copyright (c) 2001-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;
// import
import org.jostraca.util.Standard;
import org.jostraca.util.ErrorUtil;
import org.jostraca.util.Internal;
import org.jostraca.util.PropertySet;
import org.jostraca.util.RegExp;
import org.jostraca.util.RegExpException;
import java.io.File;
/** Handles template script indentification.
* Template file names matching:
* /foo[-._]bar\.jtm/ indicate that the template script language is 'bar'.
* The property main.TemplateScript can override this.
*
* All template script names are converted to canonical form.
* Property names of the form:
* jostraca.TemplateScript.CanonicalName.pl = perl
* indicate mappings to canonical names.
*/
public class BasicTemplateScript implements TemplateScript {
// public static
public static final String CN = BasicTemplateScript.class.getName();
// private static
private static final String REGEXP_CodeWriterLang = "[_.-]([^_.-]+)\\.jtm$";
private static RegExp sCodeWriterLangRegExp = null;
// private instance
private String iOriginalScriptName = Standard.EMPTY;
// static
static {
try {
sCodeWriterLangRegExp = RegExp.make( REGEXP_CodeWriterLang );
}
catch( RegExpException e ) {
ErrorUtil.fatalMsg( CN, e );
}
}
// public methods
protected BasicTemplateScript() {
// create using defineBy* methods
}
public static TemplateScript defineByPath( String pPath ) throws TemplateScriptException {
TemplateScript ts = new BasicTemplateScript();
ts.setByPath( pPath );
return ts;
}
public static TemplateScript defineByProperty( PropertySet pPropertySet ) throws TemplateScriptException {
TemplateScript ts = new BasicTemplateScript();
ts.setByProperty( pPropertySet );
return ts;
}
/** @deprecated */
public void setByPath( String pPath ) throws TemplateScriptException {
String MN = ".defineByPath: ";
String path = (String) Internal.null_arg( pPath );
try {
String lang = Standard.EMPTY;
File pathF = new File( path );
String name = pathF.getName();
lang = sCodeWriterLangRegExp.matchFirstSub( name );
setByName( lang );
}
catch( RegExpException ree ) {
throw new TemplateScriptException( MN, ree );
}
}
public void setByProperty( PropertySet pPropertySet ) throws TemplateScriptException {
String MN = ".defineByProperty: ";
PropertySet ps = (PropertySet) Internal.null_arg( pPropertySet );
// if undefined, use existing value
if( ps.has( Property.main_TemplateScript ) ) {
setByName( ps.get( Property.main_TemplateScript ) );
}
}
/** Set template script by name. The name is converted to canonical form, if it exists.
* The canonical form is generally the name of the appropriate lang config file (minus the .conf suffix).
*/
public void setByName( String pScriptName ) throws TemplateScriptException {
String MN = ".setByName: ";
String scriptName = (String) Internal.null_arg( pScriptName );
iOriginalScriptName = scriptName;
}
public String getOriginalScriptName() {
return iOriginalScriptName;
}
// FIX: need to have a hard-coded reference list of canonical names for programmatic comparison
// and for users of Generator API
public String getCanonicalScriptName( PropertySet pPropertySet ) {
String result = iOriginalScriptName;
if( null != pPropertySet ) {
String searchTerm = Property.PREFIX_jostraca_TemplateScript_CanonicalName + iOriginalScriptName;
if( pPropertySet.has( searchTerm ) ) {
result = pPropertySet.get( searchTerm );
}
}
// failsafe
if( Standard.EMPTY.equals( result ) || null == result ) {
result = pPropertySet.get( Property.main_DefaultTemplateScript );
}
return result;
}
public String toString() {
return iOriginalScriptName;
}
}