KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > dom > TreeWalkerImpl


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

16
17 package org.apache.xerces.dom;
18
19 import org.w3c.dom.DOMException JavaDoc;
20 import org.w3c.dom.Node JavaDoc;
21 import org.w3c.dom.traversal.NodeFilter;
22 import org.w3c.dom.traversal.TreeWalker;
23
24 /** This class implements the TreeWalker interface.
25  *
26  * @xerces.internal
27  *
28  * @version $Id: TreeWalkerImpl.java,v 1.10 2004/10/05 17:12:51 mrglavas Exp $
29  */

30
31 public class TreeWalkerImpl implements TreeWalker {
32     
33     //
34
// Data
35
//
36

37     /** When TRUE, the children of entites references are returned in the iterator. */
38     private boolean fEntityReferenceExpansion = false;
39     /** The whatToShow mask. */
40     int fWhatToShow = NodeFilter.SHOW_ALL;
41     /** The NodeFilter reference. */
42     NodeFilter fNodeFilter;
43     /** The current Node. */
44     Node JavaDoc fCurrentNode;
45     /** The root Node. */
46     Node JavaDoc fRoot;
47     
48     //
49
// Implementation Note: No state is kept except the data above
50
// (fWhatToShow, fNodeFilter, fCurrentNode, fRoot) such that
51
// setters could be created for these data values and the
52
// implementation will still work.
53

54     
55     //
56
// Constructor
57
//
58

59     /** Public constructor */
60     public TreeWalkerImpl(Node JavaDoc root,
61                           int whatToShow,
62                           NodeFilter nodeFilter,
63                           boolean entityReferenceExpansion) {
64         fCurrentNode = root;
65         fRoot = root;
66         fWhatToShow = whatToShow;
67         fNodeFilter = nodeFilter;
68         fEntityReferenceExpansion = entityReferenceExpansion;
69     }
70     
71     public Node JavaDoc getRoot() {
72     return fRoot;
73     }
74
75     /** Return the whatToShow value */
76     public int getWhatToShow() {
77         return fWhatToShow;
78     }
79
80     public void setWhatShow(int whatToShow){
81         fWhatToShow = whatToShow;
82     }
83     /** Return the NodeFilter */
84     public NodeFilter getFilter() {
85         return fNodeFilter;
86     }
87     
88     /** Return whether children entity references are included in the iterator. */
89     public boolean getExpandEntityReferences() {
90         return fEntityReferenceExpansion;
91     }
92             
93     /** Return the current Node. */
94     public Node JavaDoc getCurrentNode() {
95         return fCurrentNode;
96     }
97     /** Return the current Node. */
98     public void setCurrentNode(Node JavaDoc node) {
99         if (node == null) {
100             String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
101               throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, msg);
102         }
103
104         fCurrentNode = node;
105     }
106     
107     /** Return the parent Node from the current node,
108      * after applying filter, whatToshow.
109      * If result is not null, set the current Node.
110      */

111     public Node JavaDoc parentNode() {
112
113         if (fCurrentNode == null) return null;
114                 
115         Node JavaDoc node = getParentNode(fCurrentNode);
116         if (node !=null) {
117             fCurrentNode = node;
118         }
119         return node;
120         
121     }
122
123     /** Return the first child Node from the current node,
124      * after applying filter, whatToshow.
125      * If result is not null, set the current Node.
126      */

127     public Node JavaDoc firstChild() {
128         
129         if (fCurrentNode == null) return null;
130                 
131         Node JavaDoc node = getFirstChild(fCurrentNode);
132         if (node !=null) {
133             fCurrentNode = node;
134         }
135         return node;
136     }
137     /** Return the last child Node from the current node,
138      * after applying filter, whatToshow.
139      * If result is not null, set the current Node.
140      */

141     public Node JavaDoc lastChild() {
142
143         if (fCurrentNode == null) return null;
144                 
145         Node JavaDoc node = getLastChild(fCurrentNode);
146         if (node !=null) {
147             fCurrentNode = node;
148         }
149         return node;
150     }
151     
152     /** Return the previous sibling Node from the current node,
153      * after applying filter, whatToshow.
154      * If result is not null, set the current Node.
155      */

156     public Node JavaDoc previousSibling() {
157
158         if (fCurrentNode == null) return null;
159                 
160         Node JavaDoc node = getPreviousSibling(fCurrentNode);
161         if (node !=null) {
162             fCurrentNode = node;
163         }
164         return node;
165     }
166     
167     /** Return the next sibling Node from the current node,
168      * after applying filter, whatToshow.
169      * If result is not null, set the current Node.
170      */

