summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david@procyon.(none)>2010-04-13 11:58:51 +0200
committerDavid A. Madore <david@procyon.(none)>2010-04-13 11:58:51 +0200
commit734a0b78ea5508f243caf18a50e36ac8918e11db (patch)
tree8198107c3f0e353b263ff4d84735ca0b905a88dc
parent45fa82f3f6b98f3b6f81e8e753d8e7341dee14b5 (diff)
downloaddamlengine-734a0b78ea5508f243caf18a50e36ac8918e11db.tar.gz
damlengine-734a0b78ea5508f243caf18a50e36ac8918e11db.tar.bz2
damlengine-734a0b78ea5508f243caf18a50e36ac8918e11db.zip
Create a todo deque class, and use it.
-rw-r--r--org/madore/damlengine/DamlEngine.java10
-rw-r--r--org/madore/damlengine/DefaultHandler.java15
-rw-r--r--org/madore/damlengine/TodoDeque.java53
3 files changed, 72 insertions, 6 deletions
diff --git a/org/madore/damlengine/DamlEngine.java b/org/madore/damlengine/DamlEngine.java
index 6afa628..f13d376 100644
--- a/org/madore/damlengine/DamlEngine.java
+++ b/org/madore/damlengine/DamlEngine.java
@@ -18,14 +18,12 @@ public final class DamlEngine {
private DamlEngine() { } // Forbid instantiation
public static void processDocument() {
- LinkedList<TodoItem> todoStack = new LinkedList<TodoItem>();
HashMap<String,Object> options = new HashMap<String,Object>();
options.put("isRoot", true);
- todoStack.add(new TodoItem(doc.getDocumentElement(),
- new HashMap<String,Object>(),
- options));
- todoStack.removeFirst().dispatch();
- // ...
+ TodoDeque.registerAtEnd(new TodoItem(doc.getDocumentElement(),
+ new HashMap<String,Object>(),
+ options));
+ TodoDeque.dispatchLoop();
}
public static void main(String[] args)
diff --git a/org/madore/damlengine/DefaultHandler.java b/org/madore/damlengine/DefaultHandler.java
index 6a2c174..4720f67 100644
--- a/org/madore/damlengine/DefaultHandler.java
+++ b/org/madore/damlengine/DefaultHandler.java
@@ -1,11 +1,26 @@
package org.madore.damlengine;
+import java.util.Vector;
+import java.util.HashMap;
+import org.w3c.dom.*;
+
public class DefaultHandler {
private DefaultHandler() { }
public static void handle(TodoItem that) {
System.err.println("handling a "+that.node.getNodeName()+" node");
+ NodeList children = that.node.getChildNodes();
+ Node child;
+ Vector<TodoItem> toProcess = new Vector<TodoItem>();
+ 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<String,Object>());
+ toProcess.add(it);
+ }
+ }
+ TodoDeque.registerAtStart(toProcess);
}
}
diff --git a/org/madore/damlengine/TodoDeque.java b/org/madore/damlengine/TodoDeque.java
new file mode 100644
index 0000000..e030e29
--- /dev/null
+++ b/org/madore/damlengine/TodoDeque.java
@@ -0,0 +1,53 @@
+package org.madore.damlengine;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+public final class TodoDeque {
+
+ // FIXME: having everything static is ugly as hell... maybe this
+ // should be instantiatable?
+
+ private TodoDeque() { } // Forbid instantiation
+
+ private static LinkedList<TodoItem> todoDeque = new LinkedList<TodoItem>();
+
+ {
+ todoDeque = new LinkedList<TodoItem>();
+ }
+
+ public static void registerAtEnd(TodoItem it) {
+ todoDeque.addLast(it);
+ }
+
+ public static void registerAtEnd(Collection<TodoItem> them) {
+ todoDeque.addAll(them);
+ }
+
+ public static void registerAtStart(TodoItem it) {
+ todoDeque.addFirst(it);
+ }
+
+ public static void registerAtStart(Collection<TodoItem> them) {
+ todoDeque.addAll(0, them);
+ }
+
+ public static TodoItem removeNext() {
+ return todoDeque.poll();
+ }
+
+ public static boolean dispatchOne() {
+ TodoItem it = removeNext();
+ if ( it != null ) {
+ it.dispatch();
+ return true;
+ } else
+ return false;
+ }
+
+ public static void dispatchLoop() {
+ while ( dispatchOne() )
+ ;
+ }
+
+}