velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Boland" <>
Subject RE: Parsing the results of a reference
Date Sat, 18 May 2002 21:48:51 GMT
That's one of the reason's I went down this path. I actually found a
different solution which I will pass along here. As I type this
(well...before and after I type this), I'm working on these solutions
for submission to the velocity-tools endeavor. Maybe this will provide
some early feedback.

I'm attaching the Velocity Tool as it stands today. It is real simple,
so please be kind. :) It still has logging (I've been using the Common's
logging API). I've never attached a file via this list so I hope this
works. If not, I can email direct. 

I'm also attaching a VelocityToolTest.vm file for testing it. Again,
real simple stuff.

Finally, here's a very simple method that can be used as a tool if you
include the Common's project org.apache.commons.beanutils package in
your libs or classpath. This is what I finally used to dynamically
evaluate a property of a bean where the name of the property was coming
from a table. This allows for simple, nested, indexed and mapped
property evaluation. Like I said, very simple stuff. But simple is good
for building. :)


import org.apache.commons.beanutils.*;
import java.lang.reflect.*;

public class UtilityTool

    public UtilityTool()

    public Object getProperty( Object bean, String name )
        throws NoSuchMethodException, InvocationTargetException,
        return PropertyUtils.getProperty( bean, name );

    // other utility tool methods here


-----Original Message-----
From: Immo Hüneke [mailto:Immo@Huneke.Co.UK] 
Sent: Saturday, May 18, 2002 1:21 PM
Subject: RE: Parsing the results of a reference

Hi Bill, Geir,

I'm so used to the idea of running "eval" in Perl that it took me some
to realise that in VTL you can't just invoke the parser whenever you

I would be very interested in the results of your experiments with a
tool to
call Velocity.evaluate(). In case you're interested, what I am trying to
is a computed method call or property reference.

The property / method name is in a variable, "$key", and I want to
dereference the context variable "$data" by calling its "get$key" method
e.g. if $key contains the string "ContextPath", I want to call
$data.getContextPath() or retrieve the property $data.ContextPath.

On the face of it, quite simple. It is easy enough to construct an
expression in the template that would contain syntactically correct VTL,
no matter how I enclose it in quotes or braces, pass it to a macro or
whatever, I can't get the expression parsed.

My first attempt was the most naïve - I wrote ${data.$key} - but oddly
enough that gave me a result equivalent to $data.$key, in which each
half of
the expression was evaluated separately (not what I wanted at all).

Best regards,


Do You Yahoo!?

Get your free address at

View raw message