Package org.jostraca

Source Code of org.jostraca.BasicTemplate

/*
* Name:    BasicTemplate
* Authors: Richard Rodger
*
* Copyright (c) 2000-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.section.SectionSet;

import org.jostraca.util.Standard;
import org.jostraca.util.Internal;
import org.jostraca.util.ErrorUtil;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;
import org.jostraca.util.FileUtil;
import org.jostraca.util.RegExp;
import org.jostraca.util.RegExpMatch;
import org.jostraca.util.BuildResource;
import org.jostraca.util.RootBuildResource;
import org.jostraca.util.FileRefBuildResource;
import org.jostraca.util.FileRef;
import org.jostraca.util.OrderedPropertySetManager;
import org.jostraca.util.PropertySet;
import org.jostraca.util.PropertySetException;

import java.io.File;
import java.io.IOException;

import java.util.Vector;
import java.util.List;


/** Basic implementation of template functionality.
*  The text of the the file generated by the template can be accessed using
{@link #getResult} but only if the template script is Java and the
*  template is executed internally in the Jostraca process. Also, the
*  template can chose to return a custom data structure with <code>getResult</code>.
*/
public class BasicTemplate implements Template {   

  // public static
  public static final String INCLUDE_MOD_if_exists         = "if-exists";
  public static final String INCLUDE_MOD_template_relative = "template-relative";
  public static final String INCLUDE_MOD_output_relative   = "output-relative";




  // private static
  // regexp used to find conf section
  private static final String DEFAULT_REGEXP_Conf          = "<%\\s*@\\s*conf\\s+(.+?)\\s*%>";
  private static final int    REGEXP_PropertySet_props     = 1;
  private static final String DEFAULT_REGEXP_Include       = "<%\\s*@\\s*include\\s+(\".+?\"|[^ ]+)\\s*([a-z-]*)\\s*%>";
  private static final int    REGEXP_Include_file          = 1;
  private static final int    REGEXP_Include_modifier      = 2;
  private static final String DEFAULT_REGEXP_IncludeBase   = "<%\\s*@\\s*include-base\\s+[\"']?(.+?)[\"']?\\s*%>";
  private static final int    REGEXP_IncludeBase_folder    = 1;
  private static final String DEFAULT_REGEXP_IncludeBlock  = "<%\\s*@\\s*include-block\\s+[\"']?(.+?)[\"']?\\s+/(.+?)/\\s*%>";
  private static final int    REGEXP_IncludeBlock_file     = 1;
  private static final int    REGEXP_IncludeBlock_mark     = 2;

  private static final int    MAX_RECURSION_COUNT_performIncludes = 22; // don't fail on circular refs
  private static final String DEFAULT_IncludeBase                 = ".";




  // protected instance
  protected TemplatePath   iTemplatePath   = null;
  protected TemplateScript iTemplateScript = null;

  protected String         iPath                   = Standard.EMPTY;    // path to template file
  protected String         iSource                 = Standard.EMPTY;    // source text of template
  protected boolean        iForceIncludeBase       = false;             
  protected String         iForcedIncludeBase      = Standard.EMPTY;
  protected RegExp         iConfRegExp             = null;
  protected RegExp         iIncludeRegExp          = null;
  protected RegExp         iIncludeBaseRegExp      = null;
  protected RegExp         iIncludeBlockRegExp     = null;
  protected TemplatePath[] iAlsoGenerate           = new TemplatePath[] {};
  protected SectionSet     iSectionSet             = null;

  protected long           iLastMod                = 0L; // last modified time of template source file
  protected BuildResource  iBuildResource          = new RootBuildResource();
  protected Object         iContext                = null;
  protected Object         iResult                 = null;


  // FIX: probably not a good place for this
  protected String         iCodeWriterSource       = null;
  protected boolean        iCodeWriterChanged      = false;
  protected FileRef        iCodeWriterRef          = new FileRef( new File("code-writer") );
  protected FileRef        iCodeWriterExecRef      = new FileRef( new File("code-writer-exec") );

  protected OrderedPropertySetManager iOrderedPSM = DefaultPropertySets.makeStandardOrder();

  protected List iSavedFiles = new Vector();

  protected File iMetaFile = null;



  // constructors

  /** Create with empty template path */
  public BasicTemplate() {
    iTemplatePath = new BasicTemplatePath( Standard.EMPTY );
  }


  /** Create with name of template. This assumes that the
   *  template name refers to a file of the same name in the current folder. */
  public BasicTemplate( String pName ) {
    iTemplatePath = new BasicTemplatePath( pName );
  }


