From e3b7a9c657a6a5f5a7a8f3c7ee4fdff1aee6410f Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 13 Apr 2010 15:21:41 +0200 Subject: Framework for handling attributes. --- org/madore/damlengine/DamlEngine.java | 6 ++-- org/madore/damlengine/DefaultHandler.java | 27 ++++++++++++--- org/madore/damlengine/TodoAttr.java | 57 +++++++++++++++++++++++++++++++ org/madore/damlengine/TodoDeque.java | 4 +-- org/madore/damlengine/TodoElement.java | 55 +++++++++++++++++++++++++++++ org/madore/damlengine/TodoItem.java | 51 ++------------------------- 6 files changed, 141 insertions(+), 59 deletions(-) create mode 100644 org/madore/damlengine/TodoAttr.java create mode 100644 org/madore/damlengine/TodoElement.java (limited to 'org/madore') diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java index f13d376..822b645 100644 --- a/org/madore/damlengine/DamlEngine.java +++ b/org/madore/damlengine/DamlEngine.java @@ -20,9 +20,9 @@ public final class DamlEngine { public static void processDocument() { HashMap options = new HashMap(); options.put("isRoot", true); - TodoDeque.registerAtEnd(new TodoItem(doc.getDocumentElement(), - new HashMap(), - options)); + TodoDeque.registerAtEnd(new TodoElement(doc.getDocumentElement(), + new HashMap(), + options)); TodoDeque.dispatchLoop(); } diff --git a/org/madore/damlengine/DefaultHandler.java b/org/madore/damlengine/DefaultHandler.java index 4720f67..ae23a3b 100644 --- a/org/madore/damlengine/DefaultHandler.java +++ b/org/madore/damlengine/DefaultHandler.java @@ -8,19 +8,36 @@ public class DefaultHandler { private DefaultHandler() { } - public static void handle(TodoItem that) { - System.err.println("handling a "+that.node.getNodeName()+" node"); + public static 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); + } + TodoDeque.registerAtStart(toProcess); + } + + public static void handleNodeOnly(TodoElement that) { + System.err.println("handling a "+that.node.getNodeName()+" element"); NodeList children = that.node.getChildNodes(); Node child; - Vector toProcess = new Vector(); + Vector toProcess = new Vector(); for ( int i=0 ; (child=children.item(i)) != null ; i++ ) { if ( child.getNodeType() == Node.ELEMENT_NODE ) { - TodoItem it = new TodoItem(child, that.context, - new HashMap()); + TodoElement it = new TodoElement((Element)child, that.context, + new HashMap()); toProcess.add(it); } } TodoDeque.registerAtStart(toProcess); } + public static void handle(TodoElement that) { + handleAttributes(that); + handleNodeOnly(that); + } + } diff --git a/org/madore/damlengine/TodoAttr.java b/org/madore/damlengine/TodoAttr.java new file mode 100644 index 0000000..d4d877e --- /dev/null +++ b/org/madore/damlengine/TodoAttr.java @@ -0,0 +1,57 @@ +package org.madore.damlengine; + +import java.lang.reflect.Method; +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 { + + protected static Map damlHandlers; + + { + Class[] handlerArgTypes = new Class[]{ TodoAttr.class }; + damlHandlers = new HashMap(); + // try { + // ; + // } catch (NoSuchMethodException e) { + // // FIXME: this isn't good... + // throw new Error("this is impossible"); + // } + } + + Attr attr; + Element owner; + Map context; + Map options; + + public TodoAttr(Attr attr, Element owner, + Map context, Map options) { + this.attr = attr; + this.owner = owner; + this.context = context; + this.options = options; + } + + public void dispatch() { + Method handler; + String nsuri = attr.getNamespaceURI(); + if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) { + handler = damlHandlers.get(attr.getLocalName()); + } else + handler = null; + try { + if ( handler != null ) + handler.invoke(null, new Object[]{this}); + } catch (IllegalAccessException e) { + // FIXME: this isn't good... + throw new Error("this is impossible"); + } catch (InvocationTargetException e) { + // FIXME: this isn't good... + throw new Error("this is impossible"); + } + } + +} diff --git a/org/madore/damlengine/TodoDeque.java b/org/madore/damlengine/TodoDeque.java index e030e29..037723b 100644 --- a/org/madore/damlengine/TodoDeque.java +++ b/org/madore/damlengine/TodoDeque.java @@ -20,7 +20,7 @@ public final class TodoDeque { todoDeque.addLast(it); } - public static void registerAtEnd(Collection them) { + public static void registerAtEnd(Collection them) { todoDeque.addAll(them); } @@ -28,7 +28,7 @@ public final class TodoDeque { todoDeque.addFirst(it); } - public static void registerAtStart(Collection them) { + public static void registerAtStart(Collection them) { todoDeque.addAll(0, them); } diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java new file mode 100644 index 0000000..f8eaeec --- /dev/null +++ b/org/madore/damlengine/TodoElement.java @@ -0,0 +1,55 @@ +package org.madore.damlengine; + +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; +import java.util.Map; +import java.util.HashMap; +import org.w3c.dom.Element; + +public class TodoElement extends TodoItem { + + protected static Map damlHandlers; + protected static Method defaultHandler; + + { + Class[] handlerArgTypes = new Class[]{ TodoElement.class }; + damlHandlers = new HashMap(); + try { + defaultHandler = DefaultHandler.class.getMethod("handle", handlerArgTypes); + } catch (NoSuchMethodException e) { + // FIXME: this isn't good... + throw new Error("this is impossible"); + } + } + + Element node; + Map context; + Map options; + + public TodoElement(Element node, Map context, Map options) { + this.node = node; + this.context = context; + this.options = options; + } + + public void dispatch() { + Method handler; + 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; + try { + handler.invoke(null, new Object[]{this}); + } catch (IllegalAccessException e) { + // FIXME: this isn't good... + throw new Error("this is impossible"); + } catch (InvocationTargetException e) { + // FIXME: this isn't good... + throw new Error("this is impossible"); + } + } + +} diff --git a/org/madore/damlengine/TodoItem.java b/org/madore/damlengine/TodoItem.java index 941460e..677b3b1 100644 --- a/org/madore/damlengine/TodoItem.java +++ b/org/madore/damlengine/TodoItem.java @@ -1,54 +1,7 @@ package org.madore.damlengine; -import java.lang.reflect.Method; -import java.lang.reflect.InvocationTargetException; -import java.util.Map; -import java.util.HashMap; -import org.w3c.dom.Node; +public abstract class TodoItem { -public class TodoItem { - - protected static Map damlHandlers; - protected static Method defaultHandler; - - { - Class[] handlerArgTypes = new Class[]{ TodoItem.class }; - damlHandlers = new HashMap(); - try { - defaultHandler = DefaultHandler.class.getMethod("handle", handlerArgTypes); - } catch (NoSuchMethodException e) { - // FIXME: this isn't good... - throw new Error("this is impossible"); - } - } - - Node node; - Map context; - Map options; - - public TodoItem(Node node, Map context, Map options) { - this.node = node; - this.context = context; - this.options = options; - } - - public void dispatch() { - Method handler; - if ( node.getNamespaceURI().equals(DamlEngine.DAML_NS) ) { - handler = damlHandlers.get(node.getLocalName()); - if ( handler == null ) - handler = defaultHandler; - } else - handler = defaultHandler; - try { - handler.invoke(null, new Object[]{this}); - } catch (IllegalAccessException e) { - // FIXME: this isn't good... - throw new Error("this is impossible"); - } catch (InvocationTargetException e) { - // FIXME: this isn't good... - throw new Error("this is impossible"); - } - } + public abstract void dispatch(); } -- cgit v1.2.3