diff options
author | David A. Madore <david@procyon.(none)> | 2010-04-15 23:06:03 +0200 |
---|---|---|
committer | David A. Madore <david@procyon.(none)> | 2010-04-15 23:06:03 +0200 |
commit | c18d6e561179a3a96d93a9ae8c6034115ba36923 (patch) | |
tree | d349bbc9f2e53d45e693b15b965812d7b531fe0d /org/madore/damlengine/TodoElement.java | |
parent | 9176d80ae6dcad88da79714d934b226afe66c1ad (diff) | |
download | damlengine-c18d6e561179a3a96d93a9ae8c6034115ba36923.tar.gz damlengine-c18d6e561179a3a96d93a9ae8c6034115ba36923.tar.bz2 damlengine-c18d6e561179a3a96d93a9ae8c6034115ba36923.zip |
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.
Diffstat (limited to 'org/madore/damlengine/TodoElement.java')
-rw-r--r-- | org/madore/damlengine/TodoElement.java | 41 |
1 files changed, 26 insertions, 15 deletions
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<String,ElementHandler> damlHandlers; - protected static ElementHandler defaultHandler; + protected static Map<String,Constructor<? extends TodoElement>> damlConstructors; - { - damlHandlers = new HashMap<String,ElementHandler>(); - defaultHandler = new DefaultHandler(); + protected static void initializeDamlConstructors() { + // FIXME: this should be a static initializer, but for some reason does not work... + damlConstructors = new HashMap<String,Constructor<? extends TodoElement>>(); } 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<String,Object> context, + Map<String,Object> options) { + Constructor<? extends TodoElement> 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); } } |