use of org.jaffa.modules.printing.services.FormCache in project jaffa-framework by jaffa-projects.
the class VelocityTemplateHelper method renderTemplateLines.
public String[] renderTemplateLines() throws FormPrintException {
String[] lines;
String line;
if (log.isDebugEnabled())
log.debug("Begin Velocity template rendering process... ");
if (getDataSource() == null) {
log.debug("Data source is not set, returning.");
return null;
}
if (getTemplate() == null) {
log.debug("Template definition is not set, returning.");
return null;
}
try {
// Since Velocity can only be initialized one time, set the template path used by label printing.
FormCache cache = new FormCache();
String path = cache.getTemplatePath();
Velocity.setProperty("file.resource.loader.path", path);
Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
// init the runtime engine. Use mostly defaulted values.
Velocity.init();
if (log.isDebugEnabled()) {
log.debug("Initialized Velocity");
log.debug("Velocity file.resource.loader.path property = " + Velocity.getProperty("file.resource.loader.path"));
}
} catch (Exception e) {
String err = "Velocity Initialization - " + e.getLocalizedMessage();
log.error(err, e);
throw new EngineProcessingException(err, e);
}
// make a Context and put data into it
if (log.isDebugEnabled())
log.debug("Set Up Context");
VelocityContext context = new VelocityContext();
context.put("data", getDataSource());
fmt = new FormFormatHelper();
context.put("fmt", fmt);
context.put("context", ContextManagerFactory.instance().getThreadContext());
if (log.isDebugEnabled()) {
log.debug("*** Template Source Data = \n" + BeanMoulder.printBean(getDataSource()) + "\n*** End of Source Data.");
log.debug("*** Velocity Template = \n" + getTemplate() + "\n*** End of Velocity Template.");
}
try {
m_output = new StringWriter();
if (log.isDebugEnabled())
log.debug("Evaluate the Template... ");
// Render the template
Velocity.evaluate(context, m_output, "formTemplate", getTemplate());
line = m_output.toString();
lines = line.split("\n");
if (log.isDebugEnabled()) {
log.debug("*** Rendered Template = \n" + line + "\n*** End of Rendered Template.");
}
} catch (Exception e) {
String err = "Velocity Error - " + e.getLocalizedMessage();
log.error(err, e);
throw new EngineProcessingException(err, e);
}
if (log.isDebugEnabled())
log.debug("Finished Velocity template rendering process. ");
return lines;
}
Aggregations