[stringtemplate-interest] re String manipulations
John Snyders
jjsnyders at rcn.com
Thu Sep 21 22:32:14 PDT 2006
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.
What I get is $name$ expanded but in its original case.
I think this may solve the problem Zen describes below.
Does anyone know why what I am doning isn't working? I'm using version 3.0.
Thanks
-John
Zen wrote:
Can anyone provide a brief set of steps required to be taken, in
order to add a caps|allCaps|initialCaps function to ST, in order
to capitalize strings suitable for Java getter and setter method
names?
I don't want to go into my model and produce wrappers, because it
is simply a jyaml (http://jyaml.sourceforge.net/index.html) file
(or set of files) which I load and very simply plug into my ST
template.
This current setup results in an extremely simple and robust
controller, very simple model, and very clean separation of
concerns. I really don't want to have to have custom model logic
in my controller peeking inside a particular model instance
(a loaded yaml file), sucking out attributes, wrapping them, and
plugging them back in to the "model" (simply a set of Maps and
Lists). This is clearly an entanglement of the model and the
controller.
And of course, to have to put multiple instances of the same data
fields in the model, breaks the Duplication principle in the model
and looks bad to boot - redundancy is an even worse solution!
Because my model is encapsulated as pure data in a yaml file, I
really don't want to have to unnecessarily break this separation
of concerns inside the controller. It's really clean at the moment.
Any help appreciated.
Thanks in advance
Zen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20060922/c789c2eb/attachment.html
More information about the stringtemplate-interest
mailing list