KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > traversal > DOMTreeWalker


1 /*
2
3    Copyright 2000-2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.dom.traversal;
19
20 import org.apache.batik.dom.AbstractNode;
21 import org.w3c.dom.DOMException JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.traversal.NodeFilter;
24 import org.w3c.dom.traversal.TreeWalker;
25
26 /**
27  * This class implements the {@link org.w3c.dom.traversal.NodeIterator}
28  * interface.
29  *
30  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
31  * @version $Id: DOMTreeWalker.java,v 1.6 2004/08/18 07:13:37 vhardy Exp $
32  */

33 public class DOMTreeWalker implements TreeWalker {
34
35     /**
36      * The root node.
37      */

38     protected Node root;
39
40     /**
41      * Which node types are presented via the iterator.
42      */

43     protected int whatToShow;
44
45     /**
46      * The NodeFilter used to screen nodes.
47      */

48     protected NodeFilter filter;
49
50     /**
51      * Whether the children of entity reference nodes are visible
52      * to the iterator.
53      */

54     protected boolean expandEntityReferences;
55
56     /**
57      * The current node.
58      */

59     protected Node currentNode;
60
61     /**
62      * Creates a new TreeWalker object.
63      * @param n The root node.
64      * @param what Which node types are presented via the iterator.
65      * @param nf The NodeFilter used to screen nodes.
66      * @param exp Whether the children of entity reference nodes are visible
67      * to the tree walker.
68      */

69     public DOMTreeWalker(Node n, int what, NodeFilter nf, boolean exp) {
70         root = n;
71         whatToShow = what;
72         filter = nf;
73         expandEntityReferences = exp;
74
75         currentNode = root;
76     }
77
78     /**
79      * <b>DOM</b>: Implements {@link TreeWalker#getRoot()}.
80      */

81     public Node getRoot() {
82         return root;
83     }
84
85     /**
86      * <b>DOM</b>: Implements {@link TreeWalker#getWhatToShow()}.
87      */

88     public int getWhatToShow() {
89         return whatToShow;
90     }
91
92     /**
93      * <b>DOM</b>: Implements {@link TreeWalker#getFilter()}.
94      */

95     public NodeFilter getFilter() {
96         return filter;
97     }
98
99     /**
100      * <b>DOM</b>: Implements {@link TreeWalker#getExpandEntityReferences()}.
101      */

102     public boolean getExpandEntityReferences() {
103         return expandEntityReferences;
104     }
105     
106     /**
107      * <b>DOM</b>: Implements {@link TreeWalker#getCurrentNode()}.
108      */

109     public Node getCurrentNode() {
110         return currentNode;
111     }
112
113     /**
114      * <b>DOM</b>: Implements {@link TreeWalker#setCurrentNode(Node)}.
115      */

116     public void setCurrentNode(Node n) {
117         if (n == null) {
118             throw ((AbstractNode)root).createDOMException
119                 (DOMException.NOT_SUPPORTED_ERR,
120                  "null.current.node", null);
121         }
122         currentNode = n;
123     }
124
125     /**
126      * <b>DOM</b>: Implements {@link TreeWalker#parentNode()}.
127      */

128     public Node parentNode() {
129         Node result = parentNode(currentNode);
130         if (result != null) {
131             currentNode = result;
132         }
133         return result;
134     }
135
136     /**
137      * <b>DOM</b>: Implements {@link TreeWalker#firstChild()}.
138      */

139     public Node firstChild() {
140         Node result = firstChild(currentNode);
141         if (result != null) {
142             currentNode = result;
143         }
144         return result;
145     }
146
147     /**
148      * <b>DOM</b>: Implements {@link TreeWalker#lastChild()}.
149      */

150     public Node lastChild() {
151         Node result = lastChild(currentNode);
152         if (result != null) {
153             currentNode = result;
154         }
155         return result;
156     }
157
158     /**
159      * <b>DOM</b>: Implements {@link TreeWalker#previousSibling()}.
160      */

161     public Node previousSibling() {
162         Node result = previousSibling(currentNode, root);
163         if (result != null) {
164             currentNode = result;
165         }
166         return result;
167     }
168
169     /**
170      * <b>DOM</b>: Implements {@link TreeWalker#nextSibling()}.
171      */

172     public Node nextSibling() {
173         Node result = nextSibling(currentNode, root);
174         if (result != null) {
175             currentNode = result;
176         }
177         return result;
178     }
179
180     /**
181      * <b>DOM</b>: Implements {@link TreeWalker#previousNode()}.
182      */

183     public Node previousNode() {
184         Node result = previousSibling(currentNode, root);
185         if (result == null) {
186             result = parentNode(currentNode);
187             if (result != null) {
188                 currentNode = result;
189             }
190             return result;
191         }
192         Node n = lastChild(result);
193         Node last = n;
194         while (n != null) {
195             last = n;
196             n = lastChild(last);
197         }
198         return currentNode = (last != null) ? last : result;
199     }
200
201     /**
202      * <b>DOM</b>: Implements {@link TreeWalker#nextNode()}.
203      */

204     public Node nextNode() {
205         Node result;
206         if ((result = firstChild(currentNode)) != null) {
207             return currentNode = result;
208         }
209         if ((result = nextSibling(currentNode, root)) != null) {
210             return currentNode = result;
211         }
212         Node parent = currentNode;
213         for (;;) {
214             parent = parentNode(parent);
215             if (parent == null) {
216                 return null;
217             }
218             if ((result = nextSibling(parent, root)) != null) {
219                 return currentNode = result;
220             }
221         }
222     }
223
224     /**
225      * Returns the parent node of the given node.
226      */

227     protected Node parentNode(Node n) {
228         if (n == root) {
229             return null;
230         }
231         Node result = n;
232         for (;;) {
233             result = result.getParentNode();
234             if (result == null) {
235                 return null;
236             }
237             if ((whatToShow & (1 << result.getNodeType() - 1)) != 0) {
238                 if (filter == null ||
239                     filter.acceptNode(result) == NodeFilter.FILTER_ACCEPT) {
240                     return result;
241                 }
242             }
243         }
244     }
245
246     /**
247      * Returns the first child of the given node.
248      */

249     protected Node firstChild(Node n) {
250         if (n.getNodeType() == Node.ENTITY_REFERENCE_NODE &&
251             !expandEntityReferences) {
252             return null;
253         }
254         Node result = n.getFirstChild();
255         if (result == null) {
256             return null;
257         }
258         switch (acceptNode(result)) {
259         case NodeFilter.FILTER_ACCEPT:
260             return result;
261         case NodeFilter.FILTER_SKIP:
262             Node t = firstChild(result);
263             if (t != null) {
264                 return t;
265             }
266             // Fall through
267
default: // NodeFilter.FILTER_REJECT
268
return nextSibling(result, n);
269         }
270     }
271
272     /**
273      * Returns the last child of the given node.
274      */

275     protected Node lastChild(Node n) {
276         if (n.getNodeType() == Node.ENTITY_REFERENCE_NODE &&
277             !expandEntityReferences) {
278             return null;
279         }
280         Node result = n.getLastChild();
281         if (result == null) {
282             return null;
283         }
284         switch (acceptNode(result)) {
285         case NodeFilter.FILTER_ACCEPT:
286             return result;
287         case NodeFilter.FILTER_SKIP:
288             Node t = lastChild(result);
289             if (t != null) {
290                 return t;
291             }
292             // Fall through
293
default: // NodeFilter.FILTER_REJECT
294
return previousSibling(result, n);
295         }
296     }
297
298     /**
299      * Returns the previous sibling of the given node.
300      */

301     protected Node previousSibling(Node n, Node root) {
302         while (true) {
303             if (n == root) {
304                 return null;
305             }
306             Node result = n.getPreviousSibling();
307             if (result == null) {
308                 result = n.getParentNode();
309                 if (result == null || result == root) {
310                     return null;
311                 }
312                 if (acceptNode(result) == NodeFilter.FILTER_SKIP) {
313                     n = result;
314                     continue;
315                 }
316                 return null;
317             }
318             switch (acceptNode(result)) {
319             case NodeFilter.FILTER_ACCEPT:
320                 return result;
321             case NodeFilter.FILTER_SKIP:
322                 Node t = lastChild(result);
323                 if (t != null) {
324                     return t;
325                 }
326                 // Fall through
327
default: // NodeFilter.FILTER_REJECT
328
n = result;
329                 continue;
330             }
331         }
332     }
333
334     /**
335      * Returns the next sibling of the given node.
336      */

337     protected Node nextSibling(Node n, Node root) {
338         while (true) {
339             if (n == root) {
340                 return null;
341             }
342             Node result = n.getNextSibling();
343             if (result == null) {
344                 result = n.getParentNode();
345                 if (result == null || result == root) {
346                     return null;
347                 }
348                 if (acceptNode(result) == NodeFilter.FILTER_SKIP) {
349                     n = result;
350                     continue;
351                 }
352                 return null;
353             }
354
355             switch (acceptNode(result)) {
356             case NodeFilter.FILTER_ACCEPT:
357                 return result;
358             case NodeFilter.FILTER_SKIP:
359                 Node t = firstChild(result);
360                 if (t != null) {
361                     return t;
362                 }
363                 // Fall through
364
default: // NodeFilter.FILTER_REJECT
365
n = result;
366                 continue;
367             }
368         }
369     }
370
371     /**
372      * Whether or not the given node is accepted by this tree walker.
373      */

374     protected short acceptNode(Node n) {
375         if ((whatToShow & (1 << n.getNodeType() - 1)) != 0) {
376             if (filter == null) {
377                 return NodeFilter.FILTER_ACCEPT;
378             } else {
379                 return filter.acceptNode(n);
380             }
381         } else {
382             return NodeFilter.FILTER_SKIP;
383         }
384     }
385 }
386
Popular Tags