org.apache.xerces.dom

## Class DeepNodeListImpl

- java.lang.Object
- - org.apache.xerces.dom.DeepNodeListImpl

- All Implemented Interfaces:
    org.w3c.dom.NodeList

      public class DeepNodeListImpl
      extends java.lang.Object
      implements org.w3c.dom.NodeList

  This class implements the DOM's NodeList behavior for Element.getElementsByTagName()

  The DOM describes NodeList as follows:

  1) It may represent EITHER nodes scattered through a subtree (when returned by Element.getElementsByTagName), or just the immediate children (when returned by Node.getChildNodes). The latter is easy, but the former (which this class addresses) is more challenging.

  2) Its behavior is "live" -- that is, it always reflects the current state of the document tree. To put it another way, the NodeLists obtained before and after a series of insertions and deletions are effectively identical (as far as the user is concerned, the former has been dynamically updated as the changes have been made).

  3) Its API accesses individual nodes via an integer index, with the listed nodes numbered sequentially in the order that they were found during a preorder depth-first left-to-right search of the tree. (Of course in the case of getChildNodes, depth is not involved.) As nodes are inserted or deleted in the tree, and hence the NodeList, the numbering of nodes that follow them in the NodeList will change.

  It is rather painful to support the latter two in the getElementsByTagName case. The current solution is for Nodes to maintain a change count (eventually that may be a Digest instead), which the NodeList tracks and uses to invalidate itself.

  Unfortunately, this does _not_ respond efficiently in the case that the dynamic behavior was supposed to address: scanning a tree while it is being extended. That requires knowing which subtrees have changed, which can become an arbitrarily complex problem.

  We save some work by filling the ArrayList only as we access the item()s... but I suspect the same users who demanded index-based access will also start by doing a getLength() to control their loop, blowing this optimization out of the water.

  NOTE: Level 2 of the DOM will probably _not_ use NodeList for its extended search mechanisms, partly for the reasons just discussed.

  Since:
    PR-DOM-Level-1-19980818.
  Version:
    $Id: DeepNodeListImpl.java 883487 2009-11-23 20:28:10Z knoaman $

  # INTERNAL:

    Usage of this class is not supported. It may be altered or removed at any time.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected int` | `changes`
    `protected boolean` | `enableNS`
    `protected java.util.ArrayList` | `nodes`
    `protected java.lang.String` | `nsName`
    `protected NodeImpl` | `rootNode`
    `protected java.lang.String` | `tagName`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DeepNodeListImpl(NodeImpl rootNode, java.lang.String tagName)` Constructor.
    `DeepNodeListImpl(NodeImpl rootNode, java.lang.String nsName, java.lang.String tagName)` Constructor for Namespace support.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `getLength()` Returns the length of the node list.
    `org.w3c.dom.Node` | `item(int index)` Returns the node at the specified index.
    `protected org.w3c.dom.Node` | `nextMatchingElementAfter(org.w3c.dom.Node current)` Iterative tree-walker.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### rootNode

          protected NodeImpl rootNode

    - #### tagName

          protected java.lang.String tagName

    - #### changes

          protected int changes

    - #### nodes

          protected java.util.ArrayList nodes

    - #### nsName

          protected java.lang.String nsName

    - #### enableNS

          protected boolean enableNS

  - ### Constructor Detail

    - #### DeepNodeListImpl

          public DeepNodeListImpl(NodeImpl rootNode,
                          java.lang.String tagName)

      Constructor.

    - #### DeepNodeListImpl

          public DeepNodeListImpl(NodeImpl rootNode,
                          java.lang.String nsName,
                          java.lang.String tagName)

      Constructor for Namespace support.

  - ### Method Detail

    - #### getLength

          public int getLength()

      Returns the length of the node list.
      Specified by:
        `getLength` in interface `org.w3c.dom.NodeList`

    - #### item

          public org.w3c.dom.Node item(int index)

      Returns the node at the specified index.
      Specified by:
        `item` in interface `org.w3c.dom.NodeList`
      Parameters:
        `index` - Index into the collection.
      Returns:
        The node at the `index`th position in the `NodeList`, or `null` if that is not a valid index.

    - #### nextMatchingElementAfter

          protected org.w3c.dom.Node nextMatchingElementAfter(org.w3c.dom.Node current)

      Iterative tree-walker. When you have a Parent link, there's often no need to resort to recursion. NOTE THAT only Element nodes are matched since we're specifically supporting getElementsByTagName().

Copyright © 1999-2022 The Apache Software Foundation. All Rights Reserved.