  /** Create a template using a {@link TemplatePath} object. This is the standard method for
   *  creating template objects, as the <code>TemplatePath</code> object does path and library resolution. */
  public BasicTemplate( TemplatePath pTemplatePath ) {
    load( pTemplatePath );
  }



  // interface template

  public void setSectionSet( SectionSet pSectionSet ) {
    iSectionSet = (SectionSet) Internal.null_arg( pSectionSet );
  }

  public SectionSet getSectionSet() {
    return iSectionSet;
  }


  public void setContext( Object pContext ) {
    iContext = Internal.null_arg( pContext );
  }


  public Object getContext() {
    return iContext;
  }


  public void setResult( Object pResult ) {
    iResult = pResult;
  }


  public Object getResult() {
    return iResult;
  }


  public void load( TemplatePath pTemplatePath ) throws TemplateException {
    String MN = ".load: ";

    TemplatePath tp = (TemplatePath) Internal.null_arg( pTemplatePath );

    try {
      String path   = tp.getTemplatePath();
      String source = FileUtil.readFile( path );

      iLastMod      = FileUtil.lastMod( path );
      iPath         = path;
      iTemplatePath = tp;

      // set this to the template at first - the code writer is unknown at this point
      iCodeWriterRef = new FileRef( new File( path ) );

      // by default, assume executable is same as source (fine for scripting langs)
      iCodeWriterExecRef = new FileRef( iCodeWriterRef.getFile() );

      // FIX: does this conflict with init() later?
      if( tp.isLibraryTemplate() ) {
        forceIncludeBase( Standard.DOT );
      }

      setSource( source );
    }
    catch( TemplateException te ) {
      throw te;
    }
    catch( IOException ioe ) {
      throw new TemplateException( TemplateException.CODE_load_file, new ValueSet( ValueCode.FILE, pTemplatePath ) );
    }
    catch( Exception e ) {
      throw new TemplateException( MN+e );
    }
  }
 

  public TemplatePath getTemplatePath() {
    return iTemplatePath;
  }


  public void setSource( String pSource ) throws TemplateException {
    String MN = ".setSource: ";

    String source = Internal.null_arg( pSource );

    // FIX: load and setSource ARE A MESS!

    try {
      init();

      source = parseForPropertySet( source, true );
      createRegExps();

      source = performIncludes( source );
      // again, in case of new conf sections
      source = parseForPropertySet( source, false );

      processProperties();

      handleTemplateScript();

      // REVIEW: hack!!
      String version = getMergedPropertySet().get( Property.main_JostracaVersion );
      if( ! version.startsWith( Service.VERSION_NUMBER ) ) {
        modifyForOldVersion();
        createRegExps();
        source = performIncludes( source );
        source = parseForPropertySet( source, false );
      }

      // new includes might have changed it
      handleTemplateScript();

      iSource = source;
    }
    catch( TemplateException te ) {
      throw te;
    }
    catch( Exception e ) {
      throw new TemplateException( MN, e );
    }
  }


  public String getSource() {
    return iSource;
  }


  public PropertySet getPropertySet( String pConfName ) {
    return iOrderedPSM.get( pConfName );
  }


  public void setPropertySet( String pConfName, PropertySet pPropertySet ) {
    iOrderedPSM.put( pConfName, pPropertySet );
  }


  public PropertySet getMergedPropertySet() {
    return iOrderedPSM.merge();
  }
 
  public String traceProperty( String pName ) {
    return iOrderedPSM.trace( pName );
  }


  public TemplatePath[] getAlsoGenerate() {
    return iAlsoGenerate;
  }


  public String getCodeWriterLang() {
    return iTemplateScript.getCanonicalScriptName( getMergedPropertySet() );
  }


  public long getLastMod() {
    return iLastMod;
  }     


  public BuildResource getBuildResource() {
    return iBuildResource;
  }     


  public void setCodeWriterPath( File pCodeWriter ) {
    iCodeWriterRef.setFile( (File) Internal.null_arg( pCodeWriter ) );
  }


  public File getCodeWriterPath() {
    return iCodeWriterRef.getFile();
  }


  public void setCodeWriterSource( String pCodeWriterSource ) {
    iCodeWriterSource = Internal.null_arg( pCodeWriterSource );
  }


  public String getCodeWriterSource() {
    return iCodeWriterSource;
  }


  public void setCodeWriterChanged( boolean pChanged ) {
    iCodeWriterChanged = pChanged;
  }

