Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / xml / dom / DomNodeIterator.java
blob8001556e5806435351351d8a673eb88b81e62830
1 /* DomNodeIterator.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37 package gnu.xml.dom;
39 import org.w3c.dom.DOMException;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.traversal.NodeFilter;
42 import org.w3c.dom.traversal.NodeIterator;
43 import org.w3c.dom.traversal.TreeWalker;
45 /**
46 * Node iterator and tree walker.
48 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
50 public class DomNodeIterator
51 implements NodeIterator, TreeWalker
54 Node root;
55 final int whatToShow;
56 final NodeFilter filter;
57 final boolean entityReferenceExpansion;
58 final boolean walk;
59 Node current;
61 public DomNodeIterator(Node root, int whatToShow, NodeFilter filter,
62 boolean entityReferenceExpansion, boolean walk)
64 if (root == null)
66 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
68 this.root = root;
69 this.whatToShow = whatToShow;
70 this.filter = filter;
71 this.entityReferenceExpansion = entityReferenceExpansion;
72 this.walk = walk;
73 current = root;
76 public Node getRoot()
78 return root;
81 public int getWhatToShow()
83 return whatToShow;
86 public NodeFilter getFilter()
88 return filter;
91 public boolean getExpandEntityReferences()
93 return entityReferenceExpansion;
96 public Node nextNode()
97 throws DOMException
99 if (root == null)
101 throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
103 Node ret;
106 if (current.equals(root))
108 ret = root.getFirstChild();
110 else if (walk)
112 ret = current.getFirstChild();
113 if (ret == null)
115 ret = current.getNextSibling();
117 if (ret == null)
119 Node tmp = current;
120 ret = tmp.getParentNode();
121 while (!ret.equals(root) && tmp.equals(ret.getLastChild()))
123 tmp = ret;
124 ret = tmp.getParentNode();
126 if (ret.equals(root))
128 ret = null;
130 else
132 ret = ret.getNextSibling();
136 else
138 ret = current.getNextSibling();
140 current = (ret == null) ? current : ret;
142 while (!accept(ret));
144 return ret;
147 public Node previousNode()
148 throws DOMException
150 if (root == null)
152 throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
154 Node ret;
157 if (current.equals(root))
159 ret = current.getLastChild();
161 else if (walk)
163 ret = current.getLastChild();
164 if (ret == null)
166 ret = current.getPreviousSibling();
168 if (ret == null)
170 Node tmp = current;
171 ret = tmp.getParentNode();
172 while (!ret.equals(root) && tmp.equals(ret.getFirstChild()))
174 tmp = ret;
175 ret = tmp.getParentNode();
177 if (ret.equals(root))
179 ret = null;
181 else
183 ret = ret.getPreviousSibling();
187 else
189 ret = current.getPreviousSibling();
192 while (!accept(ret));
193 current = (ret == null) ? current : ret;
194 return ret;
197 public Node getCurrentNode()
199 return current;
202 public void setCurrentNode(Node current)
203 throws DOMException
205 if (current == null)
207 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
209 this.current = current;
212 public Node parentNode()
214 Node ret = current.getParentNode();
215 if (!accept (ret))
217 ret = null;
219 current = (ret == null) ? current : ret;
220 return ret;
223 public Node firstChild ()
225 Node ret = current.getFirstChild();
226 while (!accept(ret))
228 ret = ret.getNextSibling();
230 current = (ret == null) ? current : ret;
231 return ret;
234 public Node lastChild()
236 Node ret = current.getLastChild();
237 while (!accept(ret))
239 ret = ret.getPreviousSibling();
241 current = (ret == null) ? current : ret;
242 return ret;
245 public Node previousSibling()
247 Node ret = current.getPreviousSibling();
248 while (!accept(ret))
250 ret = ret.getPreviousSibling();
252 current = (ret == null) ? current : ret;
253 return ret;
256 public Node nextSibling()
258 Node ret = current.getNextSibling();
259 while (!accept(ret))
261 ret = ret.getNextSibling();
263 current = (ret == null) ? current : ret;
264 return ret;
267 public void detach()
269 root = null;
272 boolean accept(Node node)
274 if (node == null)
276 return true;
278 boolean ret;
279 switch (node.getNodeType())
281 case Node.ATTRIBUTE_NODE:
282 ret = (whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0;
283 break;
284 case Node.CDATA_SECTION_NODE:
285 ret = (whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0;
286 break;
287 case Node.COMMENT_NODE:
288 ret = (whatToShow & NodeFilter.SHOW_COMMENT) != 0;
289 break;
290 case Node.DOCUMENT_NODE:
291 ret = (whatToShow & NodeFilter.SHOW_DOCUMENT) != 0;
292 break;
293 case Node.DOCUMENT_FRAGMENT_NODE:
294 ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0;
295 break;
296 case Node.DOCUMENT_TYPE_NODE:
297 ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0;
298 break;
299 case Node.ELEMENT_NODE:
300 ret = (whatToShow & NodeFilter.SHOW_ELEMENT) != 0;
301 break;
302 case Node.ENTITY_NODE:
303 ret = (whatToShow & NodeFilter.SHOW_ENTITY) != 0;
304 break;
305 case Node.ENTITY_REFERENCE_NODE:
306 ret = (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0;
307 ret = ret && entityReferenceExpansion;
308 break;
309 case Node.NOTATION_NODE:
310 ret = (whatToShow & NodeFilter.SHOW_NOTATION) != 0;
311 break;
312 case Node.PROCESSING_INSTRUCTION_NODE:
313 ret = (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0;
314 break;
315 case Node.TEXT_NODE:
316 ret = (whatToShow & NodeFilter.SHOW_TEXT) != 0;
317 break;
318 default:
319 ret = true;
321 if (ret && filter != null)
323 ret = (filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT);
325 return ret;