1 2 58 59 package org.xquark.xpath.datamodel.xerces.dom; 60 61 import java.io.IOException ; 62 import java.io.ObjectInputStream ; 63 import java.io.ObjectOutputStream ; 64 65 import org.w3c.dom.*; 66 import org.w3c.dom.events.MutationEvent ; 67 import org.xquark.xpath.datamodel.xerces.dom.events.MutationEventImpl; 68 69 102 public abstract class ParentNode 103 extends ChildNode { 104 105 106 static final long serialVersionUID = 2815829867152120872L; 107 108 109 protected DocumentImpl ownerDocument; 110 111 112 protected ChildNode firstChild = null; 113 114 116 117 protected transient int fCachedLength = -1; 118 119 120 protected transient ChildNode fCachedChild; 121 122 123 protected transient int fCachedChildIndex = -1; 124 125 129 133 protected ParentNode(DocumentImpl ownerDocument) { 134 super(ownerDocument); 135 this.ownerDocument = ownerDocument; 136 } 137 138 139 public ParentNode() {} 140 141 145 163 public Node cloneNode(boolean deep) { 164 165 if (needsSyncChildren()) { 166 synchronizeChildren(); 167 } 168 ParentNode newnode = (ParentNode) super.cloneNode(deep); 169 170 newnode.ownerDocument = ownerDocument; 172 173 newnode.firstChild = null; 175 176 newnode.fCachedChildIndex = -1; 178 newnode.fCachedLength = -1; 179 180 if (deep) { 182 for (ChildNode child = firstChild; 183 child != null; 184 child = child.nextSibling) { 185 newnode.appendChild(child.cloneNode(true)); 186 } 187 } 188 189 return newnode; 190 191 } 193 198 public Document getOwnerDocument() { 199 return ownerDocument; 200 } 201 202 206 DocumentImpl ownerDocument() { 207 return ownerDocument; 208 } 209 210 214 void setOwnerDocument(DocumentImpl doc) { 215 if (needsSyncChildren()) { 216 synchronizeChildren(); 217 } 218 super.setOwnerDocument(doc); 219 ownerDocument = doc; 220 for (ChildNode child = firstChild; 221 child != null; child = child.nextSibling) { 222 child.setOwnerDocument(doc); 223 } 224 } 225 226 230 public boolean hasChildNodes() { 231 if (needsSyncChildren()) { 232 synchronizeChildren(); 233 } 234 return firstChild != null; 235 } 236 237 250 public NodeList getChildNodes() { 251 252 if (needsSyncChildren()) { 253 synchronizeChildren(); 254 } 255 return this; 256 257 } 259 260 public Node getFirstChild() { 261 262 if (needsSyncChildren()) { 263 synchronizeChildren(); 264 } 265 return firstChild; 266 267 } 269 270 public Node getLastChild() { 271 272 if (needsSyncChildren()) { 273 synchronizeChildren(); 274 } 275 return lastChild(); 276 277 } 279 final ChildNode lastChild() { 280 return firstChild != null ? firstChild.previousSibling : null; 282 } 283 284 final void lastChild(ChildNode node) { 285 if (firstChild != null) { 287 firstChild.previousSibling = node; 288 } 289 } 290 291 319 public Node insertBefore(Node newChild, Node refChild) 320 throws DOMException { 321 return internalInsertBefore(newChild,refChild,MUTATION_ALL); 323 } 325 330 Node internalInsertBefore(Node newChild, Node refChild,int mutationMask) 331 throws DOMException { 332 333 boolean errorChecking = ownerDocument.errorChecking; 334 335 if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) { 336 343 351 if (errorChecking) { 354 for (Node kid = newChild.getFirstChild(); kid != null; kid = kid.getNextSibling()) { 356 357 if (!ownerDocument.isKidOK(this, kid)) { 358 throw new DOMException( 359 DOMException.HIERARCHY_REQUEST_ERR, 360 "DOM006 Hierarchy request error"); 361 } 362 } 363 } 364 365 while (newChild.hasChildNodes()) { 366 insertBefore(newChild.getFirstChild(), refChild); 367 } 368 return newChild; 369 } 370 371 if (newChild == refChild) { 372 refChild = refChild.getNextSibling(); 374 removeChild(newChild); 375 insertBefore(newChild, refChild); 376 return newChild; 377 } 378 379 if (needsSyncChildren()) { 380 synchronizeChildren(); 381 } 382 383 if (errorChecking) { 384 if (isReadOnly()) { 385 throw new DOMException( 386 DOMException.NO_MODIFICATION_ALLOWED_ERR, 387 "DOM001 Modification not allowed"); 388 } 389 if (newChild.getOwnerDocument() != ownerDocument) { 390 throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, 391 "DOM005 Wrong document"); 392 } 393 if (!ownerDocument.isKidOK(this, newChild)) { 394 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, 395 "DOM006 Hierarchy request error"); 396 } 397 if (refChild != null && refChild.getParentNode() != this) { 399 throw new DOMException(DOMException.NOT_FOUND_ERR, 400 "DOM008 Not found"); 401 } 402 403 boolean treeSafe = true; 407 for (NodeImpl a = this; treeSafe && a != null; a = a.parentNode()) 408 { 409 treeSafe = newChild != a; 410 } 411 if(!treeSafe) { 412 throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, 413 "DOM006 Hierarchy request error"); 414 } 415 } 416 417 EnclosingAttr enclosingAttr=null; 418 if (MUTATIONEVENTS && ownerDocument.mutationEvents 419 && (mutationMask&MUTATION_AGGREGATE)!=0) { 420 LCount lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 425 if (lc.captures+lc.bubbles+lc.defaults>0) { 426 enclosingAttr=getEnclosingAttr(); 427 } 428 } 429 430 ChildNode newInternal = (ChildNode)newChild; 432 433 Node oldparent = newInternal.parentNode(); 434 if (oldparent != null) { 435 oldparent.removeChild(newInternal); 436 } 437 438 ChildNode refInternal = (ChildNode)refChild; 440 441 newInternal.ownerNode = this; 443 newInternal.isOwned(true); 444 445 if (firstChild == null) { 448 firstChild = newInternal; 450 newInternal.isFirstChild(true); 451 newInternal.previousSibling = newInternal; 452 } 453 else { 454 if (refInternal == null) { 455 ChildNode lastChild = firstChild.previousSibling; 457 lastChild.nextSibling = newInternal; 458 newInternal.previousSibling = lastChild; 459 firstChild.previousSibling = newInternal; 460 } 461 else { 462 if (refChild == firstChild) { 464 firstChild.isFirstChild(false); 466 newInternal.nextSibling = firstChild; 467 newInternal.previousSibling = firstChild.previousSibling; 468 firstChild.previousSibling = newInternal; 469 firstChild = newInternal; 470 newInternal.isFirstChild(true); 471 } 472 else { 473 ChildNode prev = refInternal.previousSibling; 475 newInternal.nextSibling = refInternal; 476 prev.nextSibling = newInternal; 477 refInternal.previousSibling = newInternal; 478 newInternal.previousSibling = prev; 479 } 480 } 481 } 482 483 changed(); 484 485 if (fCachedLength != -1) { 487 fCachedLength++; 488 } 489 if (fCachedChildIndex != -1) { 490 if (fCachedChild == refInternal) { 493 fCachedChild = newInternal; 494 } else { 495 fCachedChildIndex = -1; 497 } 498 } 499 500 if (MUTATIONEVENTS && ownerDocument.mutationEvents) { 501 if ((mutationMask&MUTATION_LOCAL) != 0) { 504 LCount lc = LCount.lookup(MutationEventImpl.DOM_NODE_INSERTED); 506 if (lc.captures+lc.bubbles+lc.defaults>0) { 507 MutationEvent me= new MutationEventImpl(); 508 me.initMutationEvent(MutationEventImpl.DOM_NODE_INSERTED, 509 true,false,this,null, 510 null,null,(short)0); 511 newInternal.dispatchEvent(me); 512 } 513 514 lc=LCount.lookup( 517 MutationEventImpl.DOM_NODE_INSERTED_INTO_DOCUMENT); 518 if (lc.captures+lc.bubbles+lc.defaults>0) { 519 NodeImpl eventAncestor=this; 520 if (enclosingAttr!=null) 521 eventAncestor= 522 (NodeImpl)(enclosingAttr.node.getOwnerElement()); 523 if (eventAncestor!=null) { NodeImpl p=eventAncestor; 525 while (p!=null) { 526 eventAncestor=p; if(p.getNodeType()==ATTRIBUTE_NODE) { 530 p=(ElementImpl)((AttrImpl)p).getOwnerElement(); 531 } 532 else { 533 p=p.parentNode(); 534 } 535 } 536 if(eventAncestor.getNodeType()==Node.DOCUMENT_NODE) { 537 MutationEvent me= new MutationEventImpl(); 538 me.initMutationEvent(MutationEventImpl 539 .DOM_NODE_INSERTED_INTO_DOCUMENT, 540 false,false,null,null, 541 null,null,(short)0); 542 dispatchEventToSubtree(newInternal,me); 543 } 544 } 545 } 546 } 547 548 if ((mutationMask&MUTATION_AGGREGATE) != 0) { 551 dispatchAggregateEvents(enclosingAttr); 552 } 553 } 554 555 checkNormalizationAfterInsert(newInternal); 556 557 return newChild; 558 559 } 561 573 public Node removeChild(Node oldChild) 574 throws DOMException { 575 return internalRemoveChild(oldChild,MUTATION_ALL); 577 } 579 584 Node internalRemoveChild(Node oldChild,int mutationMask) 585 throws DOMException { 586 587 DocumentImpl ownerDocument = ownerDocument(); 588 if (ownerDocument.errorChecking) { 589 if (isReadOnly()) { 590 throw new DOMException( 591 DOMException.NO_MODIFICATION_ALLOWED_ERR, 592 "DOM001 Modification not allowed"); 593 } 594 if (oldChild != null && oldChild.getParentNode() != this) { 595 throw new DOMException(DOMException.NOT_FOUND_ERR, 596 "DOM008 Not found"); 597 } 598 } 599 600 ownerDocument.removedChildNode(oldChild); 602 603 ChildNode oldInternal = (ChildNode) oldChild; 604 605 EnclosingAttr enclosingAttr=null; 606 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 607 { 608 LCount lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 613 if(lc.captures+lc.bubbles+lc.defaults>0) 614 { 615 enclosingAttr=getEnclosingAttr(); 616 } 617 618 if( (mutationMask&MUTATION_LOCAL) != 0) 619 { 620 lc=LCount.lookup(MutationEventImpl.DOM_NODE_REMOVED); 622 if(lc.captures+lc.bubbles+lc.defaults>0) 623 { 624 MutationEvent me= new MutationEventImpl(); 625 me.initMutationEvent(MutationEventImpl.DOM_NODE_REMOVED, 626 true,false,this,null, 627 null,null,(short)0); 628 oldInternal.dispatchEvent(me); 629 } 630 631 lc=LCount.lookup( 634 MutationEventImpl.DOM_NODE_REMOVED_FROM_DOCUMENT); 635 if(lc.captures+lc.bubbles+lc.defaults>0) 636 { 637 NodeImpl eventAncestor=this; 638 if(enclosingAttr!=null) 639 eventAncestor= 640 (NodeImpl) enclosingAttr.node.getOwnerElement(); 641 if(eventAncestor!=null) { 643 for(NodeImpl p=eventAncestor.parentNode(); 644 p!=null; 645 p=p.parentNode()) 646 { 647 eventAncestor=p; } 649 if(eventAncestor.getNodeType()==Node.DOCUMENT_NODE) 650 { 651 MutationEvent me= new MutationEventImpl(); 652 me.initMutationEvent(MutationEventImpl 653 .DOM_NODE_REMOVED_FROM_DOCUMENT, 654 false,false, 655 null,null,null,null,(short)0); 656 dispatchEventToSubtree(oldInternal,me); 657 } 658 } 659 } 660 } 661 } 663 if (fCachedLength != -1) { 665 fCachedLength--; 666 } 667 if (fCachedChildIndex != -1) { 668 if (fCachedChild == oldInternal) { 671 fCachedChildIndex--; 672 fCachedChild = oldInternal.previousSibling(); 673 } else { 674 fCachedChildIndex = -1; 676 } 677 } 678 679 if (oldInternal == firstChild) { 682 oldInternal.isFirstChild(false); 684 firstChild = oldInternal.nextSibling; 685 if (firstChild != null) { 686 firstChild.isFirstChild(true); 687 firstChild.previousSibling = oldInternal.previousSibling; 688 } 689 } else { 690 ChildNode prev = oldInternal.previousSibling; 691 ChildNode next = oldInternal.nextSibling; 692 prev.nextSibling = next; 693 if (next == null) { 694 firstChild.previousSibling = prev; 696 } else { 697 next.previousSibling = prev; 699 } 700 } 701 702 ChildNode oldPreviousSibling = oldInternal.previousSibling(); 704 705 oldInternal.ownerNode = ownerDocument; 707 oldInternal.isOwned(false); 708 oldInternal.nextSibling = null; 709 oldInternal.previousSibling = null; 710 711 changed(); 712 713 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 714 { 715 if( (mutationMask&MUTATION_AGGREGATE) != 0) 719 dispatchAggregateEvents(enclosingAttr); 720 } 722 checkNormalizationAfterRemove(oldPreviousSibling); 723 724 return oldInternal; 725 726 } 728 749 public Node replaceChild(Node newChild, Node oldChild) 750 throws DOMException { 751 757 EnclosingAttr enclosingAttr=null; 758 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 759 { 760 LCount lc=LCount.lookup(MutationEventImpl.DOM_ATTR_MODIFIED); 765 if(lc.captures+lc.bubbles+lc.defaults>0) 766 { 767 enclosingAttr=getEnclosingAttr(); 768 } 769 } 771 internalInsertBefore(newChild, oldChild,MUTATION_LOCAL); 772 if (newChild != oldChild) { 773 internalRemoveChild(oldChild,MUTATION_LOCAL); 774 } 775 776 if(MUTATIONEVENTS && ownerDocument.mutationEvents) 777 { 778 dispatchAggregateEvents(enclosingAttr); 779 } 780 781 return oldChild; 782 } 783 784 788 793 private int nodeListGetLength() { 794 795 if (fCachedLength == -1) { ChildNode node; 797 if (fCachedChildIndex != -1 && fCachedChild != null) { 799 fCachedLength = fCachedChildIndex; 800 node = fCachedChild; 801 } else { 802 node = firstChild; 803 fCachedLength = 0; 804 } 805 for (; node != null; node = node.nextSibling) { 806 fCachedLength++; 807 } 808 } 809 810 return fCachedLength; 811 812 } 814 818 public int getLength() { 819 return nodeListGetLength(); 820 } 821 822 827 private Node nodeListItem(int index) { 828 if (fCachedChildIndex != -1 && fCachedChild != null) { 830 if (fCachedChildIndex < index) { 831 while (fCachedChildIndex < index && fCachedChild != null) { 832 fCachedChildIndex++; 833 fCachedChild = fCachedChild.nextSibling; 834 } 835 } 836 else if (fCachedChildIndex > index) { 837 while (fCachedChildIndex > index && fCachedChild != null) { 838 fCachedChildIndex--; 839 fCachedChild = fCachedChild.previousSibling(); 840 } 841 } 842 return fCachedChild; 843 } 844 845 fCachedChild = firstChild; 847 for (fCachedChildIndex = 0; 848 fCachedChildIndex < index && fCachedChild != null; 849 fCachedChildIndex++) { 850 fCachedChild = fCachedChild.nextSibling; 851 } 852 return fCachedChild; 853 854 } 856 862 public Node item(int index) { 863 return nodeListItem(index); 864 } 866 878 protected final NodeList getChildNodesUnoptimized() { 879 if (needsSyncChildren()) { 880 synchronizeChildren(); 881 } 882 return new NodeList() { 883 886 public int getLength() { 887 return nodeListGetLength(); 888 } 890 893 public Node item(int index) { 894 return nodeListItem(index); 895 } }; 897 } 899 903 908 public void normalize() { 909 if (isNormalized()) { 911 return; 912 } 913 if (needsSyncChildren()) { 914 synchronizeChildren(); 915 } 916 ChildNode kid; 917 for (kid = firstChild; kid != null; kid = kid.nextSibling) { 918 kid.normalize(); 919 } 920 isNormalized(true); 921 } 922 923 927 935 public void setReadOnly(boolean readOnly, boolean deep) { 936 937 super.setReadOnly(readOnly, deep); 938 939 if (deep) { 940 941 if (needsSyncChildren()) { 942 synchronizeChildren(); 943 } 944 945 for (ChildNode mykid = firstChild; 947 mykid != null; 948 mykid = mykid.nextSibling) { 949 if (mykid.getNodeType() != Node.ENTITY_REFERENCE_NODE) { 950 mykid.setReadOnly(readOnly,true); 951 } 952 } 953 } 954 } 956 960 964 protected void synchronizeChildren() { 965 needsSyncChildren(false); 967 } 968 969 970 985 void checkNormalizationAfterInsert(ChildNode insertedChild) { 986 if (insertedChild.getNodeType() == Node.TEXT_NODE) { 988 ChildNode prev = insertedChild.previousSibling(); 989 ChildNode next = insertedChild.nextSibling; 990 if ((prev != null && prev.getNodeType() == Node.TEXT_NODE) || 993 (next != null && next.getNodeType() == Node.TEXT_NODE)) { 994 isNormalized(false); 995 } 996 } 997 else { 998 if (!insertedChild.isNormalized()) { 1001 isNormalized(false); 1002 } 1003 } 1004 } 1006 1018 void checkNormalizationAfterRemove(ChildNode previousSibling) { 1019 if (previousSibling != null && 1023 previousSibling.getNodeType() == Node.TEXT_NODE) { 1024 1025 ChildNode next = previousSibling.nextSibling; 1026 if (next != null && next.getNodeType() == Node.TEXT_NODE) { 1027 isNormalized(false); 1028 } 1029 } 1030 } 1032 1036 1037 private void writeObject(ObjectOutputStream out) throws IOException { 1038 1039 if (needsSyncChildren()) { 1041 synchronizeChildren(); 1042 } 1043 out.defaultWriteObject(); 1045 1046 } 1048 1049 private void readObject(ObjectInputStream ois) 1050 throws ClassNotFoundException , IOException { 1051 1052 ois.defaultReadObject(); 1054 1055 1058 needsSyncChildren(false); 1059 1060 fCachedLength = -1; 1062 fCachedChildIndex = -1; 1063 1064 } 1066} | Popular Tags |