1 16 19 package org.apache.xml.dtm.ref.dom2dtm; 20 21 import java.util.Vector ; 22 23 import javax.xml.transform.SourceLocator ; 24 import javax.xml.transform.dom.DOMSource ; 25 26 import org.apache.xml.dtm.DTM; 27 import org.apache.xml.dtm.DTMManager; 28 import org.apache.xml.dtm.DTMWSFilter; 29 import org.apache.xml.dtm.ref.DTMDefaultBaseIterators; 30 import org.apache.xml.dtm.ref.DTMManagerDefault; 31 import org.apache.xml.dtm.ref.ExpandedNameTable; 32 import org.apache.xml.dtm.ref.IncrementalSAXSource; 33 import org.apache.xml.res.XMLErrorResources; 34 import org.apache.xml.res.XMLMessages; 35 import org.apache.xml.utils.FastStringBuffer; 36 import org.apache.xml.utils.QName; 37 import org.apache.xml.utils.StringBufferPool; 38 import org.apache.xml.utils.TreeWalker; 39 import org.apache.xml.utils.XMLCharacterRecognizer; 40 import org.apache.xml.utils.XMLString; 41 import org.apache.xml.utils.XMLStringFactory; 42 import org.w3c.dom.Attr ; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.DocumentType ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.Entity ; 47 import org.w3c.dom.NamedNodeMap ; 48 import org.w3c.dom.Node ; 49 import org.xml.sax.ContentHandler ; 50 51 64 public class DOM2DTM extends DTMDefaultBaseIterators 65 { 66 static final boolean JJK_DEBUG=false; 67 static final boolean JJK_NEWCODE=true; 68 69 71 static final String NAMESPACE_DECL_NS="http://www.w3.org/XML/1998/namespace"; 72 73 75 transient private Node m_pos; 76 77 private int m_last_parent=0; 78 80 private int m_last_kid=NULL; 81 82 85 transient private Node m_root; 86 87 89 boolean m_processedFirstElement=false; 90 91 94 transient private boolean m_nodesAreProcessed; 95 96 102 protected Vector m_nodes = new Vector (); 103 104 116 public DOM2DTM(DTMManager mgr, DOMSource domSource, 117 int dtmIdentity, DTMWSFilter whiteSpaceFilter, 118 XMLStringFactory xstringfactory, 119 boolean doIndexing) 120 { 121 super(mgr, domSource, dtmIdentity, whiteSpaceFilter, 122 xstringfactory, doIndexing); 123 124 m_pos=m_root = domSource.getNode(); 126 m_last_parent=m_last_kid=NULL; 128 m_last_kid=addNode(m_root, m_last_parent,m_last_kid, NULL); 129 130 if(ELEMENT_NODE == m_root.getNodeType()) 141 { 142 NamedNodeMap attrs=m_root.getAttributes(); 143 int attrsize=(attrs==null) ? 0 : attrs.getLength(); 144 if(attrsize>0) 145 { 146 int attrIndex=NULL; for(int i=0;i<attrsize;++i) 148 { 149 attrIndex=addNode(attrs.item(i),0,attrIndex,NULL); 153 m_firstch.setElementAt(DTM.NULL,attrIndex); 154 } 155 m_nextsib.setElementAt(DTM.NULL,attrIndex); 158 159 } } 163 m_nodesAreProcessed = false; 165 } 166 167 179 protected int addNode(Node node, int parentIndex, 180 int previousSibling, int forceNodeType) 181 { 182 int nodeIndex = m_nodes.size(); 183 184 if(m_dtmIdent.size() == (nodeIndex>>>DTMManager.IDENT_DTM_NODE_BITS)) 186 { 187 try 188 { 189 if(m_mgr==null) 190 throw new ClassCastException (); 191 192 DTMManagerDefault mgrD=(DTMManagerDefault)m_mgr; 194 int id=mgrD.getFirstFreeDTMID(); 195 mgrD.addDTM(this,id,nodeIndex); 196 m_dtmIdent.addElement(id<<DTMManager.IDENT_DTM_NODE_BITS); 197 } 198 catch(ClassCastException e) 199 { 200 error(XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_DTMIDS_AVAIL, null)); } 205 } 206 207 m_size++; 208 210 int type; 211 if(NULL==forceNodeType) 212 type = node.getNodeType(); 213 else 214 type=forceNodeType; 215 216 if (Node.ATTRIBUTE_NODE == type) 234 { 235 String name = node.getNodeName(); 236 237 if (name.startsWith("xmlns:") || name.equals("xmlns")) 238 { 239 type = DTM.NAMESPACE_NODE; 240 } 241 } 242 243 m_nodes.addElement(node); 244 245 m_firstch.setElementAt(NOTPROCESSED,nodeIndex); 246 m_nextsib.setElementAt(NOTPROCESSED,nodeIndex); 247 m_prevsib.setElementAt(previousSibling,nodeIndex); 248 m_parent.setElementAt(parentIndex,nodeIndex); 249 250 if(DTM.NULL != parentIndex && 251 type != DTM.ATTRIBUTE_NODE && 252 type != DTM.NAMESPACE_NODE) 253 { 254 if(NOTPROCESSED == m_firstch.elementAt(parentIndex)) 256 m_firstch.setElementAt(nodeIndex,parentIndex); 257 } 258 259 String nsURI = node.getNamespaceURI(); 260 261 String localName = (type == Node.PROCESSING_INSTRUCTION_NODE) ? 265 node.getNodeName() : 266 node.getLocalName(); 267 268 if(((type == Node.ELEMENT_NODE) || (type == Node.ATTRIBUTE_NODE)) 270 && null == localName) 271 localName = node.getNodeName(); 273 ExpandedNameTable exnt = m_expandedNameTable; 274 275 if(node.getLocalName()==null && 284 (type==Node.ELEMENT_NODE || type==Node.ATTRIBUTE_NODE)) 285 { 286 } 288 289 int expandedNameID = (null != localName) 290 ? exnt.getExpandedTypeID(nsURI, localName, type) : 291 exnt.getExpandedTypeID(type); 292 293 m_exptype.setElementAt(expandedNameID,nodeIndex); 294 295 indexNode(expandedNameID, nodeIndex); 296 297 if (DTM.NULL != previousSibling) 298 m_nextsib.setElementAt(nodeIndex,previousSibling); 299 300 if (type == DTM.NAMESPACE_NODE) 303 declareNamespaceInContext(parentIndex,nodeIndex); 304 305 return nodeIndex; 306 } 307 308 311 public int getNumberOfNodes() 312 { 313 return m_nodes.size(); 314 } 315 316 324 protected boolean nextNode() 325 { 326 if (m_nodesAreProcessed) 332 return false; 333 334 Node pos=m_pos; 337 Node next=null; 338 int nexttype=NULL; 339 340 do 342 { 343 if (pos.hasChildNodes()) 345 { 346 next = pos.getFirstChild(); 347 348 if(next!=null && DOCUMENT_TYPE_NODE==next.getNodeType()) 351 next=next.getNextSibling(); 352 353 if(ENTITY_REFERENCE_NODE!=pos.getNodeType()) 356 { 357 m_last_parent=m_last_kid; 358 m_last_kid=NULL; 359 if(null != m_wsfilter) 361 { 362 short wsv = 363 m_wsfilter.getShouldStripSpace(makeNodeHandle(m_last_parent),this); 364 boolean shouldStrip = (DTMWSFilter.INHERIT == wsv) 365 ? getShouldStripWhitespace() 366 : (DTMWSFilter.STRIP == wsv); 367 pushShouldStripWhitespace(shouldStrip); 368 } } 370 } 371 372 else 374 { 375 if(m_last_kid!=NULL) 376 { 377 if(m_firstch.elementAt(m_last_kid)==NOTPROCESSED) 380 m_firstch.setElementAt(NULL,m_last_kid); 381 } 382 383 while(m_last_parent != NULL) 384 { 385 next = pos.getNextSibling(); 388 if(next!=null && DOCUMENT_TYPE_NODE==next.getNodeType()) 389 next=next.getNextSibling(); 390 391 if(next!=null) 392 break; 394 pos=pos.getParentNode(); 396 if(pos==null) 397 { 398 if(JJK_DEBUG) 400 { 401 System.out.println("***** DOM2DTM Pop Control Flow problem"); 402 for(;;); } 404 } 405 406 if(pos!=null && ENTITY_REFERENCE_NODE == pos.getNodeType()) 410 { 411 if(JJK_DEBUG) 413 System.out.println("***** DOM2DTM popping EntRef"); 414 } 415 else 416 { 417 popShouldStripWhitespace(); 418 if(m_last_kid==NULL) 420 m_firstch.setElementAt(NULL,m_last_parent); else 422 m_nextsib.setElementAt(NULL,m_last_kid); m_last_parent=m_parent.elementAt(m_last_kid=m_last_parent); 424 } 425 } 426 if(m_last_parent==NULL) 427 next=null; 428 } 429 430 if(next!=null) 431 nexttype=next.getNodeType(); 432 433 if (ENTITY_REFERENCE_NODE == nexttype) 439 pos=next; 440 } 441 while (ENTITY_REFERENCE_NODE == nexttype); 442 443 if(next==null) 445 { 446 m_nextsib.setElementAt(NULL,0); 447 m_nodesAreProcessed = true; 448 m_pos=null; 449 450 if(JJK_DEBUG) 451 { 452 System.out.println("***** DOM2DTM Crosscheck:"); 453 for(int i=0;i<m_nodes.size();++i) 454 System.out.println(i+":\t"+m_firstch.elementAt(i)+"\t"+m_nextsib.elementAt(i)); 455 } 456 457 return false; 458 } 459 460 475 boolean suppressNode=false; 476 Node lastTextNode=null; 477 478 nexttype=next.getNodeType(); 479 480 if(TEXT_NODE == nexttype || CDATA_SECTION_NODE == nexttype) 482 { 483 suppressNode=((null != m_wsfilter) && getShouldStripWhitespace()); 485 486 Node n=next; 489 while(n!=null) 490 { 491 lastTextNode=n; 492 if(TEXT_NODE == n.getNodeType()) 494 nexttype=TEXT_NODE; 495 suppressNode &= 498 XMLCharacterRecognizer.isWhiteSpace(n.getNodeValue()); 499 500 n=logicalNextDOMTextNode(n); 501 } 502 } 503 504 else if(PROCESSING_INSTRUCTION_NODE==nexttype) 510 { 511 suppressNode = (pos.getNodeName().toLowerCase().equals("xml")); 512 } 513 514 515 if(!suppressNode) 516 { 517 int nextindex=addNode(next,m_last_parent,m_last_kid, 521 nexttype); 522 523 m_last_kid=nextindex; 524 525 if(ELEMENT_NODE == nexttype) 526 { 527 int attrIndex=NULL; NamedNodeMap attrs=next.getAttributes(); 531 int attrsize=(attrs==null) ? 0 : attrs.getLength(); 532 if(attrsize>0) 533 { 534 for(int i=0;i<attrsize;++i) 535 { 536 attrIndex=addNode(attrs.item(i), 540 nextindex,attrIndex,NULL); 541 m_firstch.setElementAt(DTM.NULL,attrIndex); 542 543 if(!m_processedFirstElement 553 && "xmlns:xml".equals(attrs.item(i).getNodeName())) 554 m_processedFirstElement=true; 555 } 556 } if(!m_processedFirstElement) 560 { 561 attrIndex=addNode(new DOM2DTMdefaultNamespaceDeclarationNode( 568 (Element )next,"xml",NAMESPACE_DECL_NS, 569 makeNodeHandle(((attrIndex==NULL)?nextindex:attrIndex)+1) 570 ), 571 nextindex,attrIndex,NULL); 572 m_firstch.setElementAt(DTM.NULL,attrIndex); 573 m_processedFirstElement=true; 574 } 575 if(attrIndex!=NULL) 576 m_nextsib.setElementAt(DTM.NULL,attrIndex); 577 } } 580 if(TEXT_NODE == nexttype || CDATA_SECTION_NODE == nexttype) 582 { 583 585 next=lastTextNode; } 587 588 m_pos=next; 590 return true; 591 } 592 593 594 601 public Node getNode(int nodeHandle) 602 { 603 604 int identity = makeNodeIdentity(nodeHandle); 605 606 return (Node ) m_nodes.elementAt(identity); 607 } 608 609 616 protected Node lookupNode(int nodeIdentity) 617 { 618 return (Node ) m_nodes.elementAt(nodeIdentity); 619 } 620 621 628 protected int getNextNodeIdentity(int identity) 629 { 630 631 identity += 1; 632 633 if (identity >= m_nodes.size()) 634 { 635 if (!nextNode()) 636 identity = DTM.NULL; 637 } 638 639 return identity; 640 } 641 642 661 private int getHandleFromNode(Node node) 662 { 663 if (null != node) 664 { 665 int len = m_nodes.size(); 666 boolean isMore; 667 int i = 0; 668 do 669 { 670 for (; i < len; i++) 671 { 672 if (m_nodes.elementAt(i) == node) 673 return makeNodeHandle(i); 674 } 675 676 isMore = nextNode(); 677 678 len = m_nodes.size(); 679 680 } 681 while(isMore || i < len); 682 } 683 684 return DTM.NULL; 685 } 686 687 700 public int getHandleOfNode(Node node) 701 { 702 if (null != node) 703 { 704 if((m_root==node) || 708 (m_root.getNodeType()==DOCUMENT_NODE && 709 m_root==node.getOwnerDocument()) || 710 (m_root.getNodeType()!=DOCUMENT_NODE && 711 m_root.getOwnerDocument()==node.getOwnerDocument()) 712 ) 713 { 714 for(Node cursor=node; 720 cursor!=null; 721 cursor= 722 (cursor.getNodeType()!=ATTRIBUTE_NODE) 723 ? cursor.getParentNode() 724 : ((org.w3c.dom.Attr )cursor).getOwnerElement()) 725 { 726 if(cursor==m_root) 727 return getHandleFromNode(node); 729 } } } 733 return DTM.NULL; 734 } 735 736 748 public int getAttributeNode(int nodeHandle, String namespaceURI, 749 String name) 750 { 751 752 if (null == namespaceURI) 754 namespaceURI = ""; 755 756 int type = getNodeType(nodeHandle); 757 758 if (DTM.ELEMENT_NODE == type) 759 { 760 761 int identity = makeNodeIdentity(nodeHandle); 763 764 while (DTM.NULL != (identity = getNextNodeIdentity(identity))) 765 { 766 type = _type(identity); 768 769 if (type == DTM.ATTRIBUTE_NODE || type==DTM.NAMESPACE_NODE) 777 { 778 Node node = lookupNode(identity); 779 String nodeuri = node.getNamespaceURI(); 780 781 if (null == nodeuri) 782 nodeuri = ""; 783 784 String nodelocalname = node.getLocalName(); 785 786 if (nodeuri.equals(namespaceURI) && name.equals(nodelocalname)) 787 return makeNodeHandle(identity); 788 } 789 790 else { 792 break; 793 } 794 } 795 } 796 797 return DTM.NULL; 798 } 799 800 809 public XMLString getStringValue(int nodeHandle) 810 { 811 812 int type = getNodeType(nodeHandle); 813 Node node = getNode(nodeHandle); 814 if(DTM.ELEMENT_NODE == type || DTM.DOCUMENT_NODE == type 817 || DTM.DOCUMENT_FRAGMENT_NODE == type) 818 { 819 FastStringBuffer buf = StringBufferPool.get(); 820 String s; 821 822 try 823 { 824 getNodeData(node, buf); 825 826 s = (buf.length() > 0) ? buf.toString() : ""; 827 } 828 finally 829 { 830 StringBufferPool.free(buf); 831 } 832 833 return m_xstrf.newstr( s ); 834 } 835 else if(TEXT_NODE == type || CDATA_SECTION_NODE == type) 836 { 837 FastStringBuffer buf = StringBufferPool.get(); 845 while(node!=null) 846 { 847 buf.append(node.getNodeValue()); 848 node=logicalNextDOMTextNode(node); 849 } 850 String s=(buf.length() > 0) ? buf.toString() : ""; 851 StringBufferPool.free(buf); 852 return m_xstrf.newstr( s ); 853 } 854 else 855 return m_xstrf.newstr( node.getNodeValue() ); 856 } 857 858 865 public boolean isWhitespace(int nodeHandle) 866 { 867 int type = getNodeType(nodeHandle); 868 Node node = getNode(nodeHandle); 869 if(TEXT_NODE == type || CDATA_SECTION_NODE == type) 870 { 871 FastStringBuffer buf = StringBufferPool.get(); 879 while(node!=null) 880 { 881 buf.append(node.getNodeValue()); 882 node=logicalNextDOMTextNode(node); 883 } 884 boolean b = buf.isWhitespace(0, buf.length()); 885 StringBufferPool.free(buf); 886 return b; 887 } 888 return false; 889 } 890 891 913 protected static void getNodeData(Node node, FastStringBuffer buf) 914 { 915 916 switch (node.getNodeType()) 917 { 918 case Node.DOCUMENT_FRAGMENT_NODE : 919 case Node.DOCUMENT_NODE : 920 case Node.ELEMENT_NODE : 921 { 922 for (Node child = node.getFirstChild(); null != child; 923 child = child.getNextSibling()) 924 { 925 getNodeData(child, buf); 926 } 927 } 928 break; 929 case Node.TEXT_NODE : 930 case Node.CDATA_SECTION_NODE : 931 case Node.ATTRIBUTE_NODE : buf.append(node.getNodeValue()); 933 break; 934 case Node.PROCESSING_INSTRUCTION_NODE : 935 break; 937 default : 938 break; 940 } 941 } 942 943 952 public String getNodeName(int nodeHandle) 953 { 954 955 Node node = getNode(nodeHandle); 956 957 return node.getNodeName(); 959 } 960 961 969 public String getNodeNameX(int nodeHandle) 970 { 971 972 String name; 973 short type = getNodeType(nodeHandle); 974 975 switch (type) 976 { 977 case DTM.NAMESPACE_NODE : 978 { 979 Node node = getNode(nodeHandle); 980 981 name = node.getNodeName(); 983 if(name.startsWith("xmlns:")) 984 { 985 name = QName.getLocalPart(name); 986 } 987 else if(name.equals("xmlns")) 988 { 989 name = ""; 990 } 991 } 992 break; 993 case DTM.ATTRIBUTE_NODE : 994 case DTM.ELEMENT_NODE : 995 case DTM.ENTITY_REFERENCE_NODE : 996 case DTM.PROCESSING_INSTRUCTION_NODE : 997 { 998 Node node = getNode(nodeHandle); 999 1000 name = node.getNodeName(); 1002 } 1003 break; 1004 default : 1005 name = ""; 1006 } 1007 1008 return name; 1009 } 1010 1011 1019 public String getLocalName(int nodeHandle) 1020 { 1021 if(JJK_NEWCODE) 1022 { 1023 int id=makeNodeIdentity(nodeHandle); 1024 if(NULL==id) return null; 1025 Node newnode=(Node )m_nodes.elementAt(id); 1026 String newname=newnode.getLocalName(); 1027 if (null == newname) 1028 { 1029 String qname = newnode.getNodeName(); 1031 if('#'==qname.charAt(0)) 1032 { 1033 newname=""; 1036 } 1037 else 1038 { 1039 int index = qname.indexOf(':'); 1040 newname = (index < 0) ? qname : qname.substring(index + 1); 1041 } 1042 } 1043 return newname; 1044 } 1045 else 1046 { 1047 String name; 1048 short type = getNodeType(nodeHandle); 1049 switch (type) 1050 { 1051 case DTM.ATTRIBUTE_NODE : 1052 case DTM.ELEMENT_NODE : 1053 case DTM.ENTITY_REFERENCE_NODE : 1054 case DTM.NAMESPACE_NODE : 1055 case DTM.PROCESSING_INSTRUCTION_NODE : 1056 { 1057 Node node = getNode(nodeHandle); 1058 1059 name = node.getLocalName(); 1061 1062 if (null == name) 1063 { 1064 String qname = node.getNodeName(); 1065 int index = qname.indexOf(':'); 1066 1067 name = (index < 0) ? qname : qname.substring(index + 1); 1068 } 1069 } 1070 break; 1071 default : 1072 name = ""; 1073 } 1074 return name; 1075 } 1076 } 1077 1078 1090 public String getPrefix(int nodeHandle) 1091 { 1092 1093 String prefix; 1094 short type = getNodeType(nodeHandle); 1095 1096 switch (type) 1097 { 1098 case DTM.NAMESPACE_NODE : 1099 { 1100 Node node = getNode(nodeHandle); 1101 1102 String qname = node.getNodeName(); 1104 int index = qname.indexOf(':'); 1105 1106 prefix = (index < 0) ? "" : qname.substring(index + 1); 1107 } 1108 break; 1109 case DTM.ATTRIBUTE_NODE : 1110 case DTM.ELEMENT_NODE : 1111 { 1112 Node node = getNode(nodeHandle); 1113 1114 String qname = node.getNodeName(); 1116 int index = qname.indexOf(':'); 1117 1118 prefix = (index < 0) ? "" : qname.substring(0, index); 1119 } 1120 break; 1121 default : 1122 prefix = ""; 1123 } 1124 1125 return prefix; 1126 } 1127 1128 1139 public String getNamespaceURI(int nodeHandle) 1140 { 1141 if(JJK_NEWCODE) 1142 { 1143 int id=makeNodeIdentity(nodeHandle); 1144 if(id==NULL) return null; 1145 Node node=(Node )m_nodes.elementAt(id); 1146 return node.getNamespaceURI(); 1147 } 1148 else 1149 { 1150 String nsuri; 1151 short type = getNodeType(nodeHandle); 1152 1153 switch (type) 1154 { 1155 case DTM.ATTRIBUTE_NODE : 1156 case DTM.ELEMENT_NODE : 1157 case DTM.ENTITY_REFERENCE_NODE : 1158 case DTM.NAMESPACE_NODE : 1159 case DTM.PROCESSING_INSTRUCTION_NODE : 1160 { 1161 Node node = getNode(nodeHandle); 1162 1163 nsuri = node.getNamespaceURI(); 1165 1166 } 1168 break; 1169 default : 1170 nsuri = null; 1171 } 1172 1173 return nsuri; 1174 } 1175 1176 } 1177 1178 1185 private Node logicalNextDOMTextNode(Node n) 1186 { 1187 Node p=n.getNextSibling(); 1188 if(p==null) 1189 { 1190 for(n=n.getParentNode(); 1192 n!=null && ENTITY_REFERENCE_NODE == n.getNodeType(); 1193 n=n.getParentNode()) 1194 { 1195 p=n.getNextSibling(); 1196 if(p!=null) 1197 break; 1198 } 1199 } 1200 n=p; 1201 while(n!=null && ENTITY_REFERENCE_NODE == n.getNodeType()) 1202 { 1203 if(n.hasChildNodes()) 1205 n=n.getFirstChild(); 1206 else 1207 n=n.getNextSibling(); 1208 } 1209 if(n!=null) 1210 { 1211 int ntype=n.getNodeType(); 1213 if(TEXT_NODE != ntype && CDATA_SECTION_NODE != ntype) 1214 n=null; 1215 } 1216 return n; 1217 } 1218 1219 1228 public String getNodeValue(int nodeHandle) 1229 { 1230 int type = _exptype(makeNodeIdentity(nodeHandle)); 1234 type=(NULL != type) ? getNodeType(nodeHandle) : NULL; 1235 1236 if(TEXT_NODE!=type && CDATA_SECTION_NODE!=type) 1237 return getNode(nodeHandle).getNodeValue(); 1238 1239 Node node = getNode(nodeHandle); 1247 Node n=logicalNextDOMTextNode(node); 1248 if(n==null) 1249 return node.getNodeValue(); 1250 1251 FastStringBuffer buf = StringBufferPool.get(); 1252 buf.append(node.getNodeValue()); 1253 while(n!=null) 1254 { 1255 buf.append(n.getNodeValue()); 1256 n=logicalNextDOMTextNode(n); 1257 } 1258 String s = (buf.length() > 0) ? buf.toString() : ""; 1259 StringBufferPool.free(buf); 1260 return s; 1261 } 1262 1263 1271 public String getDocumentTypeDeclarationSystemIdentifier() 1272 { 1273 1274 Document doc; 1275 1276 if (m_root.getNodeType() == Node.DOCUMENT_NODE) 1277 doc = (Document ) m_root; 1278 else 1279 doc = m_root.getOwnerDocument(); 1280 1281 if (null != doc) 1282 { 1283 DocumentType dtd = doc.getDoctype(); 1284 1285 if (null != dtd) 1286 { 1287 return dtd.getSystemId(); 1288 } 1289 } 1290 1291 return null; 1292 } 1293 1294 1304 public String getDocumentTypeDeclarationPublicIdentifier() 1305 { 1306 1307 Document doc; 1308 1309 if (m_root.getNodeType() == Node.DOCUMENT_NODE) 1310 doc = (Document ) m_root; 1311 else 1312 doc = m_root.getOwnerDocument(); 1313 1314 if (null != doc) 1315 { 1316 DocumentType dtd = doc.getDoctype(); 1317 1318 if (null != dtd) 1319 { 1320 return dtd.getPublicId(); 1321 } 1322 } 1323 1324 return null; 1325 } 1326 1327 1344 public int getElementById(String elementId) 1345 { 1346 1347 Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE) 1348 ? (Document ) m_root : m_root.getOwnerDocument(); 1349 1350 if(null != doc) 1351 { 1352 Node elem = doc.getElementById(elementId); 1353 if(null != elem) 1354 { 1355 int elemHandle = getHandleFromNode(elem); 1356 1357 if(DTM.NULL == elemHandle) 1358 { 1359 int identity = m_nodes.size()-1; 1360 while (DTM.NULL != (identity = getNextNodeIdentity(identity))) 1361 { 1362 Node node = getNode(identity); 1363 if(node == elem) 1364 { 1365 elemHandle = getHandleFromNode(elem); 1366 break; 1367 } 1368 } 1369 } 1370 1371 return elemHandle; 1372 } 1373 1374 } 1375 return DTM.NULL; 1376 } 1377 1378 1412 public String getUnparsedEntityURI(String name) 1413 { 1414 1415 String url = ""; 1416 Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE) 1417 ? (Document ) m_root : m_root.getOwnerDocument(); 1418 1419 if (null != doc) 1420 { 1421 DocumentType doctype = doc.getDoctype(); 1422 1423 if (null != doctype) 1424 { 1425 NamedNodeMap entities = doctype.getEntities(); 1426 if(null == entities) 1427 return url; 1428 Entity entity = (Entity ) entities.getNamedItem(name); 1429 if(null == entity) 1430 return url; 1431 1432 String notationName = entity.getNotationName(); 1433 1434 if (null != notationName) { 1436 url = entity.getSystemId(); 1446 1447 if (null == url) 1448 { 1449 url = entity.getPublicId(); 1450 } 1451 else 1452 { 1453 } 1456 } 1457 } 1458 } 1459 1460 return url; 1461 } 1462 1463 1474 public boolean isAttributeSpecified(int attributeHandle) 1475 { 1476 int type = getNodeType(attributeHandle); 1477 1478 if (DTM.ATTRIBUTE_NODE == type) 1479 { 1480 Attr attr = (Attr )getNode(attributeHandle); 1481 return attr.getSpecified(); 1482 } 1483 return false; 1484 } 1485 1486 1492 public void setIncrementalSAXSource(IncrementalSAXSource source) 1493 { 1494 } 1495 1496 1505 public org.xml.sax.ContentHandler getContentHandler() 1506 { 1507 return null; 1508 } 1509 1510 1520 public org.xml.sax.ext.LexicalHandler getLexicalHandler() 1521 { 1522 1523 return null; 1524 } 1525 1526 1527 1532 public org.xml.sax.EntityResolver getEntityResolver() 1533 { 1534 1535 return null; 1536 } 1537 1538 1543 public org.xml.sax.DTDHandler getDTDHandler() 1544 { 1545 1546 return null; 1547 } 1548 1549 1554 public org.xml.sax.ErrorHandler getErrorHandler() 1555 { 1556 1557 return null; 1558 } 1559 1560 1565 public org.xml.sax.ext.DeclHandler getDeclHandler() 1566 { 1567 1568 return null; 1569 } 1570 1571 1576 public boolean needsTwoThreads() 1577 { 1578 return false; 1579 } 1580 1581 1583 1590 private static boolean isSpace(char ch) 1591 { 1592 return XMLCharacterRecognizer.isWhiteSpace(ch); } 1594 1595 1608 public void dispatchCharactersEvents( 1609 int nodeHandle, org.xml.sax.ContentHandler ch, 1610 boolean normalize) 1611 throws org.xml.sax.SAXException 1612 { 1613 if(normalize) 1614 { 1615 XMLString str = getStringValue(nodeHandle); 1616 str = str.fixWhiteSpace(true, true, false); 1617 str.dispatchCharactersEvents(ch); 1618 } 1619 else 1620 { 1621 int type = getNodeType(nodeHandle); 1622 Node node = getNode(nodeHandle); 1623 dispatchNodeData(node, ch, 0); 1624 if(TEXT_NODE == type || CDATA_SECTION_NODE == type) 1627 { 1628 while( null != (node=logicalNextDOMTextNode(node)) ) 1629 { 1630 dispatchNodeData(node, ch, 0); 1631 } 1632 } 1633 } 1634 } 1635 1636 1658 protected static void dispatchNodeData(Node node, 1659 org.xml.sax.ContentHandler ch, 1660 int depth) 1661 throws org.xml.sax.SAXException 1662 { 1663 1664 switch (node.getNodeType()) 1665 { 1666 case Node.DOCUMENT_FRAGMENT_NODE : 1667 case Node.DOCUMENT_NODE : 1668 case Node.ELEMENT_NODE : 1669 { 1670 for (Node child = node.getFirstChild(); null != child; 1671 child = child.getNextSibling()) 1672 { 1673 dispatchNodeData(child, ch, depth+1); 1674 } 1675 } 1676 break; 1677 case Node.PROCESSING_INSTRUCTION_NODE : case Node.COMMENT_NODE : 1679 if(0 != depth) 1680 break; 1681 case Node.TEXT_NODE : 1684 case Node.CDATA_SECTION_NODE : 1685 case Node.ATTRIBUTE_NODE : 1686 String str = node.getNodeValue(); 1687 if(ch instanceof CharacterNodeHandler) 1688 { 1689 ((CharacterNodeHandler)ch).characters(node); 1690 } 1691 else 1692 { 1693 ch.characters(str.toCharArray(), 0, str.length()); 1694 } 1695 break; 1696 default : 1700 break; 1702 } 1703 } 1704 1705 TreeWalker m_walker = new TreeWalker(null); 1706 1707 1715 public void dispatchToEvents(int nodeHandle, org.xml.sax.ContentHandler ch) 1716 throws org.xml.sax.SAXException 1717 { 1718 TreeWalker treeWalker = m_walker; 1719 ContentHandler prevCH = treeWalker.getContentHandler(); 1720 1721 if(null != prevCH) 1722 { 1723 treeWalker = new TreeWalker(null); 1724 } 1725 treeWalker.setContentHandler(ch); 1726 1727 try 1728 { 1729 Node node = getNode(nodeHandle); 1730 treeWalker.traverse(node); 1731 } 1732 finally 1733 { 1734 treeWalker.setContentHandler(null); 1735 } 1736 } 1737 1738 public interface CharacterNodeHandler 1739 { 1740 public void characters(Node node) 1741 throws org.xml.sax.SAXException ; 1742 } 1743 1744 1751 public void setProperty(String property, Object value) 1752 { 1753 } 1754 1755 1762 public SourceLocator getSourceLocatorFor(int node) 1763 { 1764 return null; 1765 } 1766 1767} 1768 1769 1770 | Popular Tags |