KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 1999-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
19 package org.apache.batik.dom.traversal;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.batik.dom.AbstractDocument;
26 import org.w3c.dom.DOMException JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.traversal.NodeFilter;
29 import org.w3c.dom.traversal.NodeIterator;
30 import org.w3c.dom.traversal.TreeWalker;
31
32 /**
33  * This class provides support for traversal.
34  *
35  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
36  * @version $Id: TraversalSupport.java,v 1.4 2004/10/30 18:38:04 deweese Exp $
37  */

38 public class TraversalSupport {
39     
40     /**
41      * The iterators list.
42      */

43     protected List JavaDoc iterators;
44
45     /**
46      * Creates a new TraversalSupport.
47      */

48     public TraversalSupport() {
49     }
50
51     /**
52      * Creates a new tree walker.
53      */

54     public static TreeWalker createTreeWalker(AbstractDocument doc,
55                                               Node JavaDoc root,
56                                               int whatToShow,
57                                               NodeFilter filter,
58                                               boolean entityReferenceExpansion) {
59         if (root == null) {
60             throw doc.createDOMException
61                 (DOMException.NOT_SUPPORTED_ERR, "null.root", null);
62         }
63         return new DOMTreeWalker(root, whatToShow, filter,
64                                  entityReferenceExpansion);
65     }
66
67     /**
68      * Creates a new node iterator.
69      */

70     public NodeIterator createNodeIterator(AbstractDocument doc,
71                                            Node JavaDoc root,
72                                            int whatToShow,
73                                            NodeFilter filter,
74                                            boolean entityReferenceExpansion)
75         throws DOMException JavaDoc {
76         if (root == null) {
77             throw doc.createDOMException
78                 (DOMException.NOT_SUPPORTED_ERR, "null.root", null);
79         }
80         NodeIterator result = new DOMNodeIterator(doc, root, whatToShow,
81                                                   filter,
82                                                   entityReferenceExpansion);
83         if (iterators == null) {
84             iterators = new LinkedList JavaDoc();
85         }
86         iterators.add(result);
87
88         return result;
89     }
90
91     /**
92      * Called by the DOM when a node will be removed from the current document.
93      */

94     public void nodeToBeRemoved(Node JavaDoc removedNode) {
95         if (iterators != null) {
96             Iterator JavaDoc it = iterators.iterator();
97             while (it.hasNext()) {
98                 ((DOMNodeIterator)it.next()).nodeToBeRemoved(removedNode);
99             }
100         }
101     }
102
103     /**
104      * Detaches the given node iterator.
105      */

106     public void detachNodeIterator(NodeIterator it) {
107         iterators.remove(it);
108     }
109 }
110
Popular Tags