package org.radsimplified.genapp;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class VelocityUtil {
private static VelocityEngine engine = getVelocityEngine();
public static String mergeTemplateIntoString(String templatePath, VelocityContext appData) {
try
{
// Fetch Template to a StringWriter
Template template = engine.getTemplate(templatePath);
StringWriter writer = new StringWriter();
template.merge(appData, writer);
return writer.toString();
}
catch (Exception e)
{
System.out.println("Oops! We have an exception");
e.printStackTrace();
return null;
}
}
private static VelocityEngine getVelocityEngine() {
Properties p = new Properties();
p.setProperty("resource.loader", "file");
p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
p.setProperty("file.resource.loader.path", GenApp.properties.getProperty("templatePath"));
VelocityEngine engine = new VelocityEngine();
try {
engine.init(p);
} catch (Exception e) {
System.out.println("Oops! We have an exception");
e.printStackTrace();
}
return engine;
}
}