Wong, Christopher
2014-06-13 13:03:49 UTC
I would like to introduce a new project, "freemarker-introspection". Meant to supplement the core Freemarker project, the purpose of this project is to provide a programmatic way to inspect and modify a Freemarker template. While Freemarker has an object representation of a parsed template, this representation is generally not externally accessible. The primary feature of freemarker-introspection is to expose the parsed Freemarker template as a DOM-like AST object that you can navigate in Java:
Element root = TemplateIntrospector.getRootNode(template);
for (Element child : root.getChildren()) {
for (Expr param : child.getParams()) {
...
}
}
You can also use a Visitor pattern to walk the template nodes. I implemented 2 classes that serve as consumers of this output.
VariableFinder scans the template and returns information on variables that the template will reference from the model:
Element root = TemplateIntrospector.getRootNode(template);
List<VariableInfo> vars = new VariableFinder(root).seek().getVariableInfo();
TemplateEditor lets you replace specific nodes (elements and expressions) with your own content:
Element root = TemplateIntrospector.getRootNode(template);
Element elem = ...; // find element node in tree to replace
Expr expr = ...; // find expression node in tree to replace
String newTemplateText = new TemplateEditor(oldTemplateText)
.replace(elem, "<#include 'something_else.ftl'>")
.replace(expr, "123")
.apply()
.getModifiedTemplate();
You can find freemarker-introspection at:
https://github.com/cwong15/freemarker-introspection
You will need Gradle to build it. It currently works with Freemarker versions 2.3.19 and 2.3.20. This project is still in its very early stages, so your feedback will be very much welcome.
Chris
________________________________
This e-mail and files transmitted with it are confidential, and are intended solely for the use of the individual or entity to whom this e-mail is addressed. If you are not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you are not one of the named recipient(s) or otherwise have reason to believe that you received this message in error, please immediately notify sender by e-mail, and destroy the original message. Thank You.
Element root = TemplateIntrospector.getRootNode(template);
for (Element child : root.getChildren()) {
for (Expr param : child.getParams()) {
...
}
}
You can also use a Visitor pattern to walk the template nodes. I implemented 2 classes that serve as consumers of this output.
VariableFinder scans the template and returns information on variables that the template will reference from the model:
Element root = TemplateIntrospector.getRootNode(template);
List<VariableInfo> vars = new VariableFinder(root).seek().getVariableInfo();
TemplateEditor lets you replace specific nodes (elements and expressions) with your own content:
Element root = TemplateIntrospector.getRootNode(template);
Element elem = ...; // find element node in tree to replace
Expr expr = ...; // find expression node in tree to replace
String newTemplateText = new TemplateEditor(oldTemplateText)
.replace(elem, "<#include 'something_else.ftl'>")
.replace(expr, "123")
.apply()
.getModifiedTemplate();
You can find freemarker-introspection at:
https://github.com/cwong15/freemarker-introspection
You will need Gradle to build it. It currently works with Freemarker versions 2.3.19 and 2.3.20. This project is still in its very early stages, so your feedback will be very much welcome.
Chris
________________________________
This e-mail and files transmitted with it are confidential, and are intended solely for the use of the individual or entity to whom this e-mail is addressed. If you are not the intended recipient, or the employee or agent responsible to deliver it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you are not one of the named recipient(s) or otherwise have reason to believe that you received this message in error, please immediately notify sender by e-mail, and destroy the original message. Thank You.