DomNodeIterator.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* DomNodeIterator.java --
  2. Copyright (C) 2004 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.traversal.NodeFilter;
  35. import org.w3c.dom.traversal.NodeIterator;
  36. import org.w3c.dom.traversal.TreeWalker;
  37. /**
  38. * Node iterator and tree walker.
  39. *
  40. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  41. */
  42. public class DomNodeIterator
  43. implements NodeIterator, TreeWalker
  44. {
  45. Node root;
  46. final int whatToShow;
  47. final NodeFilter filter;
  48. final boolean entityReferenceExpansion;
  49. final boolean walk;
  50. Node current;
  51. public DomNodeIterator(Node root, int whatToShow, NodeFilter filter,
  52. boolean entityReferenceExpansion, boolean walk)
  53. {
  54. if (root == null)
  55. {
  56. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
  57. }
  58. this.root = root;
  59. this.whatToShow = whatToShow;
  60. this.filter = filter;
  61. this.entityReferenceExpansion = entityReferenceExpansion;
  62. this.walk = walk;
  63. current = root;
  64. }
  65. public Node getRoot()
  66. {
  67. return root;
  68. }
  69. public int getWhatToShow()
  70. {
  71. return whatToShow;
  72. }
  73. public NodeFilter getFilter()
  74. {
  75. return filter;
  76. }
  77. public boolean getExpandEntityReferences()
  78. {
  79. return entityReferenceExpansion;
  80. }
  81. public Node nextNode()
  82. throws DOMException
  83. {
  84. if (root == null)
  85. {
  86. throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
  87. }
  88. Node ret;
  89. do
  90. {
  91. if (current.equals(root))
  92. {
  93. ret = root.getFirstChild();
  94. }
  95. else if (walk)
  96. {
  97. ret = current.getFirstChild();
  98. if (ret == null)
  99. {
  100. ret = current.getNextSibling();
  101. }
  102. if (ret == null)
  103. {
  104. Node tmp = current;
  105. ret = tmp.getParentNode();
  106. while (!ret.equals(root) && tmp.equals(ret.getLastChild()))
  107. {
  108. tmp = ret;
  109. ret = tmp.getParentNode();
  110. }
  111. if (ret.equals(root))
  112. {
  113. ret = null;
  114. }
  115. else
  116. {
  117. ret = ret.getNextSibling();
  118. }
  119. }
  120. }
  121. else
  122. {
  123. ret = current.getNextSibling();
  124. }
  125. current = (ret == null) ? current : ret;
  126. }
  127. while (!accept(ret));
  128. return ret;
  129. }
  130. public Node previousNode()
  131. throws DOMException
  132. {
  133. if (root == null)
  134. {
  135. throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
  136. }
  137. Node ret;
  138. do
  139. {
  140. if (current.equals(root))
  141. {
  142. ret = current.getLastChild();
  143. }
  144. else if (walk)
  145. {
  146. ret = current.getLastChild();
  147. if (ret == null)
  148. {
  149. ret = current.getPreviousSibling();
  150. }
  151. if (ret == null)
  152. {
  153. Node tmp = current;
  154. ret = tmp.getParentNode();
  155. while (!ret.equals(root) && tmp.equals(ret.getFirstChild()))
  156. {
  157. tmp = ret;
  158. ret = tmp.getParentNode();
  159. }
  160. if (ret.equals(root))
  161. {
  162. ret = null;
  163. }
  164. else
  165. {
  166. ret = ret.getPreviousSibling();
  167. }
  168. }
  169. }
  170. else
  171. {
  172. ret = current.getPreviousSibling();
  173. }
  174. }
  175. while (!accept(ret));
  176. current = (ret == null) ? current : ret;
  177. return ret;
  178. }
  179. public Node getCurrentNode()
  180. {
  181. return current;
  182. }
  183. public void setCurrentNode(Node current)
  184. throws DOMException
  185. {
  186. if (current == null)
  187. {
  188. throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
  189. }
  190. this.current = current;
  191. }
  192. public Node parentNode()
  193. {
  194. Node ret = current.getParentNode();
  195. if (!accept (ret))
  196. {
  197. ret = null;
  198. }
  199. current = (ret == null) ? current : ret;
  200. return ret;
  201. }
  202. public Node firstChild ()
  203. {
  204. Node ret = current.getFirstChild();
  205. while (!accept(ret))
  206. {
  207. ret = ret.getNextSibling();
  208. }
  209. current = (ret == null) ? current : ret;
  210. return ret;
  211. }
  212. public Node lastChild()
  213. {
  214. Node ret = current.getLastChild();
  215. while (!accept(ret))
  216. {
  217. ret = ret.getPreviousSibling();
  218. }
  219. current = (ret == null) ? current : ret;
  220. return ret;
  221. }
  222. public Node previousSibling()
  223. {
  224. Node ret = current.getPreviousSibling();
  225. while (!accept(ret))
  226. {
  227. ret = ret.getPreviousSibling();
  228. }
  229. current = (ret == null) ? current : ret;
  230. return ret;
  231. }
  232. public Node nextSibling()
  233. {
  234. Node ret = current.getNextSibling();
  235. while (!accept(ret))
  236. {
  237. ret = ret.getNextSibling();
  238. }
  239. current = (ret == null) ? current : ret;
  240. return ret;
  241. }
  242. public void detach()
  243. {
  244. root = null;
  245. }
  246. boolean accept(Node node)
  247. {
  248. if (node == null)
  249. {
  250. return true;
  251. }
  252. boolean ret;
  253. switch (node.getNodeType())
  254. {
  255. case Node.ATTRIBUTE_NODE:
  256. ret = (whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0;
  257. break;
  258. case Node.CDATA_SECTION_NODE:
  259. ret = (whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0;
  260. break;
  261. case Node.COMMENT_NODE:
  262. ret = (whatToShow & NodeFilter.SHOW_COMMENT) != 0;
  263. break;
  264. case Node.DOCUMENT_NODE:
  265. ret = (whatToShow & NodeFilter.SHOW_DOCUMENT) != 0;
  266. break;
  267. case Node.DOCUMENT_FRAGMENT_NODE:
  268. ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0;
  269. break;
  270. case Node.DOCUMENT_TYPE_NODE:
  271. ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0;
  272. break;
  273. case Node.ELEMENT_NODE:
  274. ret = (whatToShow & NodeFilter.SHOW_ELEMENT) != 0;
  275. break;
  276. case Node.ENTITY_NODE:
  277. ret = (whatToShow & NodeFilter.SHOW_ENTITY) != 0;
  278. break;
  279. case Node.ENTITY_REFERENCE_NODE:
  280. ret = (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0;
  281. ret = ret && entityReferenceExpansion;
  282. break;
  283. case Node.NOTATION_NODE:
  284. ret = (whatToShow & NodeFilter.SHOW_NOTATION) != 0;
  285. break;
  286. case Node.PROCESSING_INSTRUCTION_NODE:
  287. ret = (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0;
  288. break;
  289. case Node.TEXT_NODE:
  290. ret = (whatToShow & NodeFilter.SHOW_TEXT) != 0;
  291. break;
  292. default:
  293. ret = true;
  294. }
  295. if (ret && filter != null)
  296. {
  297. ret = (filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT);
  298. }
  299. return ret;
  300. }
  301. }