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/TodoElement.java | 41 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'org/madore/damlengine/TodoElement.java') diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index c3dbf0b..f8dcca1 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -1,17 +1,18 @@ 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; -public class TodoElement extends TodoItem { +public abstract class TodoElement extends TodoItem { - protected static Map damlHandlers; - protected static ElementHandler defaultHandler; + protected static Map> damlConstructors; - { - damlHandlers = new HashMap(); - defaultHandler = new DefaultHandler(); + protected static void initializeDamlConstructors() { + // FIXME: this should be a static initializer, but for some reason does not work... + damlConstructors = new HashMap>(); } Element node; @@ -24,16 +25,26 @@ public class TodoElement extends TodoItem { this.options = options; } - public void dispatch() { - ElementHandler handler; + public static TodoElement getTodoElement(Element node, + Map context, + Map options) { + Constructor constructor = null; String nsuri = node.getNamespaceURI(); - if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) { - handler = damlHandlers.get(node.getLocalName()); - if ( handler == null ) - handler = defaultHandler; - } else - handler = defaultHandler; - handler.handle(this); + if ( damlConstructors == null ) + initializeDamlConstructors(); // FIXME: see above + if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) + constructor = damlConstructors.get(node.getLocalName()); + if ( constructor != null ) + try { + return constructor.newInstance(new Object[]{node, 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 new TodoDefaultElement(node, context, options); } } -- cgit v1.2.3