You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Scott Palmer <sc...@digital-rapids.com> on 2004/11/02 07:00:47 UTC

Allowing digits as the first character of a property

Hello all,

I have an existing project that already has a simple template engine 
that I would like to replace with Velocity.  As much as possible I would 
like to support the existing format of the template files that I already 
have.

My existing files have numbered properties that I access by placing a 
dot '.' followed by an integer after the property name.  E.g.   
CLIENTS.0  would refer to the first client, CLIENTS.COUNT would refer to 
the number of clients.  CLIENTS.0.NAME would refer to the name of the 
first client, etc.

I'm wondering if there is any hope of keeping that format when moving to 
Velocity.  That is, I wonder if the changes required to velocity to 
support a leading digit in a (sub)property name would be small enough 
that I would have any hope of making such a change on my own having 
never seen the Velocity code and not wanting to spend much time tweaking 
it.  I was thinking that I could use the existing support for looking up 
values in a Map object with some small modification that would result in 
the ability to allow numeric strings (e.g. "0") to be used as a key for 
the lookup.

I have a backup plan to change the syntax to something like CLIENTS(0) 
if I have to, but that would mean that my customers would have to make 
much more significant changes to their existing templates.

Should I attempt to tweak Velocity into accepting my existing syntax, or 
is it simply going to be too much work?  (It wont take much to make it 
"too much".)

Thanks,

Scott


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


Re: Allowing digits as the first character of a property

Posted by Scott Palmer <sc...@digital-rapids.com>.
Simon,

On Nov 2, 2004, at 9:47 AM, Simon Christian wrote:

> But what of $clients.get( "0.name" )?

I suspect that will work, but if I need to change existing templates 
that much, I would rather just go with $clients.get(0).name and be able 
to get the other benefits of Velocity such as

#foreach( $c in $clients)
	$c.name
#end

Which won't work if I tie together the index number with other 
properties like the name.

Thanks,

Scott


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


Re: Allowing digits as the first character of a property

Posted by Simon Christian <si...@stoutstick.com>.
Scott,

Scott Palmer wrote:
> Hello Simon,
> 
> On Nov 2, 2004, at 4:59 AM, Simon Christian wrote:
> 
>> Hi Scott,
>>
>> No expert myself, but I suspect changing the Velocity internals to 
>> allow  you do use something like $clients.0.name would definitely make 
>> it into the 'too much' camp, as the parser would currently interpret 
>> that (firstly) as:
>>
>>   ([ClientObj].get0() ).getName()
>>
>> Not really what you're after!
> 
> 
> Actually that is not what I'm seeing from the parser now.  The parser 
> currently stops when it sees a property starting with a number.  So 
> $clients.0 will only evaluate to [ClientObj].0.  that is, the ".0" is 
> not treated as a property of $client at all.  Which is what I expect 
> since the docs indicate that what follows the '.' must be a valid VTL 
> reference and therefore cannot start with a digit.  That's exactly the 
> thing that I'm thinking *might* be easy enough to change.
> 

Sorry you're right of course, I was being somewhat obtuse and didn't 
actually check if a reference starting with a number was valid.

>> However you could maybe work around it fairly easily by using a map 
>> within the context e.g.
>>
>>  Map clientMap = new HashMap();
>>  map.put( "count", 2 );
>>  map.put( "0.name", name0 );
>>  map.put( "0.email", email0 );
>>  .
>>  .
>>  ctx.put( "clients", clientMap );
> 
> 
> I tried that.  The output of $clients.0.name is something like:
> {0.email=test@email.addr.com, 0.name=test name, count=2}.0.name
> 

But what of $clients.get( "0.name" )? It shouldn't be too arduous to 
replace all instances of $client.<number>.<property> with $clients.get( 
"<number>.</property" ). Of course that would only be worthwhile if 
switching to the above map was a trivial task - otherwise equal effort 
might yield a solution which fits the Velocity approach more neatly.

- simon

> So the workarounds don't work... more evidence that I will need to adopt 
> a syntax that is more friendly to Velocity.
> 
> Thanks anyway,
> 
> Scott
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 


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


Re: Allowing digits as the first character of a property

Posted by Scott Palmer <sc...@digital-rapids.com>.
Hello Simon,

On Nov 2, 2004, at 4:59 AM, Simon Christian wrote:

> Hi Scott,
>
> No expert myself, but I suspect changing the Velocity internals to 
> allow  you do use something like $clients.0.name would definitely make 
> it into the 'too much' camp, as the parser would currently interpret 
> that (firstly) as:
>
>   ([ClientObj].get0() ).getName()
>
> Not really what you're after!

