From 98f35c3443d6df4db5a73916d9a4cd80c482f4c5 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sun, 18 Apr 2010 18:29:01 +0200 Subject: Hide the use of NodeList and NamedNodeMap by real Java ArrayList objects. This makes it possible to modify the underlying tree without getting hit by .item(i) returning the wrong next item. --- org/madore/damlengine/TodoDefaultElement.java | 34 ++++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'org/madore/damlengine/TodoDefaultElement.java') diff --git a/org/madore/damlengine/TodoDefaultElement.java b/org/madore/damlengine/TodoDefaultElement.java index fd916e3..50a460f 100644 --- a/org/madore/damlengine/TodoDefaultElement.java +++ b/org/madore/damlengine/TodoDefaultElement.java @@ -19,13 +19,30 @@ public class TodoDefaultElement extends TodoElement { super(node, context, options); } - public void handleAttributes() { - NamedNodeMap attrs = this.node.getAttributes(); + public static ArrayList getAttrList(Element node) { + NamedNodeMap attrs = node.getAttributes(); + ArrayList attrList = new ArrayList(attrs.getLength()); Node attr; - ArrayList toProcess = new ArrayList(); - for ( int i=0 ; (attr=attrs.item(i)) != null ; i++ ) { + for ( int i=0 ; (attr=attrs.item(i)) != null ; i++ ) + attrList.add((Attr)attr); + return attrList; + } + + public static ArrayList getChildList(Element node) { + NodeList children = node.getChildNodes(); + ArrayList childList = new ArrayList(children.getLength()); + Node child; + for ( int i=0 ; (child=children.item(i)) != null ; i++ ) + childList.add(child); + return childList; + } + + public void handleAttributes() { + ArrayList attrList = getAttrList(this.node); + ArrayList toProcess = new ArrayList(attrList.size()); + for ( Attr attr : attrList ) { TodoAttr it - = TodoAttr.getTodoAttr((Attr)attr, this.node, this.context, + = TodoAttr.getTodoAttr(attr, this.node, this.context, new TodoItem.Options()); if ( it != null ) toProcess.add(it); @@ -35,10 +52,9 @@ public class TodoDefaultElement extends TodoElement { public void handleNodeOnly() { System.err.println("handling a "+this.node.getNodeName()+" element"); - NodeList children = this.node.getChildNodes(); - Node child; - ArrayList toProcess = new ArrayList(); - for ( int i=0 ; (child=children.item(i)) != null ; i++ ) { + ArrayList childList = getChildList(this.node); + ArrayList toProcess = new ArrayList(childList.size()); + for ( Node child : childList ) { if ( child.getNodeType() == Node.ELEMENT_NODE ) { TodoElement it = TodoElement.getTodoElement((Element)child, this.context, -- cgit v1.2.3