[stringtemplate-interest] String manipulations

John Snyders jjsnyders at rcn.com
Mon Sep 25 12:30:55 PDT 2006


The UpperCase renderer is very specific and should only be registered with
the template called upperCase. It is not registered with a template group so
it won't be inherited. When looking for a renderer first the template is
checked then any enclosing templates and finally the group is checked which
will also look in super groups.

The need came from Zenaan Harkness
(http://www.antlr.org:8080/pipermail/stringtemplate-interest/2006-September/
000646.html) who wanted the template writer to have control over when to
uppercase model data without having to change the model to add wrappers.

I agree that it is better to create wrapper classes or sub classes with
pseudo properties. For example creating a String wrapper class WrapperString
with property method getUpperCase.

With template "upperCase" defined as "$it$"
and template "message" defined as "Hello $p0$!"
and data value "name"="John"
and the UpperCase renderer (described in previous message) registered with
upperCase template only
Here are some test cases
1) $message(p0=name)$                   produces: Hello John!
2) $message(p0={$upperCase(it=name)$})$ produces: Hello JOHN!
3) $upperCase(it={$fmt2(p0=name)$})$    produces: Hello JOHN!
4) $upperCase(it="some text")$          produces: SOME TEXT

Note that test 3 does not produce the expected "HELLO JOHN!" I think that
this is expected because renderers are only called on attributes. In case 3
all strings expanded within the anon. template will get upperCased. Test 4
shows that literal strings passed as parameters to templates act as strings.

To compare with the method of using a wrapper class for strings and assuming
all strings are wrapped when added to templates as attributes.
$upperCase(it=name)$ would become $name.upperCase$
I don't see a way for the upperCase property to do with was done in test
case 4 above unless "some text" was moved into the data model.

The upperCase property is simpler to type. I'm not sure either syntax is
clear in expressing what it does - affect rendering.

-John

-----Original Message-----
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Terence
Parr
Sent: Monday, September 25, 2006 2:03 PM
To: stringtemplate-interest
Subject: Re: [stringtemplate-interest] String manipulations



On Sep 25, 2006, at 10:53 AM, Terence Parr wrote:

>
> On Sep 23, 2006, at 7:11 AM, John Snyders wrote:
>
>> Here is the change I made to StringTemplate.java
>>
>> It is just the one line added to the end:
>> 		to.attributeRenderers = from.attributeRenderers;
>
> Fixed.  Thanks.
>
>> Then in your templates you can use
>> $upperCase(it=name)$
>> or
>> $name:upperCase()$
>
> Yes, but doesn't that uppercase all strings?  Calling that upperCase

Ok, I think i get it, but don't you have to register a render with
your subgroup to prevent it inheriting the renderer?

Ter


_______________________________________________
stringtemplate-interest mailing list
stringtemplate-interest at antlr.org
http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest


More information about the stringtemplate-interest mailing list