summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david@procyon.(none)>2010-04-15 23:06:03 +0200
committerDavid A. Madore <david@procyon.(none)>2010-04-15 23:06:03 +0200
commitc18d6e561179a3a96d93a9ae8c6034115ba36923 (patch)
treed349bbc9f2e53d45e693b15b965812d7b531fe0d
parent9176d80ae6dcad88da79714d934b226afe66c1ad (diff)
downloaddamlengine-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.
-rw-r--r--org/madore/damlengine/AttrHandler.java10
-rw-r--r--org/madore/damlengine/DamlEngine.java6
-rw-r--r--org/madore/damlengine/DefaultHandler.java9
-rw-r--r--org/madore/damlengine/ElementHandler.java42
-rw-r--r--org/madore/damlengine/TodoAttr.java45
-rw-r--r--org/madore/damlengine/TodoDefaultElement.java49
-rw-r--r--org/madore/damlengine/TodoDeque.java2
-rw-r--r--org/madore/damlengine/TodoElement.java41
-rw-r--r--org/madore/damlengine/TodoItem.java2
-rw-r--r--org/madore/damlengine/TodoXemptyAttr.java18
-rw-r--r--org/madore/damlengine/XemptyAttrHandler.java12
11 files changed, 130 insertions, 106 deletions
diff --git a/org/madore/damlengine/AttrHandler.java b/org/madore/damlengine/AttrHandler.java
deleted file mode 100644
index df1e18a..0000000
--- a/org/madore/damlengine/AttrHandler.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.madore.damlengine;
-
-import org.w3c.dom.*;
-
-public abstract class AttrHandler {
-
- public void handle(TodoAttr that) {
- }
-
-}
diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java
index b87b069..712f7e3 100644
--- a/org/madore/damlengine/DamlEngine.java
+++ b/org/madore/damlengine/DamlEngine.java
@@ -21,9 +21,9 @@ public final class DamlEngine {
TodoDeque todoDeque = new TodoDeque();
HashMap<String,Object> options = new HashMap<String,Object>();
options.put("isRoot", true);
- todoDeque.registerAtEnd(new TodoElement(doc.getDocumentElement(),
- new HashMap<String,Object>(),
- options));
+ todoDeque.registerAtEnd(TodoElement.getTodoElement(doc.getDocumentElement(),
+ new HashMap<String,Object>(),
+ options));
todoDeque.dispatchLoop();
}
diff --git a/org/madore/damlengine/DefaultHandler.java b/org/madore/damlengine/DefaultHandler.java
deleted file mode 100644
index d22d8f4..0000000
--- a/org/madore/damlengine/DefaultHandler.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.madore.damlengine;
-
-import java.util.Vector;
-import java.util.HashMap;
-import org.w3c.dom.*;
-
-public class DefaultHandler extends ElementHandler {
-
-}
diff --git a/org/madore/damlengine/ElementHandler.java b/org/madore/damlengine/ElementHandler.java
deleted file mode 100644
index 59981ac..0000000
--- a/org/madore/damlengine/ElementHandler.java
+++ /dev/null
@@ -1,42 +0,0 @@
-package org.madore.damlengine;
-
-import java.util.Vector;
-import java.util.HashMap;
-import org.w3c.dom.*;
-
-public abstract class ElementHandler {
-
- public void handleAttributes(TodoElement that) {
- NamedNodeMap attrs = that.node.getAttributes();
- Node attr;
- Vector<TodoAttr> toProcess = new Vector<TodoAttr>();
- for ( int i=0 ; (attr=attrs.item(i)) != null ; i++ ) {
- TodoAttr it = new TodoAttr((Attr)attr, that.node, that.context,
- new HashMap<String,Object>());
- toProcess.add(it);
- }
- that.ownerDeque.registerAtStart(toProcess);
- }
-
- public void handleNodeOnly(TodoElement that) {
- System.err.println("handling a "+that.node.getNodeName()+" element");
- NodeList children = that.node.getChildNodes();
- Node child;
- Vector<TodoElement> toProcess = new Vector<TodoElement>();
- for ( int i=0 ; (child=children.item(i)) != null ; i++ ) {
- if ( child.getNodeType() == Node.ELEMENT_NODE ) {
- TodoElement it = new TodoElement((Element)child, that.context,
- new HashMap<String,Object>());
- toProcess.add(it);
- }
- }
- that.ownerDeque.registerAtStart(toProcess);
- }
-
- public void handle(TodoElement that) {
- assert(that.ownerDeque != null);
- handleAttributes(that);
- handleNodeOnly(that);
- }
-
-}
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<String,AttrHandler> damlAttrHandlers;
+ protected static Map<String,Constructor<? extends TodoAttr>> damlAttrConstructors;
- {
- damlAttrHandlers = new HashMap<String,AttrHandler>();
- 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<String,Constructor<? extends TodoAttr>>();
+ 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<String,Object> context,
+ Map<String,Object> options) {
+ Constructor<? extends TodoAttr> 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;
}
}
diff --git a/org/madore/damlengine/TodoDefaultElement.java b/org/madore/damlengine/TodoDefaultElement.java
new file mode 100644
index 0000000..3329696
--- /dev/null
+++ b/org/madore/damlengine/TodoDefaultElement.java
@@ -0,0 +1,49 @@
+package org.madore.damlengine;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Vector;
+import org.w3c.dom.*;
+
+public class TodoDefaultElement extends TodoElement {
+
+ public TodoDefaultElement(Element node,
+ Map<String,Object> context, Map<String,Object> options) {
+ super(node,context,options);
+ }
+
+ public void handleAttributes() {
+ NamedNodeMap attrs = this.node.getAttributes();
+ Node attr;
+ Vector<TodoAttr> toProcess = new Vector<TodoAttr>();
+ for ( int i=0 ; (attr=attrs.item(i)) != null ; i++ ) {
+ TodoAttr it = TodoAttr.getTodoAttr((Attr)attr, this.node, this.context,
+ new HashMap<String,Object>());
+ if ( it != null )
+ toProcess.add(it);
+ }
+ this.ownerDeque.registerAtStart(toProcess);
+ }
+
+ public void handleNodeOnly() {
+ System.err.println("handling a "+this.node.getNodeName()+" element");
+ NodeList children = this.node.getChildNodes();
+ Node child;
+ Vector<TodoElement> toProcess = new Vector<TodoElement>();
+ for ( int i=0 ; (child=children.item(i)) != null ; i++ ) {
+ if ( child.getNodeType() == Node.ELEMENT_NODE ) {
+ TodoElement it = TodoElement.getTodoElement((Element)child, this.context,
+ new HashMap<String,Object>());
+ toProcess.add(it);
+ }
+ }
+ this.ownerDeque.registerAtStart(toProcess);
+ }
+
+ public void handle() {
+ assert(this.ownerDeque != null);
+ handleAttributes();
+ handleNodeOnly();
+ }
+
+}
diff --git a/org/madore/damlengine/TodoDeque.java b/org/madore/damlengine/TodoDeque.java
index eb90006..4cb1d38 100644
--- a/org/madore/damlengine/TodoDeque.java
+++ b/org/madore/damlengine/TodoDeque.java
@@ -41,7 +41,7 @@ public final class TodoDeque {
TodoItem it = removeNext();
if ( it != null ) {
assert(it.ownerDeque == this);
- it.dispatch();
+ it.handle();
return true;
} else
return false;
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);
}
}
diff --git a/org/madore/damlengine/TodoItem.java b/org/madore/damlengine/TodoItem.java
index 84dfd05..226b771 100644
--- a/org/madore/damlengine/TodoItem.java
+++ b/org/madore/damlengine/TodoItem.java
@@ -4,6 +4,6 @@ public abstract class TodoItem {
public TodoDeque ownerDeque;
- public abstract void dispatch();
+ public abstract void handle();
}
diff --git a/org/madore/damlengine/TodoXemptyAttr.java b/org/madore/damlengine/TodoXemptyAttr.java
new file mode 100644
index 0000000..e2cbe61
--- /dev/null
+++ b/org/madore/damlengine/TodoXemptyAttr.java
@@ -0,0 +1,18 @@
+package org.madore.damlengine;
+
+import java.util.Map;
+import org.w3c.dom.*;
+
+public class TodoXemptyAttr extends TodoAttr {
+
+ public TodoXemptyAttr(Attr attr, Element owner,
+ Map<String,Object> context, Map<String,Object> options) {
+ super(attr, owner, context, options);
+ }
+
+ public void handle() {
+ this.owner.removeAttribute(this.attr.getName());
+ this.owner.appendChild(DamlEngine.doc.createComment(" EMPTY "));
+ }
+
+}
diff --git a/org/madore/damlengine/XemptyAttrHandler.java b/org/madore/damlengine/XemptyAttrHandler.java
deleted file mode 100644
index 16f8173..0000000
--- a/org/madore/damlengine/XemptyAttrHandler.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package org.madore.damlengine;
-
-import org.w3c.dom.*;
-
-public class XemptyAttrHandler extends AttrHandler {
-
- public void handle(TodoAttr that) {
- that.owner.removeAttribute(that.attr.getName());
- that.owner.appendChild(DamlEngine.doc.createComment(" EMPTY "));
- }
-
-}