  public boolean getCodeWriterChanged() {
    return iCodeWriterChanged;
  }


  public void setCodeWriterExecutablePath( File pCodeWriterExecutablePath ) {
    File cwep = (File) Internal.null_arg( pCodeWriterExecutablePath );
    iCodeWriterExecRef.setFile( cwep );
  }

  public File getCodeWriterExecutablePath() {
    return iCodeWriterExecRef.getFile();
  }



  public void setSavedFiles( List pSavedFiles ) {
    iSavedFiles = pSavedFiles;
  }

  public List getSavedFiles() {
    return iSavedFiles;
  }


  public void setMetaFile( File pMetaFile ) {
    iMetaFile = pMetaFile;
  }


  public File getMetaFile() {
    return iMetaFile;
  }


  public boolean hasMetaFile() {
    return null != iMetaFile;
  }


  public String toString() {
    return "Template[p:"+iTemplatePath+"]";
  }




  // public methods

  /** Force template to use a specified include base. Any
   *  includebase actions in template will be ignored
   *  @param pIncludeBase include base folder path
   */
  public void forceIncludeBase( String pIncludeBase ) {
    if( ErrorUtil.not_null( pIncludeBase, "pIncludeBase" ) ) {
      iForceIncludeBase  = true;
      iForcedIncludeBase = pIncludeBase;
    }
  }


  /** Modify template properties to handle older template versions. */
  public void modifyForOldVersion() {
   
    PropertySet mps  = getMergedPropertySet();
    PropertySet tmps = iOrderedPSM.get( Service.CONF_template );

    String version = mps.get( Property.main_JostracaVersion );

    if( ! version.startsWith( Service.VERSION_NUMBER ) ) {
      if( "0.1".equals( version ) ) {
        // set markers
        tmps.set( Property.parse_SectionMarker,     "@" );
        tmps.set( Property.parse_DirectiveMarker,   "!" );
        tmps.set( Property.parse_DeclarationMarker, "" ); // no declarations in v0.1
         
        // section marker is a prefix in v0.1
        tmps.set( Property.jostraca_regexp_MatchSectionName
                  ,"$<jostraca.regexp.AnyWhiteSpaceAtStart>"
                  +"$<parse.SectionMarker>$<jostraca.regexp.SubmatchSectionName>"
                  +"$<jostraca.regexp.SubmatchAnyWhiteSpaceAnyCharsAtEnd>" );

      }

      // old perl and python writer formats
      if( "0.1".equals( version ) || "0.2".equals( version ) || "0.3".equals( version ) ) {
        String csn = iTemplateScript.getCanonicalScriptName( mps );

        if( "perl".equals( csn ) && -1 != mps.get( Property.main_CodeWriterFormat ).indexOf("BasicPerlWriterFormat") ) {
          tmps.set( Property.main_CodeWriterFormat, "NonObjectPerlWriterFormat" );
          tmps.set( Property.lang_InsertPrefix,     "_insert( " );
        }
        else if( "python".equals( csn ) && -1 != mps.get( Property.main_CodeWriterFormat ).indexOf("BasicPythonWriterFormat") ) {
          tmps.set( Property.main_CodeWriterFormat, "NonObjectPythonWriterFormat" );
          tmps.set( Property.lang_InsertPrefix,     "_py_insert( " );
        }
      }
    }
  }



  // private methods

