[stringtemplate-interest] v4 renderers

Udo Borkowski ub at abego-software.de
Wed Dec 22 14:10:21 PST 2010


> 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.

Rather than caching a.getClass -> adap I would ask adap for an object I could use for caching (and accessing a property value). This is because it may still be expensive for the adapter to calculate the property value, but easier if some "calculation" is already done (and only done once).

E.g. imagine you want to implement the current property access behaviour (as defined by Interpreter#getObjectProperty(…)) by an adapter. This would require first checking for the "Map" case, then the "ST" case, then the "getFoo()" case, then the "isFoo()" case and finally the field "foo" case. Actually this only needs do be done once per class and property(name). 

Therefore I defined one "Access class" for each of these cases and store the proper one with each class and property (name) pair. (Undefined properties map to a unique "UNDEFINED" accessor).

E.g. for the "ST" case the code looks like this:

	private final static PropertyAccessor PROPERTY_ACCESSOR_FOR_ST = new PropertyAccessor() {

		@Override
		public Object getProperty(Object o, Object property, ST template,
				Interpreter interpreter) {
			if (o instanceof ST) {
				ST st = (ST) o;
				return st.getAttribute((String) property);
			} else {
				ErrorManager.runTimeError(
						template,
						interpreter.current_ip,
						ErrorType.INTERNAL_ERROR,
						null,
						String.format("Expected instance of ST, got %s",
								o.getClass()));
				return null;
			}
		}
	};


Udo


On 22.12.2010, at 22:29, Terence Parr wrote:

> 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
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20101222/ef28bb79/attachment.html 


More information about the stringtemplate-interest mailing list