[stringtemplate-interest] re String manipulations

Zenaan Harkness zen at freedbms.net
Thu Sep 21 23:19:32 PDT 2006


On Fri, Sep 22, 2006 at 01:32:14AM -0400, John Snyders wrote:
> I had the idea that I could use a template to format strings rather than
> create a wrapper around Strings.
> I would create a template and add it to a group the template would simply
> expand its argument.
> Registering a renderer with just this template should cause strings to be
> uppercased.
> 
> ...
> StringTemplate uc = s_builtinTemplates.defineTemplate("upperCase", "$it$");
> uc.registerRenderer(String.class, new UpperCaseRenderer());
> ...
>     public static class UpperCaseRenderer implements AttributeRenderer
>     {
> 
>         public String toString(Object o)
>         {
>             if (o instanceof String)
>             {
>                 String s = (String)o;
>                 return s.toUpperCase();
>             }
>             return o.toString();
>         }
>     }
> 
> Then I thought I could use it in a template like so
> $upperCase(it=name)$
> 
> or
> 
> $name:upperCase()$
> 
> The trouble is that for some reason by the time the expression is getting
> evaluated the renderer is missing from the template.
> Before calling toString I can look in the template and see the renderer map
> has my class in it. When I set a breakpoint in ASTExpr just where it checks
> for the renderer it is no longer in the template.

Does indeed sound like it would do the job. Great thinking.

Are you sure that the same object (ST) is in both places?

To be entirely sure, you can use st.hashCode() to make sure you have
the same object references (or use System.identityHashCode()).

It would be awefully strange to have the renderer go missing, so my
first assumption is just an oversight bug in your code.

Hope you crack it...
Zen


-- 
Free Australia - www.UPMART.org
Please respect the confidentiality of this email as sensibly warranted.


More information about the stringtemplate-interest mailing list