1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.dom;
  58. import org.w3c.dom.Node;
  59. import org.w3c.dom.NodeList;
  60. import java.util.Vector;
  61. /**
  62. * This class implements the DOM's NodeList behavior for
  63. * Element.getElementsByTagName()
  64. * <P>
  65. * The DOM describes NodeList as follows:
  66. * <P>
  67. * 1) It may represent EITHER nodes scattered through a subtree (when
  68. * returned by Element.getElementsByTagName), or just the immediate
  69. * children (when returned by Node.getChildNodes). The latter is easy,
  70. * but the former (which this class addresses) is more challenging.
  71. * <P>
  72. * 2) Its behavior is "live" -- that is, it always reflects the
  73. * current state of the document tree. To put it another way, the
  74. * NodeLists obtained before and after a series of insertions and
  75. * deletions are effectively identical (as far as the user is
  76. * concerned, the former has been dynamically updated as the changes
  77. * have been made).
  78. * <P>
  79. * 3) Its API accesses individual nodes via an integer index, with the
  80. * listed nodes numbered sequentially in the order that they were
  81. * found during a preorder depth-first left-to-right search of the tree.
  82. * (Of course in the case of getChildNodes, depth is not involved.) As
  83. * nodes are inserted or deleted in the tree, and hence the NodeList,
  84. * the numbering of nodes that follow them in the NodeList will
  85. * change.
  86. * <P>
  87. * It is rather painful to support the latter two in the
  88. * getElementsByTagName case. The current solution is for Nodes to
  89. * maintain a change count (eventually that may be a Digest instead),
  90. * which the NodeList tracks and uses to invalidate itself.
  91. * <P>
  92. * Unfortunately, this does _not_ respond efficiently in the case that
  93. * the dynamic behavior was supposed to address: scanning a tree while
  94. * it is being extended. That requires knowing which subtrees have
  95. * changed, which can become an arbitrarily complex problem.
  96. * <P>
  97. * We save some work by filling the vector only as we access the
  98. * item()s... but I suspect the same users who demanded index-based
  99. * access will also start by doing a getLength() to control their loop,
  100. * blowing this optimization out of the water.
  101. * <P>
  102. * NOTE: Level 2 of the DOM will probably _not_ use NodeList for its
  103. * extended search mechanisms, partly for the reasons just discussed.
  104. *
  105. * @version $Id: DeepNodeListImpl.java,v 1.7 2002/01/29 01:15:07 lehors Exp $
  106. * @since PR-DOM-Level-1-19980818.
  107. */
  108. public class DeepNodeListImpl
  109. implements NodeList {
  110. //
  111. // Data
  112. //
  113. protected NodeImpl rootNode; // Where the search started
  114. protected String tagName; // Or "*" to mean all-tags-acceptable
  115. protected int changes=0;
  116. protected Vector nodes;
  117. protected String nsName;
  118. protected boolean enableNS = false;
  119. //
  120. // Constructors
  121. //
  122. /** Constructor. */
  123. public DeepNodeListImpl(NodeImpl rootNode, String tagName) {
  124. this.rootNode = rootNode;
  125. this.tagName = tagName;
  126. nodes = new Vector();
  127. }
  128. /** Constructor for Namespace support. */
  129. public DeepNodeListImpl(NodeImpl rootNode,
  130. String nsName, String tagName) {
  131. this(rootNode, tagName);
  132. this.nsName = (nsName != null && !nsName.equals("")) ? nsName : null;
  133. enableNS = true;
  134. }
  135. //
  136. // NodeList methods
  137. //
  138. /** Returns the length of the node list. */
  139. public int getLength() {
  140. // Preload all matching elements. (Stops when we run out of subtree!)
  141. item(java.lang.Integer.MAX_VALUE);
  142. return nodes.size();
  143. }
  144. /** Returns the node at the specified index. */
  145. public Node item(int index) {
  146. Node thisNode;
  147. // Tree changed. Do it all from scratch!
  148. if(rootNode.changes() != changes) {
  149. nodes = new Vector();
  150. changes = rootNode.changes();
  151. }
  152. // In the cache
  153. if (index < nodes.size())
  154. return (Node)nodes.elementAt(index);
  155. // Not yet seen
  156. else {
  157. // Pick up where we left off (Which may be the beginning)
  158. if (nodes.size() == 0)
  159. thisNode = rootNode;
  160. else
  161. thisNode=(NodeImpl)(nodes.lastElement());
  162. // Add nodes up to the one we're looking for
  163. while(thisNode != null && index >= nodes.size()) {
  164. thisNode=nextMatchingElementAfter(thisNode);
  165. if (thisNode != null)
  166. nodes.addElement(thisNode);
  167. }
  168. // Either what we want, or null (not avail.)
  169. return thisNode;
  170. }
  171. } // item(int):Node
  172. //
  173. // Protected methods (might be overridden by an extending DOM)
  174. //
  175. /**
  176. * Iterative tree-walker. When you have a Parent link, there's often no
  177. * need to resort to recursion. NOTE THAT only Element nodes are matched
  178. * since we're specifically supporting getElementsByTagName().
  179. */
  180. protected Node nextMatchingElementAfter(Node current) {
  181. Node next;
  182. while (current != null) {
  183. // Look down to first child.
  184. if (current.hasChildNodes()) {
  185. current = (current.getFirstChild());
  186. }
  187. // Look right to sibling (but not from root!)
  188. else if (current != rootNode && null != (next = current.getNextSibling())) {
  189. current = next;
  190. }
  191. // Look up and right (but not past root!)
  192. else {
  193. next = null;
  194. for (; current != rootNode; // Stop when we return to starting point
  195. current = current.getParentNode()) {
  196. next = current.getNextSibling();
  197. if (next != null)
  198. break;
  199. }
  200. current = next;
  201. }
  202. // Have we found an Element with the right tagName?
  203. // ("*" matches anything.)
  204. if (current != rootNode
  205. && current != null
  206. && current.getNodeType() == Node.ELEMENT_NODE) {
  207. if (!enableNS) {
  208. if (tagName.equals("*") ||
  209. ((ElementImpl) current).getTagName().equals(tagName))
  210. {
  211. return current;
  212. }
  213. } else {
  214. // DOM2: Namespace logic.
  215. if (tagName.equals("*")) {
  216. if (nsName != null && nsName.equals("*")) {
  217. return current;
  218. } else {
  219. ElementImpl el = (ElementImpl) current;
  220. if ((nsName == null
  221. && el.getNamespaceURI() == null)
  222. || (nsName != null
  223. && nsName.equals(el.getNamespaceURI())))
  224. {
  225. return current;
  226. }
  227. }
  228. } else {
  229. ElementImpl el = (ElementImpl) current;
  230. if (el.getLocalName() != null
  231. && el.getLocalName().equals(tagName)) {
  232. if (nsName != null && nsName.equals("*")) {
  233. return current;
  234. } else {
  235. if ((nsName == null
  236. && el.getNamespaceURI() == null)
  237. || (nsName != null &&
  238. nsName.equals(el.getNamespaceURI())))
  239. {
  240. return current;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. // Otherwise continue walking the tree
  248. }
  249. // Fell out of tree-walk; no more instances found
  250. return null;
  251. } // nextMatchingElementAfter(int):Node
  252. } // class DeepNodeListImpl