src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/DeepNodeListImpl.java
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
--- a/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/DeepNodeListImpl.java	Tue Sep 05 13:40:14 2017 +0200
+++ b/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/dom/DeepNodeListImpl.java	Wed Oct 18 13:25:49 2017 -0700
@@ -1,6 +1,6 @@
 /*
- * reserved comment block
- * DO NOT REMOVE OR ALTER!
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * @LastModified: Oct 2017
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -18,14 +18,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package com.sun.org.apache.xerces.internal.dom;
 
+import java.util.ArrayList;
+import java.util.List;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
-import java.util.Vector;
-
 /**
  * This class implements the DOM's NodeList behavior for
  * Element.getElementsByTagName()
@@ -75,16 +74,15 @@
  * @since  PR-DOM-Level-1-19980818.
  */
 public class DeepNodeListImpl
-    implements NodeList {
+        implements NodeList {
 
     //
     // Data
     //
-
     protected NodeImpl rootNode; // Where the search started
     protected String tagName;   // Or "*" to mean all-tags-acceptable
-    protected int changes=0;
-    protected Vector nodes;
+    protected int changes = 0;
+    protected List<Node> nodes;
 
     protected String nsName;
     protected boolean enableNS = false;
@@ -96,15 +94,15 @@
     /** Constructor. */
     public DeepNodeListImpl(NodeImpl rootNode, String tagName) {
         this.rootNode = rootNode;
-        this.tagName  = tagName;
-        nodes = new Vector();
+        this.tagName = tagName;
+        nodes = new ArrayList<>();
     }
 
     /** Constructor for Namespace support. */
     public DeepNodeListImpl(NodeImpl rootNode,
-                            String nsName, String tagName) {
+            String nsName, String tagName) {
         this(rootNode, tagName);
-        this.nsName = (nsName != null && !nsName.equals("")) ? nsName : null;
+        this.nsName = (nsName != null && nsName.length() != 0) ? nsName : null;
         enableNS = true;
     }
 
@@ -124,34 +122,36 @@
         Node thisNode;
 
         // Tree changed. Do it all from scratch!
-        if(rootNode.changes() != changes) {
-            nodes   = new Vector();
+        if (rootNode.changes() != changes) {
+            nodes = new ArrayList<>();
             changes = rootNode.changes();
         }
 
         // In the cache
-        if (index < nodes.size())
-            return (Node)nodes.elementAt(index);
-
-        // Not yet seen
+        final int currentSize = nodes.size();
+        if (index < currentSize) {
+            return nodes.get(index);
+        } // Not yet seen
         else {
 
             // Pick up where we left off (Which may be the beginning)
-                if (nodes.size() == 0)
-                    thisNode = rootNode;
-                else
-                    thisNode=(NodeImpl)(nodes.lastElement());
+            if (currentSize == 0) {
+                thisNode = rootNode;
+            } else {
+                thisNode = (NodeImpl) (nodes.get(currentSize - 1));
+            }
 
-                // Add nodes up to the one we're looking for
-                while(thisNode != null && index >= nodes.size()) {
-                        thisNode=nextMatchingElementAfter(thisNode);
-                        if (thisNode != null)
-                            nodes.addElement(thisNode);
-                    }
+            // Add nodes up to the one we're looking for
+            while (thisNode != null && index >= nodes.size()) {
+                thisNode = nextMatchingElementAfter(thisNode);
+                if (thisNode != null) {
+                    nodes.add(thisNode);
+                }
+            }
 
             // Either what we want, or null (not avail.)
-                    return thisNode;
-            }
+            return thisNode;
+        }
 
     } // item(int):Node
 
@@ -166,82 +166,76 @@
      */
     protected Node nextMatchingElementAfter(Node current) {
 
-            Node next;
-            while (current != null) {
-                    // Look down to first child.
-                    if (current.hasChildNodes()) {
-                            current = (current.getFirstChild());
-                    }
-
-                    // Look right to sibling (but not from root!)
-                    else if (current != rootNode && null != (next = current.getNextSibling())) {
-                                current = next;
-                        }
+        Node next;
+        while (current != null) {
+            // Look down to first child.
+            if (current.hasChildNodes()) {
+                current = (current.getFirstChild());
+            } // Look right to sibling (but not from root!)
+            else if (current != rootNode && null != (next = current.getNextSibling())) {
+                current = next;
+            } // Look up and right (but not past root!)
+            else {
+                next = null;
+                for (; current != rootNode; // Stop when we return to starting point
+                        current = current.getParentNode()) {
 
-                        // Look up and right (but not past root!)
-                        else {
-                                next = null;
-                                for (; current != rootNode; // Stop when we return to starting point
-                                        current = current.getParentNode()) {
-
-                                        next = current.getNextSibling();
-                                        if (next != null)
-                                                break;
-                                }
-                                current = next;
-                        }
+                    next = current.getNextSibling();
+                    if (next != null) {
+                        break;
+                    }
+                }
+                current = next;
+            }
 
                         // Have we found an Element with the right tagName?
-                        // ("*" matches anything.)
-                    if (current != rootNode
-                        && current != null
-                        && current.getNodeType() ==  Node.ELEMENT_NODE) {
-                        if (!enableNS) {
-                            if (tagName.equals("*") ||
-                                ((ElementImpl) current).getTagName().equals(tagName))
-                            {
+            // ("*" matches anything.)
+            if (current != rootNode
+                    && current != null
+                    && current.getNodeType() == Node.ELEMENT_NODE) {
+                if (!enableNS) {
+                    if (tagName.equals("*")
+                            || ((ElementImpl) current).getTagName().equals(tagName)) {
+                        return current;
+                    }
+                } else {
+                    // DOM2: Namespace logic.
+                    if (tagName.equals("*")) {
+                        if (nsName != null && nsName.equals("*")) {
+                            return current;
+                        } else {
+                            ElementImpl el = (ElementImpl) current;
+                            if ((nsName == null
+                                    && el.getNamespaceURI() == null)
+                                    || (nsName != null
+                                    && nsName.equals(el.getNamespaceURI()))) {
                                 return current;
                             }
-                        } else {
-                            // DOM2: Namespace logic.
-                            if (tagName.equals("*")) {
-                                if (nsName != null && nsName.equals("*")) {
-                                    return current;
-                                } else {
-                                    ElementImpl el = (ElementImpl) current;
-                                    if ((nsName == null
-                                         && el.getNamespaceURI() == null)
+                        }
+                    } else {
+                        ElementImpl el = (ElementImpl) current;
+                        if (el.getLocalName() != null
+                                && el.getLocalName().equals(tagName)) {
+                            if (nsName != null && nsName.equals("*")) {
+                                return current;
+                            } else {
+                                if ((nsName == null
+                                        && el.getNamespaceURI() == null)
                                         || (nsName != null
-                                            && nsName.equals(el.getNamespaceURI())))
-                                    {
-                                        return current;
-                                    }
-                                }
-                            } else {
-                                ElementImpl el = (ElementImpl) current;
-                                if (el.getLocalName() != null
-                                    && el.getLocalName().equals(tagName)) {
-                                    if (nsName != null && nsName.equals("*")) {
-                                        return current;
-                                    } else {
-                                        if ((nsName == null
-                                             && el.getNamespaceURI() == null)
-                                            || (nsName != null &&
-                                                nsName.equals(el.getNamespaceURI())))
-                                        {
-                                            return current;
-                                        }
-                                    }
+                                        && nsName.equals(el.getNamespaceURI()))) {
+                                    return current;
                                 }
                             }
                         }
                     }
-
-                // Otherwise continue walking the tree
+                }
             }
 
-            // Fell out of tree-walk; no more instances found
-            return null;
+            // Otherwise continue walking the tree
+        }
+
+        // Fell out of tree-walk; no more instances found
+        return null;
 
     } // nextMatchingElementAfter(int):Node