171     public Node JavaDoc nextSibling(){
172         if (fCurrentNode == null) return null;
173                 
174         Node JavaDoc node = getNextSibling(fCurrentNode);
175         if (node !=null) {
176             fCurrentNode = node;
177         }
178         return node;
179     }
180     
181     /** Return the previous Node from the current node,
182      * after applying filter, whatToshow.
183      * If result is not null, set the current Node.
184      */

185     public Node JavaDoc previousNode() {
186         Node JavaDoc result;
187         
188         if (fCurrentNode == null) return null;
189         
190         // get sibling
191
result = getPreviousSibling(fCurrentNode);
192         if (result == null) {
193             result = getParentNode(fCurrentNode);
194             if (result != null) {
195                 fCurrentNode = result;
196                 return fCurrentNode;
197             }
198             return null;
199         }
200         
201         // get the lastChild of result.
202
Node JavaDoc lastChild = getLastChild(result);
203         
204         Node JavaDoc prev = lastChild ;
205         while (lastChild != null) {
206           prev = lastChild ;
207           lastChild = getLastChild(prev) ;
208         }
209
210         lastChild = prev ;
211         
212         // if there is a lastChild which passes filters return it.
213
if (lastChild != null) {
214             fCurrentNode = lastChild;
215             return fCurrentNode;
216         }
217         
218         // otherwise return the previous sibling.
219
if (result != null) {
220             fCurrentNode = result;
221             return fCurrentNode;
222         }
223         
224         // otherwise return null.
225
return null;
226     }
227     
228     /** Return the next Node from the current node,
229      * after applying filter, whatToshow.
230      * If result is not null, set the current Node.
231      */

232     public Node JavaDoc nextNode() {
233         
234         if (fCurrentNode == null) return null;
235         
236         Node JavaDoc result = getFirstChild(fCurrentNode);
237         
238         if (result != null) {
239             fCurrentNode = result;
240             return result;
241         }
242         
243         result = getNextSibling(fCurrentNode);
244         
245         if (result != null) {
246             fCurrentNode = result;
247             return result;
248         }
249                 
250         // return parent's 1st sibling.
251
Node JavaDoc parent = getParentNode(fCurrentNode);
252         while (parent != null) {
253             result = getNextSibling(parent);
254             if (result != null) {
255                 fCurrentNode = result;
256                 return result;
257             } else {
258                 parent = getParentNode(parent);
259             }
260         }
261         
262         // end , return null
263
return null;
264     }
265     
266     /** Internal function.
267      * Return the parent Node, from the input node
268      * after applying filter, whatToshow.
269      * The current node is not consulted or set.
270      */

271     Node JavaDoc getParentNode(Node JavaDoc node) {
272         
273         if (node == null || node == fRoot) return null;
274         
275         Node JavaDoc newNode = node.getParentNode();
276         if (newNode == null) return null;
277                         
278         int accept = acceptNode(newNode);
279         
280         if (accept == NodeFilter.FILTER_ACCEPT)
281             return newNode;
282         else
283         //if (accept == NodeFilter.SKIP_NODE) // and REJECT too.
284
{
285             return getParentNode(newNode);
286         }
287         
288         
289     }
290     
291     /** Internal function.
292      * Return the nextSibling Node, from the input node
293      * after applying filter, whatToshow.
294      * The current node is not consulted or set.
295      */

296     Node JavaDoc getNextSibling(Node JavaDoc node) {
297         return getNextSibling(node, fRoot);
298     }
299
300     /** Internal function.
301      * Return the nextSibling Node, from the input node
302      * after applying filter, whatToshow.
303      * NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
304      * The current node is not consulted or set.
305      */

306     Node JavaDoc getNextSibling(Node JavaDoc node, Node JavaDoc root) {
307         
308         if (node == null || node == root) return null;
309         
310         Node JavaDoc newNode = node.getNextSibling();
311         if (newNode == null) {
312                 
313             newNode = node.getParentNode();
314                 
315             if (newNode == null || newNode == root) return null;
316                 
317             int parentAccept = acceptNode(newNode);
318                 
319             if (parentAccept==NodeFilter.FILTER_SKIP) {
320                 return getNextSibling(newNode, root);
321             }
322                 
323             return null;
324         }
325         
326         int accept = acceptNode(newNode);
327         
328         if (accept == NodeFilter.FILTER_ACCEPT)
329             return newNode;
330         else
331         if (accept == NodeFilter.FILTER_SKIP) {
332             Node JavaDoc fChild = getFirstChild(newNode);
333             if (fChild == null) {
334                 return getNextSibling(newNode, root);
335             }
336             return fChild;
337         }
338         else
339         //if (accept == NodeFilter.REJECT_NODE)
340
{
341             return getNextSibling(newNode, root);
342         }
343         
344     } // getNextSibling(Node node) {
345

346     /** Internal function.
347      * Return the previous sibling Node, from the input node
348      * after applying filter, whatToshow.
349      * The current node is not consulted or set.
350      */

