You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Jean-Baptiste BRIAUD -- Novlog <j-...@novlog.com> on 2010/12/20 18:33:22 UTC

How to list Velocity template variables ?

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


Re: How to list Velocity template variables ?

Posted by Sergey Maslov <th...@gmail.com>.
Hi Jean-Baptiste,
I am glad I could help you.
Don`t get deep into JavaCC tree walker, better study velocity`s
javadocs and source code)

On Wed, Dec 22, 2010 at 11:39 AM, Jean-Baptiste BRIAUD -- Novlog
<j-...@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-...@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


Re: How to list Velocity template variables ?

Posted by Nathan Bubna <nb...@gmail.com>.
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-...@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-...@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


Re: How to list Velocity template variables ?

Posted by Jean-Baptiste BRIAUD -- Novlog <j-...@novlog.com>.
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-...@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


Re: How to list Velocity template variables ?

Posted by Sergey Maslov <th...@gmail.com>.
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-...@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


Re: How to list Velocity template variables ?

Posted by Antonio Petrelli <an...@gmail.com>.
2010/12/22 Jean-Baptiste BRIAUD -- Novlog <j-...@novlog.com>:
>
> On 22 déc. 2010, at 09:08, Antonio Petrelli wrote:
>> So, I think that you might create a tool, or a custom directive, to
>> say that you "require" a particular special object and put it in your
>> template context.
>
> Unfortunately, I can't do that because I can't force a user not to forget one #require.
> Also to validate the #require are all there, I'll need to get the list of all variables :-)

This is not what I meant. The client will notice that he forgot one
#require when he runs the template, not at upload time.
Anyway, just to help, the starting point is RuntimeInstance.parse
method, that will return the root SimpleNode. All the nodes can be one
in AST* classes.

Antonio

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


Re: How to list Velocity template variables ?

Posted by Jean-Baptiste BRIAUD -- Novlog <j-...@novlog.com>.
On 22 déc. 2010, at 09:08, Antonio Petrelli wrote:

> 2010/12/20 Jean-Baptiste BRIAUD -- Novlog <j-...@novlog.com>:
>> Before processing a template, I need to have a list of all variables needed by the template.
>> I had to proceed to complex validation.
>> ...
>> The reason why I need that : the end user can upload a template.
> 
> Sorry for the late response, but I was thinking about your question
> and it has not a good answer.
> For example, suppose that you use a #set directive, or a tool that
> fills one variable, have you got to remove it from the list of
> variables?
> So, I think that you might create a tool, or a custom directive, to
> say that you "require" a particular special object and put it in your
> template context.
> 
> Antonio
> 
Hi Antonio,

Unfortunately, I can't do that because I can't force a user not to forget one #require.
Also to validate the #require are all there, I'll need to get the list of all variables :-)

I'll try the template AST walker approach.

Thanks !


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


Re: How to list Velocity template variables ?

Posted by Antonio Petrelli <an...@gmail.com>.
2010/12/20 Jean-Baptiste BRIAUD -- Novlog <j-...@novlog.com>:
> Before processing a template, I need to have a list of all variables needed by the template.
> I had to proceed to complex validation.
>...
> The reason why I need that : the end user can upload a template.

Sorry for the late response, but I was thinking about your question
and it has not a good answer.
For example, suppose that you use a #set directive, or a tool that
fills one variable, have you got to remove it from the list of
variables?
So, I think that you might create a tool, or a custom directive, to
say that you "require" a particular special object and put it in your
template context.

Antonio

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