/** * Add an adaptor for a kind of object so ST knows how to pull properties from them. Add adaptors * in increasing order of specificity. ST adds Object, Map, and ST model adaptors for you first. * Adaptors you add have priority over default adaptors. * * <p>If an adaptor for type T already exists, it is replaced by the adaptor arg. * * <p>This must invalidate cache entries, so set your adaptors up before render()ing your * templates for efficiency. */ public void registerModelAdaptor(Class attributeType, ModelAdaptor adaptor) { if (attributeType.isPrimitive()) { throw new IllegalArgumentException( "can't register ModelAdaptor for primitive type " + attributeType.getSimpleName()); } adaptors.put(attributeType, adaptor); invalidateModelAdaptorCache(attributeType); }
/** * Register a renderer for all objects of a particular "kind" for all templates evaluated relative * to this group. Use r to render if object in question is instanceof(attributeType). */ public void registerRenderer(Class attributeType, AttributeRenderer r) { if (attributeType.isPrimitive()) { throw new IllegalArgumentException( "can't register renderer for primitive type " + attributeType.getSimpleName()); } typeToAdaptorCache.clear(); // be safe, not clever; wack all values if (renderers == null) { renderers = Collections.synchronizedMap(new LinkedHashMap<Class, AttributeRenderer>()); } renderers.put(attributeType, r); }
public ModelAdaptor getModelAdaptor(Class attributeType) { ModelAdaptor a = typeToAdaptorCache.get(attributeType); if (a != null) return a; // System.out.println("looking for adaptor for "+attributeType); // Else, we must find adaptor that fits; // find last fit (most specific) for (Class t : adaptors.keySet()) { // t works for attributeType if attributeType subclasses t or implements // System.out.println("checking "+t.getSimpleName()+" against "+attributeType); if (t.isAssignableFrom(attributeType)) { // System.out.println(t.getName()+" = "+attributeType.getName()); a = adaptors.get(t); } } // System.out.println("adaptor for "+attributeType+" is "+a); typeToAdaptorCache.put(attributeType, a); // cache it for next time return a; }
public AttributeRenderer getAttributeRenderer(Class attributeType) { if (renderers == null) return null; AttributeRenderer r = null; if (typeToRendererCache != null) { r = typeToRendererCache.get(attributeType); if (r != null) return r; } // Else look up, finding first first for (Class t : renderers.keySet()) { // t works for attributeType if attributeType subclasses t or implements if (t.isAssignableFrom(attributeType)) { r = renderers.get(t); if (typeToRendererCache == null) { typeToRendererCache = Collections.synchronizedMap(new LinkedHashMap<Class, AttributeRenderer>()); } typeToRendererCache.put(attributeType, r); return r; } } return null; }