351     Node JavaDoc getPreviousSibling(Node JavaDoc node) {
352         return getPreviousSibling(node, fRoot);
353     }
354
355     /** Internal function.
356      * Return the previousSibling Node, from the input node
357      * after applying filter, whatToshow.
358      * NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
359      * The current node is not consulted or set.
360      */

361     Node JavaDoc getPreviousSibling(Node JavaDoc node, Node JavaDoc root) {
362         
363         if (node == null || node == root) return null;
364         
365         Node JavaDoc newNode = node.getPreviousSibling();
366         if (newNode == null) {
367                 
368             newNode = node.getParentNode();
369             if (newNode == null || newNode == root) return null;
370                 
371             int parentAccept = acceptNode(newNode);
372                 
373             if (parentAccept==NodeFilter.FILTER_SKIP) {
374                 return getPreviousSibling(newNode, root);
375             }
376             
377             return null;
378         }
379         
380         int accept = acceptNode(newNode);
381         
382         if (accept == NodeFilter.FILTER_ACCEPT)
383             return newNode;
384         else
385         if (accept == NodeFilter.FILTER_SKIP) {
386             Node JavaDoc fChild = getLastChild(newNode);
387             if (fChild == null) {
388                 return getPreviousSibling(newNode, root);
389             }
390             return fChild;
391         }
392         else
393         //if (accept == NodeFilter.REJECT_NODE)
394
{
395             return getPreviousSibling(newNode, root);
396         }
397         
398     } // getPreviousSibling(Node node) {
399

400     /** Internal function.
401      * Return the first child Node, from the input node
402      * after applying filter, whatToshow.
403      * The current node is not consulted or set.
404      */

405     Node JavaDoc getFirstChild(Node JavaDoc node) {
406         if (node == null) return null;
407         
408         if ( !fEntityReferenceExpansion
409              && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
410             return null;
411         Node JavaDoc newNode = node.getFirstChild();
412         if (newNode == null) return null;
413         int accept = acceptNode(newNode);
414         
415         if (accept == NodeFilter.FILTER_ACCEPT)
416             return newNode;
417         else
418         if (accept == NodeFilter.FILTER_SKIP
419             && newNode.hasChildNodes())
420         {
421             Node JavaDoc fChild = getFirstChild(newNode);
422             
423             if (fChild == null) {
424                 return getNextSibling(newNode, node);
425             }
426             return fChild;
427         }
428         else
429         //if (accept == NodeFilter.REJECT_NODE)
430
{
431             return getNextSibling(newNode, node);
432         }
433         
434         
435     }
436    
437     /** Internal function.
438      * Return the last child Node, from the input node
439      * after applying filter, whatToshow.
440      * The current node is not consulted or set.
441      */

442     Node JavaDoc getLastChild(Node JavaDoc node) {
443         
444         if (node == null) return null;
445         
446         if ( !fEntityReferenceExpansion
447              && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
448             return null;
449             
450         Node JavaDoc newNode = node.getLastChild();
451         if (newNode == null) return null;
452         
453         int accept = acceptNode(newNode);
454         
455         if (accept == NodeFilter.FILTER_ACCEPT)
456             return newNode;
457         else
458         if (accept == NodeFilter.FILTER_SKIP
459             && newNode.hasChildNodes())
460         {
461             Node JavaDoc lChild = getLastChild(newNode);
462             if (lChild == null) {
463                 return getPreviousSibling(newNode, node);
464             }
465             return lChild;
466         }
467         else
468         //if (accept == NodeFilter.REJECT_NODE)
469
{
470             return getPreviousSibling(newNode, node);
471         }
472         
473         
474     }
475     
476     /** Internal function.
477      * The node whatToShow and the filter are combined into one result. */

478     short acceptNode(Node JavaDoc node) {
479         /***
480          7.1.2.4. Filters and whatToShow flags
481
482          Iterator and TreeWalker apply whatToShow flags before applying Filters. If a node is rejected by the
483          active whatToShow flags, a Filter will not be called to evaluate that node. When a node is rejected by
484          the active whatToShow flags, children of that node will still be considered, and Filters may be called to
485          evaluate them.
486          ***/

487                 
488         if (fNodeFilter == null) {
489             if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) {
490                 return NodeFilter.FILTER_ACCEPT;
491             } else {
492                 return NodeFilter.FILTER_SKIP;
493             }
494         } else {
495             if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) {
496                 return fNodeFilter.acceptNode(node);
497             } else {
498                 // What to show has failed. See above excerpt from spec.
499
// Equivalent to FILTER_SKIP.
500
return NodeFilter.FILTER_SKIP;
501             }
502         }
503     }
504 }
505
Popular Tags