From c18d6e561179a3a96d93a9ae8c6034115ba36923 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 15 Apr 2010 23:06:03 +0200 Subject: Change dispatching approach: handlers are now part of todoItems. Instead of dispatching the todo item in function of the DAML node's local name at todo-handling time, the appropriate todo handler subclass is now instantiated in the todo deque by dispatching the creation of the todo item to the appropriate constructor. --- org/madore/damlengine/TodoAttr.java | 45 ++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'org/madore/damlengine/TodoAttr.java') diff --git a/org/madore/damlengine/TodoAttr.java b/org/madore/damlengine/TodoAttr.java index e11abac..b9cb800 100644 --- a/org/madore/damlengine/TodoAttr.java +++ b/org/madore/damlengine/TodoAttr.java @@ -1,17 +1,25 @@ package org.madore.damlengine; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.HashMap; import org.w3c.dom.Element; import org.w3c.dom.Attr; -public class TodoAttr extends TodoItem { +public abstract class TodoAttr extends TodoItem { - protected static Map damlAttrHandlers; + protected static Map> damlAttrConstructors; - { - damlAttrHandlers = new HashMap(); - damlAttrHandlers.put("xempty", new XemptyAttrHandler()); + protected static void initializeDamlAttrConstructors() { + // FIXME: this should be a static initializer, but for some reason does not work... + damlAttrConstructors = new HashMap>(); + Class[] argTypes = new Class[]{ Attr.class, Element.class, Map.class, Map.class }; + try { + damlAttrConstructors.put("xempty", TodoXemptyAttr.class.getConstructor(argTypes)); + } catch (NoSuchMethodException e) { + // FIXME: Do something intelligent here! + } } Attr attr; @@ -27,15 +35,26 @@ public class TodoAttr extends TodoItem { this.options = options; } - public void dispatch() { - AttrHandler handler; + public static TodoAttr getTodoAttr(Attr attr, Element owner, + Map context, + Map options) { + Constructor constructor = null; String nsuri = attr.getNamespaceURI(); - if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) { - handler = damlAttrHandlers.get(attr.getLocalName()); - } else - handler = null; - if ( handler != null ) - handler.handle(this); + if ( damlAttrConstructors == null ) + initializeDamlAttrConstructors(); // FIXME: see above + if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) + constructor = damlAttrConstructors.get(attr.getLocalName()); + if ( constructor != null ) + try { + return constructor.newInstance(new Object[]{attr, owner, context, options}); + } catch (InstantiationException e) { + // FIXME: Do something intelligent here! + } catch (IllegalAccessException e) { + // FIXME: Do something intelligent here! + } catch (InvocationTargetException e) { + // FIXME: Do something intelligent here! + } + return null; } } -- cgit v1.2.3