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)
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
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
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. */
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
;
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
56 final NodeFilter filter
;
57 final boolean entityReferenceExpansion
;
61 public DomNodeIterator(Node root
, int whatToShow
, NodeFilter filter
,
62 boolean entityReferenceExpansion
, boolean walk
)
66 throw new DOMException(DOMException
.NOT_SUPPORTED_ERR
, "null root");
69 this.whatToShow
= whatToShow
;
71 this.entityReferenceExpansion
= entityReferenceExpansion
;
81 public int getWhatToShow()
86 public NodeFilter
getFilter()
91 public boolean getExpandEntityReferences()
93 return entityReferenceExpansion
;
96 public Node
nextNode()
101 throw new DOMException(DOMException
.INVALID_STATE_ERR
, "null root");
106 if (current
.equals(root
))
108 ret
= root
.getFirstChild();
112 ret
= current
.getFirstChild();
115 ret
= current
.getNextSibling();
120 ret
= tmp
.getParentNode();
121 while (!ret
.equals(root
) && tmp
.equals(ret
.getLastChild()))
124 ret
= tmp
.getParentNode();
126 if (ret
.equals(root
))
132 ret
= ret
.getNextSibling();
138 ret
= current
.getNextSibling();
140 current
= (ret
== null) ? current
: ret
;
142 while (!accept(ret
));
147 public Node
previousNode()
152 throw new DOMException(DOMException
.INVALID_STATE_ERR
, "null root");
157 if (current
.equals(root
))
159 ret
= current
.getLastChild();
163 ret
= current
.getLastChild();
166 ret
= current
.getPreviousSibling();
171 ret
= tmp
.getParentNode();
172 while (!ret
.equals(root
) && tmp
.equals(ret
.getFirstChild()))
175 ret
= tmp
.getParentNode();
177 if (ret
.equals(root
))
183 ret
= ret
.getPreviousSibling();
189 ret
= current
.getPreviousSibling();
192 while (!accept(ret
));
193 current
= (ret
== null) ? current
: ret
;
197 public Node
getCurrentNode()
202 public void setCurrentNode(Node current
)
207 throw new DOMException(DOMException
.NOT_SUPPORTED_ERR
, "null root");
209 this.current
= current
;
212 public Node
parentNode()
214 Node ret
= current
.getParentNode();
219 current
= (ret
== null) ? current
: ret
;
223 public Node
firstChild ()
225 Node ret
= current
.getFirstChild();
228 ret
= ret
.getNextSibling();
230 current
= (ret
== null) ? current
: ret
;
234 public Node
lastChild()
236 Node ret
= current
.getLastChild();
239 ret
= ret
.getPreviousSibling();
241 current
= (ret
== null) ? current
: ret
;
245 public Node
previousSibling()
247 Node ret
= current
.getPreviousSibling();
250 ret
= ret
.getPreviousSibling();
252 current
= (ret
== null) ? current
: ret
;
256 public Node
nextSibling()
258 Node ret
= current
.getNextSibling();
261 ret
= ret
.getNextSibling();
263 current
= (ret
== null) ? current
: ret
;
272 boolean accept(Node node
)
279 switch (node
.getNodeType())
281 case Node
.ATTRIBUTE_NODE
:
282 ret
= (whatToShow
& NodeFilter
.SHOW_ATTRIBUTE
) != 0;
284 case Node
.CDATA_SECTION_NODE
:
285 ret
= (whatToShow
& NodeFilter
.SHOW_CDATA_SECTION
) != 0;
287 case Node
.COMMENT_NODE
:
288 ret
= (whatToShow
& NodeFilter
.SHOW_COMMENT
) != 0;
290 case Node
.DOCUMENT_NODE
:
291 ret
= (whatToShow
& NodeFilter
.SHOW_DOCUMENT
) != 0;
293 case Node
.DOCUMENT_FRAGMENT_NODE
:
294 ret
= (whatToShow
& NodeFilter
.SHOW_DOCUMENT_FRAGMENT
) != 0;
296 case Node
.DOCUMENT_TYPE_NODE
:
297 ret
= (whatToShow
& NodeFilter
.SHOW_DOCUMENT_TYPE
) != 0;
299 case Node
.ELEMENT_NODE
:
300 ret
= (whatToShow
& NodeFilter
.SHOW_ELEMENT
) != 0;
302 case Node
.ENTITY_NODE
:
303 ret
= (whatToShow
& NodeFilter
.SHOW_ENTITY
) != 0;
305 case Node
.ENTITY_REFERENCE_NODE
:
306 ret
= (whatToShow
& NodeFilter
.SHOW_ENTITY_REFERENCE
) != 0;
307 ret
= ret
&& entityReferenceExpansion
;
309 case Node
.NOTATION_NODE
:
310 ret
= (whatToShow
& NodeFilter
.SHOW_NOTATION
) != 0;
312 case Node
.PROCESSING_INSTRUCTION_NODE
:
313 ret
= (whatToShow
& NodeFilter
.SHOW_PROCESSING_INSTRUCTION
) != 0;
316 ret
= (whatToShow
& NodeFilter
.SHOW_TEXT
) != 0;
321 if (ret
&& filter
!= null)
323 ret
= (filter
.acceptNode(node
) == NodeFilter
.FILTER_ACCEPT
);