velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan Bubna <nbu...@gmail.com>
Subject Re: How to list Velocity template variables ?
Date Thu, 23 Dec 2010 15:59:29 GMT
Yes, it would be useful to other people if you could share that.  This
is a regular, if infrequent, request.

On Wed, Dec 22, 2010 at 12:39 AM, Jean-Baptiste BRIAUD -- Novlog
<j-b.briaud@novlog.com> wrote:
> Hi Sergey,
>
> Thanks for the answer, this is exactly what I need.
> I don't know JavaCC but I know SableCC which has AST walker built-in.
> I'll learn what I need about JavaCC tree walker classes.
>
> Thanks a lot.
> I'll let you know what happen.
> I could give that code as a Velocity tool if it is found useful.
>
> On 22 déc. 2010, at 09:07, Sergey Maslov wrote:
>
>> 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
>>
>
>
> ---------------------------------------------------------------------
> 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