1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package org.jaxen;
51
52 import java.util.Iterator;
53
54 import org.jaxen.pattern.Pattern;
55 import org.jaxen.util.AncestorAxisIterator;
56 import org.jaxen.util.AncestorOrSelfAxisIterator;
57 import org.jaxen.util.DescendantAxisIterator;
58 import org.jaxen.util.DescendantOrSelfAxisIterator;
59 import org.jaxen.util.FollowingAxisIterator;
60 import org.jaxen.util.FollowingSiblingAxisIterator;
61 import org.jaxen.util.PrecedingAxisIterator;
62 import org.jaxen.util.PrecedingSiblingAxisIterator;
63 import org.jaxen.util.SelfAxisIterator;
64
65 /*** Default implementation of {@link Navigator}.
66 *
67 * <p>
68 * This implementation is an abstract class, since
69 * some required operations cannot be implemented without
70 * additional knowledge of the object model.
71 * </p>
72 *
73 * <p>
74 * When possible, default method implementations build
75 * upon each other, to reduce the number of methods required
76 * to be implemented for each object model. All methods,
77 * of course, may be overridden, to provide more-efficient
78 * implementations.
79 * </p>
80 *
81 * @author bob mcwhirter (bob@werken.com)
82 * @author Erwin Bolwidt (ejb@klomp.org)
83 */
84 public abstract class DefaultNavigator implements Navigator
85 {
86
87 /*** Throws <code>UnsupportedAxisException</code>
88 *
89 * @param contextNode
90 * @return never returns
91 * @throws UnsupportedAxisException always
92 */
93 public Iterator getChildAxisIterator(Object contextNode) throws UnsupportedAxisException
94 {
95 throw new UnsupportedAxisException("child");
96 }
97
98
99
100
101 public Iterator getDescendantAxisIterator(Object contextNode) throws UnsupportedAxisException
102 {
103 return new DescendantAxisIterator( contextNode,
104 this );
105 }
106
107 /*** Throws <code>UnsupportedAxisException</code>
108 *
109 * @param contextNode
110 * @return never returns
111 * @throws UnsupportedAxisException
112 */
113 public Iterator getParentAxisIterator(Object contextNode) throws UnsupportedAxisException
114 {
115 throw new UnsupportedAxisException("parent");
116 }
117
118 public Iterator getAncestorAxisIterator(Object contextNode) throws UnsupportedAxisException
119 {
120 return new AncestorAxisIterator( contextNode,
121 this );
122 }
123
124
125 public Iterator getFollowingSiblingAxisIterator(Object contextNode) throws UnsupportedAxisException
126 {
127 return new FollowingSiblingAxisIterator( contextNode,
128 this );
129 }
130
131
132 public Iterator getPrecedingSiblingAxisIterator(Object contextNode) throws UnsupportedAxisException
133 {
134 return new PrecedingSiblingAxisIterator( contextNode,
135 this );
136 }
137
138 public Iterator getFollowingAxisIterator(Object contextNode) throws UnsupportedAxisException
139 {
140 return new FollowingAxisIterator( contextNode,
141 this );
142
143
144 }
145
146
147 public Iterator getPrecedingAxisIterator(Object contextNode) throws UnsupportedAxisException
148 {
149 return new PrecedingAxisIterator( contextNode,
150 this );
151
152
153 }
154
155 /*** Throws <code>UnsupportedAxisException</code>. Subclasses that
156 * support the attribute axis must override this method.
157 *
158 * @param contextNode
159 * @return never returns
160 * @throws UnsupportedAxisException
161 */
162 public Iterator getAttributeAxisIterator(Object contextNode) throws UnsupportedAxisException
163 {
164 throw new UnsupportedAxisException("attribute");
165 }
166
167 /*** Throws <code>UnsupportedAxisException</code>. Subclasses that
168 * support the namespace axis must override this method.
169 *
170 * @param contextNode
171 * @return never returns
172 * @throws UnsupportedAxisException
173 */
174 public Iterator getNamespaceAxisIterator(Object contextNode) throws UnsupportedAxisException
175 {
176 throw new UnsupportedAxisException("namespace");
177 }
178
179 public Iterator getSelfAxisIterator(Object contextNode) throws UnsupportedAxisException
180 {
181 return new SelfAxisIterator( contextNode );
182 }
183
184 public Iterator getDescendantOrSelfAxisIterator(Object contextNode) throws UnsupportedAxisException
185 {
186 return new DescendantOrSelfAxisIterator( contextNode,
187 this );
188 }
189
190 public Iterator getAncestorOrSelfAxisIterator(Object contextNode) throws UnsupportedAxisException
191 {
192 return new AncestorOrSelfAxisIterator( contextNode,
193 this );
194 }
195
196 public Object getDocumentNode(Object contextNode)
197 {
198 return null;
199 }
200
201 public String translateNamespacePrefixToUri(String prefix, Object element)
202 {
203 return null;
204 }
205
206 public String getProcessingInstructionTarget(Object obj)
207 {
208 return null;
209 }
210
211 public String getProcessingInstructionData(Object obj)
212 {
213 return null;
214 }
215
216 public short getNodeType(Object node)
217 {
218 if ( isElement(node) )
219 {
220 return Pattern.ELEMENT_NODE;
221 }
222 else if ( isAttribute(node) )
223 {
224 return Pattern.ATTRIBUTE_NODE;
225 }
226 else if ( isText(node) )
227 {
228 return Pattern.TEXT_NODE;
229 }
230 else if ( isComment(node) )
231 {
232 return Pattern.COMMENT_NODE;
233 }
234 else if ( isDocument(node) )
235 {
236 return Pattern.DOCUMENT_NODE;
237 }
238 else if ( isProcessingInstruction(node) )
239 {
240 return Pattern.PROCESSING_INSTRUCTION_NODE;
241 }
242 else if ( isNamespace(node) )
243 {
244 return Pattern.NAMESPACE_NODE;
245 }
246 else {
247 return Pattern.UNKNOWN_NODE;
248 }
249 }
250
251 /***
252 * Default inefficient implementation. Subclasses
253 * should override this method.
254 *
255 * @param contextNode the node whose parent to return
256 * @return the parent node
257 * @throws UnsupportedAxisException if the parent axis is not supported
258 */
259 public Object getParentNode(Object contextNode) throws UnsupportedAxisException
260 {
261 Iterator iter = getParentAxisIterator( contextNode );
262 if ( iter != null && iter.hasNext() )
263 {
264 return iter.next();
265 }
266 return null;
267 }
268
269 /***
270 * Default implementation that always returns null. Override in subclass
271 * if the subclass can load documents.
272 *
273 * @param url the URL of the document to load
274 *
275 * @return null
276 * @throws FunctionCallException if an error occurs while loading the
277 * URL; e.g. an I/O error or the document is malformed
278 */
279 public Object getDocument(String url) throws FunctionCallException
280 {
281 return null;
282 }
283
284 /***
285 * Default implementation that cannot find elements. Override in subclass
286 * if subclass does know about attribute types.
287 *
288 * @param contextNode a node from the document in which to look for the
289 * id
290 * @param elementId id to look for
291 *
292 * @return null
293 */
294 public Object getElementById(Object contextNode, String elementId)
295 {
296 return null;
297 }
298
299 }