1 61 62 66 package org.jaxen.dom; 67 68 import java.util.HashMap ; 69 70 import org.jaxen.pattern.Pattern; 71 import org.w3c.dom.DOMException ; 72 import org.w3c.dom.Document ; 73 import org.w3c.dom.NamedNodeMap ; 74 import org.w3c.dom.Node ; 75 import org.w3c.dom.NodeList ; 76 import org.w3c.dom.UserDataHandler ; 77 78 79 119 public class NamespaceNode implements Node 120 { 121 122 123 127 132 public final static short NAMESPACE_NODE = Pattern.NAMESPACE_NODE; 133 134 137 141 142 149 public NamespaceNode (Node parent, String name, String value) 150 { 151 this.parent = parent; 152 this.name = name; 153 this.value = value; 154 } 155 156 157 164 NamespaceNode (Node parent, Node attribute) 165 { 166 String attributeName = attribute.getNodeName(); 167 168 if (attributeName.equals("xmlns")) { 169 this.name = ""; 170 } 171 else if (attributeName.startsWith("xmlns:")) { 172 this.name = attributeName.substring(6); } 174 else { this.name = attributeName; 176 } 177 this.parent = parent; 178 this.value = attribute.getNodeValue(); 179 } 180 181 182 183 187 188 193 public String getNodeName () 194 { 195 return name; 196 } 197 198 199 204 public String getNodeValue () 205 { 206 return value; 207 } 208 209 210 216 public void setNodeValue (String value) throws DOMException 217 { 218 disallowModification(); 219 } 220 221 222 227 public short getNodeType () 228 { 229 return NAMESPACE_NODE; 230 } 231 232 233 242 public Node getParentNode () 243 { 244 return parent; 245 } 246 247 248 253 public NodeList getChildNodes () 254 { 255 return new EmptyNodeList(); 256 } 257 258 259 264 public Node getFirstChild () 265 { 266 return null; 267 } 268 269 270 275 public Node getLastChild () 276 { 277 return null; 278 } 279 280 281 286 public Node getPreviousSibling () 287 { 288 return null; 289 } 290 291 292 297 public Node getNextSibling () 298 { 299 return null; 300 } 301 302 303 308 public NamedNodeMap getAttributes () 309 { 310 return null; 311 } 312 313 314 319 public Document getOwnerDocument () 320 { 321 if (parent == null) return null; 322 return parent.getOwnerDocument(); 323 } 324 325 326 335 public Node insertBefore (Node newChild, Node refChild) 336 throws DOMException 337 { 338 disallowModification(); 339 return null; 340 } 341 342 343 352 public Node replaceChild (Node newChild, Node oldChild) 353 throws DOMException 354 { 355 disallowModification(); 356 return null; 357 } 358 359 360 368 public Node removeChild (Node oldChild) 369 throws DOMException 370 { 371 disallowModification(); 372 return null; 373 } 374 375 376 384 public Node appendChild (Node newChild) 385 throws DOMException 386 { 387 disallowModification(); 388 return null; 389 } 390 391 392 397 public boolean hasChildNodes () 398 { 399 return false; 400 } 401 402 403 410 public Node cloneNode (boolean deep) 411 { 412 return new NamespaceNode(parent, name, value); 413 } 414 415 416 422 public void normalize () 423 { 424 } 426 427 428 435 public boolean isSupported (String feature, String version) 436 { 437 return false; 438 } 439 440 441 449 public String getNamespaceURI () 450 { 451 return null; 452 } 453 454 455 464 public String getPrefix () 465 { 466 return null; 467 } 468 469 470 476 public void setPrefix (String prefix) 477 throws DOMException 478 { 479 disallowModification(); 480 } 481 482 483 489 public String getLocalName () 490 { 491 return name; 492 } 493 494 495 500 public boolean hasAttributes () 501 { 502 return false; 503 } 504 505 506 511 private void disallowModification () throws DOMException 512 { 513 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 514 "Namespace node may not be modified"); 515 } 516 517 518 519 523 524 529 public int hashCode () 530 { 531 return hashCode(parent) + hashCode(name) + hashCode(value); 532 } 533 534 535 545 public boolean equals (Object o) 546 { 547 if (o == this) return true; 548 else if (o == null) return false; 549 else if (o instanceof NamespaceNode) { 550 NamespaceNode ns = (NamespaceNode)o; 551 return (equals(parent, ns.getParentNode()) && 552 equals(name, ns.getNodeName()) && 553 equals(value, ns.getNodeValue())); 554 } else { 555 return false; 556 } 557 } 558 559 560 567 private int hashCode (Object o) 568 { 569 return (o == null ? 0 : o.hashCode()); 570 } 571 572 573 581 private boolean equals (Object a, Object b) 582 { 583 return ((a == null && b == null) || 584 (a != null && a.equals(b))); 585 } 586 587 588 592 private Node parent; 593 private String name; 594 private String value; 595 596 597 598 602 603 610 private static class EmptyNodeList implements NodeList 611 { 612 613 616 public int getLength () 617 { 618 return 0; 619 } 620 621 622 625 public Node item(int index) 626 { 627 return null; 628 } 629 630 } 631 632 636 641 public String getBaseURI() { 642 return null; 644 } 645 646 647 653 public short compareDocumentPosition(Node other) throws DOMException { 654 DOMException ex = new DOMException ( 655 DOMException.NOT_SUPPORTED_ERR, 656 "DOM level 3 interfaces are not fully implemented in Jaxen's NamespaceNode class" 657 ); 658 throw ex; 659 } 660 661 662 668 public String getTextContent() { 669 return value; 670 } 671 672 673 679 public void setTextContent(String textContent) throws DOMException { 680 disallowModification(); 681 } 682 683 684 693 public boolean isSameNode(Node other) { 694 return this.isEqualNode(other) 695 && this.getParentNode() == other.getParentNode(); 698 } 699 700 701 708 public String lookupPrefix(String namespaceURI) { 709 throw new UnsupportedOperationException ("Changing interfaces in a JDK blows chunks!"); 713 } 714 715 716 723 public boolean isDefaultNamespace(String namespaceURI) { 724 return namespaceURI.equals(this.lookupNamespaceURI(null)); 725 } 726 727 728 735 public String lookupNamespaceURI(String prefix) { 736 throw new UnsupportedOperationException ("Changing interfaces in a JDK blows chunks!"); 740 } 741 742 743 750 public boolean isEqualNode(Node arg) { 751 if (arg.getNodeType() == this.getNodeType()) { 752 NamespaceNode other = (NamespaceNode) arg; 753 if (other.name == null && this.name != null) return false; 754 else if (other.name != null && this.name == null) return false; 755 else if (other.value == null && this.value != null) return false; 756 else if (other.value != null && this.value == null) return false; 757 else if (other.name == null && this.name == null) { 758 return other.value.equals(this.value); 759 } 760 761 return other.name.equals(this.name) && other.value.equals(this.value); 762 } 763 return false; 764 } 765 766 767 772 public Object getFeature(String feature, String version) { 773 return null; 774 } 775 776 777 private HashMap userData = new HashMap (); 779 780 790 public Object setUserData(String key, Object data, UserDataHandler handler) { 791 Object oldValue = getUserData(key); 792 userData.put(key, data); 793 return oldValue; 794 } 795 796 797 802 public Object getUserData(String key) { 803 return userData.get(key); 804 } 805 806 } 807 808 | Popular Tags |