Here I only want to demonstrate how to integrate it with Domino nicely as it requires to write TemplateLoader class.
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
Get the template (uses cache internally)
DominoTemplateLoader dominoLoader = new DominoTemplateLoader(getDatabase());
cfg.setTemplateLoader(dominoLoader);
Template template = cfg.getTemplate("page");
/* Merge data-model with template */
HashMap tags = new HashMap();
tags.put("title", "hellow world");
tags.put("description", "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.");
Writer out = new StringWriter();
template.process(tags, out);
String html = out.toString();
The most important part was actually to build DominoTemplateLoader class and below you can see it
public class DominoTemplateLoader implements TemplateLoader {
private View m_view;
public DominoTemplateLoader(Database database) throws NotesException {
m_view = database.getView("($Template)");
}
public void closeTemplateSource(Object templateSource) throws IOException {
Document doc = (Document) templateSource;
try {
doc.recycle();
} catch (NotesException e) {
e.printStackTrace();
}
}
public Object findTemplateSource(String id) throws IOException {
try {
return m_view.getDocumentByKey(id, true);
} catch (NotesException e) {
e.printStackTrace();
}
return null;
}
public long getLastModified(Object templateSource) {
Document doc = (Document) templateSource;
try {
return doc.getLastModified().toJavaDate().getTime();
} catch (NotesException e) {
e.printStackTrace();
}
return 0;
}
public Reader getReader(Object templateSource, String encoding) throws IOException {
if (templateSource == null) return null;
Document doc = (Document) templateSource;
try {
return doc.getFirstItem("Body").getReader();
} catch (NotesException e) {
e.printStackTrace();
}
return null;
}
}
As you can see the Loader class get document form a view and simply get data from item Body.
No comments :
Post a Comment