}
ve.init();
// Create the text generator.
Generator generator = Generator.getInstance();
generator.setVelocityEngine(ve);
generator.setOutputPath(outputDirectory);
generator.setInputEncoding(inputEncoding);
generator.setOutputEncoding(outputEncoding);
if (templatePath != null)
{
generator.setTemplatePath(templatePath);
}
// Make sure the output directory exists, if it doesn't
// then create it.
File file = new File(outputDirectory);
if (! file.exists())
{
file.mkdirs();
}
String path = outputDirectory + File.separator + outputFile;
log("Generating to file " + path, Project.MSG_INFO);
Writer writer = generator.getWriter(path, outputEncoding);
// The generator and the output path should
// be placed in the init context here and
// not in the generator class itself.
Context c = initControlContext();
// Everything in the generator class should be
// pulled out and placed in here. What the generator
// class does can probably be added to the Velocity
// class and the generator class can probably
// be removed all together.
populateInitialContext(c);
// Feed all the options into the initial
// control context so they are available
// in the control/worker templates.
if (contextProperties != null)
{
Iterator i = contextProperties.getKeys();
while (i.hasNext())
{
String property = (String) i.next();
String value = StringUtils.nullTrim(contextProperties.getString(property));
// Now lets quickly check to see if what
// we have is numeric and try to put it
// into the context as an Integer.
try
{
c.put(property, new Integer(value));
}
catch (NumberFormatException nfe)
{
// Now we will try to place the value into
// the context as a boolean value if it
// maps to a valid boolean value.
String booleanString =
contextProperties.testBoolean(value);
if (booleanString != null)
{
c.put(property, Boolean.valueOf(booleanString));
}
else
{
// 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.
if (property.endsWith("file.contents"))
{
// We need to turn the license file from relative to
// absolute, and let Ant help :)
value = StringUtils.fileContentsToString(
project.resolveFile(value).getCanonicalPath());
property = property.substring(
0, property.indexOf("file.contents") - 1);
}
c.put(property, value);
}
}
}
}
writer.write(generator.parse(controlTemplate, c));
writer.flush();
writer.close();
generator.shutdown();
cleanup();
}
catch( BuildException e)
{
throw e;