velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Maslov <therealma...@gmail.com>
Subject Re: How to list Velocity template variables ?
Date Wed, 22 Dec 2010 08:07:22 GMT
Well, according to the project page grammar for Velocity is processed
in a parser generated by JavaCC (Java Compiler Compiler) using the
JJTree extension to create an Abstract Syntax Tree.
You can read Velocity template in a node-walking style:
you can extend BaseVisitor and create your own visitor for velocity
template, implementing visit() methods.
This methods are called implicitly during template parsing.
For example, visit( ASTReference node, Object data) will be called on
each reference (variable) visiting.
visit( ASTDirective node, Object data) will be called on each
directive visiting.

So you can create class MyVariableVisitor, storing some kind of list
(set or possibly map) of template variables.

You can implement visit( ASTReference node, Object data) in a such way:

 public Object visit( ASTReference node, Object data)
    {

        String name = node.literal(); //here we`ll have variable name
...
        allVariables.add(name); //suppose, we have allVariables list
to store template variables

}

And then you can add method getList() or something, to return the
variables list.
So the simplified code will look like:

 public List variableList(Template template )
    {
        SimpleNode sn = (SimpleNode) template.getData();

        MyVariableVisitor mvv = new MyVariableVisitor();

        Object o1 = new Object();

        sn.jjtAccept(mvv, o1);

        return mvv.getList();
    }


You can read more about visitor pattern and abstract syntax trees for
better understanding of the concept.

Sometimes it is necessary to know about variable context if it is used
in foreach (complex object with getter methods), or, if-else clause
(boolean values)). So you`ll need to implement visit( ASTDirective
node, Object data) and put your logic inside (don`t forget to
accept children of the node).


On Mon, Dec 20, 2010 at 8:33 PM, Jean-Baptiste BRIAUD -- Novlog
<j-b.briaud@novlog.com> wrote:
> Hi,
>
> Before processing a template, I need to have a list of all variables needed by the template.
> I had to proceed to complex validation.
>
> I thought to process the template without context and handle the event to get all missing
variables.
> Unfortunately, this would not take the variable in #ELSE clause for example in case of
#IF.
>
> VTL is interpreted but is there a way to handle all variable in a template ?
>
> The other approach I was thinking about is to build a grammar using SableCC for example.
> Then, I would be able to parse a template and check all variables.
> The bonus would be to ensure the template VTL syntax is OK.
>
> The reason why I need that : the end user can upload a template.
>
> Any ideas, advices ?
>
> Thanks !
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Mime
View raw message