1 16 19 package com.sun.org.apache.xml.internal.dtm.ref; 20 21 import com.sun.org.apache.xml.internal.dtm.*; 22 import com.sun.org.apache.xml.internal.utils.SuballocatedIntVector; 23 import com.sun.org.apache.xml.internal.utils.BoolStack; 24 25 import java.util.Vector ; 26 27 import javax.xml.transform.Source ; 28 29 import com.sun.org.apache.xml.internal.utils.XMLString; 30 import com.sun.org.apache.xml.internal.utils.XMLStringFactory; 31 32 import com.sun.org.apache.xml.internal.res.XMLMessages; 33 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 34 35 import java.io.*; 37 42 public abstract class DTMDefaultBase implements DTM 43 { 44 static boolean JJK_DEBUG=false; 45 46 50 public static final int ROOTNODE = 0; 51 52 56 protected int m_size = 0; 57 58 59 protected SuballocatedIntVector m_exptype; 60 61 62 protected SuballocatedIntVector m_firstch; 63 64 65 protected SuballocatedIntVector m_nextsib; 66 67 68 protected SuballocatedIntVector m_prevsib; 69 70 71 protected SuballocatedIntVector m_parent; 72 73 74 protected Vector m_namespaceDeclSets = null; 75 76 78 protected SuballocatedIntVector m_namespaceDeclSetElements = null; 79 80 86 protected int[][][] m_elemIndexes; 87 88 89 public static final int DEFAULT_BLOCKSIZE = 512; 91 92 public static final int DEFAULT_NUMBLOCKS = 32; 93 94 95 public static final int DEFAULT_NUMBLOCKS_SMALL = 4; 96 97 98 100 103 protected static final int NOTPROCESSED = DTM.NULL - 1; 104 105 108 109 public DTMManager m_mgr; 110 111 115 protected DTMManagerDefault m_mgrDefault=null; 116 117 118 120 protected SuballocatedIntVector m_dtmIdent; 121 122 125 127 128 protected String m_documentBaseURI; 129 130 133 protected DTMWSFilter m_wsfilter; 134 135 136 protected boolean m_shouldStripWS = false; 137 138 139 protected BoolStack m_shouldStripWhitespaceStack; 140 141 142 protected XMLStringFactory m_xstrf; 143 144 148 protected ExpandedNameTable m_expandedNameTable; 149 150 151 protected boolean m_indexing; 152 153 166 public DTMDefaultBase(DTMManager mgr, Source source, int dtmIdentity, 167 DTMWSFilter whiteSpaceFilter, 168 XMLStringFactory xstringfactory, boolean doIndexing) 169 { 170 this(mgr, source, dtmIdentity, whiteSpaceFilter, xstringfactory, 171 doIndexing, DEFAULT_BLOCKSIZE, true, false); 172 } 173 174 190 public DTMDefaultBase(DTMManager mgr, Source source, int dtmIdentity, 191 DTMWSFilter whiteSpaceFilter, 192 XMLStringFactory xstringfactory, boolean doIndexing, 193 int blocksize, boolean usePrevsib, 194 boolean newNameTable) 195 { 196 int numblocks; 199 if (blocksize <= 64) 200 { 201 numblocks = DEFAULT_NUMBLOCKS_SMALL; 202 m_dtmIdent= new SuballocatedIntVector(4, 1); 203 } 204 else 205 { 206 numblocks = DEFAULT_NUMBLOCKS; 207 m_dtmIdent= new SuballocatedIntVector(32); 208 } 209 210 m_exptype = new SuballocatedIntVector(blocksize, numblocks); 211 m_firstch = new SuballocatedIntVector(blocksize, numblocks); 212 m_nextsib = new SuballocatedIntVector(blocksize, numblocks); 213 m_parent = new SuballocatedIntVector(blocksize, numblocks); 214 215 if (usePrevsib) 219 m_prevsib = new SuballocatedIntVector(blocksize, numblocks); 220 221 m_mgr = mgr; 222 if(mgr instanceof DTMManagerDefault) 223 m_mgrDefault=(DTMManagerDefault)mgr; 224 225 m_documentBaseURI = (null != source) ? source.getSystemId() : null; 226 m_dtmIdent.setElementAt(dtmIdentity,0); 227 m_wsfilter = whiteSpaceFilter; 228 m_xstrf = xstringfactory; 229 m_indexing = doIndexing; 230 231 if (doIndexing) 232 { 233 m_expandedNameTable = new ExpandedNameTable(); 234 } 235 else 236 { 237 m_expandedNameTable = m_mgrDefault.getExpandedNameTable(this); 240 } 241 242 if (null != whiteSpaceFilter) 243 { 244 m_shouldStripWhitespaceStack = new BoolStack(); 245 246 pushShouldStripWhitespace(false); 247 } 248 } 249 250 256 protected void ensureSizeOfIndex(int namespaceID, int LocalNameID) 257 { 258 259 if (null == m_elemIndexes) 260 { 261 m_elemIndexes = new int[namespaceID + 20][][]; 262 } 263 else if (m_elemIndexes.length <= namespaceID) 264 { 265 int[][][] indexes = m_elemIndexes; 266 267 m_elemIndexes = new int[namespaceID + 20][][]; 268 269 System.arraycopy(indexes, 0, m_elemIndexes, 0, indexes.length); 270 } 271 272 int[][] localNameIndex = m_elemIndexes[namespaceID]; 273 274 if (null == localNameIndex) 275 { 276 localNameIndex = new int[LocalNameID + 100][]; 277 m_elemIndexes[namespaceID] = localNameIndex; 278 } 279 else if (localNameIndex.length <= LocalNameID) 280 { 281 int[][] indexes = localNameIndex; 282 283 localNameIndex = new int[LocalNameID + 100][]; 284 285 System.arraycopy(indexes, 0, localNameIndex, 0, indexes.length); 286 287 m_elemIndexes[namespaceID] = localNameIndex; 288 } 289 290 int[] elemHandles = localNameIndex[LocalNameID]; 291 292 if (null == elemHandles) 293 { 294 elemHandles = new int[128]; 295 localNameIndex[LocalNameID] = elemHandles; 296 elemHandles[0] = 1; 297 } 298 else if (elemHandles.length <= elemHandles[0] + 1) 299 { 300 int[] indexes = elemHandles; 301 302 elemHandles = new int[elemHandles[0] + 1024]; 303 304 System.arraycopy(indexes, 0, elemHandles, 0, indexes.length); 305 306 localNameIndex[LocalNameID] = elemHandles; 307 } 308 } 309 310 317 protected void indexNode(int expandedTypeID, int identity) 318 { 319 320 ExpandedNameTable ent = m_expandedNameTable; 321 short type = ent.getType(expandedTypeID); 322 323 if (DTM.ELEMENT_NODE == type) 324 { 325 int namespaceID = ent.getNamespaceID(expandedTypeID); 326 int localNameID = ent.getLocalNameID(expandedTypeID); 327 328 ensureSizeOfIndex(namespaceID, localNameID); 329 330 int[] index = m_elemIndexes[namespaceID][localNameID]; 331 332 index[index[0]] = identity; 333 334 index[0]++; 335 } 336 } 337 338 351 protected int findGTE(int[] list, int start, int len, int value) 352 { 353 354 int low = start; 355 int high = start + (len - 1); 356 int end = high; 357 358 while (low <= high) 359 { 360 int mid = (low + high) / 2; 361 int c = list[mid]; 362 363 if (c > value) 364 high = mid - 1; 365 else if (c < value) 366 low = mid + 1; 367 else 368 return mid; 369 } 370 371 return (low <= end && list[low] > value) ? low : -1; 372 } 373 374 385 int findElementFromIndex(int nsIndex, int lnIndex, int firstPotential) 386 { 387 388 int[][][] indexes = m_elemIndexes; 389 390 if (null != indexes && nsIndex < indexes.length) 391 { 392 int[][] lnIndexs = indexes[nsIndex]; 393 394 if (null != lnIndexs && lnIndex < lnIndexs.length) 395 { 396 int[] elems = lnIndexs[lnIndex]; 397 398 if (null != elems) 399 { 400 int pos = findGTE(elems, 1, elems[0], firstPotential); 401 402 if (pos > -1) 403 { 404 return elems[pos]; 405 } 406 } 407 } 408 } 409 410 return NOTPROCESSED; 411 } 412 413 420 protected abstract int getNextNodeIdentity(int identity); 421 422 428 protected abstract boolean nextNode(); 429 430 435 protected abstract int getNumberOfNodes(); 436 437 438 protected DTMAxisTraverser[] m_traversers; 439 440 452 459 protected short _type(int identity) 460 { 461 462 int info = _exptype(identity); 463 464 if (NULL != info) 465 return m_expandedNameTable.getType(info); 466 else 467 return NULL; 468 } 469 470 477 protected int _exptype(int identity) 478 { 479 if (identity == DTM.NULL) 480 return NULL; 481 while (identity>=m_size) 485 { 486 if (!nextNode() && identity >= m_size) 487 return NULL; 488 } 489 return m_exptype.elementAt(identity); 490 491 } 492 493 500 protected int _level(int identity) 501 { 502 while (identity>=m_size) 503 { 504 boolean isMore = nextNode(); 505 if (!isMore && identity >= m_size) 506 return NULL; 507 } 508 509 int i=0; 510 while(NULL != (identity=_parent(identity))) 511 ++i; 512 return i; 513 } 514 515 522 protected int _firstch(int identity) 523 { 524 525 int info = (identity >= m_size) ? NOTPROCESSED : m_firstch.elementAt(identity); 527 528 while (info == NOTPROCESSED) 532 { 533 boolean isMore = nextNode(); 534 535 if (identity >= m_size &&!isMore) 536 return NULL; 537 else 538 { 539 info = m_firstch.elementAt(identity); 540 if(info == NOTPROCESSED && !isMore) 541 return NULL; 542 } 543 } 544 545 return info; 546 } 547 548 555 protected int _nextsib(int identity) 556 { 557 int info = (identity >= m_size) ? NOTPROCESSED : m_nextsib.elementAt(identity); 559 560 while (info == NOTPROCESSED) 564 { 565 boolean isMore = nextNode(); 566 567 if (identity >= m_size &&!isMore) 568 return NULL; 569 else 570 { 571 info = m_nextsib.elementAt(identity); 572 if(info == NOTPROCESSED && !isMore) 573 return NULL; 574 } 575 } 576 577 return info; 578 } 579 580 587 protected int _prevsib(int identity) 588 { 589 590 if (identity < m_size) 591 return m_prevsib.elementAt(identity); 592 593 while (true) 597 { 598 boolean isMore = nextNode(); 599 600 if (identity >= m_size && !isMore) 601 return NULL; 602 else if (identity < m_size) 603 return m_prevsib.elementAt(identity); 604 } 605 } 606 607 614 protected int _parent(int identity) 615 { 616 617 if (identity < m_size) 618 return m_parent.elementAt(identity); 619 620 while (true) 624 { 625 boolean isMore = nextNode(); 626 627 if (identity >= m_size && !isMore) 628 return NULL; 629 else if (identity < m_size) 630 return m_parent.elementAt(identity); 631 } 632 } 633 634 637 public void dumpDTM(OutputStream os) 638 { 639 try 640 { 641 if(os==null) 642 { 643 File f = new File("DTMDump"+((Object )this).hashCode()+".txt"); 644 System.err.println("Dumping... "+f.getAbsolutePath()); 645 os=new FileOutputStream(f); 646 } 647 PrintStream ps = new PrintStream(os); 648 649 while (nextNode()){} 650 651 int nRecords = m_size; 652 653 ps.println("Total nodes: " + nRecords); 654 655 for (int index = 0; index < nRecords; ++index) 656 { 657 int i=makeNodeHandle(index); 658 ps.println("=========== index=" + index + " handle=" + i + " ==========="); 659 ps.println("NodeName: " + getNodeName(i)); 660 ps.println("NodeNameX: " + getNodeNameX(i)); 661 ps.println("LocalName: " + getLocalName(i)); 662 ps.println("NamespaceURI: " + getNamespaceURI(i)); 663 ps.println("Prefix: " + getPrefix(i)); 664 665 int exTypeID = _exptype(index); 666 667 ps.println("Expanded Type ID: " 668 + Integer.toHexString(exTypeID)); 669 670 int type = _type(index); 671 String typestring; 672 673 switch (type) 674 { 675 case DTM.ATTRIBUTE_NODE : 676 typestring = "ATTRIBUTE_NODE"; 677 break; 678 case DTM.CDATA_SECTION_NODE : 679 typestring = "CDATA_SECTION_NODE"; 680 break; 681 case DTM.COMMENT_NODE : 682 typestring = "COMMENT_NODE"; 683 break; 684 case DTM.DOCUMENT_FRAGMENT_NODE : 685 typestring = "DOCUMENT_FRAGMENT_NODE"; 686 break; 687 case DTM.DOCUMENT_NODE : 688 typestring = "DOCUMENT_NODE"; 689 break; 690 case DTM.DOCUMENT_TYPE_NODE : 691 typestring = "DOCUMENT_NODE"; 692 break; 693 case DTM.ELEMENT_NODE : 694 typestring = "ELEMENT_NODE"; 695 break; 696 case DTM.ENTITY_NODE : 697 typestring = "ENTITY_NODE"; 698 break; 699 case DTM.ENTITY_REFERENCE_NODE : 700 typestring = "ENTITY_REFERENCE_NODE"; 701 break; 702 case DTM.NAMESPACE_NODE : 703 typestring = "NAMESPACE_NODE"; 704 break; 705 case DTM.NOTATION_NODE : 706 typestring = "NOTATION_NODE"; 707 break; 708 case DTM.NULL : 709 typestring = "NULL"; 710 break; 711 case DTM.PROCESSING_INSTRUCTION_NODE : 712 typestring = "PROCESSING_INSTRUCTION_NODE"; 713 break; 714 case DTM.TEXT_NODE : 715 typestring = "TEXT_NODE"; 716 break; 717 default : 718 typestring = "Unknown!"; 719 break; 720 } 721 722 ps.println("Type: " + typestring); 723 724 int firstChild = _firstch(index); 725 726 if (DTM.NULL == firstChild) 727 ps.println("First child: DTM.NULL"); 728 else if (NOTPROCESSED == firstChild) 729 ps.println("First child: NOTPROCESSED"); 730 else 731 ps.println("First child: " + firstChild); 732 733 if (m_prevsib != null) 734 { 735 int prevSibling = _prevsib(index); 736 737 if (DTM.NULL == prevSibling) 738 ps.println("Prev sibling: DTM.NULL"); 739 else if (NOTPROCESSED == prevSibling) 740 ps.println("Prev sibling: NOTPROCESSED"); 741 else 742 ps.println("Prev sibling: " + prevSibling); 743 } 744 745 int nextSibling = _nextsib(index); 746 747 if (DTM.NULL == nextSibling) 748 ps.println("Next sibling: DTM.NULL"); 749 else if (NOTPROCESSED == nextSibling) 750 ps.println("Next sibling: NOTPROCESSED"); 751 else 752 ps.println("Next sibling: " + nextSibling); 753 754 int parent = _parent(index); 755 756 if (DTM.NULL == parent) 757 ps.println("Parent: DTM.NULL"); 758 else if (NOTPROCESSED == parent) 759 ps.println("Parent: NOTPROCESSED"); 760 else 761 ps.println("Parent: " + parent); 762 763 int level = _level(index); 764 765 ps.println("Level: " + level); 766 ps.println("Node Value: " + getNodeValue(i)); 767 ps.println("String Value: " + getStringValue(i)); 768 } 769 } 770 catch(IOException ioe) 771 { 772 ioe.printStackTrace(System.err); 773 System.exit(-1); 774 } 775 } 776 777 790 public String dumpNode(int nodeHandle) 791 { 792 if(nodeHandle==DTM.NULL) 793 return "[null]"; 794 795 String typestring; 796 switch (getNodeType(nodeHandle)) 797 { 798 case DTM.ATTRIBUTE_NODE : 799 typestring = "ATTR"; 800 break; 801 case DTM.CDATA_SECTION_NODE : 802 typestring = "CDATA"; 803 break; 804 case DTM.COMMENT_NODE : 805 typestring = "COMMENT"; 806 break; 807 case DTM.DOCUMENT_FRAGMENT_NODE : 808 typestring = "DOC_FRAG"; 809 break; 810 case DTM.DOCUMENT_NODE : 811 typestring = "DOC"; 812 break; 813 case DTM.DOCUMENT_TYPE_NODE : 814 typestring = "DOC_TYPE"; 815 break; 816 case DTM.ELEMENT_NODE : 817 typestring = "ELEMENT"; 818 break; 819 case DTM.ENTITY_NODE : 820 typestring = "ENTITY"; 821 break; 822 case DTM.ENTITY_REFERENCE_NODE : 823 typestring = "ENT_REF"; 824 break; 825 case DTM.NAMESPACE_NODE : 826 typestring = "NAMESPACE"; 827 break; 828 case DTM.NOTATION_NODE : 829 typestring = "NOTATION"; 830 break; 831 case DTM.NULL : 832 typestring = "null"; 833 break; 834 case DTM.PROCESSING_INSTRUCTION_NODE : 835 typestring = "PI"; 836 break; 837 case DTM.TEXT_NODE : 838 typestring = "TEXT"; 839 break; 840 default : 841 typestring = "Unknown!"; 842 break; 843 } 844 845 StringBuffer sb=new StringBuffer (); 846 sb.append("["+nodeHandle+": "+typestring+ 847 "(0x"+Integer.toHexString(getExpandedTypeID(nodeHandle))+") "+ 848 getNodeNameX(nodeHandle)+" {"+getNamespaceURI(nodeHandle)+"}"+ 849 "=\""+ getNodeValue(nodeHandle)+"\"]"); 850 return sb.toString(); 851 } 852 853 855 863 public void setFeature(String featureId, boolean state){} 864 865 867 879 public boolean hasChildNodes(int nodeHandle) 880 { 881 882 int identity = makeNodeIdentity(nodeHandle); 883 int firstChild = _firstch(identity); 884 885 return firstChild != DTM.NULL; 886 } 887 888 902 final public int makeNodeHandle(int nodeIdentity) 903 { 904 if(NULL==nodeIdentity) return NULL; 905 906 if(JJK_DEBUG && nodeIdentity>DTMManager.IDENT_NODE_DEFAULT) 907 System.err.println("GONK! (only useful in limited situations)"); 908 909 return m_dtmIdent.elementAt(nodeIdentity >>> DTMManager.IDENT_DTM_NODE_BITS) 910 + (nodeIdentity & DTMManager.IDENT_NODE_DEFAULT) ; 911 } 912 913 929 final public int makeNodeIdentity(int nodeHandle) 930 { 931 if(NULL==nodeHandle) return NULL; 932 933 if(m_mgrDefault!=null) 934 { 935 939 int whichDTMindex=nodeHandle>>>DTMManager.IDENT_DTM_NODE_BITS; 940 941 if(m_mgrDefault.m_dtms[whichDTMindex]!=this) 946 return NULL; 947 else 948 return 949 m_mgrDefault.m_dtm_offsets[whichDTMindex] 950 | (nodeHandle & DTMManager.IDENT_NODE_DEFAULT); 951 } 952 953 int whichDTMid=m_dtmIdent.indexOf(nodeHandle & DTMManager.IDENT_DTM_DEFAULT); 954 return (whichDTMid==NULL) 955 ? NULL 956 : (whichDTMid << DTMManager.IDENT_DTM_NODE_BITS) 957 + (nodeHandle & DTMManager.IDENT_NODE_DEFAULT); 958 } 959 960 961 969 public int getFirstChild(int nodeHandle) 970 { 971 972 int identity = makeNodeIdentity(nodeHandle); 973 int firstChild = _firstch(identity); 974 975 return makeNodeHandle(firstChild); 976 } 977 978 986 public int getTypedFirstChild(int nodeHandle, int nodeType) 987 { 988 989 int firstChild, eType; 990 if (nodeType < DTM.NTYPES) { 991 for (firstChild = _firstch(makeNodeIdentity(nodeHandle)); 992 firstChild != DTM.NULL; 993 firstChild = _nextsib(firstChild)) { 994 eType = _exptype(firstChild); 995 if (eType == nodeType 996 || (eType >= DTM.NTYPES 997 && m_expandedNameTable.getType(eType) == nodeType)) { 998 return makeNodeHandle(firstChild); 999 } 1000 } 1001 } else { 1002 for (firstChild = _firstch(makeNodeIdentity(nodeHandle)); 1003 firstChild != DTM.NULL; 1004 firstChild = _nextsib(firstChild)) { 1005 if (_exptype(firstChild) == nodeType) { 1006 return makeNodeHandle(firstChild); 1007 } 1008 } 1009 } 1010 return DTM.NULL; 1011 } 1012 1013 1022 public int getLastChild(int nodeHandle) 1023 { 1024 1025 int identity = makeNodeIdentity(nodeHandle); 1026 int child = _firstch(identity); 1027 int lastChild = DTM.NULL; 1028 1029 while (child != DTM.NULL) 1030 { 1031 lastChild = child; 1032 child = _nextsib(child); 1033 } 1034 1035 return makeNodeHandle(lastChild); 1036 } 1037 1038 1050 public abstract int getAttributeNode(int nodeHandle, String namespaceURI, 1051 String name); 1052 1053 1059 public int getFirstAttribute(int nodeHandle) 1060 { 1061 int nodeID = makeNodeIdentity(nodeHandle); 1062 1063 return makeNodeHandle(getFirstAttributeIdentity(nodeID)); 1064 } 1065 1066 1072 protected int getFirstAttributeIdentity(int identity) { 1073 int type = _type(identity); 1074 1075 if (DTM.ELEMENT_NODE == type) 1076 { 1077 while (DTM.NULL != (identity = getNextNodeIdentity(identity))) 1079 { 1080 1081 type = _type(identity); 1083 1084 if (type == DTM.ATTRIBUTE_NODE) 1085 { 1086 return identity; 1087 } 1088 else if (DTM.NAMESPACE_NODE != type) 1089 { 1090 break; 1091 } 1092 } 1093 } 1094 1095 return DTM.NULL; 1096 } 1097 1098 1107 protected int getTypedAttribute(int nodeHandle, int attType) { 1108 int type = getNodeType(nodeHandle); 1109 if (DTM.ELEMENT_NODE == type) { 1110 int identity = makeNodeIdentity(nodeHandle); 1111 1112 while (DTM.NULL != (identity = getNextNodeIdentity(identity))) 1113 { 1114 type = _type(identity); 1115 1116 if (type == DTM.ATTRIBUTE_NODE) 1117 { 1118 if (_exptype(identity) == attType) return makeNodeHandle(identity); 1119 } 1120 else if (DTM.NAMESPACE_NODE != type) 1121 { 1122 break; 1123 } 1124 } 1125 } 1126 1127 return DTM.NULL; 1128 } 1129 1130 1138 public int getNextSibling(int nodeHandle) 1139 { 1140 if (nodeHandle == DTM.NULL) 1141 return DTM.NULL; 1142 return makeNodeHandle(_nextsib(makeNodeIdentity(nodeHandle))); 1143 } 1144 1145 1153 public int getTypedNextSibling(int nodeHandle, int nodeType) 1154 { 1155 if (nodeHandle == DTM.NULL) 1156 return DTM.NULL; 1157 int node = makeNodeIdentity(nodeHandle); 1158 int eType; 1159 while ((node = _nextsib(node)) != DTM.NULL && 1160 ((eType = _exptype(node)) != nodeType && 1161 m_expandedNameTable.getType(eType)!= nodeType)); 1162 1164 return (node == DTM.NULL ? DTM.NULL : makeNodeHandle(node)); 1165 } 1166 1167 1176 public int getPreviousSibling(int nodeHandle) 1177 { 1178 if (nodeHandle == DTM.NULL) 1179 return DTM.NULL; 1180 1181 if (m_prevsib != null) 1182 return makeNodeHandle(_prevsib(makeNodeIdentity(nodeHandle))); 1183 else 1184 { 1185 int nodeID = makeNodeIdentity(nodeHandle); 1189 int parent = _parent(nodeID); 1190 int node = _firstch(parent); 1191 int result = DTM.NULL; 1192 while (node != nodeID) 1193 { 1194 result = node; 1195 node = _nextsib(node); 1196 } 1197 return makeNodeHandle(result); 1198 } 1199 } 1200 1201 1210 public int getNextAttribute(int nodeHandle) { 1211 int nodeID = makeNodeIdentity(nodeHandle); 1212 1213 if (_type(nodeID) == DTM.ATTRIBUTE_NODE) { 1214 return makeNodeHandle(getNextAttributeIdentity(nodeID)); 1215 } 1216 1217 return DTM.NULL; 1218 } 1219 1220 1230 protected int getNextAttributeIdentity(int identity) { 1231 while (DTM.NULL != (identity = getNextNodeIdentity(identity))) { 1233 int type = _type(identity); 1234 1235 if (type == DTM.ATTRIBUTE_NODE) { 1236 return identity; 1237 } else if (type != DTM.NAMESPACE_NODE) { 1238 break; 1239 } 1240 } 1241 1242 return DTM.NULL; 1243 } 1244 1245 1246 private Vector m_namespaceLists = null; 1248 1249 1262 protected void declareNamespaceInContext(int elementNodeIndex,int namespaceNodeIndex) 1263 { 1264 SuballocatedIntVector nsList=null; 1265 if(m_namespaceDeclSets==null) 1266 { 1267 1268 m_namespaceDeclSetElements=new SuballocatedIntVector(32); 1270 m_namespaceDeclSetElements.addElement(elementNodeIndex); 1271 m_namespaceDeclSets=new Vector(); 1272 nsList=new SuballocatedIntVector(32); 1273 m_namespaceDeclSets.addElement(nsList); 1274 } 1275 else 1276 { 1277 int last=m_namespaceDeclSetElements.size()-1; 1280 1281 if(last>=0 && elementNodeIndex==m_namespaceDeclSetElements.elementAt(last)) 1282 { 1283 nsList=(SuballocatedIntVector)m_namespaceDeclSets.elementAt(last); 1284 } 1285 } 1286 if(nsList==null) 1287 { 1288 m_namespaceDeclSetElements.addElement(elementNodeIndex); 1289 1290 SuballocatedIntVector inherited = 1291 findNamespaceContext(_parent(elementNodeIndex)); 1292 1293 if (inherited!=null) { 1294 int isize=inherited.size(); 1298 1299 nsList=new SuballocatedIntVector(Math.max(Math.min(isize+16,2048), 1302 32)); 1303 1304 for(int i=0;i<isize;++i) 1305 { 1306 nsList.addElement(inherited.elementAt(i)); 1307 } 1308 } else { 1309 nsList=new SuballocatedIntVector(32); 1310 } 1311 1312 m_namespaceDeclSets.addElement(nsList); 1313 } 1314 1315 int newEType=_exptype(namespaceNodeIndex); 1320 1321 for(int i=nsList.size()-1;i>=0;--i) 1322 { 1323 if(newEType==getExpandedTypeID(nsList.elementAt(i))) 1324 { 1325 nsList.setElementAt(makeNodeHandle(namespaceNodeIndex),i); 1326 return; 1327 } 1328 } 1329 nsList.addElement(makeNodeHandle(namespaceNodeIndex)); 1330 } 1331 1332 1339 protected SuballocatedIntVector findNamespaceContext(int elementNodeIndex) 1340 { 1341 if (null!=m_namespaceDeclSetElements) 1342 { 1343 int wouldBeAt=findInSortedSuballocatedIntVector(m_namespaceDeclSetElements, 1346 elementNodeIndex); 1347 if(wouldBeAt>=0) return (SuballocatedIntVector) m_namespaceDeclSets.elementAt(wouldBeAt); 1349 if(wouldBeAt == -1) return null; 1352 wouldBeAt=-1-wouldBeAt; 1355 1356 int candidate=m_namespaceDeclSetElements.elementAt(-- wouldBeAt); 1358 int ancestor=_parent(elementNodeIndex); 1359 1360 if (wouldBeAt == 0 && candidate < ancestor) { 1364 int rootHandle = getDocumentRoot(makeNodeHandle(elementNodeIndex)); 1365 int rootID = makeNodeIdentity(rootHandle); 1366 int uppermostNSCandidateID; 1367 1368 if (getNodeType(rootHandle) == DTM.DOCUMENT_NODE) { 1369 int ch = _firstch(rootID); 1370 uppermostNSCandidateID = (ch != DTM.NULL) ? ch : rootID; 1371 } else { 1372 uppermostNSCandidateID = rootID; 1373 } 1374 1375 if (candidate == uppermostNSCandidateID) { 1376 return (SuballocatedIntVector)m_namespaceDeclSets.elementAt(wouldBeAt); 1377 } 1378 } 1379 1380 while(wouldBeAt>=0 && ancestor>0) 1381 { 1382 if (candidate==ancestor) { 1383 return (SuballocatedIntVector)m_namespaceDeclSets.elementAt(wouldBeAt); 1385 } else if (candidate<ancestor) { 1386 do { 1388 ancestor=_parent(ancestor); 1389 } while (candidate < ancestor); 1390 } else if(wouldBeAt > 0){ 1391 candidate=m_namespaceDeclSetElements.elementAt(--wouldBeAt); 1393 } 1394 else 1395 break; 1396 } 1397 } 1398 1399 return null; } 1401 1402 1417 protected int findInSortedSuballocatedIntVector(SuballocatedIntVector vector, int lookfor) 1418 { 1419 int i = 0; 1421 if(vector != null) { 1422 int first = 0; 1423 int last = vector.size() - 1; 1424 1425 while (first <= last) { 1426 i = (first + last) / 2; 1427 int test = lookfor-vector.elementAt(i); 1428 if(test == 0) { 1429 return i; } 1431 else if (test < 0) { 1432 last = i - 1; } 1434 else { 1435 first = i + 1; } 1437 } 1438 1439 if (first > i) { 1440 i = first; } 1442 } 1443 1444 return -1 - i; } 1446 1447 1448 1461 public int getFirstNamespaceNode(int nodeHandle, boolean inScope) 1462 { 1463 if(inScope) 1464 { 1465 int identity = makeNodeIdentity(nodeHandle); 1466 if (_type(identity) == DTM.ELEMENT_NODE) 1467 { 1468 SuballocatedIntVector nsContext=findNamespaceContext(identity); 1469 if(nsContext==null || nsContext.size()<1) 1470 return NULL; 1471 1472 return nsContext.elementAt(0); 1473 } 1474 else 1475 return NULL; 1476 } 1477 else 1478 { 1479 int identity = makeNodeIdentity(nodeHandle); 1486 if (_type(identity) == DTM.ELEMENT_NODE) 1487 { 1488 while (DTM.NULL != (identity = getNextNodeIdentity(identity))) 1489 { 1490 int type = _type(identity); 1491 if (type == DTM.NAMESPACE_NODE) 1492 return makeNodeHandle(identity); 1493 else if (DTM.ATTRIBUTE_NODE != type) 1494 break; 1495 } 1496 return NULL; 1497 } 1498 else 1499 return NULL; 1500 } 1501 } 1502 1503 1515 public int getNextNamespaceNode(int baseHandle, int nodeHandle, 1516 boolean inScope) 1517 { 1518 if(inScope) 1519 { 1520 1525 SuballocatedIntVector nsContext=findNamespaceContext(makeNodeIdentity(baseHandle)); 1526 1527 if(nsContext==null) 1528 return NULL; 1529 int i=1 + nsContext.indexOf(nodeHandle); 1530 if(i<=0 || i==nsContext.size()) 1531 return NULL; 1532 1533 return nsContext.elementAt(i); 1534 } 1535 else 1536 { 1537 int identity = makeNodeIdentity(nodeHandle); 1539 while (DTM.NULL != (identity = getNextNodeIdentity(identity))) 1540 { 1541 int type = _type(identity); 1542 if (type == DTM.NAMESPACE_NODE) 1543 { 1544 return makeNodeHandle(identity); 1545 } 1546 else if (type != DTM.ATTRIBUTE_NODE) 1547 { 1548 break; 1549 } 1550 } 1551 } 1552 return DTM.NULL; 1553 } 1554 1555 1562 public int getParent(int nodeHandle) 1563 { 1564 1565 int identity = makeNodeIdentity(nodeHandle); 1566 1567 if (identity > 0) 1568 return makeNodeHandle(_parent(identity)); 1569 else 1570 return DTM.NULL; 1571 } 1572 1573 1582 public int getDocument() 1583 { 1584 return m_dtmIdent.elementAt(0); } 1586 1587 1599 public int getOwnerDocument(int nodeHandle) 1600 { 1601 1602 if (DTM.DOCUMENT_NODE == getNodeType(nodeHandle)) 1603 return DTM.NULL; 1604 1605 return getDocumentRoot(nodeHandle); 1606 } 1607 1608 1616 public int getDocumentRoot(int nodeHandle) 1617 { 1618 return getDocument(); 1619 } 1620 1621 1630 public abstract XMLString getStringValue(int nodeHandle); 1631 1632 1644 public int getStringValueChunkCount(int nodeHandle) 1645 { 1646 1647 error(XMLMessages.createXMLMessage(XMLErrorResources.ER_METHOD_NOT_SUPPORTED, null)); 1650 return 0; 1651 } 1652 1653 1666 public char[] getStringValueChunk(int nodeHandle, int chunkIndex, 1667 int[] startAndLen) 1668 { 1669 1670 error(XMLMessages.createXMLMessage(XMLErrorResources.ER_METHOD_NOT_SUPPORTED, null)); 1673 return null; 1674 } 1675 1676 1683 public int getExpandedTypeID(int nodeHandle) 1684 { 1685 int id=makeNodeIdentity(nodeHandle); 1688 if(id==NULL) 1689 return NULL; 1690 return _exptype(id); 1691 } 1692 1693 1710 public int getExpandedTypeID(String namespace, String localName, int type) 1711 { 1712 1713 ExpandedNameTable ent = m_expandedNameTable; 1714 1715 return ent.getExpandedTypeID(namespace, localName, type); 1716 } 1717 1718 1724 public String getLocalNameFromExpandedNameID(int expandedNameID) 1725 { 1726 return m_expandedNameTable.getLocalName(expandedNameID); 1727 } 1728 1729 1736 public String getNamespaceFromExpandedNameID(int expandedNameID) 1737 { 1738 return m_expandedNameTable.getNamespace(expandedNameID); 1739 } 1740 1741 1746 public int getNamespaceType(final int nodeHandle) 1747 { 1748 1749 int identity = makeNodeIdentity(nodeHandle); 1750 int expandedNameID = _exptype(identity); 1751 1752 return m_expandedNameTable.getNamespaceID(expandedNameID); 1753 } 1754 1755 1764 public abstract String getNodeName(int nodeHandle); 1765 1766 1774 public String getNodeNameX(int nodeHandle) 1775 { 1776 1777 1778 error(XMLMessages.createXMLMessage(XMLErrorResources.ER_METHOD_NOT_SUPPORTED, null)); 1780 return null; 1781 } 1782 1783 1791 public abstract String getLocalName(int nodeHandle); 1792 1793 1805 public abstract String getPrefix(int nodeHandle); 1806 1807 1818 public abstract String getNamespaceURI(int nodeHandle); 1819 1820 1829 public abstract String getNodeValue(int nodeHandle); 1830 1831 1840 public short getNodeType(int nodeHandle) 1841 { 1842 if (nodeHandle == DTM.NULL) 1843 return DTM.NULL; 1844 return m_expandedNameTable.getType(_exptype(makeNodeIdentity(nodeHandle))); 1845 } 1846 1847 1855 public short getLevel(int nodeHandle) 1856 { 1857 int identity = makeNodeIdentity(nodeHandle); 1859 return (short) (_level(identity) + 1); 1860 } 1861 1862 1869 public int getNodeIdent(int nodeHandle) 1870 { 1871 1875 1876 return makeNodeIdentity(nodeHandle); 1877 } 1878 1879 1886 public int getNodeHandle(int nodeId) 1887 { 1888 1892 1893 return makeNodeHandle(nodeId); 1894 } 1895 1896 1898 1911 public boolean isSupported(String feature, String version) 1912 { 1913 1914 return false; 1916 } 1917 1918 1925 public String getDocumentBaseURI() 1926 { 1927 return m_documentBaseURI; 1928 } 1929 1930 1935 public void setDocumentBaseURI(String baseURI) 1936 { 1937 m_documentBaseURI = baseURI; 1938 } 1939 1940 1947 public String getDocumentSystemIdentifier(int nodeHandle) 1948 { 1949 1950 return m_documentBaseURI; 1952 } 1953 1954 1962 public String getDocumentEncoding(int nodeHandle) 1963 { 1964 1965 return "UTF-8"; 1967 } 1968 1969 1979 public String getDocumentStandalone(int nodeHandle) 1980 { 1981 return null; 1982 } 1983 1984 1994 public String getDocumentVersion(int documentHandle) 1995 { 1996 return null; 1997 } 1998 1999 2009 public boolean getDocumentAllDeclarationsProcessed() 2010 { 2011 2012 return true; 2014 } 2015 2016 2024 public abstract String getDocumentTypeDeclarationSystemIdentifier(); 2025 2026 2036 public abstract String getDocumentTypeDeclarationPublicIdentifier(); 2037 2038 2055 public abstract int getElementById(String elementId); 2056 2057 2091 public abstract String getUnparsedEntityURI(String name); 2092 2093 2095 2101 public boolean supportsPreStripping() 2102 { 2103 return true; 2104 } 2105 2106 2122 public boolean isNodeAfter(int nodeHandle1, int nodeHandle2) 2123 { 2124 int index1 = makeNodeIdentity(nodeHandle1); 2126 int index2 = makeNodeIdentity(nodeHandle2); 2127 2128 return index1!=NULL & index2!=NULL & index1 <= index2; 2129 } 2130 2131 2147 public boolean isCharacterElementContentWhitespace(int nodeHandle) 2148 { 2149 2150 return false; 2152 } 2153 2154 2168 public boolean isDocumentAllDeclarationsProcessed(int documentHandle) 2169 { 2170 return true; 2171 } 2172 2173 2183 public abstract boolean isAttributeSpecified(int attributeHandle); 2184 2185 2187 2204 public abstract void dispatchCharactersEvents( 2205 int nodeHandle, org.xml.sax.ContentHandler ch, boolean normalize) 2206 throws org.xml.sax.SAXException ; 2207 2208 2216 public abstract void dispatchToEvents( 2217 int nodeHandle, org.xml.sax.ContentHandler ch) 2218 throws org.xml.sax.SAXException ; 2219 2220 2227 public org.w3c.dom.Node getNode(int nodeHandle) 2228 { 2229 return new DTMNodeProxy(this, nodeHandle); 2230 } 2231 2232 2234 2246 public void appendChild(int newChild, boolean clone, boolean cloneDepth) 2247 { 2248 error(XMLMessages.createXMLMessage(XMLErrorResources.ER_METHOD_NOT_SUPPORTED, null)); } 2250 2251 2260 public void appendTextChild(String str) 2261 { 2262 error(XMLMessages.createXMLMessage(XMLErrorResources.ER_METHOD_NOT_SUPPORTED, null)); } 2264 2265 2270 protected void error(String msg) 2271 { 2272 throw new DTMException(msg); 2273 } 2274 2275 2281 protected boolean getShouldStripWhitespace() 2282 { 2283 return m_shouldStripWS; 2284 } 2285 2286 2292 protected void pushShouldStripWhitespace(boolean shouldStrip) 2293 { 2294 2295 m_shouldStripWS = shouldStrip; 2296 2297 if (null != m_shouldStripWhitespaceStack) 2298 m_shouldStripWhitespaceStack.push(shouldStrip); 2299 } 2300 2301 2306 protected void popShouldStripWhitespace() 2307 { 2308 if (null != m_shouldStripWhitespaceStack) 2309 m_shouldStripWS = m_shouldStripWhitespaceStack.popAndTop(); 2310 } 2311 2312 2319 protected void setShouldStripWhitespace(boolean shouldStrip) 2320 { 2321 2322 m_shouldStripWS = shouldStrip; 2323 2324 if (null != m_shouldStripWhitespaceStack) 2325 m_shouldStripWhitespaceStack.setTop(shouldStrip); 2326 } 2327 2328 2333 public void documentRegistration() 2334 { 2335 } 2336 2337 2342 public void documentRelease() 2343 { 2344 } 2345 2346 2353 public void migrateTo(DTMManager mgr) 2354 { 2355 m_mgr = mgr; 2356 if(mgr instanceof DTMManagerDefault) 2357 m_mgrDefault=(DTMManagerDefault)mgr; 2358 } 2359 2360 2366 public DTMManager getManager() 2367 { 2368 return m_mgr; 2369 } 2370 2371 2377 public SuballocatedIntVector getDTMIDs() 2378 { 2379 if(m_mgr==null) return null; 2380 return m_dtmIdent; 2381 } 2382} 2383 | Popular Tags |