[stringtemplate-interest] v4 renderers
Terence Parr
parrt at cs.usfca.edu
Wed Dec 22 13:29:00 PST 2010
ok, v4 works for renderers now with instanceof functionality:
@Test public void testInstanceofRenderer() throws Exception {
String templates =
"numberThing(x,y,z) ::= \"numbers: <x>, <y>; <z>\"\n";
writeFile(tmpdir, "t.stg", templates);
STGroup group = new STGroupFile(tmpdir+"/t.stg");
group.registerRenderer(Number.class, new NumberRenderer());
ST st = group.getInstanceOf("numberThing");
st.add("x", -2100);
st.add("y", 3.14159);
st.add("z", "hi");
String expecting = "numbers: -2100, 3.14159; hi";
String result = st.render();
assertEquals(expecting, result);
}
Note that Integer and Double are subclasses of Number.
Look up for each attribute ref is hyperslow at the moment (checks attributeType against all renderers):
public AttributeRenderer getAttributeRenderer(Class attributeType) {
if ( renderers==null ) return null;
// TODO: cache this lookup
for (Class t : renderers.keySet()) {
// t works for attributeType if attributeType subclasses t or implements
if ( t.isAssignableFrom(attributeType) ) return renderers.get(t);
}
return null;
}
Look ok? Same would work for registerModelAdaptor(type, adap), right? Upon <a.p> and a.getClass() is instanceof or implements type, then I'd ask adap how to get p from 'a', right? Later I can cache a.getClass -> adap mapping.
Latest is here, updated hourly: (the ant build should work)
http://www.stringtemplate.org/depot/ST4/java/main
Ter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20101222/9179b6da/attachment.html
More information about the stringtemplate-interest
mailing list