DomIterator.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* DomIterator.java --
  2. Copyright (C) 1999, 2000, 2001, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.dom;
  32. import org.w3c.dom.DOMException;
  33. import org.w3c.dom.Node;
  34. import org.w3c.dom.events.Event;
  35. import org.w3c.dom.events.EventListener;
  36. import org.w3c.dom.events.EventTarget;
  37. import org.w3c.dom.events.MutationEvent;
  38. import org.w3c.dom.traversal.NodeFilter;
  39. import org.w3c.dom.traversal.NodeIterator;
  40. /**
  41. * <p> "NodeIterator" implementation, usable with any L2 DOM which
  42. * supports MutationEvents. </p>
  43. *
  44. * @author David Brownell
  45. */
  46. public final class DomIterator
  47. implements NodeIterator, EventListener
  48. {
  49. private Node reference;
  50. private boolean right;
  51. private boolean done;
  52. private final Node root;
  53. private final int whatToShow;
  54. private final NodeFilter filter;
  55. private final boolean expandEntityReferences;
  56. /**
  57. * Constructs and initializes an iterator.
  58. */
  59. protected DomIterator(Node root,
  60. int whatToShow,
  61. NodeFilter filter,
  62. boolean entityReferenceExpansion)
  63. {
  64. if (!root.isSupported("MutationEvents", "2.0"))
  65. {
  66. throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR,
  67. "Iterator needs mutation events", root, 0);
  68. }
  69. this.root = root;
  70. this.whatToShow = whatToShow;
  71. this.filter = filter;
  72. this.expandEntityReferences = entityReferenceExpansion;
  73. // start condition: going right, seen nothing yet.
  74. reference = null;
  75. right = true;
  76. EventTarget target = (EventTarget) root;
  77. target.addEventListener("DOMNodeRemoved", this, false);
  78. }
  79. /**
  80. * <b>DOM L2</b>
  81. * Flags the iterator as done, unregistering its event listener so
  82. * that the iterator can be garbage collected without relying on weak
  83. * references (a "Java 2" feature) in the event subsystem.
  84. */
  85. public void detach()
  86. {
  87. EventTarget target = (EventTarget) root;
  88. target.removeEventListener("DOMNodeRemoved", this, false);
  89. done = true;
  90. }
  91. /**
  92. * <b>DOM L2</b>
  93. * Returns the flag controlling whether iteration descends
  94. * through entity references.
  95. */
  96. public boolean getExpandEntityReferences()
  97. {
  98. return expandEntityReferences;
  99. }
  100. /**
  101. * <b>DOM L2</b>
  102. * Returns the filter provided during construction.
  103. */
  104. public NodeFilter getFilter()
  105. {
  106. return filter;
  107. }
  108. /**
  109. * <b>DOM L2</b>
  110. * Returns the root of the tree this is iterating through.
  111. */
  112. public Node getRoot()
  113. {
  114. return root;
  115. }
  116. /**
  117. * <b>DOM L2</b>
  118. * Returns the mask of flags provided during construction.
  119. */
  120. public int getWhatToShow()
  121. {
  122. return whatToShow;
  123. }
  124. /**
  125. * <b>DOM L2</b>
  126. * Returns the next node in a forward iteration, masked and filtered.
  127. * Note that the node may be read-only due to entity expansions.
  128. * A null return indicates the iteration is complete, but may still
  129. * be processed backwards.
  130. */
  131. public Node nextNode()
  132. {
  133. if (done)
  134. {
  135. throw new DomDOMException(DOMException.INVALID_STATE_ERR);
  136. }
  137. right = true;
  138. return walk(true);
  139. }
  140. /**
  141. * <b>DOM L2</b>
  142. * Returns the next node in a backward iteration, masked and filtered.
  143. * Note that the node may be read-only due to entity expansions.
  144. * A null return indicates the iteration is complete, but may still
  145. * be processed forwards.
  146. */
  147. public Node previousNode()
  148. {
  149. if (done)
  150. {
  151. throw new DomDOMException(DOMException.INVALID_STATE_ERR);
  152. }
  153. Node previous = reference;
  154. right = false;
  155. walk(false);
  156. return previous;
  157. }
  158. private boolean shouldShow(Node node)
  159. // raises Runtime exceptions indirectly, via acceptNode()
  160. {
  161. if ((whatToShow & (1 << (node.getNodeType() - 1))) == 0)
  162. {
  163. return false;
  164. }
  165. if (filter == null)
  166. {
  167. return true;
  168. }
  169. return filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT;
  170. }
  171. //
  172. // scenario: root = 1, sequence = 1 2 ... 3 4
  173. // forward walk: 1 2 ... 3 4 null
  174. // then backward: 4 3 ... 2 1 null
  175. //
  176. // At the leftmost end, "previous" == null
  177. // At the rightmost end, "previous" == 4
  178. //
  179. // The current draft spec really seem to make no sense re the
  180. // role of the reference node, so what it says is ignored here.
  181. //
  182. private Node walk(boolean forward)
  183. {
  184. Node here = reference;
  185. while ((here = successor(here, forward)) != null
  186. && !shouldShow(here))
  187. {
  188. continue;
  189. }
  190. if (here != null || !forward)
  191. {
  192. reference = here;
  193. }
  194. return here;
  195. }
  196. private boolean isLeaf(Node here)
  197. {
  198. boolean leaf = !here.hasChildNodes();
  199. if (!leaf && !expandEntityReferences)
  200. {
  201. leaf = (here.getNodeType() == Node.ENTITY_REFERENCE_NODE);
  202. }
  203. return leaf;
  204. }
  205. //
  206. // Returns the immediate successor in a forward (or backward)
  207. // document order walk, sans filtering ... except that it knows
  208. // how to stop, returning null when done. This is a depth first
  209. // preorder traversal when run in the forward direction.
  210. //
  211. private Node successor(Node here, boolean forward)
  212. {
  213. Node next;
  214. // the "leftmost" end is funky
  215. if (here == null)
  216. {
  217. return forward ? root : null;
  218. }
  219. //
  220. // Forward, this is preorder: children before siblings.
  221. // Backward, it's postorder: we saw the children already.
  222. //
  223. if (forward && !isLeaf(here))
  224. {
  225. return here.getFirstChild();
  226. }
  227. // There's no way up or sideways from the root, so if we
  228. // couldn't move down to a child, there's nowhere to go.
  229. //
  230. if (here == root)
  231. return null;
  232. //
  233. // Siblings ... if forward, we visit them, if backwards
  234. // we visit their children first.
  235. //
  236. if (forward)
  237. {
  238. if ((next = here.getNextSibling()) != null)
  239. {
  240. return next;
  241. }
  242. }
  243. else if ((next = here.getPreviousSibling()) != null)
  244. {
  245. if (isLeaf(next))
  246. {
  247. return next;
  248. }
  249. next = next.getLastChild();
  250. while (!isLeaf(next))
  251. {
  252. next = next.getLastChild();
  253. }
  254. return next;
  255. }
  256. //
  257. // We can't go down or lateral -- it's up, then. The logic is
  258. // the converse of what's above: backwards is easy (the parent
  259. // is next), forwards isn't.
  260. //
  261. next = here.getParentNode();
  262. if (!forward)
  263. {
  264. return next;
  265. }
  266. Node temp = null;
  267. while (next != null
  268. && next != root
  269. && (temp = next.getNextSibling()) == null)
  270. {
  271. next = next.getParentNode();
  272. }
  273. // If we have exceeded the root node then stop traversing.
  274. if (next == root.getParentNode())
  275. {
  276. return null;
  277. }
  278. return temp;
  279. }
  280. /**
  281. * Not for public use. This lets the iterator know when its
  282. * reference node will be removed from the tree, so that a new
  283. * one may be selected.
  284. *
  285. * <p> This version works by watching removal events as they
  286. * bubble up. So, don't prevent them from bubbling.
  287. */
  288. public void handleEvent(Event e)
  289. {
  290. MutationEvent event;
  291. Node ancestor, removed;
  292. if (reference == null
  293. || !"DOMNodeRemoved".equals(e.getType())
  294. || e.getEventPhase() != Event.BUBBLING_PHASE)
  295. {
  296. return;
  297. }
  298. event = (MutationEvent) e;
  299. removed = (Node) event.getTarget();
  300. // See if the removal will cause trouble for this iterator
  301. // by being the reference node or an ancestor of it.
  302. for (ancestor = reference;
  303. ancestor != null && ancestor != root;
  304. ancestor = ancestor.getParentNode())
  305. {
  306. if (ancestor == removed)
  307. {
  308. break;
  309. }
  310. }
  311. if (ancestor != removed)
  312. {
  313. return;
  314. }
  315. // OK, it'll cause trouble. We want to make the "next"
  316. // node in our current traversal direction seem right.
  317. // So we pick the nearest node that's not getting removed,
  318. // but go in the _opposite_ direction from our current
  319. // traversal ... so the "next" doesn't skip anything.
  320. Node candidate;
  321. search:
  322. while ((candidate = walk(!right)) != null)
  323. {
  324. for (ancestor = candidate;
  325. ancestor != null && ancestor != root;
  326. ancestor = ancestor.getParentNode())
  327. {
  328. if (ancestor == removed)
  329. {
  330. continue search;
  331. }
  332. }
  333. return;
  334. }
  335. // The current DOM WD talks about a special case here;
  336. // I've not yet seen it.
  337. }
  338. }