  /** Create conf regexp to bootstrap - this will all be cleaned up when conf, include etc are proper directives
   *  @param pPropertySet conf regular expression definitions by properties
   */
  private void createConfRegExps( PropertySet pPropertySet ) {
    String regexpFailed = "RegExp invalid for ";

    try {
      String confREDef = pPropertySet.get( Property.jostraca_regexp_template_ConfDirective, DEFAULT_REGEXP_Conf );
      iConfRegExp = RegExp.make( confREDef, RegExp.ModeSet.DotMatchesNewline );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_re_conf, e );
    }
  }


  /** Create regexps used to implement preprocessing directives */
  private void createRegExps() {
    String regexpFailed = "RegExp invalid for ";

    PropertySet ps = getMergedPropertySet();

    try {
      String confREDef = ps.get( Property.jostraca_regexp_template_ConfDirective, DEFAULT_REGEXP_Conf );
      iConfRegExp = RegExp.make( confREDef, RegExp.ModeSet.DotMatchesNewline );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_re_conf, e );
    }


    try {
      String includeREDef = ps.get( Property.jostraca_regexp_template_IncludeDirective, DEFAULT_REGEXP_Include );
      iIncludeRegExp = RegExp.make( includeREDef, RegExp.ModeSet.DotMatchesNewline );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_re_include, e );
    }


    try {
      String includeBaseREDef = ps.get( Property.jostraca_regexp_template_IncludeBaseDirective, DEFAULT_REGEXP_IncludeBase );
      iIncludeBaseRegExp = RegExp.make( includeBaseREDef, RegExp.ModeSet.DotMatchesNewline );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_re_includebase, e );
    }


    try {
      String includeBlockREDef = ps.get( Property.jostraca_regexp_template_IncludeBlockDirective, DEFAULT_REGEXP_IncludeBlock );
      iIncludeBlockRegExp = RegExp.make( includeBlockREDef, RegExp.ModeSet.DotMatchesNewline );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_re_includeblock, e );
    }
  }


  // HACK
  // the entire include handling needs to be redone

  /** Include specified files: <% @include "filepath" %> */
  private String performIncludes( String pSource ) {
    String source      = pSource;
    String includeBase = DEFAULT_IncludeBase;

    try {

      if( iForceIncludeBase ) {
        includeBase = iForcedIncludeBase;
      }

      // include-base or template folder
      else {

        // check for and remove include base
        String templateIncludeBase = includeBase;
        String includeBaseMatch    = iIncludeBaseRegExp.matchFirstSub( source );
        if( !Standard.EMPTY.equals( includeBaseMatch ) ) {
          templateIncludeBase = includeBaseMatch;
          String includeBaseContent  = iIncludeBaseRegExp.match( source );
          int    lengthOfIncludeBase = includeBaseContent.length();
          int    startOfIncludeBase  = source.indexOf( includeBaseContent );
          source
            = source.substring( 0, startOfIncludeBase )
            + source.substring( startOfIncludeBase + lengthOfIncludeBase, source.length() )
            ;
          includeBase = templateIncludeBase;
        }
     
        // template relative by default
        else {
          includeBase = iTemplatePath.getTemplateFolder();
        }
      }

      return performRecursiveInclude( source, includeBase, 0 );
    }
    catch( TemplateException te ) {
      throw te;
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_re_includebase, iPath, e );
    }
  }



  /** perform includes recursively - include directives in included files work */
  private String performRecursiveInclude( String pSource, String pIncludeBase, int pRecursionCount )
    {

    if( pRecursionCount > MAX_RECURSION_COUNT_performIncludes ) {
      throw new TemplateException( TemplateException.CODE_infinite_recursion, pRecursionCount+" > "+MAX_RECURSION_COUNT_performIncludes );
    }

    String        source           = pSource;
    RegExpMatch   includeMatch     = null;
    String        includeText      = null;
    String        includeFile      = null;
    String        includeModifier  = null;
    int           numIncludes      = 0;
    int           numIncludeBlocks = 0;


    // normal includes
   
    String includeFilePath = Standard.EMPTY;
    try {
      RegExpMatch[] allincludes      = iIncludeRegExp.matchAll( source );
      numIncludes = allincludes.length;

      for( int includeI = 0; includeI < numIncludes; includeI++ ) {
        includeMatch = allincludes[ includeI ];
        includeText  = includeMatch.match();

        includeFile = includeMatch.matchFirstSub();
        includeFile = ( null == includeFile ? Standard.EMPTY : includeFile );
        includeFile    
          = ( includeFile.startsWith( Standard.QUOTE ) && includeFile.endsWith( Standard.QUOTE ) )
          ? includeFile.substring( 1, includeFile.length()-1 ) : includeFile;

        includeModifier = includeMatch.matchSecondSub();
        includeModifier = ( null == includeModifier ? Standard.EMPTY : includeModifier.trim() );

        // REVIEW: TemplatePath interaction with Template needs refactoring

        // include the file
        String includeBase = pIncludeBase;
        if( -1 != includeModifier.indexOf( INCLUDE_MOD_template_relative ) ) {
          includeBase = iTemplatePath.getTemplateFolder();
        }
        else if( -1 != includeModifier.indexOf( INCLUDE_MOD_output_relative ) ) {
          includeBase = getMergedPropertySet().get( Property.main_OutputFolder );
        }

        // another HACK
        File includeF = new File( includeFile );
        if( includeF.isAbsolute() || includeFile.startsWith("/") || includeFile.startsWith("\\") ) {
          includeFilePath = includeFile;
        }
        else {
          // REVIEW: possible user info message that include file does not exist when "if-exists" used?
          includeFilePath = includeBase + File.separatorChar + includeFile;
        }

        boolean onlyifexists = -1 != includeModifier.indexOf( INCLUDE_MOD_if_exists );

        String includeContent = Standard.EMPTY;

        try {
          includeContent = FileUtil.readFile( includeFilePath, ( onlyifexists ? FileUtil.EMPTY_IF_IO_ERROR : FileUtil.FAIL_ON_IO_ERROR ) );
        }

        // HACK
        // try to load from template path
        catch( Throwable t ) {
          BasicTemplatePath includeRef = new BasicTemplatePath( includeFile );
          //includeRef.resolve( iParameters.getList( Property.main_TemplatePath, Standard.COMMA  ));
          includeRef.resolve( getMergedPropertySet().getList( Property.main_TemplatePath, Standard.COMMA  ));
          includeFilePath = includeRef.getTemplatePath();
          includeContent = FileUtil.readFile( includeFilePath, ( onlyifexists ? FileUtil.EMPTY_IF_IO_ERROR : FileUtil.FAIL_ON_IO_ERROR ) );
        }

        int    startOfInclude = source.indexOf( includeText );
        if( 0 > startOfInclude ) { // paranoia ;)
          continue;
        }
        int   lengthOfInclude = includeText.length();
        source
          = source.substring( 0, startOfInclude )
          + includeContent
          + source.substring( startOfInclude + lengthOfInclude, source.length() )
          ;

        FileRefBuildResource includeBuildResource = new FileRefBuildResource( new FileRef( new File( includeFilePath ) ), iCodeWriterRef );
        iBuildResource.add( includeBuildResource );
      }
    }
    catch( IOException ioe ) {
      //ioe.printStackTrace();
      throw new TemplateException( TemplateException.CODE_include,
                                   new String[] { ValueCode.FILE, includeFilePath, ValueCode.TEMPLATE, iPath }, ioe );
    }
    catch( Exception e ) {
      e.printStackTrace();
      throw new TemplateException( TemplateException.CODE_include,
                                   new String[] { ValueCode.FILE, includeFilePath, ValueCode.TEMPLATE, iPath }, e );
    }


    // block includes

    RegExpMatch includeBlockMatch = null;
    String      includeBlockText  = null;
    String      includeBlockFile  = null;
    String      includeBlockMark = null;

    try {
      RegExpMatch[] allincludeblocks = iIncludeBlockRegExp.matchAll( source );
      numIncludeBlocks = allincludeblocks.length;

      for( int includeBlockI = 0; includeBlockI < numIncludeBlocks; includeBlockI++ ) {
        includeBlockMatch = allincludeblocks[ includeBlockI ];
        includeBlockText  = includeBlockMatch.match();
        includeBlockFile  = includeBlockMatch.matchFirstSub();
        includeBlockMark  = includeBlockMatch.matchSecondSub();

        RegExp markRegExp = RegExp.make( includeBlockMark, RegExp.ModeSet.DotMatchesNewline );

        // include the file block
        String  includeFileContent  = FileUtil.readFile( pIncludeBase + File.separatorChar + includeBlockFile );       
        String  includeBlockContent = markRegExp.match( includeFileContent );

        // very important - prevent data loss
        if( Standard.EMPTY.equals( includeBlockContent ) ) {
          throw new TemplateException( TemplateException.CODE_includeblock_find,
                                       new ValueSet( ValueCode.FILE, includeBlockFile, ValueCode.VALUE, includeBlockMark ) );
        }

        int    startOfIncludeBlock = source.indexOf( includeBlockText );
        if( 0 > startOfIncludeBlock ) { // paranoia ;)
          continue;
        }
        int   lengthOfIncludeBlock = includeBlockText.length();
        source
          = source.substring( 0, startOfIncludeBlock )
          + includeBlockContent
          + source.substring( startOfIncludeBlock + lengthOfIncludeBlock, source.length() )
          ;
      }
    }
    catch( IOException ioe ) {
      throw new TemplateException( TemplateException.CODE_includeblock,
                                   new String[] { ValueCode.VALUE, includeBlockText, ValueCode.TEMPLATE, iPath }, ioe );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_includeblock,
                                   new String[] { ValueCode.VALUE, includeBlockText, ValueCode.TEMPLATE, iPath }, e );
    }

    if( 0 < ( numIncludes + numIncludeBlocks ) ) {
      return performRecursiveInclude( source, pIncludeBase, pRecursionCount + 1 );
    }
    else {
      return pSource;
    }
  }



  /** Parse source text for property set definition */
  private String parseForPropertySet( String pSource, boolean pConfRequired ) {
    Internal.null_state( iConfRegExp );

    try {
      String templateSource = pSource;

      RegExpMatch[] allconfs = iConfRegExp.matchAll( templateSource );
      int           numConfs = allconfs.length;
      if( 0 == numConfs && pConfRequired ) {
        throw new TemplateException( TemplateException.CODE_no_conf, new ValueSet( ValueCode.TEMPLATE, iPath ) );
      }
      else {
        PropertySet ps        = new PropertySet();
        boolean     foundConf = false;
        RegExpMatch conf      = null;
        String      props     = null;
        PropertySet propSet   = null;
        for( int confI = 0; confI < numConfs; confI++ ) {
          conf    = allconfs[ confI ];
          props   = conf.matchFirstSub();
          propSet = new PropertySet();
          propSet.parse( props );
          ps.overrideWith( propSet );
          foundConf = true;
        }
       
        iOrderedPSM.get( Service.CONF_template ).overrideWith( ps );

        if( foundConf ) {
          // remove conf sections from template
          // WARNING: relying on default replace to be empty string here
          templateSource = iConfRegExp.replaceAll( templateSource );
        }
        else if( pConfRequired ) {
          throw new TemplateException( TemplateException.CODE_no_conf, new ValueSet( ValueCode.TEMPLATE, iPath ) );
        }
      }

      return templateSource;

    }
    catch( TemplateException te ) {
      throw te;
    }
    catch( PropertySetException pse ) {
      throw new TemplateException( TemplateException.CODE_parse_conf, new ValueSet( ValueCode.TEMPLATE, iPath ), pse );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_no_conf, new ValueSet( ValueCode.TEMPLATE, iPath ), e );
    }
  }


  /** Perform special processing on template property set */
  private void processProperties() throws Exception {
    PropertySet tmps = iOrderedPSM.get( Service.CONF_template );

    if( null != iTemplatePath ) {
      PropertySet tpps = iTemplatePath.resolvePropertySet();
      tmps.inheritFrom( tpps );
    }

    tmps.set( Property.jostraca_template_path,      iTemplatePath.getTemplatePath() );
    tmps.set( Property.jostraca_template_name,      iTemplatePath.getTemplateName() );
    tmps.set( Property.jostraca_template_file,      iTemplatePath.getTemplateFileName() );
    tmps.set( Property.jostraca_template_folder,    iTemplatePath.getTemplateFolder() );
    tmps.set( Property.jostraca_template_isLibrary, iTemplatePath.isLibraryTemplate() );

    /* DECOMISSIONED
    // handle also generate templates
    Vector alsoTemplates = new Vector();
    String alsoGenerate = tmps.get( Property.main_AlsoGenerate );
    StringTokenizer st = new StringTokenizer( alsoGenerate, "," );
    while( st.hasMoreTokens() ) {
      String alsoTemplateName = st.nextToken().trim();
      if( 0 < alsoTemplateName.length() ) {
        alsoTemplates.addElement( new BasicTemplatePath( alsoTemplateName ) );
      }
    }

    iAlsoGenerate = new TemplatePath[ alsoTemplates.size() ];
    alsoTemplates.copyInto( iAlsoGenerate );
    tmps.set( Property.main_AlsoGenerate, Standard.EMPTY ); // very important!
    */
  }



  /** Determine template script */
  private void handleTemplateScript() {
    PropertySet mps = getMergedPropertySet();
   
    if( null == iTemplateScript ) {
      // FIX: not the right way to create it
      iTemplateScript = BasicTemplateScript.defineByProperty( mps );
    }

    iTemplateScript.setByProperty( mps );

    // FIX: so suffixes are not automatic anymore? check this
    if( mps.isYes( "jostraca.support.ForceScriptByPath" ) ) {
      TemplateScript ts = BasicTemplateScript.defineByPath( iTemplatePath.getTemplatePath() );
      if( null != ts ) {
        iTemplateScript = ts;
      }
    }
  }


  /** Initiliase template source */
  private void init() {
    PropertySet ps = getMergedPropertySet();

    // PropertySet include base has precedence
    if( ps.isDefined( Property.main_IncludeBase ) ) {
      forceIncludeBase( ps.get( Property.main_IncludeBase ) );
    }

    // hack: to be removed
    createConfRegExps( ps );

    //iParameters = ps;
  }

}
TOP

Related Classes of org.jostraca.BasicTemplate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.