Actually that is not what I'm seeing from the parser now.  The parser 
currently stops when it sees a property starting with a number.  So 
$clients.0 will only evaluate to [ClientObj].0.  that is, the ".0" is 
not treated as a property of $client at all.  Which is what I expect 
since the docs indicate that what follows the '.' must be a valid VTL 
reference and therefore cannot start with a digit.  That's exactly the 
thing that I'm thinking *might* be easy enough to change.

> However you could maybe work around it fairly easily by using a map 
> within the context e.g.
>
>  Map clientMap = new HashMap();
>  map.put( "count", 2 );
>  map.put( "0.name", name0 );
>  map.put( "0.email", email0 );
>  .
>  .
>  ctx.put( "clients", clientMap );

I tried that.  The output of $clients.0.name is something like:
{0.email=test@email.addr.com, 0.name=test name, count=2}.0.name

So the workarounds don't work... more evidence that I will need to 
adopt a syntax that is more friendly to Velocity.

Thanks anyway,

Scott


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


Re: Allowing digits as the first character of a property

Posted by Scott Palmer <sc...@digital-rapids.com>.
On Nov 4, 2004, at 6:24 PM, Daniel Rall wrote:

> Scott, do not give up hope!  You can implement a generic get(String)
> method which can provide this functionality in a dynamic fashion:
>
> public class Ident
> {
>     public Client get(String name)
>     {
>         ...
>     }
> }
>
> $ident.foo will map to Ident.get("foo"), which you can in turn have
> return your Client object.

Tried implementing the 'get' method, and as I suspected it is only 
called if 'name' is a valid VTL identifier name.  "0" didn't work, "_0" 
didn't work, but "A0" worked.

Thanks anyway,

Scott


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


Re: Allowing digits as the first character of a property

Posted by Daniel Rall <dl...@collab.net>.
On Thu, 2004-11-04 at 16:29 -0800, Daniel Rall wrote:
> On Thu, 2004-11-04 at 19:11 -0500, Scott Palmer wrote:
> > On Nov 4, 2004, at 6:24 PM, Daniel Rall wrote:
> > 
> > > Scott, do not give up hope!  You can implement a generic get(String)
> > > method which can provide this functionality in a dynamic fashion:
> > >
> > > public class Ident
> > > {
> > >     public Client get(String name)
> > >     {
> > >         ...
> > >     }
> > > }
> > >
> > > $ident.foo will map to Ident.get("foo"), which you can in turn have
> > > return your Client object.  Here's a link to the gory details:
> > 
> > But will it call me if 'foo' starts with a digit or is it already too 
> > late by that point?  I thought it was too late, since if I add a Map to 
> > the context that has the string '0' (zero) as a key it does not work.  
> > Other keys that don't start with a digit are successfully retrieved 
> > from the map.
> 
> I haven't tried it, but what you say about the Map is interesting and
> makes me guess that what I suggested above would fail.  So the problem
> with retrieval likely stems from the fact that the key name is numeric.
> Have you tried an Ident object which takes a numeric parameter for the
> key name (e.g. int or Integer)?

Looking again at GetExcecutor, the argument would have to be instanceof
Object, so make that Integer.


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


Re: Allowing digits as the first character of a property

Posted by Daniel Rall <dl...@collab.net>.
On Thu, 2004-11-04 at 19:11 -0500, Scott Palmer wrote:
> On Nov 4, 2004, at 6:24 PM, Daniel Rall wrote:
> 
> > Scott, do not give up hope!  You can implement a generic get(String)
> > method which can provide this functionality in a dynamic fashion:
> >
> > public class Ident
> > {
> >     public Client get(String name)
> >     {
> >         ...
> >     }
> > }
> >
> > $ident.foo will map to Ident.get("foo"), which you can in turn have
> > return your Client object.  Here's a link to the gory details:
> 
> But will it call me if 'foo' starts with a digit or is it already too 
> late by that point?  I thought it was too late, since if I add a Map to 
> the context that has the string '0' (zero) as a key it does not work.  
> Other keys that don't start with a digit are successfully retrieved 
> from the map.

I haven't tried it, but what you say about the Map is interesting and
makes me guess that what I suggested above would fail.  So the problem
with retrieval likely stems from the fact that the key name is numeric.
Have you tried an Ident object which takes a numeric parameter for the
key name (e.g. int or Integer)?



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


Re: Allowing digits as the first character of a property

Posted by Scott Palmer <sc...@digital-rapids.com>.
On Nov 4, 2004, at 6:24 PM, Daniel Rall wrote:

> Scott, do not give up hope!  You can implement a generic get(String)
> method which can provide this functionality in a dynamic fashion:
>
> public class Ident
> {
>     public Client get(String name)
>     {
>         ...
>     }
> }
>
> $ident.foo will map to Ident.get("foo"), which you can in turn have
> return your Client object.  Here's a link to the gory details:
>

