| 1 | |
package org.jaxen.jdom; |
| 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.HashMap; |
| 52 | |
import java.util.Iterator; |
| 53 | |
import java.util.List; |
| 54 | |
import java.util.Map; |
| 55 | |
|
| 56 | |
import org.jaxen.DefaultNavigator; |
| 57 | |
import org.jaxen.FunctionCallException; |
| 58 | |
import org.jaxen.NamedAccessNavigator; |
| 59 | |
import org.jaxen.Navigator; |
| 60 | |
import org.jaxen.XPath; |
| 61 | |
import org.jaxen.JaxenConstants; |
| 62 | |
import org.jaxen.saxpath.SAXPathException; |
| 63 | |
import org.jaxen.util.SingleObjectIterator; |
| 64 | |
import org.jdom.Attribute; |
| 65 | |
import org.jdom.CDATA; |
| 66 | |
import org.jdom.Comment; |
| 67 | |
import org.jdom.Document; |
| 68 | |
import org.jdom.Element; |
| 69 | |
import org.jdom.Namespace; |
| 70 | |
import org.jdom.ProcessingInstruction; |
| 71 | |
import org.jdom.Text; |
| 72 | |
import org.jdom.input.SAXBuilder; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | 1465 | public class DocumentNavigator extends DefaultNavigator implements NamedAccessNavigator |
| 88 | |
{ |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private static final long serialVersionUID = -1636727587303584165L; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | 1465 | private static class Singleton |
| 97 | |
{ |
| 98 | |
|
| 99 | |
|
| 100 | 10 | private static DocumentNavigator instance = new DocumentNavigator(); |
| 101 | |
} |
| 102 | |
|
| 103 | |
public static Navigator getInstance() |
| 104 | |
{ |
| 105 | 75 | return Singleton.instance; |
| 106 | |
} |
| 107 | |
|
| 108 | |
public boolean isElement(Object obj) |
| 109 | |
{ |
| 110 | 559600 | return obj instanceof Element; |
| 111 | |
} |
| 112 | |
|
| 113 | |
public boolean isComment(Object obj) |
| 114 | |
{ |
| 115 | 5615 | return obj instanceof Comment; |
| 116 | |
} |
| 117 | |
|
| 118 | |
public boolean isText(Object obj) |
| 119 | |
{ |
| 120 | 378080 | return ( obj instanceof Text |
| 121 | |
|| |
| 122 | |
obj instanceof CDATA ); |
| 123 | |
} |
| 124 | |
|
| 125 | |
public boolean isAttribute(Object obj) |
| 126 | |
{ |
| 127 | 22170 | return obj instanceof Attribute; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public boolean isProcessingInstruction(Object obj) |
| 131 | |
{ |
| 132 | 5465 | return obj instanceof ProcessingInstruction; |
| 133 | |
} |
| 134 | |
|
| 135 | |
public boolean isDocument(Object obj) |
| 136 | |
{ |
| 137 | 5945 | return obj instanceof Document; |
| 138 | |
} |
| 139 | |
|
| 140 | |
public boolean isNamespace(Object obj) |
| 141 | |
{ |
| 142 | 20230 | return obj instanceof Namespace || obj instanceof XPathNamespace; |
| 143 | |
} |
| 144 | |
|
| 145 | |
public String getElementName(Object obj) |
| 146 | |
{ |
| 147 | 185350 | Element elem = (Element) obj; |
| 148 | |
|
| 149 | 185350 | return elem.getName(); |
| 150 | |
} |
| 151 | |
|
| 152 | |
public String getElementNamespaceUri(Object obj) |
| 153 | |
{ |
| 154 | 184195 | Element elem = (Element) obj; |
| 155 | |
|
| 156 | 184195 | String uri = elem.getNamespaceURI(); |
| 157 | 184195 | if ( uri != null && uri.length() == 0 ) |
| 158 | 184055 | return null; |
| 159 | |
else |
| 160 | 140 | return uri; |
| 161 | |
} |
| 162 | |
|
| 163 | |
public String getAttributeName(Object obj) |
| 164 | |
{ |
| 165 | 150 | Attribute attr = (Attribute) obj; |
| 166 | |
|
| 167 | 150 | return attr.getName(); |
| 168 | |
} |
| 169 | |
|
| 170 | |
public String getAttributeNamespaceUri(Object obj) |
| 171 | |
{ |
| 172 | 150 | Attribute attr = (Attribute) obj; |
| 173 | |
|
| 174 | 150 | String uri = attr.getNamespaceURI(); |
| 175 | 150 | if ( uri != null && uri.length() == 0 ) |
| 176 | 0 | return null; |
| 177 | |
else |
| 178 | 150 | return uri; |
| 179 | |
} |
| 180 | |
|
| 181 | |
public Iterator getChildAxisIterator(Object contextNode) |
| 182 | |
{ |
| 183 | 583080 | if ( contextNode instanceof Element ) |
| 184 | |
{ |
| 185 | 202520 | return ((Element)contextNode).getContent().iterator(); |
| 186 | |
} |
| 187 | 380560 | else if ( contextNode instanceof Document ) |
| 188 | |
{ |
| 189 | 900 | return ((Document)contextNode).getContent().iterator(); |
| 190 | |
} |
| 191 | |
|
| 192 | 379660 | return JaxenConstants.EMPTY_ITERATOR; |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
public Iterator getChildAxisIterator( |
| 206 | |
Object contextNode, String localName, String namespacePrefix, String namespaceURI) { |
| 207 | |
|
| 208 | 11830 | if ( contextNode instanceof Element ) { |
| 209 | 4675 | Element node = (Element) contextNode; |
| 210 | 4675 | if (namespaceURI == null) { |
| 211 | 4630 | return node.getChildren(localName).iterator(); |
| 212 | |
} |
| 213 | 45 | return node.getChildren(localName, Namespace.getNamespace(namespacePrefix, namespaceURI)).iterator(); |
| 214 | |
} |
| 215 | 7155 | if ( contextNode instanceof Document ) { |
| 216 | 590 | Document node = (Document) contextNode; |
| 217 | |
|
| 218 | 590 | Element el = node.getRootElement(); |
| 219 | 590 | if (el.getName().equals(localName) == false) { |
| 220 | 80 | return JaxenConstants.EMPTY_ITERATOR; |
| 221 | |
} |
| 222 | 510 | if (namespaceURI != null) { |
| 223 | |
|
| 224 | 50 | if (!Namespace.getNamespace(namespacePrefix, namespaceURI).equals(el.getNamespace())) { |
| 225 | 5 | return JaxenConstants.EMPTY_ITERATOR; |
| 226 | |
} |
| 227 | |
} |
| 228 | 460 | else if(el.getNamespace() != Namespace.NO_NAMESPACE) { |
| 229 | 10 | return JaxenConstants.EMPTY_ITERATOR; |
| 230 | |
} |
| 231 | |
|
| 232 | 495 | return new SingleObjectIterator(el); |
| 233 | |
} |
| 234 | |
|
| 235 | 6565 | return JaxenConstants.EMPTY_ITERATOR; |
| 236 | |
} |
| 237 | |
|
| 238 | |
public Iterator getNamespaceAxisIterator(Object contextNode) |
| 239 | |
{ |
| 240 | 640 | if ( ! ( contextNode instanceof Element ) ) |
| 241 | |
{ |
| 242 | 370 | return JaxenConstants.EMPTY_ITERATOR; |
| 243 | |
} |
| 244 | |
|
| 245 | 270 | Element elem = (Element) contextNode; |
| 246 | |
|
| 247 | 270 | Map nsMap = new HashMap(); |
| 248 | |
|
| 249 | 270 | Element current = elem; |
| 250 | |
|
| 251 | 970 | while ( current != null ) { |
| 252 | |
|
| 253 | 700 | Namespace ns = current.getNamespace(); |
| 254 | |
|
| 255 | 700 | if ( ns != Namespace.NO_NAMESPACE ) { |
| 256 | 165 | if ( !nsMap.containsKey(ns.getPrefix()) ) |
| 257 | 165 | nsMap.put( ns.getPrefix(), new XPathNamespace(elem, ns) ); |
| 258 | |
} |
| 259 | |
|
| 260 | 700 | Iterator additional = current.getAdditionalNamespaces().iterator(); |
| 261 | |
|
| 262 | 1150 | while ( additional.hasNext() ) { |
| 263 | |
|
| 264 | 450 | ns = (Namespace)additional.next(); |
| 265 | 450 | if ( !nsMap.containsKey(ns.getPrefix()) ) |
| 266 | 300 | nsMap.put( ns.getPrefix(), new XPathNamespace(elem, ns) ); |
| 267 | |
} |
| 268 | |
|
| 269 | 700 | Iterator attributes = current.getAttributes().iterator(); |
| 270 | |
|
| 271 | 1050 | while ( attributes.hasNext() ) { |
| 272 | |
|
| 273 | 350 | Attribute attribute = (Attribute)attributes.next(); |
| 274 | |
|
| 275 | 350 | Namespace attrNS = attribute.getNamespace(); |
| 276 | |
|
| 277 | 350 | if ( attrNS != Namespace.NO_NAMESPACE ) { |
| 278 | 10 | if ( !nsMap.containsKey(attrNS.getPrefix()) ) |
| 279 | 10 | nsMap.put( attrNS.getPrefix(), new XPathNamespace(elem, attrNS) ); |
| 280 | |
} |
| 281 | 350 | } |
| 282 | |
|
| 283 | 700 | if (current.getParent() instanceof Element) { |
| 284 | 430 | current = (Element)current.getParent(); |
| 285 | |
} else { |
| 286 | 270 | current = null; |
| 287 | |
} |
| 288 | 700 | } |
| 289 | |
|
| 290 | 270 | nsMap.put( "xml", new XPathNamespace(elem, Namespace.XML_NAMESPACE) ); |
| 291 | |
|
| 292 | 270 | return nsMap.values().iterator(); |
| 293 | |
} |
| 294 | |
|
| 295 | |
public Iterator getParentAxisIterator(Object contextNode) |
| 296 | |
{ |
| 297 | 125900 | Object parent = null; |
| 298 | |
|
| 299 | 125900 | if ( contextNode instanceof Document ) |
| 300 | |
{ |
| 301 | 24595 | return JaxenConstants.EMPTY_ITERATOR; |
| 302 | |
} |
| 303 | 101305 | else if ( contextNode instanceof Element ) |
| 304 | |
{ |
| 305 | 98875 | parent = ((Element)contextNode).getParent(); |
| 306 | |
|
| 307 | 98875 | if ( parent == null ) |
| 308 | |
{ |
| 309 | 0 | if ( ((Element)contextNode).isRootElement() ) |
| 310 | |
{ |
| 311 | 0 | parent = ((Element)contextNode).getDocument(); |
| 312 | |
} |
| 313 | |
} |
| 314 | |
} |
| 315 | 2430 | else if ( contextNode instanceof Attribute ) |
| 316 | |
{ |
| 317 | 370 | parent = ((Attribute)contextNode).getParent(); |
| 318 | |
} |
| 319 | 2060 | else if ( contextNode instanceof XPathNamespace ) |
| 320 | |
{ |
| 321 | 1710 | parent = ((XPathNamespace)contextNode).getJDOMElement(); |
| 322 | |
} |
| 323 | 350 | else if ( contextNode instanceof ProcessingInstruction ) |
| 324 | |
{ |
| 325 | 100 | parent = ((ProcessingInstruction)contextNode).getParent(); |
| 326 | |
} |
| 327 | 250 | else if ( contextNode instanceof Comment ) |
| 328 | |
{ |
| 329 | 100 | parent = ((Comment)contextNode).getParent(); |
| 330 | |
} |
| 331 | 150 | else if ( contextNode instanceof Text ) |
| 332 | |
{ |
| 333 | 150 | parent = ((Text)contextNode).getParent(); |
| 334 | |
} |
| 335 | |
|
| 336 | 101305 | if ( parent != null ) |
| 337 | |
{ |
| 338 | 101305 | return new SingleObjectIterator( parent ); |
| 339 | |
} |
| 340 | |
|
| 341 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
| 342 | |
} |
| 343 | |
|
| 344 | |
public Iterator getAttributeAxisIterator(Object contextNode) |
| 345 | |
{ |
| 346 | 255 | if ( ! ( contextNode instanceof Element ) ) |
| 347 | |
{ |
| 348 | 0 | return JaxenConstants.EMPTY_ITERATOR; |
| 349 | |
} |
| 350 | |
|
| 351 | 255 | Element elem = (Element) contextNode; |
| 352 | |
|
| 353 | 255 | return elem.getAttributes().iterator(); |
| 354 | |
} |
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
public Iterator getAttributeAxisIterator( |
| 367 | |
Object contextNode, String localName, String namespacePrefix, String namespaceURI) { |
| 368 | |
|
| 369 | 2970 | if ( contextNode instanceof Element ) { |
| 370 | 2740 | Element node = (Element) contextNode; |
| 371 | 2740 | Namespace namespace = (namespaceURI == null ? Namespace.NO_NAMESPACE : |
| 372 | |
Namespace.getNamespace(namespacePrefix, namespaceURI)); |
| 373 | 2740 | Attribute attr = node.getAttribute(localName, namespace); |
| 374 | 2740 | if (attr != null) { |
| 375 | 1585 | return new SingleObjectIterator(attr); |
| 376 | |
} |
| 377 | |
} |
| 378 | 1385 | return JaxenConstants.EMPTY_ITERATOR; |
| 379 | |
} |
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
public XPath parseXPath (String xpath) throws SAXPathException |
| 385 | |
{ |
| 386 | 35 | return new JDOMXPath(xpath); |
| 387 | |
} |
| 388 | |
|
| 389 | |
public Object getDocumentNode(Object contextNode) |
| 390 | |
{ |
| 391 | 1340 | if ( contextNode instanceof Document ) |
| 392 | |
{ |
| 393 | 995 | return contextNode; |
| 394 | |
} |
| 395 | |
|
| 396 | 345 | Element elem = (Element) contextNode; |
| 397 | |
|
| 398 | 345 | return elem.getDocument(); |
| 399 | |
} |
| 400 | |
|
| 401 | |
public String getElementQName(Object obj) |
| 402 | |
{ |
| 403 | 130 | Element elem = (Element) obj; |
| 404 | |
|
| 405 | 130 | String prefix = elem.getNamespacePrefix(); |
| 406 | |
|
| 407 | 130 | if ( prefix == null || prefix.length() == 0 ) |
| 408 | |
{ |
| 409 | 130 | return elem.getName(); |
| 410 | |
} |
| 411 | |
|
| 412 | 0 | return prefix + ":" + elem.getName(); |
| 413 | |
} |
| 414 | |
|
| 415 | |
public String getAttributeQName(Object obj) |
| 416 | |
{ |
| 417 | 0 | Attribute attr = (Attribute) obj; |
| 418 | |
|
| 419 | 0 | String prefix = attr.getNamespacePrefix(); |
| 420 | |
|
| 421 | 0 | if ( prefix == null || "".equals( prefix ) ) |
| 422 | |
{ |
| 423 | 0 | return attr.getName(); |
| 424 | |
} |
| 425 | |
|
| 426 | 0 | return prefix + ":" + attr.getName(); |
| 427 | |
} |
| 428 | |
|
| 429 | |
public String getNamespaceStringValue(Object obj) |
| 430 | |
{ |
| 431 | 0 | if (obj instanceof Namespace) { |
| 432 | |
|
| 433 | 0 | Namespace ns = (Namespace) obj; |
| 434 | 0 | return ns.getURI(); |
| 435 | |
} else { |
| 436 | |
|
| 437 | 0 | XPathNamespace ns = (XPathNamespace) obj; |
| 438 | 0 | return ns.getJDOMNamespace().getURI(); |
| 439 | |
} |
| 440 | |
|
| 441 | |
} |
| 442 | |
|
| 443 | |
public String getNamespacePrefix(Object obj) |
| 444 | |
{ |
| 445 | 585 | if (obj instanceof Namespace) { |
| 446 | |
|
| 447 | 0 | Namespace ns = (Namespace) obj; |
| 448 | 0 | return ns.getPrefix(); |
| 449 | |
} else { |
| 450 | |
|
| 451 | 585 | XPathNamespace ns = (XPathNamespace) obj; |
| 452 | 585 | return ns.getJDOMNamespace().getPrefix(); |
| 453 | |
} |
| 454 | |
} |
| 455 | |
|
| 456 | |
public String getTextStringValue(Object obj) |
| 457 | |
{ |
| 458 | 70 | if ( obj instanceof Text ) |
| 459 | |
{ |
| 460 | 70 | return ((Text)obj).getText(); |
| 461 | |
} |
| 462 | |
|
| 463 | 0 | if ( obj instanceof CDATA ) |
| 464 | |
{ |
| 465 | 0 | return ((CDATA)obj).getText(); |
| 466 | |
} |
| 467 | |
|
| 468 | 0 | return ""; |
| 469 | |
} |
| 470 | |
|
| 471 | |
public String getAttributeStringValue(Object obj) |
| 472 | |
{ |
| 473 | 1435 | Attribute attr = (Attribute) obj; |
| 474 | |
|
| 475 | 1435 | return attr.getValue(); |
| 476 | |
} |
| 477 | |
|
| 478 | |
public String getElementStringValue(Object obj) |
| 479 | |
{ |
| 480 | 690 | Element elem = (Element) obj; |
| 481 | |
|
| 482 | 690 | StringBuffer buf = new StringBuffer(); |
| 483 | |
|
| 484 | 690 | List content = elem.getContent(); |
| 485 | 690 | Iterator contentIter = content.iterator(); |
| 486 | 690 | Object each = null; |
| 487 | |
|
| 488 | 1400 | while ( contentIter.hasNext() ) |
| 489 | |
{ |
| 490 | 710 | each = contentIter.next(); |
| 491 | |
|
| 492 | 710 | if ( each instanceof Text ) |
| 493 | |
{ |
| 494 | 665 | buf.append( ((Text)each).getText() ); |
| 495 | |
} |
| 496 | 45 | else if ( each instanceof CDATA ) |
| 497 | |
{ |
| 498 | 0 | buf.append( ((CDATA)each).getText() ); |
| 499 | |
} |
| 500 | 45 | else if ( each instanceof Element ) |
| 501 | |
{ |
| 502 | 45 | buf.append( getElementStringValue( each ) ); |
| 503 | |
} |
| 504 | |
} |
| 505 | |
|
| 506 | 690 | return buf.toString(); |
| 507 | |
} |
| 508 | |
|
| 509 | |
public String getProcessingInstructionTarget(Object obj) |
| 510 | |
{ |
| 511 | 30 | ProcessingInstruction pi = (ProcessingInstruction) obj; |
| 512 | |
|
| 513 | 30 | return pi.getTarget(); |
| 514 | |
} |
| 515 | |
|
| 516 | |
public String getProcessingInstructionData(Object obj) |
| 517 | |
{ |
| 518 | 15 | ProcessingInstruction pi = (ProcessingInstruction) obj; |
| 519 | |
|
| 520 | 15 | return pi.getData(); |
| 521 | |
} |
| 522 | |
|
| 523 | |
public String getCommentStringValue(Object obj) |
| 524 | |
{ |
| 525 | 0 | Comment cmt = (Comment) obj; |
| 526 | |
|
| 527 | 0 | return cmt.getText(); |
| 528 | |
} |
| 529 | |
|
| 530 | |
public String translateNamespacePrefixToUri(String prefix, Object context) |
| 531 | |
{ |
| 532 | 0 | Element element = null; |
| 533 | 0 | if ( context instanceof Element ) |
| 534 | |
{ |
| 535 | 0 | element = (Element) context; |
| 536 | |
} |
| 537 | 0 | else if ( context instanceof Text ) |
| 538 | |
{ |
| 539 | 0 | element = (Element)((Text)context).getParent(); |
| 540 | |
} |
| 541 | 0 | else if ( context instanceof Attribute ) |
| 542 | |
{ |
| 543 | 0 | element = ((Attribute)context).getParent(); |
| 544 | |
} |
| 545 | 0 | else if ( context instanceof XPathNamespace ) |
| 546 | |
{ |
| 547 | 0 | element = ((XPathNamespace)context).getJDOMElement(); |
| 548 | |
} |
| 549 | 0 | else if ( context instanceof Comment ) |
| 550 | |
{ |
| 551 | 0 | element = (Element)((Comment)context).getParent(); |
| 552 | |
} |
| 553 | 0 | else if ( context instanceof ProcessingInstruction ) |
| 554 | |
{ |
| 555 | 0 | element = (Element)((ProcessingInstruction)context).getParent(); |
| 556 | |
} |
| 557 | |
|
| 558 | 0 | if ( element != null ) |
| 559 | |
{ |
| 560 | 0 | Namespace namespace = element.getNamespace( prefix ); |
| 561 | |
|
| 562 | 0 | if ( namespace != null ) |
| 563 | |
{ |
| 564 | 0 | return namespace.getURI(); |
| 565 | |
} |
| 566 | |
} |
| 567 | 0 | return null; |
| 568 | |
} |
| 569 | |
|
| 570 | |
public Object getDocument(String url) throws FunctionCallException |
| 571 | |
{ |
| 572 | |
try |
| 573 | |
{ |
| 574 | 340 | SAXBuilder builder = new SAXBuilder(); |
| 575 | |
|
| 576 | 340 | return builder.build( url ); |
| 577 | |
} |
| 578 | 0 | catch (Exception e) |
| 579 | |
{ |
| 580 | 0 | throw new FunctionCallException( e.getMessage() ); |
| 581 | |
} |
| 582 | |
} |
| 583 | |
} |