| 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 | 1470 | 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 | 1470 | private static class Singleton |
| 103 | |
{ |
| 104 | |
|
| 105 | |
|
| 106 | 10 | private static DocumentNavigator instance = new DocumentNavigator(); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public static Navigator getInstance() |
| 112 | |
{ |
| 113 | 80 | return Singleton.instance; |
| 114 | |
} |
| 115 | |
|
| 116 | |
public boolean isElement(Object obj) |
| 117 | |
{ |
| 118 | 559520 | return obj instanceof Element; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public boolean isComment(Object obj) |
| 122 | |
{ |
| 123 | 5555 | return obj instanceof Comment; |
| 124 | |
} |
| 125 | |
|
| 126 | |
public boolean isText(Object obj) |
| 127 | |
{ |
| 128 | 378005 | return ( obj instanceof Text |
| 129 | |
|| |
| 130 | |
obj instanceof CDATA ); |
| 131 | |
} |
| 132 | |
|
| 133 | |
public boolean isAttribute(Object obj) |
| 134 | |
{ |
| 135 | 22090 | return obj instanceof Attribute; |
| 136 | |
} |
| 137 | |
|
| 138 | |
public boolean isProcessingInstruction(Object obj) |
| 139 | |
{ |
| 140 | 5355 | return obj instanceof ProcessingInstruction; |
| 141 | |
} |
| 142 | |
|
| 143 | |
public boolean isDocument(Object obj) |
| 144 | |
{ |
| 145 | 5860 | return obj instanceof Document; |
| 146 | |
} |
| 147 | |
|
| 148 | |
public boolean isNamespace(Object obj) |
| 149 | |
{ |
| 150 | 20150 | return obj instanceof Namespace; |
| 151 | |
} |
| 152 | |
|
| 153 | |
public String getElementName(Object obj) |
| 154 | |
{ |
| 155 | 185350 | Element elem = (Element) obj; |
| 156 | |
|
| 157 | 185350 | return elem.getName(); |
| 158 | |
} |
| 159 | |
|
| 160 | |
public String getElementNamespaceUri(Object obj) |
| 161 | |
{ |
| 162 | 184195 | Element elem = (Element) obj; |
| 163 | |
|
| 164 | 184195 | String uri = elem.getNamespaceURI(); |
| 165 | 184195 | if ( uri == null) |
| 166 | 0 | return ""; |
| 167 | |
else |
| 168 | 184195 | return uri; |
| 169 | |
} |
| 170 | |
|
| 171 | |
public String getElementQName(Object obj) |
| 172 | |
{ |
| 173 | 130 | Element elem = (Element) obj; |
| 174 | |
|
| 175 | 130 | return elem.getQualifiedName(); |
| 176 | |
} |
| 177 | |
|
| 178 | |
public String getAttributeName(Object obj) |
| 179 | |
{ |
| 180 | 150 | Attribute attr = (Attribute) obj; |
| 181 | |
|
| 182 | 150 | return attr.getName(); |
| 183 | |
} |
| 184 | |
|
| 185 | |
public String getAttributeNamespaceUri(Object obj) |
| 186 | |
{ |
| 187 | 150 | Attribute attr = (Attribute) obj; |
| 188 | |
|
| 189 | 150 | String uri = attr.getNamespaceURI(); |
| 190 | 150 | if ( uri == null) |
| 191 | 0 | return ""; |
| 192 | |
else |
| 193 | 150 | 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 | 583310 | Iterator result = null; |
| 206 | 583310 | if ( contextNode instanceof Branch ) |
| 207 | |
{ |
| 208 | 203440 | Branch node = (Branch) contextNode; |
| 209 | 203440 | result = node.nodeIterator(); |
| 210 | |
} |
| 211 | 583310 | if (result != null) { |
| 212 | 203440 | return result; |
| 213 | |
} |
| 214 | 379870 | 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 | 11810 | if ( contextNode instanceof Element ) { |
| 232 | 4670 | Element node = (Element) contextNode; |
| 233 | 4670 | return node.elementIterator(QName.get(localName, namespacePrefix, namespaceURI)); |
| 234 | |
} |
| 235 | 7140 | if ( contextNode instanceof Document ) { |
| 236 | 590 | Document node = (Document) contextNode; |
| 237 | 590 | Element el = node.getRootElement(); |
| 238 | 590 | if (el == null || el.getName().equals(localName) == false) { |
| 239 | 80 | return JaxenConstants.EMPTY_ITERATOR; |
| 240 | |
} |
| 241 | 510 | if (namespaceURI != null) { |
| 242 | 50 | if (namespaceURI.equals(el.getNamespaceURI()) == false) { |
| 243 | 5 | return JaxenConstants.EMPTY_ITERATOR; |
| 244 | |
} |
| 245 | |
} |
| 246 | 505 | return new SingleObjectIterator(el); |
| 247 | |
} |
| 248 | |
|
| 249 | 6550 | return JaxenConstants.EMPTY_ITERATOR; |
| 250 | |
} |
| 251 | |
|
| 252 | |
public Iterator getParentAxisIterator(Object contextNode) |
| 253 | |
{ |
| 254 | 70 | if ( contextNode instanceof Document ) |
| 255 | |
{ |
| 256 | 5 | return JaxenConstants.EMPTY_ITERATOR; |
| 257 | |
} |
| 258 | |
|
| 259 | 65 | Node node = (Node) contextNode; |
| 260 | |
|
| 261 | 65 | Object parent = node.getParent(); |
| 262 | |
|
| 263 | 65 | if ( parent == null ) |
| 264 | |
{ |
| 265 | 25 | parent = node.getDocument(); |
| 266 | |
} |
| 267 | |
|
| 268 | 65 | return new SingleObjectIterator( parent ); |
| 269 | |
} |
| 270 | |
|
| 271 | |
public Iterator getAttributeAxisIterator(Object contextNode) |
| 272 | |
{ |
| 273 | 255 | if ( ! ( contextNode instanceof Element ) ) |
| 274 | |
{ |
| 275 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
| 276 | |
} |
| 277 | |
|
| 278 | 255 | Element elem = (Element) contextNode; |
| 279 | |
|
| 280 | 255 | 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 | 2970 | if ( contextNode instanceof Element ) { |
| 297 | 2740 | Element node = (Element) contextNode; |
| 298 | 2740 | Attribute attr = node.attribute(QName.get(localName, namespacePrefix, namespaceURI)); |
| 299 | 2740 | if (attr == null) { |
| 300 | 1155 | return JaxenConstants.EMPTY_ITERATOR; |
| 301 | |
} |
| 302 | 1585 | return new SingleObjectIterator(attr); |
| 303 | |
} |
| 304 | 230 | return JaxenConstants.EMPTY_ITERATOR; |
| 305 | |
} |
| 306 | |
|
| 307 | |
public Iterator getNamespaceAxisIterator(Object contextNode) |
| 308 | |
{ |
| 309 | 835 | if ( ! ( contextNode instanceof Element ) ) |
| 310 | |
{ |
| 311 | 520 | return JaxenConstants.EMPTY_ITERATOR; |
| 312 | |
} |
| 313 | |
|
| 314 | 315 | Element element = (Element) contextNode; |
| 315 | 315 | List nsList = new ArrayList(); |
| 316 | 315 | HashSet prefixes = new HashSet(); |
| 317 | 1140 | for ( Element context = element; context != null; context = context.getParent() ) { |
| 318 | 825 | List declaredNS = new ArrayList(context.declaredNamespaces()); |
| 319 | 825 | declaredNS.add(context.getNamespace()); |
| 320 | |
|
| 321 | 825 | for ( Iterator iter = context.attributes().iterator(); iter.hasNext(); ) |
| 322 | |
{ |
| 323 | 410 | Attribute attr = (Attribute) iter.next(); |
| 324 | 410 | declaredNS.add(attr.getNamespace()); |
| 325 | 410 | } |
| 326 | |
|
| 327 | 825 | for ( Iterator iter = declaredNS.iterator(); iter.hasNext(); ) |
| 328 | |
{ |
| 329 | 1765 | Namespace namespace = (Namespace) iter.next(); |
| 330 | 1765 | if (namespace != Namespace.NO_NAMESPACE) |
| 331 | |
{ |
| 332 | 735 | String prefix = namespace.getPrefix(); |
| 333 | 735 | if ( ! prefixes.contains( prefix ) ) { |
| 334 | 555 | prefixes.add( prefix ); |
| 335 | 555 | nsList.add( namespace.asXPathResult( element ) ); |
| 336 | |
} |
| 337 | |
} |
| 338 | 1765 | } |
| 339 | |
} |
| 340 | 315 | nsList.add( Namespace.XML_NAMESPACE.asXPathResult( element ) ); |
| 341 | 315 | return nsList.iterator(); |
| 342 | |
} |
| 343 | |
|
| 344 | |
public Object getDocumentNode(Object contextNode) |
| 345 | |
{ |
| 346 | 1335 | if ( contextNode instanceof Document ) |
| 347 | |
{ |
| 348 | 995 | return contextNode; |
| 349 | |
} |
| 350 | 340 | else if ( contextNode instanceof Node ) |
| 351 | |
{ |
| 352 | 340 | Node node = (Node) contextNode; |
| 353 | 340 | return node.getDocument(); |
| 354 | |
} |
| 355 | 0 | return null; |
| 356 | |
} |
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
public XPath parseXPath (String xpath) throws SAXPathException |
| 362 | |
{ |
| 363 | 35 | return new Dom4jXPath(xpath); |
| 364 | |
} |
| 365 | |
|
| 366 | |
public Object getParentNode(Object contextNode) |
| 367 | |
{ |
| 368 | 125785 | if ( contextNode instanceof Node ) |
| 369 | |
{ |
| 370 | 125785 | Node node = (Node) contextNode; |
| 371 | 125785 | Object answer = node.getParent(); |
| 372 | 125785 | if ( answer == null ) |
| 373 | |
{ |
| 374 | 49200 | answer = node.getDocument(); |
| 375 | 49200 | if (answer == contextNode) { |
| 376 | 24510 | return null; |
| 377 | |
} |
| 378 | |
} |
| 379 | 101275 | return answer; |
| 380 | |
} |
| 381 | 0 | return null; |
| 382 | |
} |
| 383 | |
|
| 384 | |
public String getTextStringValue(Object obj) |
| 385 | |
{ |
| 386 | 70 | return getNodeStringValue( (Node) obj ); |
| 387 | |
} |
| 388 | |
|
| 389 | |
public String getElementStringValue(Object obj) |
| 390 | |
{ |
| 391 | 650 | return getNodeStringValue( (Node) obj ); |
| 392 | |
} |
| 393 | |
|
| 394 | |
public String getAttributeStringValue(Object obj) |
| 395 | |
{ |
| 396 | 1435 | return getNodeStringValue( (Node) obj ); |
| 397 | |
} |
| 398 | |
|
| 399 | |
private String getNodeStringValue(Node node) |
| 400 | |
{ |
| 401 | 2155 | 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 | 585 | Namespace ns = (Namespace) obj; |
| 414 | |
|
| 415 | 585 | 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 | |
} |
| 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 | 125 | if ( node instanceof Node ) |
| 452 | |
{ |
| 453 | 125 | return ((Node) node).getNodeType(); |
| 454 | |
} |
| 455 | 0 | return 0; |
| 456 | |
} |
| 457 | |
|
| 458 | |
public Object getDocument(String uri) throws FunctionCallException |
| 459 | |
{ |
| 460 | |
try |
| 461 | |
{ |
| 462 | 345 | 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 | 30 | ProcessingInstruction pi = (ProcessingInstruction) obj; |
| 473 | |
|
| 474 | 30 | return pi.getTarget(); |
| 475 | |
} |
| 476 | |
|
| 477 | |
public String getProcessingInstructionData(Object obj) |
| 478 | |
{ |
| 479 | 15 | ProcessingInstruction pi = (ProcessingInstruction) obj; |
| 480 | |
|
| 481 | 15 | return pi.getText(); |
| 482 | |
} |
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
public SAXReader getSAXReader() |
| 487 | |
{ |
| 488 | 345 | if ( reader == null ) |
| 489 | |
{ |
| 490 | 340 | reader = new SAXReader(); |
| 491 | 340 | reader.setMergeAdjacentText( true ); |
| 492 | |
} |
| 493 | 345 | return reader; |
| 494 | |
} |
| 495 | |
|
| 496 | |
public void setSAXReader(SAXReader reader) |
| 497 | |
{ |
| 498 | 0 | this.reader = reader; |
| 499 | 0 | } |
| 500 | |
|
| 501 | |
} |