1 7 package org.enhydra.xml; 8 9 import org.w3c.dom.DOMException ; 10 import org.w3c.dom.Document ; 11 import org.w3c.dom.NamedNodeMap ; 12 import org.w3c.dom.Node ; 13 import org.w3c.dom.NodeList ; 14 import org.w3c.dom.UserDataHandler ; 15 16 27 public class NodeImpl implements Node , NodeList { 28 29 32 protected Document ownerDocument; 33 34 35 38 protected String nodeName = null; 39 40 41 44 protected String nodeValue = null; 45 46 47 50 protected short type = ELEMENT_NODE; 51 52 53 57 protected NodeImpl parent = null; 58 59 60 63 protected int numChildren = 0; 64 65 66 70 protected NodeImpl firstChild = null; 71 72 73 77 protected NodeImpl lastChild = null; 78 79 80 84 protected NodeImpl nextSibling = null; 85 86 87 91 protected NodeImpl previousSibling = null; 92 93 94 97 public NodeImpl() { 98 } 99 100 101 107 public NodeImpl(NodeImpl node) { 108 ownerDocument = node.ownerDocument; 109 nodeName = node.nodeName; 110 nodeValue = node.nodeValue; 111 type = node.type; 112 parent = node.parent; 113 numChildren = node.numChildren; 114 firstChild = node.firstChild; 115 lastChild = node.lastChild; 116 nextSibling = node.nextSibling; 117 previousSibling = node.previousSibling; 118 } 119 120 121 127 public NodeImpl(Node node) { 128 this(node, true); 129 } 130 131 132 141 public NodeImpl(Node node, boolean deep) { 142 this.ownerDocument = node.getOwnerDocument(); 143 this.nodeName = node.getNodeName(); 144 this.type = node.getNodeType(); 145 this.nodeValue = node.getNodeValue(); 146 if (deep) 147 this.initNodeImplChildren(node); 148 } 149 150 151 157 public NodeImpl(Document ownerDoc, String name) { 158 this.ownerDocument = ownerDoc; 159 this.nodeName = nodeName; 160 } 161 162 163 171 public NodeImpl(Document ownerDoc, String nodeName, short type) { 172 this.ownerDocument = ownerDoc; 173 this.nodeName = nodeName; 174 this.type = type; 175 } 176 177 178 187 public NodeImpl(Document ownerDoc, String nodeName, short type, String value) { 188 this.ownerDocument = ownerDoc; 189 this.nodeName = nodeName; 190 this.type = type; 191 this.nodeValue = value; 192 } 193 194 195 196 202 protected void initNodeImplChildren(Node node) { 203 Node child = node.getFirstChild(); 205 while (child != null) { 206 switch (child.getNodeType()) { 207 case Node.ELEMENT_NODE : { 208 this.appendChild(newElementInstance(child)); 209 }; break; 210 case Node.TEXT_NODE : { 211 this.appendChild(newTextInstance(child)); 212 }; break; 213 case Node.COMMENT_NODE : { 214 this.appendChild(newCommentInstance(child)); 215 }; break; 216 default : { 217 this.appendChild(newDefaultInstance(child)); 218 }; 219 } 220 child = child.getNextSibling(); 221 } 222 } 223 224 225 231 protected Node newElementInstance(Node node) { 232 return new ElementImpl(node); 233 } 234 235 241 protected Node newTextInstance(Node node) { 242 return new TextImpl(node); 243 } 244 245 251 protected Node newCommentInstance(Node node) { 252 return new CommentImpl(node); 253 } 254 255 261 protected Node newDefaultInstance(Node node) { 262 return new NodeImpl(node); 263 } 264 265 266 272 private void checkNode(Node node) throws DOMException { 273 if (node == null) { 274 return; 275 } 276 if (!(node instanceof NodeImpl)) 277 throw new NodeDOMException(DOMException.WRONG_DOCUMENT_ERR, "Node not an NodeImpl!"); 278 } 279 280 281 282 284 289 public String getNodeName() { 290 return nodeName; 291 } 292 293 294 299 public String getNodeValue() { 300 return nodeValue; 301 } 302 303 304 309 public void setNodeValue(String nodeValue) { 310 this.nodeValue = nodeValue; 311 } 312 313 314 319 public short getNodeType() { 320 return type; 321 } 322 323 324 337 public Node getParentNode() { 338 return parent; 339 } 340 341 342 349 public NodeList getChildNodes() { 350 return this; 351 } 352 353 354 361 public Node getFirstChild() { 362 return firstChild; 363 } 364 365 366 373 public Node getLastChild() { 374 return lastChild; 375 } 376 377 378 385 public Node getPreviousSibling() { 386 return previousSibling; 387 } 388 389 390 397 public Node getNextSibling() { 398 return nextSibling; 399 } 400 401 402 408 public Document getOwnerDocument() { 409 return ownerDocument; 410 } 411 412 413 427 public Node insertBefore(Node newChild, Node refChild) { 428 if (newChild == null) { 429 throw new IllegalArgumentException ("newChild == null!"); 430 } 431 432 checkNode(newChild); 433 checkNode(refChild); 434 435 NodeImpl newChildNode = (NodeImpl) newChild; 436 NodeImpl refChildNode = (NodeImpl) refChild; 437 438 NodeImpl previous = null; 440 NodeImpl next = null; 441 442 if (refChild == null) { 443 previous = this.lastChild; 444 next = null; 445 this.lastChild = newChildNode; 446 } else { 447 previous = refChildNode.previousSibling; 448 next = refChildNode; 449 } 450 451 if (previous != null) { 452 previous.nextSibling = newChildNode; 453 } 454 if (next != null) { 455 next.previousSibling = newChildNode; 456 } 457 458 newChildNode.parent = this; 459 newChildNode.previousSibling = previous; 460 newChildNode.nextSibling = next; 461 462 if (this.firstChild == refChildNode) { 464 this.firstChild = newChildNode; 465 } 466 ++numChildren; 467 return newChildNode; 468 } 469 470 471 484 public Node replaceChild(Node newChild, Node oldChild) { 485 if (newChild == null) { 486 throw new IllegalArgumentException ("newChild == null!"); 487 } 488 489 checkNode(newChild); 490 checkNode(oldChild); 491 492 NodeImpl newChildNode = (NodeImpl) newChild; 493 NodeImpl oldChildNode = (NodeImpl) oldChild; 494 495 NodeImpl previous = oldChildNode.previousSibling; 496 NodeImpl next = oldChildNode.nextSibling; 497 498 if (previous != null) { 499 previous.nextSibling = newChildNode; 500 } 501 if (next != null) { 502 next.previousSibling = newChildNode; 503 } 504 505 newChildNode.parent = this; 506 newChildNode.previousSibling = previous; 507 newChildNode.nextSibling = next; 508 509 if (firstChild == oldChildNode) { 510 firstChild = newChildNode; 511 } 512 if (lastChild == oldChildNode) { 513 lastChild = newChildNode; 514 } 515 516 oldChildNode.parent = null; 517 oldChildNode.previousSibling = null; 518 oldChildNode.nextSibling = null; 519 520 return oldChildNode; 521 } 522 523 524 535 public Node removeChild(Node oldChild) { 536 if (oldChild == null) { 537 throw new IllegalArgumentException ("oldChild == null!"); 538 } 539 checkNode(oldChild); 540 541 NodeImpl oldChildNode = (NodeImpl) oldChild; 542 543 NodeImpl previous = oldChildNode.previousSibling; 544 NodeImpl next = oldChildNode.nextSibling; 545 546 if (previous != null) { 547 previous.nextSibling = next; 548 } 549 if (next != null) { 550 next.previousSibling = previous; 551 } 552 553 if (this.firstChild == oldChildNode) { 554 this.firstChild = next; 555 } 556 if (this.lastChild == oldChildNode) { 557 this.lastChild = previous; 558 } 559 560 oldChildNode.parent = null; 561 oldChildNode.previousSibling = null; 562 oldChildNode.nextSibling = null; 563 564 --numChildren; 565 return oldChildNode; 566 } 567 568 569 580 public Node appendChild(Node newChild) { 581 if (newChild == null) { 582 throw new IllegalArgumentException ("newChild == null!"); 583 } 584 checkNode(newChild); 585 586 return insertBefore(newChild, null); 588 } 589 590 591 596 public boolean hasChildNodes() { 597 return numChildren > 0; 598 } 599 600 601 615 public Node cloneNode(boolean deep) { 616 return new NodeImpl(this, deep); 617 } 618 619 620 624 public void normalize() { 625 } 626 627 628 637 public boolean isSupported(String feature, String version) { 638 return false; 639 } 640 641 642 649 public String getPrefix() { 650 return null; 651 } 652 653 654 661 public void setPrefix(String prefix) { 662 } 663 664 665 670 public String getLocalName() { 671 return nodeName; 672 } 673 674 675 680 public NamedNodeMap getAttributes() { 681 return null; 682 } 683 684 685 691 public boolean hasAttributes() { 692 return false; 693 } 694 695 696 697 698 700 701 706 public int getLength() { 707 return numChildren; 708 } 709 710 711 716 public Node item(int index) { 717 if (index < 0) { 718 return null; 719 } 720 721 Node child = getFirstChild(); 722 while (child != null && index-- > 0) { 723 child = child.getNextSibling(); 724 } 725 return child; 726 } 727 728 729 730 732 737 public String toString() { 738 return toString(Indent.DEFAULT_TAB); 739 } 740 741 742 749 public String toString(String tab) { 750 StringBuffer sb = new StringBuffer (); 751 this.allToString(sb, new Indent(0,tab)); 752 return sb.toString(); 753 } 754 755 756 767 protected void beginToString(StringBuffer sb, Indent indent) { 768 } 769 770 771 782 protected void endToString(StringBuffer sb, Indent indent) { 783 } 784 785 786 private void allToString(StringBuffer sb, Indent indent) { 787 this.beginToString(sb, indent); 788 Node child = getFirstChild(); 789 while (child != null) { 790 ((NodeImpl) child).allToString(sb, indent); 791 child = child.getNextSibling(); 792 } 793 this.endToString(sb, indent); 794 } 795 796 797 800 public String getBaseURI() { 801 return null; 803 } 804 805 806 809 public short compareDocumentPosition(Node arg0) throws DOMException { 810 return 0; 812 } 813 814 815 818 public String getTextContent() throws DOMException { 819 return null; 821 } 822 823 824 827 public void setTextContent(String arg0) throws DOMException { 828 830 } 831 832 833 836 public boolean isSameNode(Node arg0) { 837 return false; 839 } 840 841 842 845 public String lookupPrefix(String arg0) { 846 return null; 848 } 849 850 851 854 public boolean isDefaultNamespace(String arg0) { 855 return false; 857 } 858 859 860 863 public String lookupNamespaceURI(String arg0) { 864 return null; 866 } 867 868 869 872 public boolean isEqualNode(Node arg0) { 873 return false; 875 } 876 877 878 881 public Object getFeature(String arg0, String arg1) { 882 return null; 884 } 885 886 887 890 public Object setUserData(String arg0, Object arg1, UserDataHandler arg2) { 891 return null; 893 } 894 895 896 899 public Object getUserData(String arg0) { 900 return null; 902 } 903 904 905 908 public String getNamespaceURI() { 909 return null; 911 } 912 913 } 914 915 916 917 | Popular Tags |