But will it call me if 'foo' starts with a digit or is it already too 
late by that point?  I thought it was too late, since if I add a Map to 
the context that has the string '0' (zero) as a key it does not work.  
Other keys that don't start with a digit are successfully retrieved 
from the map.


Scott


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


Re: Allowing digits as the first character of a property

Posted by Daniel Rall <dl...@collab.net>.
On Thu, 2004-11-04 at 00:52 -0500, Scott Palmer wrote:
...
> This is true.  Though I'm not considering that a digit will ever follow 
> a '$' when used in an identifier.  In my case a digit would only be 
> considered as a property of an identifier, or rather the object 
> represented by that identifier, only in a case such as $ident.0  or 
> $ident.0.something. And even then only if the object referenced by 
> $ident had a "get0()" method or was a map with a "0" among the keys.
> 
> But I've given up nonetheless :)

Scott, do not give up hope!  You can implement a generic get(String)
method which can provide this functionality in a dynamic fashion:

public class Ident
{
    public Client get(String name)
    {
        ...
    }
}

$ident.foo will map to Ident.get("foo"), which you can in turn have
return your Client object.  Here's a link to the gory details:

<http://cvs.apache.org/viewcvs.cgi/jakarta-
velocity/src/java/org/apache/velocity/runtime/parser/node/GetExecutor.java?rev=1&view=auto>

And an example of an implementation which I use every day in production
for L10N:

<http://jakarta.apache.org/turbine/turbine/turbine-2.3.1/apidocs/org/apache/turbine/services/localization/LocalizationTool.html#get(java.lang.String)>



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


Re: Allowing digits as the first character of a property

Posted by Scott Palmer <sc...@digital-rapids.com>.
On Nov 4, 2004, at 12:11 AM, Michael Dykman wrote:

> Scott,
>
> At the risk of sounding pedantic (rats, too late):
>
> If the point hasn't been already been made, I can't think of a language
> that allows digits as the first character of an identifier.  Lexical
> units starting with digits are assumed to be numbers.  '4NAME' might
> seem harmless, but against conventions like '4L' ( long suffix, and
> several other similar ones in the 'C' world), it's gets confusing in a
> hurry.
>

This is true.  Though I'm not considering that a digit will ever follow 
a '$' when used in an identifier.  In my case a digit would only be 
considered as a property of an identifier, or rather the object 
represented by that identifier, only in a case such as $ident.0  or 
$ident.0.something. And even then only if the object referenced by 
$ident had a "get0()" method or was a map with a "0" among the keys.

But I've given up nonetheless :)

Scott


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


Re: Allowing digits as the first character of a property

Posted by Michael Dykman <mi...@dykman.org>.
Scott,

At the risk of sounding pedantic (rats, too late):

If the point hasn't been already been made, I can't think of a language
that allows digits as the first character of an identifier.  Lexical
units starting with digits are assumed to be numbers.  '4NAME' might
seem harmless, but against conventions like '4L' ( long suffix, and
several other similar ones in the 'C' world), it's gets confusing in a
hurry.

> Scott Palmer wrote:
> > Hello all,
> > 
> > I have an existing project that already has a simple template engine 
> > that I would like to replace with Velocity.  As much as possible I would 
> > like to support the existing format of the template files that I already 
> > have.
> > 
> > My existing files have numbered properties that I access by placing a 
> > dot '.' followed by an integer after the property name.  E.g.   
> > CLIENTS.0  would refer to the first client, CLIENTS.COUNT would refer to 
> > the number of clients.  CLIENTS.0.NAME would refer to the name of the 
> > first client, etc.
> > 
> > I'm wondering if there is any hope of keeping that format when moving to 
> > Velocity.  That is, I wonder if the changes required to velocity to 
> > support a leading digit in a (sub)property name would be small enough 
> > that I would have any hope of making such a change on my own having 
> > never seen the Velocity code and not wanting to spend much time tweaking 
> > it.  I was thinking that I could use the existing support for looking up 
> > values in a Map object with some small modification that would result in 
> > the ability to allow numeric strings (e.g. "0") to be used as a key for 
> > the lookup.
> > 
> > I have a backup plan to change the syntax to something like CLIENTS(0) 
> > if I have to, but that would mean that my customers would have to make 
> > much more significant changes to their existing templates.
> > 
> > Should I attempt to tweak Velocity into accepting my existing syntax, or 
> > is it simply going to be too much work?  (It wont take much to make it 
> > "too much".)
> > 
> > Thanks,
> > 
> > Scott
> > 
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
-- 
 - michael dykman
 - michael@dykman.org


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


