Velocity

About

Community

Docs

Tools

Comparisons

Site Translations

What is Texen?

Texen is a general purpose text generating utility. It is capable of producing almost any sort of text output. Driven by Ant, essentially an Ant Task, Texen uses a control template, an optional set of worker templates, and control context to govern the generated output. Although TexenTask can be used directly, it is usually subclassed to initialize your control context before generating any output.

Texen was created to deal with the source generating requirements of the Turbine web application framework. The Torque utility in Turbine, which is a subclass of the TexenTask, is responsible for generating the SQL, and the Object-Relational mapping sources for a Turbine project. This is only one example; you can use Texen to generate almost any sort of text output!


The TexenTask

This trivial example, which shows how to use Texen from an Ant build.xml, is intended to illustrate how the Texen mechanism works.

Ant Build File

<project name="HtmlGenerator" default="main" basedir=".">

  <taskdef name="texen" classname="org.apache.velocity.texen.ant.TexenTask"/>

  <!-- ================================================================ -->
  <!-- G E N E R A T E  H T M L  P A G E S                              -->
  <!-- ================================================================ -->
  <!-- This target will generate a set of HTML pages based on           -->
  <!-- the information in our control context.                          -->
  <!-- ================================================================ -->

  <target name="main">

    <echo message="+------------------------------------------+"/>
    <echo message="|                                          |"/>
    <echo message="| Generating HTML pages!                   |"/>
    <echo message="|                                          |"/>
    <echo message="+------------------------------------------+"/>

    <texen
      controlTemplate="Control.vm"
      outputDirectory="."
      templatePath="."
      outputFile="generation.report"
    />

  </target>

</project>

Control Template

#*

file: Control.vm

This is the control template for our HTML
page generator!

*#

#set ($Planets = ["Earth", "Mars", "Venus"])

#foreach ($planet in $Planets)

    #set ($outputFile = $strings.concat([$planet, ".html"]))
    $generator.parse("HtmlTemplate.vm", $outputFile, "planet", $planet)

#end

Worker Template

#*

file: HtmlTemplate.vm

This is worker template. It is called by the
control template to produce useful output (or
not so useful in this case). :-)

*#

#set ($bgcolor = "#ffffff")

<html>
  <head>
    <title>
      Everything you wanted to know about $planet!
    </title>
  </head>
  <body bgcolor="$bgcolor">

  $planet is a great place to live!

  </body>
</html>

Texen produces three html pages: Earth.html, Mars.html, and Venus.html. To do something more useful, you would subclass the TexenTask, place some objects in the control context, and use the information placed in the control context to generate useful output.

See the Torque utility in Turbine for a full working example of Texen. A standalone version of Torque is available here.

Name Description
templatePath [REQUIRED] Set the path where Velocity will look for templates using the file template loader. This is only required if useClasspath is not turned on.
useClasspath [REQUIRED] Set the use of the classpath in locating templates. true means the classpath will be used. This is only required if templatePath is not set.
useResourceLoaderCache Optional argument used when useClasspath is turned on. See the Developer's Guide for details on resource caching.
resourceLoaderModificationCheckInterval Optional argument used when useClasspath is turned on. See the Developer's Guide for details on resource caching.
controlTemplate [REQUIRED] Set the control template for the generating process.
outputDirectory [REQUIRED] Set the output directory. It will be created if it doesn't exist.
outputFile [REQUIRED] Set the output file for the generation process.
outputEncoding Set the output encoding.
inputEncoding Set the input (template) encoding.
contextProperties Set the context properties that will be fed into the initial context be the generating process starts.

We are going to do something special for properties that have a "file.contents" suffix: for these properties will pull in the contents of the file and make them available in the context. So for a line like the following in a properties file:

license.file.contents = license.txt
We will pull in the contents of license.txt and make it available in the context as $license. This should make texen a little more flexible.


Predefined Context Objects

The Texen Ant task places several objects into the Context for you.

Name Value
$generator This contains the the Generator instance used to output the text files.
$outputDirectory This contains the the value of the outputDirectory property given to the Texen Ant Task.
$strings This contains the an instance of a StringUtils object.
$files This contains the an instance of a FileUtil object.
$properties This contains the an instance of a PropertiesUtil object.



Copyright © 1999-2005, The Apache Software Foundation