Introducing freemarker-introspection

"Wong, Christopher" <[email protected]> Fri, 13 Jun 2014 09:03:49 -0400
Newsgroups gmane.comp.web.freemarker.user
Message-ID <5900C695468EF8469DF7867FD277BA1E95A1B69280@PMACPWEXCLS01.corp.epsilon.com>
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.

------------------------------------------------------------------------------
HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions
Find What Matters Most in Your Big Data with HPCC Systems
Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.
Leverages Graph Analysis for Fast Processing & Easy Data Exploration
http://p.sf.net/sfu/hpccsystems

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user