From 98f35c3443d6df4db5a73916d9a4cd80c482f4c5 Mon Sep 17 00:00:00 2001
From: "David A. Madore" <david@procyon.(none)>
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(-)

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<Attr> getAttrList(Element node) {
+	NamedNodeMap attrs = node.getAttributes();
+	ArrayList<Attr> attrList = new ArrayList<Attr>(attrs.getLength());
 	Node attr;
-	ArrayList<TodoAttr> toProcess = new ArrayList<TodoAttr>();
-	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<Node> getChildList(Element node) {
+	NodeList children = node.getChildNodes();
+	ArrayList<Node> childList = new ArrayList<Node>(children.getLength());
+	Node child;
+	for ( int i=0 ; (child=children.item(i)) != null ; i++ )
+	    childList.add(child);
+	return childList;
+    }
+
+    public void handleAttributes() {
+	ArrayList<Attr> attrList = getAttrList(this.node);
+	ArrayList<TodoAttr> toProcess = new ArrayList<TodoAttr>(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<TodoElement> toProcess = new ArrayList<TodoElement>();
-	for ( int i=0 ; (child=children.item(i)) != null ; i++ ) {
+	ArrayList<Node> childList = getChildList(this.node);
+	ArrayList<TodoElement> toProcess = new ArrayList<TodoElement>(childList.size());
+	for ( Node child : childList ) {
 	    if ( child.getNodeType() == Node.ELEMENT_NODE ) {
 		TodoElement it
 		    = TodoElement.getTodoElement((Element)child, this.context,
-- 
cgit v1.2.3