freemarker.ext.ant

Class FreemarkerXmlTask


public class FreemarkerXmlTask
extends MatchingTask

This is an Ant task for transforming XML documents using FreeMarker templates. It uses the adapter class NodeListModel. It will read a set of XML documents, and pass them to the template for processing, building the corresponding output files in the destination directory.

It makes the following variables available to the template in the data model:

It supports the following attributes:

AttributeDescriptionRequired
basedirlocation of the XML files. Defaults to the project's basedir.No
destdirlocation to store the generated files.Yes
includescomma-separated list of patterns of files that must be included; all files are included when omitted.No
includesfilethe name of a file that contains include patterns.No
excludescomma-separated list of patterns of files that must be excluded; no files (except default excludes) are excluded when omitted.No
excludesfilethe name of a file that contains exclude patterns.No
defaultexcludesindicates whether default excludes should be used (yes | no); default excludes are used when omitted.No
extensionextension of generated files. Defaults to .html.No
templatename of the FreeMarker template file that will be applied by default to XML filesNo
templateDirlocation of the FreeMarker template(s) to be used, defaults to the project's baseDirNo
projectfilepath to the project file. The poject file must be an XML file. If omitted, it will not be available to templates No
incrementalindicates whether all files should be regenerated (no), or only those that are older than the XML file, the template file, or the project file (yes). Defaults to yes. No
encodingThe encoding of the output files. Defaults to platform default encoding.No
templateEncodingThe encoding of the template files. Defaults to platform default encoding.No
validationWhether to validate the XML input. Defaults to off.No
modelsA list of [name=]className pairs separated by spaces, commas, or semicolons that specifies further models that should be available to templates. If name is omitted, the unqualified class name is used as the name. Every class that is specified must implement the TemplateModel interface and have a no-args constructor.No

It supports the following nesed elements:

ElementDescriptionRequired
prepareModel This element executes Jython script before the processing of each XML files, that you can use to modify the data model. You either enter the Jython script directly nested into this element, or specify a Jython script file with the file attribute. The following variables are added to the Jython runtime's local namespace before the script is invoked:
  • model: The data model as java.util.HashMap. You can read and modify the data model with this variable.
  • doc: The XML document as org.w3c.dom.Document.
  • project: The project document (if used) as org.w3c.dom.Document.
If this element is used, Jython classes (tried with Jython 2.1) must be available.
No
prepareEnvironmentThis element executes Jython script before the processing of each XML files, that you can use to modify the freemarker environment (Environment). The script is executed after the prepareModel element. The accessible Jython variables are the same as with the prepareModel element, except that there is no model variable, but there is env variable, which is the FreeMarker environment (Environment). If this element is used, Jython classes (tried with Jython 2.1) must be available. No
Version:
$Id: FreemarkerXmlTask.java,v 1.58.2.1 2006/04/26 11:07:58 revusky Exp $
Authors:
Attila Szegedi
Jonathan Revusky, jon@revusky.com

Constructor Summary

FreemarkerXmlTask()
Constructor creates the SAXBuilder.

Method Summary

void
addConfiguredJython(JythonAntTask jythonAntTask)
void
addConfiguredPrepareEnvironment(JythonAntTask prepareEnvironment)
void
addConfiguredPrepareModel(JythonAntTask prepareModel)
void
execute()
protected void
insertDefaults(Map root)
void
setBasedir(File dir)
Set the base directory.
void
setDestdir(File dir)
Set the destination directory into which the generated files should be copied to
void
setEncoding(String encoding)
Set encoding for generated files.
void
setExtension(String extension)
Set the output file extension.
void
setIncremental(String incremental)
Turn on/off incremental processing.
void
setModels(String models)
void
setProjectfile(String projectAttribute)
Set the path to the project XML file
void
setTemplate(String templateName)
void
setTemplateDir(File templateDir)
void
setTemplateEncoding(String inputEncoding)
void
setValidation(boolean validation)
Sets whether to validate the XML input.

Constructor Details

FreemarkerXmlTask

public FreemarkerXmlTask()
Constructor creates the SAXBuilder.

Method Details

addConfiguredJython

public void addConfiguredJython(JythonAntTask jythonAntTask)

addConfiguredPrepareEnvironment

public void addConfiguredPrepareEnvironment(JythonAntTask prepareEnvironment)

addConfiguredPrepareModel

public void addConfiguredPrepareModel(JythonAntTask prepareModel)

execute

public void execute()
            throws BuildException

insertDefaults

protected void insertDefaults(Map root)

setBasedir

public void setBasedir(File dir)
Set the base directory. Defaults to .

setDestdir

public void setDestdir(File dir)
Set the destination directory into which the generated files should be copied to
Parameters:
dir - the name of the destination directory

setEncoding

public void setEncoding(String encoding)
Set encoding for generated files. Defaults to platform default encoding.

setExtension

public void setExtension(String extension)
Set the output file extension. .html by default.

setIncremental

public void setIncremental(String incremental)
Turn on/off incremental processing. On by default

setModels

public void setModels(String models)

setProjectfile

public void setProjectfile(String projectAttribute)
Set the path to the project XML file

setTemplate

public void setTemplate(String templateName)

setTemplateDir

public void setTemplateDir(File templateDir)
            throws BuildException

setTemplateEncoding

public void setTemplateEncoding(String inputEncoding)

setValidation

public void setValidation(boolean validation)
Sets whether to validate the XML input.