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/AttrHandler.java | 10 ------ org/madore/damlengine/DamlEngine.java | 6 ++-- org/madore/damlengine/DefaultHandler.java | 9 ----- org/madore/damlengine/ElementHandler.java | 42 ----------------------- org/madore/damlengine/TodoAttr.java | 45 +++++++++++++++++------- org/madore/damlengine/TodoDefaultElement.java | 49 +++++++++++++++++++++++++++ org/madore/damlengine/TodoDeque.java | 2 +- org/madore/damlengine/TodoElement.java | 41 ++++++++++++++-------- org/madore/damlengine/TodoItem.java | 2 +- org/madore/damlengine/TodoXemptyAttr.java | 18 ++++++++++ org/madore/damlengine/XemptyAttrHandler.java | 12 ------- 11 files changed, 130 insertions(+), 106 deletions(-) delete mode 100644 org/madore/damlengine/AttrHandler.java delete mode 100644 org/madore/damlengine/DefaultHandler.java delete mode 100644 org/madore/damlengine/ElementHandler.java create mode 100644 org/madore/damlengine/TodoDefaultElement.java create mode 100644 org/madore/damlengine/TodoXemptyAttr.java delete mode 100644 org/madore/damlengine/XemptyAttrHandler.java (limited to 'org/madore/damlengine') 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 options = new HashMap(); options.put("isRoot", true); - todoDeque.registerAtEnd(new TodoElement(doc.getDocumentElement(), - new HashMap(), - options)); + todoDeque.registerAtEnd(TodoElement.getTodoElement(doc.getDocumentElement(), + new HashMap(), + 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 toProcess = new Vector(); - for ( int i=0 ; (attr=attrs.item(i)) != null ; i++ ) { - TodoAttr it = new TodoAttr((Attr)attr, that.node, that.context, - new HashMap()); - 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 toProcess = new Vector(); - 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()); - 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 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; } } 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 context, Map options) { + super(node,context,options); + } + + public void handleAttributes() { + NamedNodeMap attrs = this.node.getAttributes(); + Node attr; + Vector toProcess = new Vector(); + for ( int i=0 ; (attr=attrs.item(i)) != null ; i++ ) { + TodoAttr it = TodoAttr.getTodoAttr((Attr)attr, this.node, this.context, + new HashMap()); + 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 toProcess = new Vector(); + 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()); + 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 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); } } 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 context, Map 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 ")); - } - -} -- cgit v1.2.3