| 1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import org.w3c.dom.Element ; 61 import org.w3c.dom.Node ; 62 import org.w3c.dom.TypeInfo ; 63 64 import java.util.Vector ; 65 66 85 public class DeferredDocumentImpl 86 extends DocumentImpl 87 implements DeferredNode { 88 89 93 94 static final long serialVersionUID = 5186323580749626857L; 95 96 98 99 private static final boolean DEBUG_PRINT_REF_COUNTS = false; 100 101 102 private static final boolean DEBUG_PRINT_TABLES = false; 103 104 105 private static final boolean DEBUG_IDS = false; 106 107 109 110 protected static final int CHUNK_SHIFT = 11; 112 113 protected static final int CHUNK_SIZE = (1 << CHUNK_SHIFT); 114 115 116 protected static final int CHUNK_MASK = CHUNK_SIZE - 1; 117 118 119 protected static final int INITIAL_CHUNK_COUNT = (1 << (16 - CHUNK_SHIFT)); 121 125 129 130 protected transient int fNodeCount = 0; 131 132 133 protected transient int fNodeType[][]; 134 135 136 protected transient Object fNodeName[][]; 137 138 139 protected transient Object fNodeValue[][]; 140 141 142 protected transient int fNodeParent[][]; 143 144 145 protected transient int fNodeLastChild[][]; 146 147 148 protected transient int fNodePrevSib[][]; 149 150 151 protected transient Object fNodeURI[][]; 152 153 154 protected transient int fNodeExtra[][]; 155 156 157 protected transient int fIdCount; 158 159 160 protected transient String fIdName[]; 161 162 163 protected transient int fIdElement[]; 164 165 167 protected boolean fNamespacesEnabled = false; 170 171 private transient final StringBuffer fBufferStr = new StringBuffer (); 175 private transient final Vector fStrChunks = new Vector (); 176 177 181 185 public DeferredDocumentImpl() { 186 this(false); 187 } 189 193 public DeferredDocumentImpl(boolean namespacesEnabled) { 194 this(namespacesEnabled, false); 195 } 197 198 public DeferredDocumentImpl(boolean namespaces, boolean grammarAccess) { 199 super(grammarAccess); 200 201 needsSyncData(true); 202 needsSyncChildren(true); 203 204 fNamespacesEnabled = namespaces; 205 206 } 208 212 213 boolean getNamespacesEnabled() { 214 return fNamespacesEnabled; 215 } 216 217 void setNamespacesEnabled(boolean enable) { 218 fNamespacesEnabled = enable; 219 } 220 221 223 224 public int createDeferredDocument() { 225 int nodeIndex = createNode(Node.DOCUMENT_NODE); 226 return nodeIndex; 227 } 228 229 230 public int createDeferredDocumentType(String rootElementName, 231 String publicId, String systemId) { 232 233 int nodeIndex = createNode(Node.DOCUMENT_TYPE_NODE); 235 int chunk = nodeIndex >> CHUNK_SHIFT; 236 int index = nodeIndex & CHUNK_MASK; 237 238 setChunkValue(fNodeName, rootElementName, chunk, index); 240 setChunkValue(fNodeValue, publicId, chunk, index); 241 setChunkValue(fNodeURI, systemId, chunk, index); 242 243 return nodeIndex; 245 246 } 248 public void setInternalSubset(int doctypeIndex, String subset) { 249 int chunk = doctypeIndex >> CHUNK_SHIFT; 250 int index = doctypeIndex & CHUNK_MASK; 251 252 int extraDataIndex = createNode(Node.DOCUMENT_TYPE_NODE); 254 int echunk = extraDataIndex >> CHUNK_SHIFT; 255 int eindex = extraDataIndex & CHUNK_MASK; 256 setChunkIndex(fNodeExtra, extraDataIndex, chunk, index); 257 setChunkValue(fNodeValue, subset, echunk, eindex); 258 } 259 260 261 public int createDeferredNotation(String notationName, 262 String publicId, String systemId, String baseURI) { 263 264 int nodeIndex = createNode(Node.NOTATION_NODE); 266 int chunk = nodeIndex >> CHUNK_SHIFT; 267 int index = nodeIndex & CHUNK_MASK; 268 269 270 int extraDataIndex = createNode(Node.NOTATION_NODE); 272 int echunk = extraDataIndex >> CHUNK_SHIFT; 273 int eindex = extraDataIndex & CHUNK_MASK; 274 275 setChunkValue(fNodeName, notationName, chunk, index); 277 setChunkValue(fNodeValue, publicId, chunk, index); 278 setChunkValue(fNodeURI, systemId, chunk, index); 279 280 setChunkIndex(fNodeExtra, extraDataIndex, chunk, index); 282 setChunkValue(fNodeName, baseURI, echunk, eindex); 283 284 return nodeIndex; 286 287 } 289 290 public int createDeferredEntity(String entityName, String publicId, 291 String systemId, String notationName, 292 String baseURI) { 293 int nodeIndex = createNode(Node.ENTITY_NODE); 295 int chunk = nodeIndex >> CHUNK_SHIFT; 296 int index = nodeIndex & CHUNK_MASK; 297 298 int extraDataIndex = createNode(Node.ENTITY_NODE); 300 int echunk = extraDataIndex >> CHUNK_SHIFT; 301 int eindex = extraDataIndex & CHUNK_MASK; 302 303 setChunkValue(fNodeName, entityName, chunk, index); 305 setChunkValue(fNodeValue, publicId, chunk, index); 306 setChunkValue(fNodeURI, systemId, chunk, index); 307 setChunkIndex(fNodeExtra, extraDataIndex, chunk, index); 308 setChunkValue(fNodeName, notationName, echunk, eindex); 311 setChunkValue(fNodeValue, null, echunk, eindex); 313 setChunkValue(fNodeURI, null, echunk, eindex); 315 316 317 int extraDataIndex2 = createNode(Node.ENTITY_NODE); 318 int echunk2 = extraDataIndex2 >> CHUNK_SHIFT; 319 int eindex2 = extraDataIndex2 & CHUNK_MASK; 320 321 setChunkIndex(fNodeExtra, extraDataIndex2, echunk, eindex); 322 323 setChunkValue(fNodeName, baseURI, echunk2, eindex2); 325 326 return nodeIndex; 328 329 } 331 public String getDeferredEntityBaseURI (int entityIndex){ 332 if (entityIndex != -1) { 333 int extraDataIndex = getNodeExtra(entityIndex, false); 334 extraDataIndex = getNodeExtra(extraDataIndex, false); 335 return getNodeName (extraDataIndex, false); 336 } 337 return null; 338 } 339 340 public void setEntityInfo(int currentEntityDecl, 342 String version, String encoding){ 343 int eNodeIndex = getNodeExtra(currentEntityDecl, false); 344 if (eNodeIndex !=-1) { 345 int echunk = eNodeIndex >> CHUNK_SHIFT; 346 int eindex = eNodeIndex & CHUNK_MASK; 347 setChunkValue(fNodeValue, version, echunk, eindex); 348 setChunkValue(fNodeURI, encoding, echunk, eindex); 349 } 350 } 351 352 353 361 public void setInputEncoding(int currentEntityDecl, String value){ 362 int nodeIndex = getNodeExtra(currentEntityDecl, false); 364 int extraDataIndex = getNodeExtra(nodeIndex, false); 366 367 int echunk = extraDataIndex >> CHUNK_SHIFT; 368 int eindex = extraDataIndex & CHUNK_MASK; 369 370 setChunkValue(fNodeValue, value, echunk, eindex); 371 372 } 373 374 375 public int createDeferredEntityReference(String name, String baseURI) { 376 377 int nodeIndex = createNode(Node.ENTITY_REFERENCE_NODE); 379 int chunk = nodeIndex >> CHUNK_SHIFT; 380 int index = nodeIndex & CHUNK_MASK; 381 setChunkValue(fNodeName, name, chunk, index); 382 setChunkValue(fNodeValue, baseURI, chunk, index); 383 384 return nodeIndex; 386 387 } 389 390 391 public int createDeferredElement(String elementURI, String elementName, 392 TypeInfo type) { 393 394 int elementNodeIndex = createNode(Node.ELEMENT_NODE); 396 int elementChunk = elementNodeIndex >> CHUNK_SHIFT; 397 int elementIndex = elementNodeIndex & CHUNK_MASK; 398 setChunkValue(fNodeName, elementName, elementChunk, elementIndex); 399 setChunkValue(fNodeURI, elementURI, elementChunk, elementIndex); 400 setChunkValue(fNodeValue, type, elementChunk, elementIndex); 401 402 return elementNodeIndex; 404 405 } 407 408 public int createDeferredElement(String elementName) { 409 return createDeferredElement(null, elementName); 410 } 411 412 413 public int createDeferredElement(String elementURI, String elementName) { 414 415 int elementNodeIndex = createNode(Node.ELEMENT_NODE); 417 int elementChunk = elementNodeIndex >> CHUNK_SHIFT; 418 int elementIndex = elementNodeIndex & CHUNK_MASK; 419 setChunkValue(fNodeName, elementName, elementChunk, elementIndex); 420 setChunkValue(fNodeURI, elementURI, elementChunk, elementIndex); 421 422 return elementNodeIndex; 424 425 } 427 428 439 public int setDeferredAttribute(int elementNodeIndex, 440 String attrName, 441 String attrURI, 442 String attrValue, 443 boolean specified, 444 boolean id, 445 TypeInfo type) { 446 447 int attrNodeIndex = createDeferredAttribute(attrName, attrURI, attrValue, specified); 449 int attrChunk = attrNodeIndex >> CHUNK_SHIFT; 450 int attrIndex = attrNodeIndex & CHUNK_MASK; 451 setChunkIndex(fNodeParent, elementNodeIndex, attrChunk, attrIndex); 453 454 int elementChunk = elementNodeIndex >> CHUNK_SHIFT; 455 int elementIndex = elementNodeIndex & CHUNK_MASK; 456 457 int lastAttrNodeIndex = getChunkIndex(fNodeExtra, elementChunk, elementIndex); 459 if (lastAttrNodeIndex != 0) { 460 int lastAttrChunk = lastAttrNodeIndex >> CHUNK_SHIFT; 461 int lastAttrIndex = lastAttrNodeIndex & CHUNK_MASK; 462 setChunkIndex(fNodePrevSib, lastAttrNodeIndex, attrChunk, attrIndex); 464 } 465 setChunkIndex(fNodeExtra, attrNodeIndex, elementChunk, elementIndex); 467 468 int extra = getChunkIndex(fNodeExtra, attrChunk, attrIndex); 469 if (id) { 470 extra = extra | ID; 471 setChunkIndex(fNodeExtra, extra, attrChunk, attrIndex); 472 String value = getChunkValue(fNodeValue, attrChunk, attrIndex); 473 putIdentifier(value, elementNodeIndex); 474 } 475 if (type != null) { 477 int extraDataIndex = createNode(DeferredNode.TYPE_NODE); 478 int echunk = extraDataIndex >> CHUNK_SHIFT; 479 int eindex = extraDataIndex & CHUNK_MASK; 480 481 setChunkIndex(fNodeLastChild, extraDataIndex, attrChunk, attrIndex); 482 setChunkValue(fNodeValue, type, echunk, eindex); 483 } 484 485 return attrNodeIndex; 487 } 488 489 490 public int setDeferredAttribute(int elementNodeIndex, 491 String attrName, String attrURI, 492 String attrValue, boolean specified) { 493 int attrNodeIndex = createDeferredAttribute(attrName, attrURI, 495 attrValue, specified); 496 int attrChunk = attrNodeIndex >> CHUNK_SHIFT; 497 int attrIndex = attrNodeIndex & CHUNK_MASK; 498 setChunkIndex(fNodeParent, elementNodeIndex, attrChunk, attrIndex); 500 501 int elementChunk = elementNodeIndex >> CHUNK_SHIFT; 502 int elementIndex = elementNodeIndex & CHUNK_MASK; 503 504 int lastAttrNodeIndex = getChunkIndex(fNodeExtra, 506 elementChunk, elementIndex); 507 if (lastAttrNodeIndex != 0) { 508 int lastAttrChunk = lastAttrNodeIndex >> CHUNK_SHIFT; 509 int lastAttrIndex = lastAttrNodeIndex & CHUNK_MASK; 510 setChunkIndex(fNodePrevSib, lastAttrNodeIndex, 512 attrChunk, attrIndex); 513 } 514 setChunkIndex(fNodeExtra, attrNodeIndex, 516 elementChunk, elementIndex); 517 518 return attrNodeIndex; 520 521 } 523 524 public int createDeferredAttribute(String attrName, String attrValue, 525 boolean specified) { 526 return createDeferredAttribute(attrName, null, attrValue, specified); 527 } 528 529 530 public int createDeferredAttribute(String attrName, String attrURI, 531 String attrValue, boolean specified) { 532 533 int nodeIndex = createNode(NodeImpl.ATTRIBUTE_NODE); 535 int chunk = nodeIndex >> CHUNK_SHIFT; 536 int index = nodeIndex & CHUNK_MASK; 537 setChunkValue(fNodeName, attrName, chunk, index); 538 setChunkValue(fNodeURI, attrURI, chunk, index); 539 setChunkValue(fNodeValue, attrValue, chunk, index); 540 int extra = specified ? SPECIFIED : 0; 541 setChunkIndex(fNodeExtra, extra, chunk, index); 542 543 return nodeIndex; 545 546 } 548 549 public int createDeferredElementDefinition(String elementName) { 550 551 int nodeIndex = createNode(NodeImpl.ELEMENT_DEFINITION_NODE); 553 int chunk = nodeIndex >> CHUNK_SHIFT; 554 int index = nodeIndex & CHUNK_MASK; 555 setChunkValue(fNodeName, elementName, chunk, index); 556 557 return nodeIndex; 559 560 } 562 563 public int createDeferredTextNode(String data, 564 boolean ignorableWhitespace) { 565 566 int nodeIndex = createNode(Node.TEXT_NODE); 568 int chunk = nodeIndex >> CHUNK_SHIFT; 569 int index = nodeIndex & CHUNK_MASK; 570 setChunkValue(fNodeValue, data, chunk, index); 571 setChunkIndex(fNodeExtra, ignorableWhitespace ? 1 : 0, chunk, index); 573 574 return nodeIndex; 576 577 } 579 580 public int createDeferredCDATASection(String data) { 581 582 int nodeIndex = createNode(Node.CDATA_SECTION_NODE); 584 int chunk = nodeIndex >> CHUNK_SHIFT; 585 int index = nodeIndex & CHUNK_MASK; 586 setChunkValue(fNodeValue, data, chunk, index); 587 588 return nodeIndex; 590 591 } 593 594 public int createDeferredProcessingInstruction(String target, 595 String data) { 596 int nodeIndex = createNode(Node.PROCESSING_INSTRUCTION_NODE); 598 int chunk = nodeIndex >> CHUNK_SHIFT; 599 int index = nodeIndex & CHUNK_MASK; 600 setChunkValue(fNodeName, target, chunk, index); 601 setChunkValue(fNodeValue, data, chunk, index); 602 return nodeIndex; 604 605 } 607 608 public int createDeferredComment(String data) { 609 610 int nodeIndex = createNode(Node.COMMENT_NODE); 612 int chunk = nodeIndex >> CHUNK_SHIFT; 613 int index = nodeIndex & CHUNK_MASK; 614 setChunkValue(fNodeValue, data, chunk, index); 615 616 return nodeIndex; 618 619 } 621 622 public int cloneNode(int nodeIndex, boolean deep) { 623 624 626 int nchunk = nodeIndex >> CHUNK_SHIFT; 627 int nindex = nodeIndex & CHUNK_MASK; 628 int nodeType = fNodeType[nchunk][nindex]; 629 int cloneIndex = createNode((short)nodeType); 630 int cchunk = cloneIndex >> CHUNK_SHIFT; 631 int cindex = cloneIndex & CHUNK_MASK; 632 setChunkValue(fNodeName, fNodeName[nchunk][nindex], cchunk, cindex); 633 setChunkValue(fNodeValue, fNodeValue[nchunk][nindex], cchunk, cindex); 634 setChunkValue(fNodeURI, fNodeURI[nchunk][nindex], cchunk, cindex); 635 int extraIndex = fNodeExtra[nchunk][nindex]; 636 if (extraIndex != -1) { 637 if (nodeType != Node.ATTRIBUTE_NODE && nodeType != Node.TEXT_NODE) { 638 extraIndex = cloneNode(extraIndex, false); 639 } 640 setChunkIndex(fNodeExtra, extraIndex, cchunk, cindex); 641 } 642 643 if (deep) { 645 int prevIndex = -1; 646 int childIndex = getLastChild(nodeIndex, false); 647 while (childIndex != -1) { 648 int clonedChildIndex = cloneNode(childIndex, deep); 649 insertBefore(cloneIndex, clonedChildIndex, prevIndex); 650 prevIndex = clonedChildIndex; 651 childIndex = getRealPrevSibling(childIndex, false); 652 } 653 654 655 } 656 657 return cloneIndex; 659 660 } 662 663 public void appendChild(int parentIndex, int childIndex) { 664 665 int pchunk = parentIndex >> CHUNK_SHIFT; 667 int pindex = parentIndex & CHUNK_MASK; 668 int cchunk = childIndex >> CHUNK_SHIFT; 669 int cindex = childIndex & CHUNK_MASK; 670 setChunkIndex(fNodeParent, parentIndex, cchunk, cindex); 671 672 int olast = getChunkIndex(fNodeLastChild, pchunk, pindex); 674 setChunkIndex(fNodePrevSib, olast, cchunk, cindex); 675 676 setChunkIndex(fNodeLastChild, childIndex, pchunk, pindex); 678 679 } 681 682 public int setAttributeNode(int elemIndex, int attrIndex) { 683 684 int echunk = elemIndex >> CHUNK_SHIFT; 685 int eindex = elemIndex & CHUNK_MASK; 686 int achunk = attrIndex >> CHUNK_SHIFT; 687 int aindex = attrIndex & CHUNK_MASK; 688 689 String attrName = getChunkValue(fNodeName, achunk, aindex); 691 int oldAttrIndex = getChunkIndex(fNodeExtra, echunk, eindex); 692 int nextIndex = -1; 693 int oachunk = -1; 694 int oaindex = -1; 695 while (oldAttrIndex != -1) { 696 oachunk = oldAttrIndex >> CHUNK_SHIFT; 697 oaindex = oldAttrIndex & CHUNK_MASK; 698 String oldAttrName = getChunkValue(fNodeName, oachunk, oaindex); 699 if (oldAttrName.equals(attrName)) { 700 break; 701 } 702 nextIndex = oldAttrIndex; 703 oldAttrIndex = getChunkIndex(fNodePrevSib, oachunk, oaindex); 704 } 705 706 if (oldAttrIndex != -1) { 708 709 int prevIndex = getChunkIndex(fNodePrevSib, oachunk, oaindex); 711 if (nextIndex == -1) { 712 setChunkIndex(fNodeExtra, prevIndex, echunk, eindex); 713 } 714 else { 715 int pchunk = nextIndex >> CHUNK_SHIFT; 716 int pindex = nextIndex & CHUNK_MASK; 717 setChunkIndex(fNodePrevSib, prevIndex, pchunk, pindex); 718 } 719 720 clearChunkIndex(fNodeType, oachunk, oaindex); 722 clearChunkValue(fNodeName, oachunk, oaindex); 723 clearChunkValue(fNodeValue, oachunk, oaindex); 724 clearChunkIndex(fNodeParent, oachunk, oaindex); 725 clearChunkIndex(fNodePrevSib, oachunk, oaindex); 726 int attrTextIndex = 727 clearChunkIndex(fNodeLastChild, oachunk, oaindex); 728 int atchunk = attrTextIndex >> CHUNK_SHIFT; 729 int atindex = attrTextIndex & CHUNK_MASK; 730 clearChunkIndex(fNodeType, atchunk, atindex); 731 clearChunkValue(fNodeValue, atchunk, atindex); 732 clearChunkIndex(fNodeParent, atchunk, atindex); 733 clearChunkIndex(fNodeLastChild, atchunk, atindex); 734 } 735 736 int prevIndex = getChunkIndex(fNodeExtra, echunk, eindex); 738 setChunkIndex(fNodeExtra, attrIndex, echunk, eindex); 739 setChunkIndex(fNodePrevSib, prevIndex, achunk, aindex); 740 741 return oldAttrIndex; 743 744 } 746 747 748 public void setIdAttributeNode(int elemIndex, int attrIndex) { 749 750 int chunk = attrIndex >> CHUNK_SHIFT; 751 int index = attrIndex & CHUNK_MASK; 752 int extra = getChunkIndex(fNodeExtra, chunk, index); 753 extra = extra | ID; 754 setChunkIndex(fNodeExtra, extra, chunk, index); 755 756 String value = getChunkValue(fNodeValue, chunk, index); 757 putIdentifier(value, elemIndex); 758 } 759 760 761 762 public void setIdAttribute(int attrIndex) { 763 764 int chunk = attrIndex >> CHUNK_SHIFT; 765 int index = attrIndex & CHUNK_MASK; 766 int extra = getChunkIndex(fNodeExtra, chunk, index); 767 extra = extra | ID; 768 setChunkIndex(fNodeExtra, extra, chunk, index); 769 } 770 771 772 public int insertBefore(int parentIndex, int newChildIndex, int refChildIndex) { 773 774 if (refChildIndex == -1) { 775 appendChild(parentIndex, newChildIndex); 776 return newChildIndex; 777 } 778 779 int nchunk = newChildIndex >> CHUNK_SHIFT; 780 int nindex = newChildIndex & CHUNK_MASK; 781 int rchunk = refChildIndex >> CHUNK_SHIFT; 782 int rindex = refChildIndex & CHUNK_MASK; 783 int previousIndex = getChunkIndex(fNodePrevSib, rchunk, rindex); 784 setChunkIndex(fNodePrevSib, newChildIndex, rchunk, rindex); 785 setChunkIndex(fNodePrevSib, previousIndex, nchunk, nindex); 786 787 return newChildIndex; 788 789 } 791 792 public void setAsLastChild(int parentIndex, int childIndex) { 793 794 int pchunk = parentIndex >> CHUNK_SHIFT; 795 int pindex = parentIndex & CHUNK_MASK; 796 int chunk = childIndex >> CHUNK_SHIFT; 797 int index = childIndex & CHUNK_MASK; 798 setChunkIndex(fNodeLastChild, childIndex, pchunk, pindex); 799 } 801 805 public int getParentNode(int nodeIndex) { 806 return getParentNode(nodeIndex, false); 807 } 808 809 813 public int getParentNode(int nodeIndex, boolean free) { 814 815 if (nodeIndex == -1) { 816 return -1; 817 } 818 819 int chunk = nodeIndex >> CHUNK_SHIFT; 820 int index = nodeIndex & CHUNK_MASK; 821 return free ? clearChunkIndex(fNodeParent, chunk, index) 822 : getChunkIndex(fNodeParent, chunk, index); 823 824 } 826 827 public int getLastChild(int nodeIndex) { 828 return getLastChild(nodeIndex, true); 829 } 830 831 835 public int getLastChild(int nodeIndex, boolean free) { 836 837 if (nodeIndex == -1) { 838 return -1; 839 } 840 841 int chunk = nodeIndex >> CHUNK_SHIFT; 842 int index = nodeIndex & CHUNK_MASK; 843 return free ? clearChunkIndex(fNodeLastChild, chunk, index) 844 : getChunkIndex(fNodeLastChild, chunk, index); 845 846 } 848 852 public int getPrevSibling(int nodeIndex) { 853 return getPrevSibling(nodeIndex, true); 854 } 855 856 860 public int getPrevSibling(int nodeIndex, boolean free) { 861 862 if (nodeIndex == -1) { 863 return -1; 864 } 865 866 int chunk = nodeIndex >> CHUNK_SHIFT; 867 int index = nodeIndex & CHUNK_MASK; 868 int type = getChunkIndex(fNodeType, chunk, index); 869 if (type == Node.TEXT_NODE) { 870 do { 871 nodeIndex = getChunkIndex(fNodePrevSib, chunk, index); 872 if (nodeIndex == -1) { 873 break; 874 } 875 chunk = nodeIndex >> CHUNK_SHIFT; 876 index = nodeIndex & CHUNK_MASK; 877 type = getChunkIndex(fNodeType, chunk, index); 878 } while (type == Node.TEXT_NODE); 879 } 880 else { 881 nodeIndex = getChunkIndex(fNodePrevSib, chunk, index); 882 } 883 884 return nodeIndex; 885 886 } 888 893 public int getRealPrevSibling(int nodeIndex) { 894 return getRealPrevSibling(nodeIndex, true); 895 } 896 897 901 public int getRealPrevSibling(int nodeIndex, boolean free) { 902 903 &n
|