1 | |
package org.jaxen.dom4j; |
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 | |
|
51 | |
import java.util.ArrayList; |
52 | |
import java.util.HashSet; |
53 | |
import java.util.Iterator; |
54 | |
import java.util.List; |
55 | |
|
56 | |
import org.dom4j.Attribute; |
57 | |
import org.dom4j.Branch; |
58 | |
import org.dom4j.CDATA; |
59 | |
import org.dom4j.Comment; |
60 | |
import org.dom4j.Document; |
61 | |
import org.dom4j.DocumentException; |
62 | |
import org.dom4j.Element; |
63 | |
import org.dom4j.Namespace; |
64 | |
import org.dom4j.Node; |
65 | |
import org.dom4j.ProcessingInstruction; |
66 | |
import org.dom4j.QName; |
67 | |
import org.dom4j.Text; |
68 | |
import org.dom4j.io.SAXReader; |
69 | |
import org.jaxen.DefaultNavigator; |
70 | |
import org.jaxen.FunctionCallException; |
71 | |
import org.jaxen.NamedAccessNavigator; |
72 | |
import org.jaxen.Navigator; |
73 | |
import org.jaxen.XPath; |
74 | |
import org.jaxen.JaxenConstants; |
75 | |
import org.jaxen.saxpath.SAXPathException; |
76 | |
import org.jaxen.util.SingleObjectIterator; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 588 | public class DocumentNavigator extends DefaultNavigator implements NamedAccessNavigator |
92 | |
{ |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private static final long serialVersionUID = 5582300797286535936L; |
98 | |
private transient SAXReader reader; |
99 | |
|
100 | |
|
101 | |
|
102 | 588 | private static class Singleton |
103 | |
{ |
104 | |
|
105 | |
|
106 | 4 | private static DocumentNavigator instance = new DocumentNavigator(); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
public static Navigator getInstance() |
112 | |
{ |
113 | 32 | return Singleton.instance; |
114 | |
} |
115 | |
|
116 | |
public boolean isElement(Object obj) |
117 | |
{ |
118 | 223808 | return obj instanceof Element; |
119 | |
} |
120 | |
|
121 | |
public boolean isComment(Object obj) |
122 | |
{ |
123 | 2222 | return obj instanceof Comment; |
124 | |
} |
125 | |
|
126 | |
public boolean isText(Object obj) |
127 | |
{ |
128 | 151202 | return ( obj instanceof Text |
129 | |
|| |
130 | |
obj instanceof CDATA ); |
131 | |
} |
132 | |
|
133 | |
public boolean isAttribute(Object obj) |
134 | |
{ |
135 | 8836 | return obj instanceof Attribute; |
136 | |
} |
137 | |
|
138 | |
public boolean isProcessingInstruction(Object obj) |
139 | |
{ |
140 | 2142 | return obj instanceof ProcessingInstruction; |
141 | |
} |
142 | |
|
143 | |
public boolean isDocument(Object obj) |
144 | |
{ |
145 | 2344 | return obj instanceof Document; |
146 | |
} |
147 | |
|
148 | |
public boolean isNamespace(Object obj) |
149 | |
{ |
150 | 8060 | return obj instanceof Namespace; |
151 | |
} |
152 | |
|
153 | |
public String getElementName(Object obj) |
154 | |
{ |
155 | 74140 | Element elem = (Element) obj; |
156 | |
|
157 | 74140 | return elem.getName(); |
158 | |
} |
159 | |
|
160 | |
public String getElementNamespaceUri(Object obj) |
161 | |
{ |
162 | 73678 | Element elem = (Element) obj; |
163 | |
|
164 | 73678 | String uri = elem.getNamespaceURI(); |
165 | 73678 | if ( uri == null) |
166 | 0 | return ""; |
167 | |
else |
168 | 73678 | return uri; |
169 | |
} |
170 | |
|
171 | |
public String getElementQName(Object obj) |
172 | |
{ |
173 | 52 | Element elem = (Element) obj; |
174 | |
|
175 | 52 | return elem.getQualifiedName(); |
176 | |
} |
177 | |
|
178 | |
public String getAttributeName(Object obj) |
179 | |
{ |
180 | 60 | Attribute attr = (Attribute) obj; |
181 | |
|
182 | 60 | return attr.getName(); |
183 | |
} |
184 | |
|
185 | |
public String getAttributeNamespaceUri(Object obj) |
186 | |
{ |
187 | 60 | Attribute attr = (Attribute) obj; |
188 | |
|
189 | 60 | String uri = attr.getNamespaceURI(); |
190 | 60 | if ( uri == null) |
191 | 0 | return ""; |
192 | |
else |
193 | 60 | return uri; |
194 | |
} |
195 | |
|
196 | |
public String getAttributeQName(Object obj) |
197 | |
{ |
198 | 0 | Attribute attr = (Attribute) obj; |
199 | |
|
200 | 0 | return attr.getQualifiedName(); |
201 | |
} |
202 | |
|
203 | |
public Iterator getChildAxisIterator(Object contextNode) |
204 | |
{ |
205 | 233324 | Iterator result = null; |
206 | 233324 | if ( contextNode instanceof Branch ) |
207 | |
{ |
208 | 81376 | Branch node = (Branch) contextNode; |
209 | 81376 | result = node.nodeIterator(); |
210 | |
} |
211 | 233324 | if (result != null) { |
212 | 81376 | return result; |
213 | |
} |
214 | 151948 | return JaxenConstants.EMPTY_ITERATOR; |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public Iterator getChildAxisIterator( |
229 | |
Object contextNode, String localName, String namespacePrefix, String namespaceURI) { |
230 | |
|
231 | 4724 | if ( contextNode instanceof Element ) { |
232 | 1868 | Element node = (Element) contextNode; |
233 | 1868 | return node.elementIterator(QName.get(localName, namespacePrefix, namespaceURI)); |
234 | |
} |
235 | 2856 | if ( contextNode instanceof Document ) { |
236 | 236 | Document node = (Document) contextNode; |
237 | 236 | Element el = node.getRootElement(); |
238 | 236 | if (el == null || el.getName().equals(localName) == false) { |
239 | 32 | return JaxenConstants.EMPTY_ITERATOR; |
240 | |
} |
241 | 204 | if (namespaceURI != null) { |
242 | 20 | if (namespaceURI.equals(el.getNamespaceURI()) == false) { |
243 | 2 | return JaxenConstants.EMPTY_ITERATOR; |
244 | |
} |
245 | |
} |
246 | 202 | return new SingleObjectIterator(el); |
247 | |
} |
248 | |
|
249 | 2620 | return JaxenConstants.EMPTY_ITERATOR; |
250 | |
} |
251 | |
|
252 | |
public Iterator getParentAxisIterator(Object contextNode) |
253 | |
{ |
254 | 28 | if ( contextNode instanceof Document ) |
255 | |
{ |
256 | 2 | return JaxenConstants.EMPTY_ITERATOR; |
257 | |
} |
258 | |
|
259 | 26 | Node node = (Node) contextNode; |
260 | |
|
261 | 26 | Object parent = node.getParent(); |
262 | |
|
263 | 26 | if ( parent == null ) |
264 | |
{ |
265 | 10 | parent = node.getDocument(); |
266 | |
} |
267 | |
|
268 | 26 | return new SingleObjectIterator( parent ); |
269 | |
} |
270 | |
|
271 | |
public Iterator getAttributeAxisIterator(Object contextNode) |
272 | |
{ |
273 | 102 | if ( ! ( contextNode instanceof Element ) ) |
274 | |
{ |
275 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
276 | |
} |
277 | |
|
278 | 102 | Element elem = (Element) contextNode; |
279 | |
|
280 | 102 | return elem.attributeIterator(); |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public Iterator getAttributeAxisIterator( |
294 | |
Object contextNode, String localName, String namespacePrefix, String namespaceURI) { |
295 | |
|
296 | 1188 | if ( contextNode instanceof Element ) { |
297 | 1096 | Element node = (Element) contextNode; |
298 | 1096 | Attribute attr = node.attribute(QName.get(localName, namespacePrefix, namespaceURI)); |
299 | 1096 | if (attr == null) { |
300 | 462 | return JaxenConstants.EMPTY_ITERATOR; |
301 | |
} |
302 | 634 | return new SingleObjectIterator(attr); |
303 | |
} |
304 | 92 | return JaxenConstants.EMPTY_ITERATOR; |
305 | |
} |
306 | |
|
307 | |
public Iterator getNamespaceAxisIterator(Object contextNode) |
308 | |
{ |
309 | 334 | if ( ! ( contextNode instanceof Element ) ) |
310 | |
{ |
311 | 208 | return JaxenConstants.EMPTY_ITERATOR; |
312 | |
} |
313 | |
|
314 | 126 | Element element = (Element) contextNode; |
315 | 126 | List nsList = new ArrayList(); |
316 | 126 | HashSet prefixes = new HashSet(); |
317 | 456 | for ( Element context = element; context != null; context = context.getParent() ) { |
318 | 330 | List declaredNS = new ArrayList(context.declaredNamespaces()); |
319 | 330 | declaredNS.add(context.getNamespace()); |
320 | |
|
321 | 330 | for ( Iterator iter = context.attributes().iterator(); iter.hasNext(); ) |
322 | |
{ |
323 | 164 | Attribute attr = (Attribute) iter.next(); |
324 | 164 | declaredNS.add(attr.getNamespace()); |
325 | 164 | } |
326 | |
|
327 | 330 | for ( Iterator iter = declaredNS.iterator(); iter.hasNext(); ) |
328 | |
{ |
329 | 706 | Namespace namespace = (Namespace) iter.next(); |
330 | 706 | if (namespace != Namespace.NO_NAMESPACE) |
331 | |
{ |
332 | 294 | String prefix = namespace.getPrefix(); |
333 | 294 | if ( ! prefixes.contains( prefix ) ) { |
334 | 222 | prefixes.add( prefix ); |
335 | 222 | nsList.add( namespace.asXPathResult( element ) ); |
336 | |
} |
337 | |
} |
338 | 706 | } |
339 | |
} |
340 | 126 | nsList.add( Namespace.XML_NAMESPACE.asXPathResult( element ) ); |
341 | 126 | return nsList.iterator(); |
342 | |
} |
343 | |
|
344 | |
public Object getDocumentNode(Object contextNode) |
345 | |
{ |
346 | 534 | if ( contextNode instanceof Document ) |
347 | |
{ |
348 | 398 | return contextNode; |
349 | |
} |
350 | 136 | else if ( contextNode instanceof Node ) |
351 | |
{ |
352 | 136 | Node node = (Node) contextNode; |
353 | 136 | return node.getDocument(); |
354 | |
} |
355 | 0 | return null; |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
public XPath parseXPath (String xpath) throws SAXPathException |
362 | |
{ |
363 | 14 | return new Dom4jXPath(xpath); |
364 | |
} |
365 | |
|
366 | |
public Object getParentNode(Object contextNode) |
367 | |
{ |
368 | 50314 | if ( contextNode instanceof Node ) |
369 | |
{ |
370 | 50314 | Node node = (Node) contextNode; |
371 | 50314 | Object answer = node.getParent(); |
372 | 50314 | if ( answer == null ) |
373 | |
{ |
374 | 19680 | answer = node.getDocument(); |
375 | 19680 | if (answer == contextNode) { |
376 | 9804 | return null; |
377 | |
} |
378 | |
} |
379 | 40510 | return answer; |
380 | |
} |
381 | 0 | return null; |
382 | |
} |
383 | |
|
384 | |
public String getTextStringValue(Object obj) |
385 | |
{ |
386 | 28 | return getNodeStringValue( (Node) obj ); |
387 | |
} |
388 | |
|
389 | |
public String getElementStringValue(Object obj) |
390 | |
{ |
391 | 260 | return getNodeStringValue( (Node) obj ); |
392 | |
} |
393 | |
|
394 | |
public String getAttributeStringValue(Object obj) |
395 | |
{ |
396 | 574 | return getNodeStringValue( (Node) obj ); |
397 | |
} |
398 | |
|
399 | |
private String getNodeStringValue(Node node) |
400 | |
{ |
401 | 862 | return node.getStringValue(); |
402 | |
} |
403 | |
|
404 | |
public String getNamespaceStringValue(Object obj) |
405 | |
{ |
406 | 0 | Namespace ns = (Namespace) obj; |
407 | |
|
408 | 0 | return ns.getURI(); |
409 | |
} |
410 | |
|
411 | |
public String getNamespacePrefix(Object obj) |
412 | |
{ |
413 | 234 | Namespace ns = (Namespace) obj; |
414 | |
|
415 | 234 | return ns.getPrefix(); |
416 | |
} |
417 | |
|
418 | |
public String getCommentStringValue(Object obj) |
419 | |
{ |
420 | 0 | Comment cmt = (Comment) obj; |
421 | |
|
422 | 0 | return cmt.getText(); |
423 | |
} |
424 | |
|
425 | |
public String translateNamespacePrefixToUri(String prefix, Object context) |
426 | |
{ |
427 | 0 | Element element = null; |
428 | 0 | if ( context instanceof Element ) |
429 | |
{ |
430 | 0 | element = (Element) context; |
431 | 0 | } |
432 | 0 | else if ( context instanceof Node ) |
433 | |
{ |
434 | 0 | Node node = (Node) context; |
435 | 0 | element = node.getParent(); |
436 | |
} |
437 | 0 | if ( element != null ) |
438 | |
{ |
439 | 0 | Namespace namespace = element.getNamespaceForPrefix( prefix ); |
440 | |
|
441 | 0 | if ( namespace != null ) |
442 | |
{ |
443 | 0 | return namespace.getURI(); |
444 | |
} |
445 | |
} |
446 | 0 | return null; |
447 | |
} |
448 | |
|
449 | |
public short getNodeType(Object node) |
450 | |
{ |
451 | 50 | if ( node instanceof Node ) |
452 | |
{ |
453 | 50 | return ((Node) node).getNodeType(); |
454 | |
} |
455 | 0 | return 0; |
456 | |
} |
457 | |
|
458 | |
public Object getDocument(String uri) throws FunctionCallException |
459 | |
{ |
460 | |
try |
461 | |
{ |
462 | 138 | return getSAXReader().read( uri ); |
463 | |
} |
464 | 0 | catch (DocumentException e) |
465 | |
{ |
466 | 0 | throw new FunctionCallException("Failed to parse document for URI: " + uri, e); |
467 | |
} |
468 | |
} |
469 | |
|
470 | |
public String getProcessingInstructionTarget(Object obj) |
471 | |
{ |
472 | 12 | ProcessingInstruction pi = (ProcessingInstruction) obj; |
473 | |
|
474 | 12 | return pi.getTarget(); |
475 | |
} |
476 | |
|
477 | |
public String getProcessingInstructionData(Object obj) |
478 | |
{ |
479 | 6 | ProcessingInstruction pi = (ProcessingInstruction) obj; |
480 | |
|
481 | 6 | return pi.getText(); |
482 | |
} |
483 | |
|
484 | |
|
485 | |
|
486 | |
public SAXReader getSAXReader() |
487 | |
{ |
488 | 138 | if ( reader == null ) |
489 | |
{ |
490 | 136 | reader = new SAXReader(); |
491 | 136 | reader.setMergeAdjacentText( true ); |
492 | |
} |
493 | 138 | return reader; |
494 | |
} |
495 | |
|
496 | |
public void setSAXReader(SAXReader reader) |
497 | |
{ |
498 | 0 | this.reader = reader; |
499 | 0 | } |
500 | |
|
501 | |
} |