You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Simon Gubler <sg...@gmail.com> on 2008/05/12 08:29:31 UTC
A tool to analyze your templates
Hi all
I have written a small tool that scanns velocity-files and extracts all
called variables. It's a command-line tool, you can execute it like this:
java -jar TemplateAnalyzer.jar folder1 folder2
In folder1 are your velocity files to analyze, in folder2 the tool will
generate velocity-files with the same name as in folder1 but only with the
accessed variables as content.
I made it an sourceforge project so you can view the code. You can find the
project here: http://sourceforge.net/projects/vmanalyzer. I don't know if
anything like this already exists, I didn't find anything under sourceforge.
There are some things I would like to add in the future, like for example
recursively scanning the velocity-files, but for now I hope it can be of use
to you.
Simon Gubler
Re: A tool to analyze your templates
Posted by Simon Gubler <sg...@gmail.com>.
Nathan Bubna schrieb:
> On Mon, May 12, 2008 at 1:10 PM, Simon Gubler <sg...@gmail.com> wrote:
>
>> Hi Ahmed
>>
>> I have to say, that I don't use the velocity API, I basically scan the files
>> for #foreach, #end and ${.*} or $!{.*} Expressions and list them in the
>> outputfile. When I have time (probably next month) I will look into the
>> velocity API. If I can use it I won't have to implement the grammar myself.
>>
>
> When you do that, referencing the following might help:
> http://svn.apache.org/repos/asf/velocity/engine/trunk/experimental/templatetool/
>
> it's an old experiment. i've no idea if it still works, but it had a
> similar goal.
>
>
>> Greetings
>>
>> Simon
>>
>>
>>
>> On Mon, May 12, 2008 at 5:32 PM, Ahmed Mohombe <am...@yahoo.com> wrote:
>>
>> > I think it has to know this because if you have for example #foreach($day
>> > > in
>> > > ${days}) you don't want to include the variable ${day} in the list but
>> > > rather ${days}.
>> > > Do you agree with me?
>> > >
>> > Of course.
>> > I just thought that the Velocity AST is able to distinct between "local
>> > variabls" like
>> > $day and "global" ones.
>> >
>> >
>> > Ahmed.
>> >
>> >
>> > ---------------------------------------------------------------------
>> > 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
>
>
>
Hi everyone
As I said earlier, I've worked on the tool and rebuilt it. Now it works
with the velocity API that Nathan Bubna has given me. Instead of putting
all the refernces in a list I build a tree structure where the
references are leaves and the #foreach-loops are nodes.
TemplateAnalyzer_TreeGui.jar shows this structure in a Swing-GUI as a
JTree.
Execute it like this: java -jar TemplateAnalyzer_TreeGui.jar foo.vm
foo.vm is the name of the template.
You can download the new version 1.1 here:
http://sourceforge.net/project/showfiles.php?group_id=226718.
I hope you can use it to get an overview of your templates.
Kind regards
Simon
PS: @Ahmed: To scan for the used references for the user written
templates I would recommend you to use the TemplateTool from Nathan
Bubna: Just get your template, call TemplateTool.referenceList() with
the template as parameter, and look at each entry in the returned list
if it contains a forbidden reference.
Re: A tool to analyze your templates
Posted by Simon Gubler <sg...@gmail.com>.
Thank you, it works! Looks like the visitor pattern. I think I can use this
as a basis for the rework.
Greetings
Simon
On Mon, May 12, 2008 at 10:14 PM, Nathan Bubna <nb...@gmail.com> wrote:
> On Mon, May 12, 2008 at 1:10 PM, Simon Gubler <sg...@gmail.com> wrote:
> > Hi Ahmed
> >
> > I have to say, that I don't use the velocity API, I basically scan the
> files
> > for #foreach, #end and ${.*} or $!{.*} Expressions and list them in the
> > outputfile. When I have time (probably next month) I will look into the
> > velocity API. If I can use it I won't have to implement the grammar
> myself.
>
> When you do that, referencing the following might help:
>
> http://svn.apache.org/repos/asf/velocity/engine/trunk/experimental/templatetool/
>
> it's an old experiment. i've no idea if it still works, but it had a
> similar goal.
>
> > Greetings
> >
> > Simon
> >
> >
> >
> > On Mon, May 12, 2008 at 5:32 PM, Ahmed Mohombe <am...@yahoo.com>
> wrote:
> >
> > > I think it has to know this because if you have for example
> #foreach($day
> > > > in
> > > > ${days}) you don't want to include the variable ${day} in the list
> but
> > > > rather ${days}.
> > > > Do you agree with me?
> > > >
> > > Of course.
> > > I just thought that the Velocity AST is able to distinct between
> "local
> > > variabls" like
> > > $day and "global" ones.
> > >
> > >
> > > Ahmed.
> > >
> > >
> > > ---------------------------------------------------------------------
> > > 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: A tool to analyze your templates
Posted by Nathan Bubna <nb...@gmail.com>.
On Mon, May 12, 2008 at 1:10 PM, Simon Gubler <sg...@gmail.com> wrote:
> Hi Ahmed
>
> I have to say, that I don't use the velocity API, I basically scan the files
> for #foreach, #end and ${.*} or $!{.*} Expressions and list them in the
> outputfile. When I have time (probably next month) I will look into the
> velocity API. If I can use it I won't have to implement the grammar myself.
When you do that, referencing the following might help:
http://svn.apache.org/repos/asf/velocity/engine/trunk/experimental/templatetool/
it's an old experiment. i've no idea if it still works, but it had a
similar goal.
> Greetings
>
> Simon
>
>
>
> On Mon, May 12, 2008 at 5:32 PM, Ahmed Mohombe <am...@yahoo.com> wrote:
>
> > I think it has to know this because if you have for example #foreach($day
> > > in
> > > ${days}) you don't want to include the variable ${day} in the list but
> > > rather ${days}.
> > > Do you agree with me?
> > >
> > Of course.
> > I just thought that the Velocity AST is able to distinct between "local
> > variabls" like
> > $day and "global" ones.
> >
> >
> > Ahmed.
> >
> >
> > ---------------------------------------------------------------------
> > 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: A tool to analyze your templates
Posted by Simon Gubler <sg...@gmail.com>.
Hi Ahmed
I have to say, that I don't use the velocity API, I basically scan the files
for #foreach, #end and ${.*} or $!{.*} Expressions and list them in the
outputfile. When I have time (probably next month) I will look into the
velocity API. If I can use it I won't have to implement the grammar myself.
Greetings
Simon
On Mon, May 12, 2008 at 5:32 PM, Ahmed Mohombe <am...@yahoo.com> wrote:
> I think it has to know this because if you have for example #foreach($day
> > in
> > ${days}) you don't want to include the variable ${day} in the list but
> > rather ${days}.
> > Do you agree with me?
> >
> Of course.
> I just thought that the Velocity AST is able to distinct between "local
> variabls" like
> $day and "global" ones.
>
>
> Ahmed.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
Re: A tool to analyze your templates
Posted by Ahmed Mohombe <am...@yahoo.com>.
> I think it has to know this because if you have for example #foreach($day in
> ${days}) you don't want to include the variable ${day} in the list but
> rather ${days}.
> Do you agree with me?
Of course.
I just thought that the Velocity AST is able to distinct between "local variabls" like
$day and "global" ones.
Ahmed.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: A tool to analyze your templates
Posted by Simon Gubler <sg...@gmail.com>.
Hi Ahmed
Thank you for your reply. I think this could be possible but I will have to
work on it because now it doesn't recognize when a foreach-Block ends.
I think it has to know this because if you have for example #foreach($day in
${days}) you don't want to include the variable ${day} in the list but
rather ${days}.
Do you agree with me?
Greetings
Simon
On Mon, May 12, 2008 at 12:34 PM, Ahmed Mohombe <am...@yahoo.com> wrote:
> I have written a small tool that scanns velocity-files and extracts all
> > called variables.
> >
> Very nice, thank you.
>
> It's a command-line tool, you can execute it like this:
> >
> > java -jar TemplateAnalyzer.jar folder1 folder2
> > In folder1 are your velocity files to analyze, in folder2 the tool will
> > generate velocity-files with the same name as in folder1 but only with
> > the
> > accessed variables as content.
> >
> Would you consider adding a simple and *documented* API about how to use
> it directly from code? (E.g. to get a List with all variables from a
> specific template?)
>
> This would be very usable for "user written templates" like CMSes to check
> if the
> user in it's code is accessing only allowed variables :) (and e.g. only
> than to allow upload/save on the server).
>
> Thanks in advance,
>
> Ahmed.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
Re: A tool to analyze your templates
Posted by Ahmed Mohombe <am...@yahoo.com>.
> I have written a small tool that scanns velocity-files and extracts all
> called variables.
Very nice, thank you.
> It's a command-line tool, you can execute it like this:
>
> java -jar TemplateAnalyzer.jar folder1 folder2
> In folder1 are your velocity files to analyze, in folder2 the tool will
> generate velocity-files with the same name as in folder1 but only with the
> accessed variables as content.
Would you consider adding a simple and *documented* API about how to use
it directly from code? (E.g. to get a List with all variables from a specific template?)
This would be very usable for "user written templates" like CMSes to check if the
user in it's code is accessing only allowed variables :) (and e.g. only than to allow upload/save on
the server).
Thanks in advance,
Ahmed.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org