Re: generate java class for a VTL template, is there any solution?
Sergiu Dumitriu <[email protected]>
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <[email protected]> |
On 05/14/2013 09:44 PM, bluejoe2008 wrote:
> Dear all,
>
> I am looking for a solution of generating a java class from a template written in VTL.
>
> Below I descible the process how I want to use it:
>
> 1. I load a template from a.vtl which is written by user:
>
> #set($b=$a+1)
>
> 2. then I use the generator tool Vtl2javaTool:
>
> new Vtl2javaTool().generate("a.vtl", "test.A");
>
> 3. then I can use the test.A class:
>
> cntext.put("a", 100);
> Class.forName("test.A").newInstance().execute(context, writer);
> int b = context.get("b");
>
> is there any existing solution avaliable? Any comments from you are welcome.
> many thanks!
Not really, since Velocity is purely an interpreted language.
While many JVM based languages do get compiled into real bytecode, which
can be treated as any other compiled Java class, Velocity is parsed into
an AST [1], which is then rendered by the Velocity engine.
If you're just interested in performance, then this AST can be cached in
order to avoid reparsing the same file.
[1] http://en.wikipedia.org/wiki/Abstract_syntax_tree