org.formaria.template
Class TemplateEngine

java.lang.Object
  extended by org.formaria.template.TemplateEngine

public class TemplateEngine
extends java.lang.Object

An engine for processing templates containing paramaterized resources.

Copyright (c) Formaria Ltd., 2008, This software is licensed under the GNU Public License (GPL), please see license.txt for more details. If you make commercial use of this software you must purchase a commercial license from formaria.

$Revision: 1.5 $


Field Summary
protected  java.util.HashMap contents
           
protected  Project currentProject
           
protected  DefaultAttributeEvaluator evaluator
           
protected  java.util.HashMap includes
           
protected  java.util.Hashtable<java.lang.String,TemplateProcessor> processors
           
protected  java.io.File sourceDir
           
protected  java.lang.String sourcePath
           
protected  java.io.File targetDir
           
protected  java.lang.String targetPath
           
 
Constructor Summary
TemplateEngine(Project proj, java.lang.String source, java.lang.String target)
          Creates a new instance of TemplateEngine
 
Method Summary
 java.lang.Object evaluateAttribute(java.lang.String attribValue)
          Evaluates an attribute value.
 java.lang.String getContent(java.lang.String key)
           
 java.util.HashMap getContents(TemplateProcessor tp)
           
 java.util.HashMap getIncludes(TemplateProcessor tp)
           
 TemplateProcessor getProcessor(java.lang.String sourceName, int processingType)
          Get the processor for a particular type
 java.lang.String getTargetResourceName(java.lang.String sourcePattern, java.lang.String sourceName, java.lang.String targetPattern, int processingType)
          Get the name of the resource when processed
 boolean includes(java.lang.String elementName)
          Include a given element
 int process(java.lang.String[] sourceResources)
          Process the template resources.
 int process(java.lang.String[] sourceResources, java.lang.String[] targetResources, int[] processingTypes)
          Process the template resources.
 void putContent(java.lang.String key, java.lang.String value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sourcePath

protected java.lang.String sourcePath

targetPath

protected java.lang.String targetPath

sourceDir

protected java.io.File sourceDir

targetDir

protected java.io.File targetDir

processors

protected java.util.Hashtable<java.lang.String,TemplateProcessor> processors

currentProject

protected Project currentProject

evaluator

protected DefaultAttributeEvaluator evaluator

includes

protected java.util.HashMap includes

contents

protected java.util.HashMap contents
Constructor Detail

TemplateEngine

public TemplateEngine(Project proj,
                      java.lang.String source,
                      java.lang.String target)
Creates a new instance of TemplateEngine

Parameters:
proj - the owner project
source - the source folder for the templates
target - the target folder for the template output
Method Detail

process

public int process(java.lang.String[] sourceResources)
Process the template resources.

Parameters:
sourceResources - the resources to process, the file names may contain wildcard characters
targetResources - the resources name following processing, the file names may contain wildcard characters, but they should match the pattern of the sourceResource argument. The number of array elements should also match that of the sourceResource. A null value may be passed for an element or for the argument, in which case the getTargetResourceName should fixup the resource name.
Returns:
zero if processing is sucessful or a positive integer if processing is completed, but with warnings, and a negative value if an error occurs that prevents complete processing

process

public int process(java.lang.String[] sourceResources,
                   java.lang.String[] targetResources,
                   int[] processingTypes)
Process the template resources.

Parameters:
sourceResources - the resources to process, the file names may contain wildcard characters
targetResources - the resources name following processing, the file names may contain wildcard characters, but they should match the pattern of the sourceResource argument. The number of array elements should also match that of the sourceResource. A null value may be passed for an element or for the argument, in which case the getTargetResourceName should fixup the resource name.
processingType - the type of processing
Returns:
zero if processing is sucessful or a positive integer if processing is completed, but with warnings, and a negative value if an error occurs that prevents complete processing

getProcessor

public TemplateProcessor getProcessor(java.lang.String sourceName,
                                      int processingType)
Get the processor for a particular type

Parameters:
sourceName - the resource name
processingType - the processing instruction
Returns:
the appropriate Processor

getTargetResourceName

public java.lang.String getTargetResourceName(java.lang.String sourcePattern,
                                              java.lang.String sourceName,
                                              java.lang.String targetPattern,
                                              int processingType)
Get the name of the resource when processed

Parameters:
sourcePattern - the pattern used to find the source file, the file name may contain wildcard characters
sourceName - the actual resource to process
targetPattern - the resource name/pattern following processing, the file name may contain wildcard characters, but they should match the pattern of the sourceResource argument.
processingType - the type of processing
Returns:
the new resource name

includes

public boolean includes(java.lang.String elementName)
Include a given element

Parameters:
elementName - the name of the element
Returns:
true if the element is included, false if it is to be excluded.

evaluateAttribute

public java.lang.Object evaluateAttribute(java.lang.String attribValue)
Evaluates an attribute value. An attribute may be a value or a method call. If brackets are part of the value it is assumed that a method call is intended. The method call is indicated by the '$' symbol e.g. ${myMethod()}

Parameters:
attribValue - the raw attribute value
Returns:
the evaluated attribute

getContent

public java.lang.String getContent(java.lang.String key)

putContent

public void putContent(java.lang.String key,
                       java.lang.String value)

getContents

public java.util.HashMap getContents(TemplateProcessor tp)

getIncludes

public java.util.HashMap getIncludes(TemplateProcessor tp)