Re: Allowing digits as the first character of a property

Posted by Simon Christian <sj...@xtians.org.uk>.
Hi Scott,

No expert myself, but I suspect changing the Velocity internals to allow 
  you do use something like $clients.0.name would definitely make it 
into the 'too much' camp, as the parser would currently interpret that 
(firstly) as:

   ([ClientObj].get0() ).getName()

Not really what you're after!

However you could maybe work around it fairly easily by using a map 
within the context e.g.

  Map clientMap = new HashMap();
  map.put( "count", 2 );
  map.put( "0.name", name0 );
  map.put( "0.email", email0 );
  .
  .
  ctx.put( "clients", clientMap );

You're presumably doing something like this to populate the context 
equivalent in your existing template system at the moment. Then just 
access it as:

   $clients.get( "0.name" )

Of course it makes better use of Velocity's facilities to put Client 
objects into a Collection and access their class variables using their 
method

- simon

Scott Palmer wrote:
> Hello all,
> 
> I have an existing project that already has a simple template engine 
> that I would like to replace with Velocity.  As much as possible I would 
> like to support the existing format of the template files that I already 
> have.
> 
> My existing files have numbered properties that I access by placing a 
> dot '.' followed by an integer after the property name.  E.g.   
> CLIENTS.0  would refer to the first client, CLIENTS.COUNT would refer to 
> the number of clients.  CLIENTS.0.NAME would refer to the name of the 
> first client, etc.
> 
> I'm wondering if there is any hope of keeping that format when moving to 
> Velocity.  That is, I wonder if the changes required to velocity to 
> support a leading digit in a (sub)property name would be small enough 
> that I would have any hope of making such a change on my own having 
> never seen the Velocity code and not wanting to spend much time tweaking 
> it.  I was thinking that I could use the existing support for looking up 
> values in a Map object with some small modification that would result in 
> the ability to allow numeric strings (e.g. "0") to be used as a key for 
> the lookup.
> 
> I have a backup plan to change the syntax to something like CLIENTS(0) 
> if I have to, but that would mean that my customers would have to make 
> much more significant changes to their existing templates.
> 
> Should I attempt to tweak Velocity into accepting my existing syntax, or 
> is it simply going to be too much work?  (It wont take much to make it 
> "too much".)
> 
> Thanks,
> 
> Scott
> 
> 


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


Re: Allowing digits as the first character of a property

Posted by Simon Christian <si...@stoutstick.com>.
Hi Scott,

No expert myself, but I suspect changing the Velocity internals to allow 
  you do use something like $clients.0.name would definitely make it 
into the 'too much' camp, as the parser would currently interpret that 
(firstly) as:

   ([ClientObj].get0() ).getName()

Not really what you're after!

However you could maybe work around it fairly easily by using a map 
within the context e.g.

  Map clientMap = new HashMap();
  map.put( "count", 2 );
  map.put( "0.name", name0 );
  map.put( "0.email", email0 );
  .
  .
  ctx.put( "clients", clientMap );

You're presumably doing something like this to populate the context 
equivalent in your existing template system at the moment. Then just 
access it as:

   $clients.get( "0.name" )

Of course it makes better use of Velocity's facilities to put Client 
objects into a Collection and access their class variables using their 
method

- simon

Scott Palmer wrote:
> Hello all,
> 
> I have an existing project that already has a simple template engine 
> that I would like to replace with Velocity.  As much as possible I would 
> like to support the existing format of the template files that I already 
> have.
> 
> My existing files have numbered properties that I access by placing a 
> dot '.' followed by an integer after the property name.  E.g.   
> CLIENTS.0  would refer to the first client, CLIENTS.COUNT would refer to 
> the number of clients.  CLIENTS.0.NAME would refer to the name of the 
> first client, etc.
> 
> I'm wondering if there is any hope of keeping that format when moving to 
> Velocity.  That is, I wonder if the changes required to velocity to 
> support a leading digit in a (sub)property name would be small enough 
> that I would have any hope of making such a change on my own having 
> never seen the Velocity code and not wanting to spend much time tweaking 
> it.  I was thinking that I could use the existing support for looking up 
> values in a Map object with some small modification that would result in 
> the ability to allow numeric strings (e.g. "0") to be used as a key for 
> the lookup.
> 
> I have a backup plan to change the syntax to something like CLIENTS(0) 
> if I have to, but that would mean that my customers would have to make 
> much more significant changes to their existing templates.
> 
> Should I attempt to tweak Velocity into accepting my existing syntax, or 
> is it simply going to be too much work?  (It wont take much to make it 
> "too much".)
> 
> Thanks,
> 
> Scott
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 


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