You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Holger Willebrandt <ho...@cs.uni-dortmund.de> on 2006/03/15 11:07:56 UTC

Collect variables from a template

Hello everybody,

i would like to parse a velocity template and collect information about
all variables that are used within it.
Therefore, i use the Parser class in the following way:

//here a reader for the template and a collector for the variables
//will be supplied
public static void findVariables(Reader reader, List collector)
		throws ParseException {
	VelocityCharStream cstream = new VelocityCharStream(reader, 0, 0);
	Parser parser = new Parser(cstream);
	findVariables(parser.process(), collector); //call method below
}

//this method will recursively collect the variables and store them
//in the collector.
public static void findVariables(Node startNode, List collector) {
	if (startNode.getClass().equals(ASTReference.class)) {
		if (!collector.contains(startNode.literal())) {
			collector.add(startNode.literal());
		}
	}
	for (int i = 0; i < startNode.jjtGetNumChildren(); i++) {
		findVariables(startNode.jjtGetChild(i), collector);
	}
}

The code is working as long as the template does not contain a #foreach statement.
Whenever i try to parse velocity code with a #foreach, i get the following error:

java.lang.NullPointerException
         at org.apache.velocity.runtime.parser.Parser.Directive(Parser.java:628)
         at org.apache.velocity.runtime.parser.Parser.Statement(Parser.java:319)
         at org.apache.velocity.runtime.parser.Parser.process(Parser.java:258)

Looking into velocity's source code, revealed that the Parser is missing a 
RuntimeServices instance, resp. a template name.

It would be nice to have a way of collecting all variables and validating the 
synatx (a ParseException would indicate a syntax error) at once.

Any kind of help or advice is welcome.

Thanks, Holger



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


Re: Collect variables from a template

Posted by Will Glass-Husain <wg...@forio.com>.
Ok, glad you will be able to get your needs met.

WILL

On 3/22/06, Holger Willebrandt <ho...@cs.uni-dortmund.de>
wrote:
>
> Der Will,
>
> I'm sorry that i can't give you results of testing with 1.5-dev, as i
> didn't manage to get velocity-1.5-dev compiled yet. Should I take it
> from the snapshosts or is there a 1.5-dev jar ?
>
> I need to get this working and can't spent much time on experiments.
> I took a look at the TemplateTool and it seems to be suitable (Thanks,
> Claude), so i for myself am through with this issue ;-)
>
> Thanks for your replies.
>
> Holger
>
> Will Glass-Husain wrote:
> > Also, have you tried this with the latest source code?  (velocity
> > 1.5dev)?   If so, can you give us the exception trace with the latest
> > source?
> >
> > WILL
> >
> > On 3/15/06, Claude Brisson <cl...@renegat.net> wrote:
> >
> >>Hi.
> >>
> >>Maybe you can check the TemplateTool in the experimental section :
> >>
> >>
> >>
> http://svn.apache.org/repos/asf/jakarta/velocity/engine/trunk/experimental/templatetool/TemplateTool.java
> >>
> >>  Claude
> >>
> >>Le mercredi 15 mars 2006 à 11:07 +0100, Holger Willebrandt a écrit :
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


--
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Collect variables from a template

Posted by Holger Willebrandt <ho...@cs.uni-dortmund.de>.
Der Will,

I'm sorry that i can't give you results of testing with 1.5-dev, as i
didn't manage to get velocity-1.5-dev compiled yet. Should I take it
from the snapshosts or is there a 1.5-dev jar ?

I need to get this working and can't spent much time on experiments.
I took a look at the TemplateTool and it seems to be suitable (Thanks,
Claude), so i for myself am through with this issue ;-)

Thanks for your replies.

Holger

Will Glass-Husain wrote:
> Also, have you tried this with the latest source code?  (velocity
> 1.5dev)?   If so, can you give us the exception trace with the latest
> source?
> 
> WILL
> 
> On 3/15/06, Claude Brisson <cl...@renegat.net> wrote:
> 
>>Hi.
>>
>>Maybe you can check the TemplateTool in the experimental section :
>>
>>
>>http://svn.apache.org/repos/asf/jakarta/velocity/engine/trunk/experimental/templatetool/TemplateTool.java
>>
>>  Claude
>>
>>Le mercredi 15 mars 2006 à 11:07 +0100, Holger Willebrandt a écrit :
>>

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


Re: Collect variables from a template

Posted by Will Glass-Husain <wg...@forio.com>.
Also, have you tried this with the latest source code?  (velocity
1.5dev)?   If so, can you give us the exception trace with the latest
source?

