private void processPlan(VelocityEngine velocity, File plan) throws Exception {
// Get the plan name, which is basically plan.getAbsolutePath() - sourceDir.getAbsolutePath()
String planName = extractPlanName(sourceDir, plan);
// load the template
Template template = velocity.getTemplate(planName);
// determine the output file
File outputFile = new File(targetDir, planName);
// create the output directory
File outputDir = outputFile.getParentFile();
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
throw new IllegalArgumentException("Could not create outputDir: " + outputDir.getAbsolutePath());
}
}
if (!outputDir.isDirectory()) {
throw new IllegalArgumentException("outputDir is not a directory: " + outputDir.getAbsolutePath());
}
if (force || outputFile.lastModified() < plan.lastModified()) {
System.out.println(" Preprocessing " + planName);
// process the plan
PrintStream out = null;
FileReader templateReader = null;
try {
out = new PrintStream(new FileOutputStream(outputFile));
PrintWriter writer = new PrintWriter(out);
template.merge(context, writer);
writer.flush();
} finally {
close(out);
close(templateReader);
}