WILL

On 3/15/06, Claude Brisson <cl...@renegat.net> wrote:
>
> Hi.
>
> Maybe you can check the TemplateTool in the experimental section :
>
>
> http://svn.apache.org/repos/asf/jakarta/velocity/engine/trunk/experimental/templatetool/TemplateTool.java
>
>   Claude
>
> Le mercredi 15 mars 2006 à 11:07 +0100, Holger Willebrandt a écrit :
> > Hello everybody,
> >
> > i would like to parse a velocity template and collect information about
> > all variables that are used within it.
> > Therefore, i use the Parser class in the following way:
> >
> > //here a reader for the template and a collector for the variables
> > //will be supplied
> > public static void findVariables(Reader reader, List collector)
> >               throws ParseException {
> >       VelocityCharStream cstream = new VelocityCharStream(reader, 0, 0);
> >       Parser parser = new Parser(cstream);
> >       findVariables(parser.process(), collector); //call method below
> > }
> >
> > //this method will recursively collect the variables and store them
> > //in the collector.
> > public static void findVariables(Node startNode, List collector) {
> >       if (startNode.getClass().equals(ASTReference.class)) {
> >               if (!collector.contains(startNode.literal())) {
> >                       collector.add(startNode.literal());
> >               }
> >       }
> >       for (int i = 0; i < startNode.jjtGetNumChildren(); i++) {
> >               findVariables(startNode.jjtGetChild(i), collector);
> >       }
> > }
> >
> > The code is working as long as the template does not contain a #foreach
> statement.
> > Whenever i try to parse velocity code with a #foreach, i get the
> following error:
> >
> > java.lang.NullPointerException
> >          at org.apache.velocity.runtime.parser.Parser.Directive(
> Parser.java:628)
> >          at org.apache.velocity.runtime.parser.Parser.Statement(
> Parser.java:319)
> >          at org.apache.velocity.runtime.parser.Parser.process(
> Parser.java:258)
> >
> > Looking into velocity's source code, revealed that the Parser is missing
> a
> > RuntimeServices instance, resp. a template name.
> >
> > It would be nice to have a way of collecting all variables and
> validating the
> > synatx (a ParseException would indicate a syntax error) at once.
> >
> > Any kind of help or advice is welcome.
> >
> > Thanks, Holger
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


--
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: Collect variables from a template

Posted by Claude Brisson <cl...@renegat.net>.
Hi.

Maybe you can check the TemplateTool in the experimental section :

http://svn.apache.org/repos/asf/jakarta/velocity/engine/trunk/experimental/templatetool/TemplateTool.java

  Claude

Le mercredi 15 mars 2006 à 11:07 +0100, Holger Willebrandt a écrit :
> Hello everybody,
> 
> i would like to parse a velocity template and collect information about
> all variables that are used within it.
> Therefore, i use the Parser class in the following way:
> 
> //here a reader for the template and a collector for the variables
> //will be supplied
> public static void findVariables(Reader reader, List collector)
> 		throws ParseException {
> 	VelocityCharStream cstream = new VelocityCharStream(reader, 0, 0);
> 	Parser parser = new Parser(cstream);
> 	findVariables(parser.process(), collector); //call method below
> }
> 
> //this method will recursively collect the variables and store them
> //in the collector.
> public static void findVariables(Node startNode, List collector) {
> 	if (startNode.getClass().equals(ASTReference.class)) {
> 		if (!collector.contains(startNode.literal())) {
> 			collector.add(startNode.literal());
> 		}
> 	}
> 	for (int i = 0; i < startNode.jjtGetNumChildren(); i++) {
> 		findVariables(startNode.jjtGetChild(i), collector);
> 	}
> }
> 
> The code is working as long as the template does not contain a #foreach statement.
> Whenever i try to parse velocity code with a #foreach, i get the following error:
> 
> java.lang.NullPointerException
>          at org.apache.velocity.runtime.parser.Parser.Directive(Parser.java:628)
>          at org.apache.velocity.runtime.parser.Parser.Statement(Parser.java:319)
>          at org.apache.velocity.runtime.parser.Parser.process(Parser.java:258)
> 
> Looking into velocity's source code, revealed that the Parser is missing a 
> RuntimeServices instance, resp. a template name.
> 
> It would be nice to have a way of collecting all variables and validating the 
> synatx (a ParseException would indicate a syntax error) at once.
> 
> Any kind of help or advice is welcome.
> 
> Thanks, Holger
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 


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