1 57 58 package org.enhydra.apache.xerces.parsers; 59 60 import java.util.Enumeration ; 61 import java.util.StringTokenizer ; 62 63 import org.enhydra.apache.xerces.dom.AttrImpl; 64 import org.enhydra.apache.xerces.dom.DeferredDocumentImpl; 65 import org.enhydra.apache.xerces.dom.DocumentImpl; 66 import org.enhydra.apache.xerces.dom.DocumentTypeImpl; 67 import org.enhydra.apache.xerces.dom.ElementDefinitionImpl; 68 import org.enhydra.apache.xerces.dom.EntityImpl; 69 import org.enhydra.apache.xerces.dom.NotationImpl; 70 import org.enhydra.apache.xerces.dom.TextImpl; 71 import org.enhydra.apache.xerces.framework.XMLAttrList; 72 import org.enhydra.apache.xerces.framework.XMLContentSpec; 73 import org.enhydra.apache.xerces.framework.XMLDocumentHandler; 74 import org.enhydra.apache.xerces.framework.XMLParser; 75 import org.enhydra.apache.xerces.readers.XMLEntityHandler; 76 import org.enhydra.apache.xerces.utils.QName; 77 import org.enhydra.apache.xerces.utils.StringPool; 78 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl; 79 import org.enhydra.apache.xerces.validators.common.XMLElementDecl; 80 import org.enhydra.apache.xerces.validators.schema.SchemaSymbols; 81 import org.enhydra.apache.xerces.validators.schema.XUtil; 82 import org.w3c.dom.Attr ; 83 import org.w3c.dom.Comment ; 84 import org.w3c.dom.Document ; 85 import org.w3c.dom.DocumentType ; 86 import org.w3c.dom.Element ; 87 import org.w3c.dom.Entity ; 88 import org.w3c.dom.EntityReference ; 89 import org.w3c.dom.NamedNodeMap ; 90 import org.w3c.dom.Node ; 91 import org.w3c.dom.ProcessingInstruction ; 92 import org.w3c.dom.Text ; 93 import org.xml.sax.SAXException ; 94 import org.xml.sax.SAXNotRecognizedException ; 95 import org.xml.sax.SAXNotSupportedException ; 96 97 103 public class DOMParser 104 extends XMLParser 105 implements XMLDocumentHandler 106 { 107 108 112 114 115 public static final String DEFAULT_DOCUMENT_CLASS_NAME = "org.enhydra.apache.xerces.dom.DocumentImpl"; 116 117 118 public static final String DEFAULT_DEFERRED_DOCUMENT_CLASS_NAME = "org.enhydra.apache.xerces.dom.DeferredDocumentImpl"; 119 120 122 123 private static final boolean DEBUG_ATTLIST_DECL = false; 124 125 127 128 private static final String RECOGNIZED_FEATURES[] = { 129 "http://apache.org/xml/features/dom/defer-node-expansion", 132 "http://apache.org/xml/features/dom/create-entity-ref-nodes", 133 "http://apache.org/xml/features/dom/include-ignorable-whitespace", 134 "http://apache.org/xml/features/domx/grammar-access", 136 }; 137 138 139 private static final String RECOGNIZED_PROPERTIES[] = { 140 "http://apache.org/xml/properties/dom/document-class-name", 143 "http://apache.org/xml/properties/dom/current-element-node", 144 }; 145 146 150 152 protected Document fDocument; 153 154 156 protected DeferredDocumentImpl fDeferredDocumentImpl; 157 protected int fDocumentIndex; 158 protected int fDocumentTypeIndex; 159 protected int fCurrentNodeIndex; 160 161 163 protected int fCurrentEntityName; protected int fCurrentEntityNode; 166 168 protected DocumentImpl fDocumentImpl; 169 protected DocumentType fDocumentType; 170 protected Node fCurrentElementNode; 171 172 174 protected boolean fInDTD; 175 protected boolean fWithinElement; 176 protected boolean fInCDATA; 177 178 private boolean fGrammarAccess; 180 181 183 private String fDocumentClassName; 186 private boolean fDeferNodeExpansion; 187 private boolean fCreateEntityReferenceNodes; 188 private boolean fIncludeIgnorableWhitespace; 189 190 192 protected int fAmpIndex; 193 protected int fLtIndex; 194 protected int fGtIndex; 195 protected int fAposIndex; 196 protected int fQuotIndex; 197 198 private boolean fSeenRootElement; 199 200 private boolean fStringPoolInUse; 201 202 private XMLAttrList fAttrList; 203 204 208 209 public DOMParser() { 210 211 initHandlers(false, this, this); 212 213 init(); 215 216 try { 218 setDocumentClassName(DEFAULT_DOCUMENT_CLASS_NAME); 219 setCreateEntityReferenceNodes(true); 220 setDeferNodeExpansion(true); 221 setIncludeIgnorableWhitespace(true); 222 } catch (SAXException e) { 223 throw new RuntimeException ("PAR001 Fatal error constructing DOMParser."); 224 } 225 226 } 228 232 234 235 public Document getDocument() { 236 return fDocument; 237 } 238 239 241 250 public String [] getFeaturesRecognized() { 251 252 String superRecognized[] = super.getFeaturesRecognized(); 254 String thisRecognized[] = RECOGNIZED_FEATURES; 255 256 int thisLength = thisRecognized.length; 258 if (thisLength == 0) { 259 return superRecognized; 260 } 261 int superLength = superRecognized.length; 262 if (superLength == 0) { 263 return thisRecognized; 264 } 265 266 String recognized[] = new String [superLength + thisLength]; 268 System.arraycopy(superRecognized, 0, recognized, 0, superLength); 269 System.arraycopy(thisRecognized, 0, recognized, superLength, thisLength); 270 return recognized; 271 272 } 274 283 public String [] getPropertiesRecognized() { 284 285 String superRecognized[] = super.getPropertiesRecognized(); 287 String thisRecognized[] = RECOGNIZED_PROPERTIES; 288 289 int thisLength = thisRecognized.length; 291 if (thisLength == 0) { 292 return superRecognized; 293 } 294 int superLength = superRecognized.length; 295 if (superLength == 0) { 296 return thisRecognized; 297 } 298 299 String recognized[] = new String [superLength + thisLength]; 301 System.arraycopy(superRecognized, 0, recognized, 0, superLength); 302 System.arraycopy(thisRecognized, 0, recognized, superLength, thisLength); 303 return recognized; 304 305 } 306 307 309 310 public void reset() throws Exception { 311 if (fStringPoolInUse) { 312 fStringPool = new StringPool(); 314 fStringPoolInUse = false; 315 } 316 super.reset(); 317 init(); 318 } 319 320 321 public void resetOrCopy() throws Exception { 322 super.resetOrCopy(); 323 init(); 324 } 325 326 330 332 336 protected void init() { 337 338 fDocument = null; 340 341 fDeferredDocumentImpl = null; 343 fDocumentIndex = -1; 344 fDocumentTypeIndex = -1; 345 fCurrentNodeIndex = -1; 346 347 fCurrentEntityNode = -1; 349 fCurrentEntityName = -1; 350 351 fDocumentImpl = null; 353 fDocumentType = null; 354 fCurrentElementNode = null; 355 356 fInDTD = false; 358 fWithinElement = false; 359 fInCDATA = false; 360 361 fAmpIndex = fStringPool.addSymbol("amp"); 363 fLtIndex = fStringPool.addSymbol("lt"); 364 fGtIndex = fStringPool.addSymbol("gt"); 365 fAposIndex = fStringPool.addSymbol("apos"); 366 fQuotIndex = fStringPool.addSymbol("quot"); 367 368 fSeenRootElement = false; 369 fStringPoolInUse = false; 370 371 fAttrList = new XMLAttrList(fStringPool); 372 373 } 375 377 384 protected void setDeferNodeExpansion(boolean deferNodeExpansion) 385 throws SAXNotRecognizedException , SAXNotSupportedException { 386 fDeferNodeExpansion = deferNodeExpansion; 387 } 388 389 395 protected boolean getDeferNodeExpansion() 396 throws SAXNotRecognizedException , SAXNotSupportedException { 397 return fDeferNodeExpansion; 398 } 399 400 415 protected void setCreateEntityReferenceNodes(boolean create) 416 throws SAXNotRecognizedException , SAXNotSupportedException { 417 fCreateEntityReferenceNodes = create; 418 } 419 420 426 public boolean getCreateEntityReferenceNodes() 427 throws SAXNotRecognizedException , SAXNotSupportedException { 428 return fCreateEntityReferenceNodes; 429 } 430 431 450 public void setIncludeIgnorableWhitespace(boolean include) 451 throws SAXNotRecognizedException , SAXNotSupportedException { 452 fIncludeIgnorableWhitespace = include; 453 } 454 455 461 public boolean getIncludeIgnorableWhitespace() 462 throws SAXNotRecognizedException , SAXNotSupportedException { 463 return fIncludeIgnorableWhitespace; 464 } 465 466 468 483 protected void setDocumentClassName(String documentClassName) 484 throws SAXNotRecognizedException , SAXNotSupportedException { 485 486 if (documentClassName == null) { 488 documentClassName = DEFAULT_DOCUMENT_CLASS_NAME; 489 } 490 491 try { 493 Class _class = Class.forName(documentClassName, true, getClass().getClassLoader()); 496 if (!Document .class.isAssignableFrom(_class)) { 499 throw new IllegalArgumentException ("PAR002 Class, \""+documentClassName+"\", is not of type org.w3c.dom.Document."+"\n"+documentClassName); 500 } 501 } 502 catch (ClassNotFoundException e) { 503 throw new IllegalArgumentException ("PAR003 Class, \""+documentClassName+"\", not found."+"\n"+documentClassName); 504 } 505 506 fDocumentClassName = documentClassName; 508 if (!documentClassName.equals(DEFAULT_DOCUMENT_CLASS_NAME)) { 509 setDeferNodeExpansion(false); 510 } 511 512 } 514 520 protected String getDocumentClassName() 521 throws SAXNotRecognizedException , SAXNotSupportedException { 522 return fDocumentClassName; 523 } 524 525 532 protected Element getCurrentElementNode() 533 throws SAXNotRecognizedException , SAXNotSupportedException { 534 535 if (fCurrentElementNode != null && 536 fCurrentElementNode.getNodeType() == Node.ELEMENT_NODE) { 537 return (Element )fCurrentElementNode; 538 } 539 return null; 540 541 } 543 547 561 public void setFeature(String featureId, boolean state) 562 throws SAXNotRecognizedException , SAXNotSupportedException { 563 564 568 if (featureId.startsWith(SAX2_FEATURES_PREFIX)) { 569 } 574 575 579 else if (featureId.startsWith(XERCES_FEATURES_PREFIX)) { 580 String feature = featureId.substring(XERCES_FEATURES_PREFIX.length()); 581 if (feature.equals("dom/defer-node-expansion")) { 592 if (fParseInProgress) { 593 throw new SAXNotSupportedException ("PAR004 Cannot setFeature("+featureId + "): parse is in progress."+"\n"+featureId); 594 } 595 setDeferNodeExpansion(state); 596 return; 597 } 598 if (feature.equals("dom/create-entity-ref-nodes")) { 609 setCreateEntityReferenceNodes(state); 610 return; 611 } 612 613 if (feature.equals("dom/include-ignorable-whitespace")) { 627 setIncludeIgnorableWhitespace(state); 628 return; 629 } 630 631 635 if (feature.equals("domx/grammar-access")) { 642 fGrammarAccess = state; 643 return; 644 } 645 646 } 650 651 super.setFeature(featureId, state); 655 656 } 658 670 public boolean getFeature(String featureId) 671 throws SAXNotRecognizedException , SAXNotSupportedException { 672 673 677 if (featureId.startsWith(SAX2_FEATURES_PREFIX)) { 678 } 683 684 688 else if (featureId.startsWith(XERCES_FEATURES_PREFIX)) { 689 String feature = featureId.substring(XERCES_FEATURES_PREFIX.length()); 690 if (feature.equals("dom/defer-node-expansion")) { 701 return getDeferNodeExpansion(); 702 } 703 else if (feature.equals("dom/create-entity-ref-nodes")) { 714 return getCreateEntityReferenceNodes(); 715 } 716 717 if (feature.equals("dom/include-ignorable-whitespace")) { 731 return getIncludeIgnorableWhitespace(); 732 } 733 734 738 if (feature.equals("domx/grammar-access")) { 745 return fGrammarAccess; 746 } 747 748 } 752 753 return super.getFeature(featureId); 757 758 } 760 775 public void setProperty(String propertyId, Object value) 776 throws SAXNotRecognizedException , SAXNotSupportedException { 777 778 782 if (propertyId.startsWith(XERCES_PROPERTIES_PREFIX)) { 783 String property = propertyId.substring(XERCES_PROPERTIES_PREFIX.length()); 784 if (property.equals("dom/current-element-node")) { 794 throw new SAXNotSupportedException ("PAR005 Property, \""+propertyId+"\" is read-only.\n"+propertyId); 795 } 796 else if (property.equals("dom/document-class-name")) { 802 if (value != null && !(value instanceof String )) { 803 throw new SAXNotSupportedException ("PAR006 Property value must be of type java.lang.String."); 804 } 805 setDocumentClassName((String )value); 806 return; 807 } 808 } 809 810 super.setProperty(propertyId, value); 814 815 } 817 831 public Object getProperty(String propertyId) 832 throws SAXNotRecognizedException , SAXNotSupportedException { 833 834 838 if (propertyId.startsWith(XERCES_PROPERTIES_PREFIX)) { 839 String property = propertyId.substring(XERCES_PROPERTIES_PREFIX.length()); 840 if (property.equals("dom/current-element-node")) { 850 boolean throwException = false; 851 try { 852 throwException = getFeature(XERCES_FEATURES_PREFIX+"dom/defer-node-expansion"); 853 } 854 catch (SAXNotSupportedException e) { 855 } 857 catch (SAXNotRecognizedException e) { 858 } 860 if (throwException) { 861 throw new SAXNotSupportedException ("PAR007 Current element node cannot be queried when node expansion is deferred."); 862 } 863 return getCurrentElementNode(); 864 } 865 else if (property.equals("dom/document-class-name")) { 871 return getDocumentClassName(); 872 } 873 } 874 875 return super.getProperty(propertyId); 879 880 } 882 886 887 public void startDocument() { 888 889 String documentClassName = null; 891 try { 892 documentClassName = getDocumentClassName(); 893 } catch (SAXException e) { 894 throw new RuntimeException ("PAR008 Fatal error getting document factory."); 895 } 896 boolean deferNodeExpansion = true; 897 try { 898 deferNodeExpansion = getDeferNodeExpansion(); 899 } catch (SAXException e) { 900 throw new RuntimeException ("PAR009 Fatal error reading expansion mode."); 901 } 902 try { 903 boolean isDocumentImpl = fDocumentClassName.equals(DEFAULT_DOCUMENT_CLASS_NAME); 904 boolean isDeferredImpl = fDocumentClassName.equals(DEFAULT_DEFERRED_DOCUMENT_CLASS_NAME); 905 if (deferNodeExpansion && (isDocumentImpl || isDeferredImpl)) { 906 boolean nsEnabled = false; 907 try { nsEnabled = getNamespaces(); } 908 catch (SAXException s) {} 909 fDeferredDocumentImpl = new DeferredDocumentImpl(fStringPool, nsEnabled, fGrammarAccess); 910 fStringPoolInUse = true; 911 fDocument = fDeferredDocumentImpl; 912 fDocumentIndex = fDeferredDocumentImpl.createDocument(); 913 fCurrentNodeIndex = fDocumentIndex; 914 } 915 916 else { 918 if (isDocumentImpl) { 919 fDocumentImpl = new DocumentImpl(fGrammarAccess); 920 fDocument = fDocumentImpl; 921 fDocumentImpl.setErrorChecking(false); 923 } 924 else { 925 Class documentClass = Class.forName(documentClassName, true, getClass().getClassLoader()); 928 try { 930 fDocument = (Document )documentClass.newInstance(); 931 } 932 catch (Exception e) { 933 throw new RuntimeException ( 935 "Failed to create document object of class: " 936 + documentClassName); 937 } 938 Class defaultDocClass = Class.forName(DEFAULT_DOCUMENT_CLASS_NAME, true, getClass().getClassLoader()); 943 if (defaultDocClass.isAssignableFrom(documentClass)) { 945 fDocumentImpl = (DocumentImpl)fDocument; 946 fDocumentImpl.setErrorChecking(false); 948 } 949 } 950 fCurrentElementNode = fDocument; 951 } 952 } 953 catch (ClassNotFoundException e) { 954 throw new RuntimeException (documentClassName); 956 } 957 958 } 960 961 public void endDocument() throws Exception { 962 if (fDocumentImpl != null) { 964 fDocumentImpl.setErrorChecking(true); 965 966 if (fDocumentType!=null) { 967 ((DocumentTypeImpl)fDocumentType).setReadOnly(true, false); 969 } 970 } 971 } 972 973 974 public void xmlDecl(int versionIndex, int encodingIndex, int standaloneIndex) throws Exception { 975 boolean standalone = (standaloneIndex!=-1)?(fStringPool.toString(standaloneIndex).equals("yes"))?true:false:false; 976 if (fDocumentImpl != null) { fDocumentImpl.setVersion(fStringPool.toString(versionIndex)); 978 fDocumentImpl.setEncoding(fStringPool.toString(encodingIndex)); 979 fDocumentImpl.setStandalone(standalone); 980 } 981 else if (fDeferredDocumentImpl != null) { 982 fDeferredDocumentImpl.setVersion(fStringPool.toString(versionIndex)); 983 fDeferredDocumentImpl.setEncoding(fStringPool.toString(encodingIndex)); 984 fDeferredDocumentImpl.setStandalone(standalone); 985 } 986 else{ 987 } 989 990 991 } 992 993 996 public void textDecl(int versionIndex, int encodingIndex) throws Exception { 997 if (fDeferredDocumentImpl != null) { 998 String name = fStringPool.toString(fCurrentEntityName); 999 if (fDocumentTypeIndex != -1 && name != null) { 1001 int entityDecl = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 1003 while (entityDecl != -1) { 1004 if (fDeferredDocumentImpl.getNodeType(entityDecl, false) == Node.ENTITY_NODE 1005 && fDeferredDocumentImpl.getNodeNameString(entityDecl, false).equals(name)) { 1006 break; 1007 } 1008 entityDecl = fDeferredDocumentImpl.getPrevSibling(entityDecl, false); 1009 } 1010 fCurrentEntityNode = entityDecl; 1011 fDeferredDocumentImpl.setEntityInfo(entityDecl, versionIndex, encodingIndex); 1012 } 1013 } 1014 else if (fDocumentImpl !=null){ 1016 NamedNodeMap entities = fDocumentType.getEntities(); 1017 if (entities!=null) { 1018 EntityImpl entityNode = (EntityImpl)entities.getNamedItem(fCurrentElementNode.getNodeName()); 1019 if (entityNode !=null) { 1020 entityNode.setVersion(fStringPool.toString(versionIndex)); 1021 entityNode.setEncoding(fStringPool.toString(encodingIndex)); 1022 } 1023 } 1024 } 1025 else { 1026 } 1028 } 1029 1030 1031 public void startNamespaceDeclScope(int prefix, int uri) throws Exception {} 1032 1033 1034 public void endNamespaceDeclScope(int prefix) throws Exception {} 1035 1036 1037 1038 1039 public void startElement(QName elementQName, 1040 XMLAttrList xmlAttrList, int attrListIndex) 1041 throws Exception { 1042 1043 if (fDeferredDocumentImpl != null) { 1045 1046 if (!fSeenRootElement) { 1048 fSeenRootElement = true; 1049 if (fGrammarAccess && fGrammarResolver.size() > 0) { 1052 if (fDocumentTypeIndex == -1) { 1053 fDocumentTypeIndex = fDeferredDocumentImpl.createDocumentType(elementQName.rawname, -1, -1); 1054 fDeferredDocumentImpl.appendChild(0, fDocumentTypeIndex); 1055 } 1056 Enumeration schemas = fGrammarResolver.nameSpaceKeys(); 1057 Document schemaDocument = fGrammarResolver.getGrammar((String )schemas.nextElement()).getGrammarDocument(); 1058 if (schemaDocument != null) { 1059 Element schema = schemaDocument.getDocumentElement(); 1060 copyInto(schema, fDocumentTypeIndex); 1061 } 1062 } 1063 } 1064 1065 int element = 1066 fDeferredDocumentImpl.createElement(elementQName.rawname, 1067 elementQName.uri, 1068 xmlAttrList, 1069 attrListIndex); 1070 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, element); 1071 fCurrentNodeIndex = element; 1072 fWithinElement = true; 1073 1074 int index = xmlAttrList.getFirstAttr(attrListIndex); 1076 while (index != -1) { 1077 if (xmlAttrList.getAttType(index) == fStringPool.addSymbol("ID")) { 1078 int nameIndex = xmlAttrList.getAttValue(index); 1079 fDeferredDocumentImpl.putIdentifier(nameIndex, element); 1080 } 1081 index = xmlAttrList.getNextAttr(index); 1082 } 1083 } 1084 1085 else { 1087 1088 boolean nsEnabled = false; 1089 try { nsEnabled = getNamespaces(); } 1090 catch (SAXException s) {} 1091 1092 String elementName = fStringPool.toString(elementQName.rawname); 1093 1094 if (!fSeenRootElement) { 1096 fSeenRootElement = true; 1097 if (fDocumentImpl != null 1098 && fGrammarAccess && fGrammarResolver.size() > 0) { 1099 if (fDocumentType == null) { 1100 String rootName = elementName; 1101 String systemId = ""; String publicId = ""; fDocumentType = fDocumentImpl.createDocumentType(rootName, publicId, systemId); 1104 fDocument.appendChild(fDocumentType); 1105 } 1114 Enumeration schemas = fGrammarResolver.nameSpaceKeys(); 1115 Document schemaDocument = fGrammarResolver.getGrammar((String )schemas.nextElement()).getGrammarDocument(); 1116 if (schemaDocument != null) { 1117 Element schema = schemaDocument.getDocumentElement(); 1118 XUtil.copyInto(schema, fDocumentType); 1119 } 1120 } 1121 } 1122 1123 Element e; 1124 if (nsEnabled) { 1125 String namespaceURI = fStringPool.toString(elementQName.uri); 1126 if (namespaceURI.length() == 0) { 1128 namespaceURI = null; 1129 } 1130 e = fDocument.createElementNS(namespaceURI, elementName); 1131 } else { 1132 e = fDocument.createElement(elementName); 1133 } 1134 int attrHandle = xmlAttrList.getFirstAttr(attrListIndex); 1135 while (attrHandle != -1) { 1136 int attName = xmlAttrList.getAttrName(attrHandle); 1137 String attrName = fStringPool.toString(attName); 1138 String attrValue = 1139 fStringPool.toString(xmlAttrList.getAttValue(attrHandle)); 1140 if (nsEnabled) { 1141 int nsURIIndex = xmlAttrList.getAttrURI(attrHandle); 1142 String namespaceURI = fStringPool.toString(nsURIIndex); 1143 int prefixIndex = xmlAttrList.getAttrPrefix(attrHandle); 1148 String prefix = fStringPool.toString(prefixIndex); 1149 if (namespaceURI.length() == 0) { 1151 namespaceURI = null; 1152 } 1153 if (namespaceURI == null) { 1154 if (prefix != null) { 1155 if (prefix.equals("xmlns")) { 1156 namespaceURI = "http://www.w3.org/2000/xmlns/"; 1157 } 1158 } else if (attrName.equals("xmlns")) { 1159 namespaceURI = "http://www.w3.org/2000/xmlns/"; 1160 } 1161 } 1162 e.setAttributeNS(namespaceURI, attrName, attrValue); 1163 } else { 1164 e.setAttribute(attrName, attrValue); 1165 } 1166 if (fDocumentImpl != null 1167 && !xmlAttrList.isSpecified(attrHandle)) { 1168 ((AttrImpl)e.getAttributeNode(attrName)) 1169 .setSpecified(false); 1170 } 1171 attrHandle = xmlAttrList.getNextAttr(attrHandle); 1172 } 1173 fCurrentElementNode.appendChild(e); 1174 fCurrentElementNode = e; 1175 fWithinElement = true; 1176 1177 if (fDocumentImpl != null) { 1179 int index = xmlAttrList.getFirstAttr(attrListIndex); 1180 while (index != -1) { 1181 if (xmlAttrList.getAttType(index) == fStringPool.addSymbol("ID")) { 1182 String name = fStringPool.toString(xmlAttrList.getAttValue(index)); 1183 fDocumentImpl.putIdentifier(name, e); 1184 } 1185 index = xmlAttrList.getNextAttr(index); 1186 } 1187 } 1188 1189 xmlAttrList.releaseAttrList(attrListIndex); 1191 } 1192 1193 } 1195 1196 public void endElement(QName elementQName) 1197 throws Exception { 1198 1199 if (fDeferredDocumentImpl != null) { 1201 fCurrentNodeIndex = fDeferredDocumentImpl.getParentNode(fCurrentNodeIndex, false); 1202 fWithinElement = false; 1203 } 1204 1205 else { 1207 fCurrentElementNode = fCurrentElementNode.getParentNode(); 1208 fWithinElement = false; 1209 } 1210 1211 } 1213 1214 public void characters(int dataIndex) 1215 throws Exception { 1216 1217 if (fDeferredDocumentImpl != null) { 1219 1220 int text; 1221 1222 if (fInCDATA) { 1223 text = fDeferredDocumentImpl.createCDATASection(dataIndex, false); 1224 } else { 1225 text = fDeferredDocumentImpl.createTextNode(dataIndex, false); 1228 } 1229 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, text); 1230 } 1231 1232 else { 1234 1235 Text text; 1236 1237 if (fInCDATA) { 1238 text = fDocument.createCDATASection(fStringPool.orphanString(dataIndex)); 1239 } 1240 else { 1241 1242 if (fWithinElement && fCurrentElementNode.getNodeType() == Node.ELEMENT_NODE) { 1243 Node lastChild = fCurrentElementNode.getLastChild(); 1244 if (lastChild != null 1245 && lastChild.getNodeType() == Node.TEXT_NODE) { 1246 ((Text )lastChild).appendData(fStringPool.orphanString(dataIndex)); 1248 return; 1249 } 1250 } 1251 text = fDocument.createTextNode(fStringPool.orphanString(dataIndex)); 1252 } 1253 1254 fCurrentElementNode.appendChild(text); 1255 1256 } 1257 1258 } 1260 1261 public void ignorableWhitespace(int dataIndex) throws Exception { 1262 1263 if (!fIncludeIgnorableWhitespace) { 1265 fStringPool.orphanString(dataIndex); 1266 return; 1267 } 1268 1269 if (fDeferredDocumentImpl != null) { 1271 1272 int text; 1273 1274 if (fInCDATA) { 1275 text = fDeferredDocumentImpl.createCDATASection(dataIndex, true); 1276 } else { 1277 text = fDeferredDocumentImpl.createTextNode(dataIndex, true); 1280 } 1281 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, text); 1282 } 1283 1284 else { 1286 1287 Text text; 1288 1289 if (fInCDATA) { 1290 text = fDocument.createCDATASection(fStringPool.orphanString(dataIndex)); 1291 } 1292 else { 1293 1294 if (fWithinElement && fCurrentElementNode.getNodeType() == Node.ELEMENT_NODE) { 1295 Node lastChild = fCurrentElementNode.getLastChild(); 1296 if (lastChild != null 1297 && lastChild.getNodeType() == Node.TEXT_NODE) { 1298 ((Text )lastChild).appendData(fStringPool.orphanString(dataIndex)); 1300 return; 1301 } 1302 } 1303 text = fDocument.createTextNode(fStringPool.orphanString(dataIndex)); 1304 } 1305 1306 if (fDocumentImpl != null) { 1307 ((TextImpl)text).setIgnorableWhitespace(true); 1308 } 1309 1310 fCurrentElementNode.appendChild(text); 1311 1312 } 1313 1314 } 1316 1317 public void processingInstruction(int targetIndex, int dataIndex) 1318 throws Exception { 1319 1320 if (fDeferredDocumentImpl != null) { 1322 int pi = fDeferredDocumentImpl.createProcessingInstruction(targetIndex, dataIndex); 1323 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, pi); 1324 } 1325 1326 else { 1328 String target = fStringPool.orphanString(targetIndex); 1329 String data = fStringPool.orphanString(dataIndex); 1330 ProcessingInstruction pi = fDocument.createProcessingInstruction(target, data); 1331 fCurrentElementNode.appendChild(pi); 1332 } 1333 1334 } 1336 1337 public void comment(int dataIndex) throws Exception { 1338 1339 if (fInDTD && !fGrammarAccess) { 1340 fStringPool.orphanString(dataIndex); 1341 } 1342 else { 1343 if (fDeferredDocumentImpl != null) { 1345 int comment = fDeferredDocumentImpl.createComment(dataIndex); 1346 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, comment); 1347 } 1348 1349 else { 1351 Comment comment = fDocument.createComment(fStringPool.orphanString(dataIndex)); 1352 fCurrentElementNode.appendChild(comment); 1353 } 1354 } 1355 1356 } 1358 public void characters(char ch[], int start, int length) throws Exception { 1363 characters(fStringPool.addSymbol(new String (ch, start, length))); 1364 } 1366 1367 public void ignorableWhitespace(char ch[], int start, int length) throws Exception {} 1368 1369 1373 1374 public void startCDATA() throws Exception { 1375 fInCDATA = true; 1376 } 1377 1378 1379 public void endCDATA() throws Exception { 1380 fInCDATA = false; 1381 } 1382 1383 1387 1388 public void startEntityReference(int entityName, int entityType, 1389 int entityContext) throws Exception { 1390 1391 fCurrentEntityName = entityName; 1392 if (!fCreateEntityReferenceNodes) { 1394 return; 1395 } 1396 1397 1398 if (entityName == fAmpIndex || 1400 entityName == fGtIndex || 1401 entityName == fLtIndex || 1402 entityName == fAposIndex || 1403 entityName == fQuotIndex) { 1404 return; 1405 } 1406 1407 if (entityContext != XMLEntityHandler.ENTITYREF_IN_CONTENT) { 1409 return; 1410 } 1411 1412 1414 if (fDeferredDocumentImpl != null) { 1415 1416 int entityRefIndex = fDeferredDocumentImpl.createEntityReference(entityName); 1417 fDeferredDocumentImpl.appendChild(fCurrentNodeIndex, entityRefIndex); 1418 1419 fCurrentNodeIndex = entityRefIndex; 1420 1421 } 1422 1423 else { 1425 1426 EntityReference er = 1427 fDocument.createEntityReference(fStringPool.toString(entityName)); 1428 1429 fCurrentElementNode.appendChild(er); 1430 fCurrentElementNode = er; 1431 } 1432 1433 } 1435 1436 public void endEntityReference(int entityName, int entityType, 1437 int entityContext) throws Exception { 1438 if (!fCreateEntityReferenceNodes) { 1440 return; 1441 } 1442 1443 if (entityName == fAmpIndex || 1445 entityName == fGtIndex || 1446 entityName == fLtIndex || 1447 entityName == fAposIndex || 1448 entityName == fQuotIndex) { 1449 return; 1450 } 1451 1452 if (entityContext != XMLEntityHandler.ENTITYREF_IN_CONTENT) { 1454 return; 1455 } 1456 1457 if (fDeferredDocumentImpl != null) { 1459 1460 String name = fStringPool.toString(entityName); 1461 1462 int erChild = fCurrentNodeIndex; 1463 fCurrentNodeIndex = fDeferredDocumentImpl.getParentNode(erChild, false); 1464 1465 if (fDeferredDocumentImpl.getNodeType(erChild, false) != Node.ENTITY_REFERENCE_NODE) return; 1467 1468 erChild = fDeferredDocumentImpl.getLastChild(erChild, false); if (fDocumentTypeIndex != -1) { 1470 if (fCurrentEntityNode == -1) { 1473 int entityDecl = fDeferredDocumentImpl.getLastChild(fDocumentTypeIndex, false); 1475 while (entityDecl != -1) { 1476 if (fDeferredDocumentImpl.getNodeType(entityDecl, false) == Node.ENTITY_NODE 1477 && fDeferredDocumentImpl.getNodeNameString(entityDecl, false).equals(name)) { 1479 break; 1480 } 1481 entityDecl = fDeferredDocumentImpl.getPrevSibling(entityDecl, false); 1482 } 1483 fCurrentEntityNode = entityDecl; 1484 } 1485 if (fCurrentEntityNode != -1 1486 && fDeferredDocumentImpl.getLastChild(fCurrentEntityNode, false) == -1) { 1487 1490 fDeferredDocumentImpl.setAsLastChild(fCurrentEntityNode, erChild); 1493 } 1494 fCurrentEntityNode = -1; 1497 fCurrentEntityName = -1; 1498 } 1499 1500 } 1501 1502 else { 1504 1505 Node erNode = fCurrentElementNode; fCurrentElementNode = erNode.getParentNode(); 1507 1508 if (fDocumentImpl != null) { 1510 NamedNodeMap entities = fDocumentType.getEntities(); 1511 String name = fStringPool.toString(entityName); 1512 Node entityNode = entities.getNamedItem(name); 1513 1514 if (entityNode == null || entityNode.hasChildNodes()) { 1517 return; 1518 } 1519 1520 EntityImpl entity = (EntityImpl) entityNode; 1521 for (Node child = erNode.getFirstChild(); 1522 child != null; 1523 child = child.getNextSibling()) { 1524 Node childClone = child.cloneNode(true); 1525 entity.appendChild(childClone); 1526 } 1527 } 1528 } 1529 1530 } 1532 1536 1540 public void startDTD(QName rootElement, int publicId, int systemId) 1541 throws Exception { 1542 1543 fInDTD = true; 1544 1545 if (fDocumentImpl != null) { 1547 String rootElementName = fStringPool.toString(rootElement.rawname); 1548 String publicString = fStringPool.toString(publicId); 1549 String systemString = fStringPool.toString(systemId); 1550 fDocumentType = fDocumentImpl. 1551 createDocumentType(rootElementName, publicString, systemString); 1552 fDocumentImpl.appendChild(fDocumentType); 1553 1554 if (fGrammarAccess) { 1555 Element schema = fDocument.createElement("schema"); 1556 schema.setAttribute("xmlns", SchemaSymbols.URI_SCHEMAFORSCHEMA); 1558 ((AttrImpl)schema.getAttributeNode("xmlns")).setSpecified(false); 1559 schema.setAttribute("finalDefault", ""); 1560 ((AttrImpl)schema.getAttributeNode("finalDefault")).setSpecified(false); 1561 schema.setAttribute("exactDefault", ""); 1562 ((AttrImpl)schema.getAttributeNode("exactDefault")).setSpecified(false); 1563 fDocumentType.appendChild(schema); 1564 fCurrentElementNode = schema; 1565 } 1566 } 1567 1568 else if (fDeferredDocumentImpl != null) { 1570 fDocumentTypeIndex = 1571 fDeferredDocumentImpl. 1572 createDocumentType(rootElement.rawname, publicId, systemId); 1573 fDeferredDocumentImpl.appendChild(fDocumentIndex, fDocumentTypeIndex); 1574 1575 if (fGrammarAccess) { 1576 int handle = fAttrList.startAttrList(); 1577 fAttrList.addAttr( 1578 fStringPool.addSymbol("xmlns"), 1579 fStringPool.addString(SchemaSymbols.URI_SCHEMAFORSCHEMA), 1580 fStringPool.addSymbol("CDATA"), 1581 false, 1582 false); fAttrList.addAttr( 1584 fStringPool.addSymbol("finalDefault"), 1585 fStringPool.addString(""), 1586 fStringPool.addSymbol("CDATA"), 1587 false, 1588 false); fAttrList.addAttr( 1590 fStringPool.addSymbol("exactDefault"), 1591 fStringPool.addString(""), 1592 fStringPool.addSymbol("CDATA"), 1593 false, 1594 false); fAttrList.endAttrList(); 1596 int schemaIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("schema"), fAttrList, handle); 1597 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, schemaIndex); 1599 fCurrentNodeIndex = schemaIndex; 1600 } 1601 } 1602 1603 } 1605 1609 public void internalSubset(int internalSubset) { 1610 1611 1613 if (fDocumentImpl != null && fDocumentType != null) { 1615 ((DocumentTypeImpl)fDocumentType).setInternalSubset(fStringPool.toString(internalSubset)); 1616 } 1617 1618 else if (fDeferredDocumentImpl != null) { 1620 fDeferredDocumentImpl.setInternalSubset(fDocumentTypeIndex, internalSubset); 1621 } 1622 1623 } 1624 1625 1626 1629 public void endDTD() throws Exception { 1630 1631 fInDTD = false; 1632 1633 if (fGrammarAccess) { 1634 if (fDocumentImpl != null) { 1635 fCurrentElementNode = fDocumentImpl; 1636 } 1637 else if (fDeferredDocumentImpl != null) { 1638 fCurrentNodeIndex = 0; 1639 } 1640 } 1641 1642 } 1644 1647 public void elementDecl(QName elementDecl, 1648 int contentSpecType, 1649 int contentSpecIndex, 1650 XMLContentSpec.Provider contentSpecProvider) throws Exception { 1651 1652 if (DEBUG_ATTLIST_DECL) { 1653 String contentModel = XMLContentSpec.toString(contentSpecProvider, fStringPool, contentSpecIndex); 1654 System.out.println("elementDecl(" + fStringPool.toString(elementDecl.rawname) + ", " + 1655 contentModel + ")"); 1656 } 1657 1658 if (fGrammarAccess) { 1662 1663 if (fDeferredDocumentImpl != null) { 1664 1665 1669 int schemaIndex = getLastChildElement(fDocumentTypeIndex, "schema"); 1671 String elementName = fStringPool.toString(elementDecl.rawname); 1672 int elementIndex = getLastChildElement(schemaIndex, "element", "name", elementName); 1673 if (elementIndex == -1) { 1674 int handle = fAttrList.startAttrList(); 1675 fAttrList.addAttr( 1676 fStringPool.addSymbol("name"), 1677 fStringPool.addString(elementName), 1678 fStringPool.addSymbol("NMTOKEN"), 1679 true, 1680 false); fAttrList.addAttr( 1682 fStringPool.addSymbol("minOccurs"), fStringPool.addString("1"), fStringPool.addSymbol("NMTOKEN"), false, false); fAttrList.addAttr( 1688 fStringPool.addSymbol("nillable"), fStringPool.addString("false"), fStringPool.addSymbol("ENUMERATION"), false, false); fAttrList.addAttr( 1694 fStringPool.addSymbol("abstract"), fStringPool.addString("false"), fStringPool.addSymbol("ENUMERATION"), false, false); fAttrList.addAttr( 1700 fStringPool.addSymbol("final"), fStringPool.addString("false"), fStringPool.addSymbol("ENUMERATION"), false, false); fAttrList.endAttrList(); 1706 elementIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("element"), fAttrList, handle); 1707 fDeferredDocumentImpl.appendChild(schemaIndex, elementIndex); 1708 } 1709 1710 1714 int typeIndex = getLastChildElement(elementIndex, "complexType"); 1716 if (typeIndex == -1 && contentSpecType != XMLElementDecl.TYPE_MIXED_SIMPLE) { 1717 typeIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("complexType"), null, -1); 1718 fDeferredDocumentImpl.insertBefore(elementIndex, typeIndex, getFirstChildElement(elementIndex)); 1720 } 1721 1722 switch (contentSpecType) { 1724 case XMLElementDecl.TYPE_EMPTY: { 1725 int attributeIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("content"), fStringPool.addString("empty"), true); 1726 fDeferredDocumentImpl.setAttributeNode(typeIndex, attributeIndex); 1727 break; 1728 } 1729 case XMLElementDecl.TYPE_ANY: { 1730 int anyIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("any"), null, -1); 1731 fDeferredDocumentImpl.insertBefore(typeIndex, anyIndex, getFirstChildElement(typeIndex)); 1732 break; 1733 } 1734 case XMLElementDecl.TYPE_MIXED_SIMPLE: { 1735 XMLContentSpec contentSpec = new XMLContentSpec(); 1736 contentSpecProvider.getContentSpec(contentSpecIndex, contentSpec); 1737 contentSpecIndex = contentSpec.value; 1738 if (contentSpecIndex == -1) { 1739 int attributeIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("type"), fStringPool.addString("string"), true); 1740 fDeferredDocumentImpl.setAttributeNode(elementIndex, attributeIndex); 1741 } 1742 else { 1743 if (typeIndex == -1) { 1744 typeIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("complexType"), null, -1); 1745 fDeferredDocumentImpl.insertBefore(elementIndex, typeIndex, getFirstChildElement(elementIndex)); 1747 } 1748 int attributeIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("content"), fStringPool.addString("mixed"), true); 1749 fDeferredDocumentImpl.setAttributeNode(typeIndex, attributeIndex); 1750 int handle = fAttrList.startAttrList(); 1751 fAttrList.addAttr( 1752 fStringPool.addSymbol("minOccurs"), 1753 fStringPool.addString("0"), 1754 fStringPool.addSymbol("NMTOKEN"), 1755 true, 1756 false); fAttrList.addAttr( 1758 fStringPool.addSymbol("maxOccurs"), 1759 fStringPool.addString("unbounded"), 1760 fStringPool.addSymbol("CDATA"), 1761 true, 1762 false); fAttrList.endAttrList(); 1764 int choiceIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("choice"), fAttrList, handle); 1765 fDeferredDocumentImpl.appendChild(typeIndex, choiceIndex); 1766 while (contentSpecIndex != -1) { 1767 1768 contentSpecProvider.getContentSpec(contentSpecIndex, contentSpec); 1770 int type = contentSpec.type; 1771 int left = contentSpec.value; 1772 int right = contentSpec.otherValue; 1773 1774 if (type == XMLContentSpec.CONTENTSPECNODE_LEAF) { 1776 break; 1777 } 1778 1779 contentSpecProvider.getContentSpec(right, contentSpec); 1781 handle = fAttrList.startAttrList(); 1782 fAttrList.addAttr( 1783 fStringPool.addSymbol("ref"), 1784 fStringPool.addString(fStringPool.toString(contentSpec.value)), 1785 fStringPool.addSymbol("NMTOKEN"), 1786 true, 1787 false); fAttrList.endAttrList(); 1789 int rightIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("element"), fAttrList, handle); 1790 int refIndex = getFirstChildElement(choiceIndex); 1791 fDeferredDocumentImpl.insertBefore(choiceIndex, rightIndex, refIndex); 1792 1793 contentSpecIndex = left; 1795 } 1796 } 1797 break; 1798 } 1799 case XMLElementDecl.TYPE_CHILDREN: { 1800 int attributeIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("content"), fStringPool.addString("elementOnly"), true); 1801 fDeferredDocumentImpl.setAttributeNode(typeIndex, attributeIndex); 1802 int children = createChildren(contentSpecProvider, 1803 contentSpecIndex, 1804 new XMLContentSpec(), 1805 fDeferredDocumentImpl, 1806 -1); 1807 fDeferredDocumentImpl.insertBefore(typeIndex, children, getFirstChildElement(typeIndex)); 1808 break; 1809 } 1810 } 1811 1812 } 1814 else if (fDocumentImpl != null) { 1815 1816 1820 Element schema = XUtil.getLastChildElement(fDocumentType, "schema"); 1822 String elementName = fStringPool.toString(elementDecl.rawname); 1823 Element element = XUtil.getLastChildElement(schema, "element", "name", elementName); 1824 if (element == null) { 1825 element = fDocumentImpl.createElement("element"); 1826 element.setAttribute("name", elementName); 1827 element.setAttribute("minOccurs", "1"); 1828 ((AttrImpl)element.getAttributeNode("minOccurs")).setSpecified(false); 1829 element.setAttribute("nillable", "false"); 1830 ((AttrImpl)element.getAttributeNode("nillable")).setSpecified(false); 1831 element.setAttribute("abstract", "false"); 1832 ((AttrImpl)element.getAttributeNode("abstract")).setSpecified(false); 1833 element.setAttribute("final", "false"); 1834 ((AttrImpl)element.getAttributeNode("final")).setSpecified(false); 1835 schema.appendChild(element); 1836 } 1837 1838 1842 Element type = XUtil.getLastChildElement(element, "complexType"); 1844 if (type == null && contentSpecType != XMLElementDecl.TYPE_MIXED_SIMPLE) { 1845 type = fDocumentImpl.createElement("complexType"); 1846 element.insertBefore(type, XUtil.getFirstChildElement(element)); 1848 } 1849 1850 switch (contentSpecType) { 1852 case XMLElementDecl.TYPE_EMPTY: { 1853 type.setAttribute("content", "empty"); 1854 break; 1855 } 1856 case XMLElementDecl.TYPE_ANY: { 1857 Element any = fDocumentImpl.createElement("any"); 1858 type.insertBefore(any, XUtil.getFirstChildElement(type)); 1859 break; 1860 } 1861 case XMLElementDecl.TYPE_MIXED_SIMPLE: { 1862 XMLContentSpec contentSpec = new XMLContentSpec(); 1863 contentSpecProvider.getContentSpec(contentSpecIndex, contentSpec); 1864 contentSpecIndex = contentSpec.value; 1865 if (contentSpecIndex == -1) { 1866 element.setAttribute("type", "string"); 1867 } 1868 else { 1869 if (type == null) { 1870 type = fDocumentImpl.createElement("complexType"); 1871 element.insertBefore(type, XUtil.getFirstChildElement(element)); 1873 } 1874 type.setAttribute("content", "mixed"); 1875 Element choice = fDocumentImpl.createElement("choice"); 1876 choice.setAttribute("minOccurs", "0"); 1877 choice.setAttribute("maxOccurs", "unbounded"); 1878 type.appendChild(choice); 1879 while (contentSpecIndex != -1) { 1880 1881 contentSpecProvider.getContentSpec(contentSpecIndex, contentSpec); 1883 int cstype = contentSpec.type; 1884 int csleft = contentSpec.value; 1885 int csright = contentSpec.otherValue; 1886 1887 if (cstype == XMLContentSpec.CONTENTSPECNODE_LEAF) { 1889 break; 1890 } 1891 1892 contentSpecProvider.getContentSpec(csright, contentSpec); 1894 Element right = fDocumentImpl.createElement("element"); 1895 right.setAttribute("ref", fStringPool.toString(contentSpec.value)); 1896 Element ref = XUtil.getFirstChildElement(choice); 1897 choice.insertBefore(right, ref); 1898 1899 contentSpecIndex = csleft; 1901 } 1902 } 1903 break; 1904 } 1905 case XMLElementDecl.TYPE_CHILDREN: { 1906 type.setAttribute("content", "elementOnly"); 1907 Element children = createChildren(contentSpecProvider, 1908 contentSpecIndex, 1909 new XMLContentSpec(), 1910 fDocumentImpl, 1911 null); 1912 type.insertBefore(children, XUtil.getFirstChildElement(type)); 1913 break; 1914 } 1915 } 1916 1917 } 1919 } 1921 } 1923 1926 public void attlistDecl(QName elementDecl, QName attributeDecl, 1927 int attType, boolean attList, String enumString, 1928 int attDefaultType, int attDefaultValue) 1929 throws Exception { 1930 1931 if (DEBUG_ATTLIST_DECL) { 1932 System.out.println("attlistDecl(" + fStringPool.toString(elementDecl.rawname) + ", " + 1933 fStringPool.toString(attributeDecl.rawname) + ", " + 1934 fStringPool.toString(attType) + ", " + 1935 enumString + ", " + 1936 fStringPool.toString(attDefaultType) + ", " + 1937 fStringPool.toString(attDefaultValue) + ")"); 1938 } 1939 1940 if (fDeferredDocumentImpl != null) { 1942 1943 if (attDefaultValue != -1) { 1945 if (DEBUG_ATTLIST_DECL) { 1946 System.out.println(" adding default attribute value: "+ 1947 fStringPool.toString(attDefaultValue)); 1948 } 1949 1950 int elementDefIndex = fDeferredDocumentImpl.lookupElementDefinition(elementDecl.rawname); 1952 1953 if (elementDefIndex == -1) { 1955 elementDefIndex = fDeferredDocumentImpl.createElementDefinition(elementDecl.rawname); 1956 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, elementDefIndex); 1957 } 1958 1959 int attrIndex = 1961 fDeferredDocumentImpl.createAttribute(attributeDecl.rawname, 1962 attributeDecl.uri, 1963 attDefaultValue, 1964 false); 1965 fDeferredDocumentImpl.appendChild(elementDefIndex, attrIndex); 1966 1967 } 1968 1969 if (fGrammarAccess) { 1973 1974 int schemaIndex = getLastChildElement(fDocumentTypeIndex, "schema"); 1976 String elementName = fStringPool.toString(elementDecl.rawname); 1977 int elementIndex = getLastChildElement(schemaIndex, "element", "name", elementName); 1978 if (elementIndex == -1) { 1979 int handle = fAttrList.startAttrList(); 1980 fAttrList.addAttr( 1981 fStringPool.addSymbol("name"), 1982 fStringPool.addString(elementName), 1983 fStringPool.addSymbol("NMTOKEN"), 1984 true, 1985 false); fAttrList.endAttrList(); 1987 elementIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("element"), fAttrList, handle); 1988 fDeferredDocumentImpl.appendChild(schemaIndex, elementIndex); 1989 } 1990 1991 int typeIndex = getLastChildElement(elementIndex, "complexType"); 1993 if (typeIndex == -1) { 1994 typeIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("complexType"), null, -1); 1995 fDeferredDocumentImpl.insertBefore(elementIndex, typeIndex, getLastChildElement(elementIndex)); 1996 } 1997 1998 String attributeName = fStringPool.toString(attributeDecl.rawname); 2000 int attributeIndex = getLastChildElement(elementIndex, "attribute", "name", attributeName); 2001 if (attributeIndex == -1) { 2002 int handle = fAttrList.startAttrList(); 2003 fAttrList.addAttr( 2004 fStringPool.addSymbol("name"), 2005 fStringPool.addString(attributeName), 2006 fStringPool.addSymbol("NMTOKEN"), 2007 true, 2008 false); fAttrList.addAttr( 2010 fStringPool.addSymbol("maxOccurs"), 2011 fStringPool.addString("1"), 2012 fStringPool.addSymbol("CDATA"), 2013 false, 2014 false); fAttrList.endAttrList(); 2016 attributeIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("attribute"), fAttrList, handle); 2017 fDeferredDocumentImpl.appendChild(typeIndex, attributeIndex); 2018 2019 if (attType == XMLAttributeDecl.TYPE_ENUMERATION) { 2021 handle = fAttrList.startAttrList(); 2022 fAttrList.addAttr( 2023 fStringPool.addSymbol("base"), 2024 fStringPool.addString("NMTOKEN"), 2025 fStringPool.addSymbol("NMTOKEN"), 2026 true, 2027 false); fAttrList.endAttrList(); 2029 int simpleTypeIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("simpleType"), fAttrList, handle); 2030 fDeferredDocumentImpl.appendChild(attributeIndex, simpleTypeIndex); 2031 String tokenizerString = enumString.substring(1, enumString.length() - 1); 2032 StringTokenizer tokenizer = new StringTokenizer (tokenizerString, "|"); 2033 while (tokenizer.hasMoreTokens()) { 2034 handle = fAttrList.startAttrList(); 2035 fAttrList.addAttr( 2036 fStringPool.addSymbol("value"), 2037 fStringPool.addString(tokenizer.nextToken()), 2038 fStringPool.addSymbol("CDATA"), 2039 true, 2040 false); fAttrList.endAttrList(); 2042 int enumerationIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("enumeration"), fAttrList, handle); 2043 fDeferredDocumentImpl.appendChild(simpleTypeIndex, enumerationIndex); 2044 } 2045 } 2046 else { 2047 int typeNameIndex = -1; 2048 switch (attType) { 2049 case XMLAttributeDecl.TYPE_ENTITY: { 2050 typeNameIndex = fStringPool.addString(attList?"ENTITIES":"ENTITY"); 2051 break; 2052 } 2053 case XMLAttributeDecl.TYPE_ID: { 2054 typeNameIndex = fStringPool.addString("ID"); 2055 break; 2056 } 2057 case XMLAttributeDecl.TYPE_IDREF: { 2058 typeNameIndex = fStringPool.addString(attList?"IDREFS":"IDREF"); 2059 break; 2060 } 2061 case XMLAttributeDecl.TYPE_NMTOKEN: { 2062 typeNameIndex = fStringPool.addString(attList?"NMTOKENS":"NMTOKEN"); 2063 break; 2064 } 2065 case XMLAttributeDecl.TYPE_NOTATION: { 2066 typeNameIndex = fStringPool.addString("NOTATION"); 2067 break; 2068 } 2069 case XMLAttributeDecl.TYPE_CDATA: 2070 default: { 2071 typeNameIndex = fStringPool.addString("string"); 2072 break; 2073 } 2074 } 2075 int attrIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("type"), typeNameIndex, true); 2076 fDeferredDocumentImpl.setAttributeNode(attributeIndex, attrIndex); 2077 } 2078 2079 boolean fixed = false; 2081 switch (attDefaultType) { 2082 case XMLAttributeDecl.DEFAULT_TYPE_REQUIRED: { 2083 int useAttrIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("use"), fStringPool.addString("required"), true); 2084 fDeferredDocumentImpl.setAttributeNode(attributeIndex, useAttrIndex); 2085 break; 2086 } 2087 case XMLAttributeDecl.DEFAULT_TYPE_FIXED: { 2088 fixed = true; 2089 int useAttrIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("use"), fStringPool.addString("fixed"), true); 2090 fDeferredDocumentImpl.setAttributeNode(attributeIndex, useAttrIndex); 2091 break; 2092 } 2093 } 2094 2095 if (attDefaultValue != -1) { 2097 if (!fixed) { 2098 int useAttrIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("use"), fStringPool.addString("default"), true); 2099 fDeferredDocumentImpl.setAttributeNode(attributeIndex, useAttrIndex); 2100 } 2101 int valueAttrIndex = fDeferredDocumentImpl.createAttribute(fStringPool.addSymbol("value"), attDefaultValue, true); 2102 fDeferredDocumentImpl.setAttributeNode(attributeIndex, valueAttrIndex); 2103 } 2104 } 2105 } 2106 } 2107 2108 else if (fDocumentImpl != null) { 2110 2111 if (attDefaultValue != -1) { 2113 if (DEBUG_ATTLIST_DECL) { 2114 System.out.println(" adding default attribute value: "+ 2115 fStringPool.toString(attDefaultValue)); 2116 } 2117 2118 String elementName = fStringPool.toString(elementDecl.rawname); 2120 2121 NamedNodeMap elements = ((DocumentTypeImpl)fDocumentType).getElements(); 2123 ElementDefinitionImpl elementDef = (ElementDefinitionImpl)elements.getNamedItem(elementName); 2124 if (elementDef == null) { 2125 elementDef = fDocumentImpl.createElementDefinition(elementName); 2126 ((DocumentTypeImpl)fDocumentType).getElements().setNamedItem(elementDef); 2127 } 2128 2129 2131 String attrName = fStringPool.toString(attributeDecl.rawname); 2133 String attrValue = fStringPool.toString(attDefaultValue); 2134 2135 boolean nsEnabled = false; 2137 try { nsEnabled = getNamespaces(); } 2138 catch (SAXException s) {} 2139 AttrImpl attr; 2140 if (nsEnabled) { 2141 String namespaceURI = fStringPool.toString(attributeDecl.uri); 2142 String prefix = fStringPool.toString(attributeDecl.prefix); 2147 if (namespaceURI.length() == 0) { 2149 namespaceURI = null; 2150 } 2151 if (namespaceURI == null) { 2152 if (prefix != null) { 2153 if (prefix.equals("xmlns")) { 2154 namespaceURI = "http://www.w3.org/2000/xmlns/"; 2155 } 2156 } else if (attrName.equals("xmlns")) { 2157 namespaceURI = "http://www.w3.org/2000/xmlns/"; 2158 } 2159 } 2160 attr = (AttrImpl)fDocumentImpl.createAttributeNS(namespaceURI,attrName); 2161 } 2162 else{ 2163 attr = (AttrImpl)fDocumentImpl.createAttribute(attrName); 2164 } 2165 attr.setValue(attrValue); 2166 attr.setSpecified(false); 2167 2168 if(nsEnabled){ 2170 elementDef.getAttributes().setNamedItemNS(attr); 2171 } 2172 else{ 2173 elementDef.getAttributes().setNamedItem(attr); 2174 } 2175 } 2176 2177 try { 2181 if (fGrammarAccess) { 2182 2183 Element schema = XUtil.getLastChildElement(fDocumentType, "schema"); 2185 String elementName = fStringPool.toString(elementDecl.rawname); 2186 Element element = XUtil.getLastChildElement(schema, "element", "name", elementName); 2187 if (element == null) { 2188 element = fDocumentImpl.createElement("element"); 2189 element.setAttribute("name", elementName); 2190 schema.appendChild(element); 2191 } 2192 2193 Element type = XUtil.getLastChildElement(element, "complexType"); 2195 if (type == null) { 2196 type = fDocumentImpl.createElement("complexType"); 2197 element.insertBefore(type, XUtil.getLastChildElement(element)); 2198 } 2199 2200 String attributeName = fStringPool.toString(attributeDecl.rawname); 2202 Element attribute = XUtil.getLastChildElement(element, "attribute", "name", attributeName); 2203 if (attribute == null) { 2204 attribute = fDocumentImpl.createElement("attribute"); 2205 attribute.setAttribute("name", attributeName); 2206 attribute.setAttribute("maxOccurs", "1"); 2207 ((AttrImpl)attribute.getAttributeNode("maxOccurs")).setSpecified(false); 2208 type.appendChild(attribute); 2209 2210 if (attType == XMLAttributeDecl.TYPE_ENUMERATION) { 2212 Element simpleType = fDocumentImpl.createElement("simpleType"); 2213 simpleType.setAttribute("base", "NMTOKEN"); 2214 attribute.appendChild(simpleType); 2215 String tokenizerString = enumString.substring(1, enumString.length() - 1); 2216 StringTokenizer tokenizer = new StringTokenizer (tokenizerString, "|"); 2217 while (tokenizer.hasMoreTokens()) { 2218 Element enumeration = fDocumentImpl.createElement("enumeration"); 2219 enumeration.setAttribute("value", tokenizer.nextToken()); 2220 simpleType.appendChild(enumeration); 2221 } 2222 } 2223 else { 2224 String typeName = null; 2225 switch (attType) { 2226 case XMLAttributeDecl.TYPE_ENTITY: { 2227 typeName = attList ? "ENTITIES" : "ENTITY"; 2228 break; 2229 } 2230 case XMLAttributeDecl.TYPE_ID: { 2231 typeName = "ID"; 2232 break; 2233 } 2234 case XMLAttributeDecl.TYPE_IDREF: { 2235 typeName = attList ? "IDREFS" : "IDREF"; 2236 break; 2237 } 2238 case XMLAttributeDecl.TYPE_NMTOKEN: { 2239 typeName = attList ? "NMTOKENS" : "NMTOKEN"; 2240 break; 2241 } 2242 case XMLAttributeDecl.TYPE_NOTATION: { 2243 typeName = "NOTATION"; 2244 break; 2245 } 2246 case XMLAttributeDecl.TYPE_CDATA: 2247 default: { 2248 typeName = "string"; 2249 break; 2250 } 2251 } 2252 attribute.setAttribute("type", typeName); 2253 } 2254 2255 boolean fixed = false; 2257 switch (attDefaultType) { 2258 case XMLAttributeDecl.DEFAULT_TYPE_REQUIRED: { 2259 attribute.setAttribute("use", "required"); 2260 break; 2261 } 2262 case XMLAttributeDecl.DEFAULT_TYPE_FIXED: { 2263 attribute.setAttribute("use", "fixed"); 2264 fixed = true; 2265 break; 2266 } 2267 } 2268 2269 if (attDefaultValue != -1) { 2271 if (!fixed) { 2272 attribute.setAttribute("use", "default"); 2273 } 2274 attribute.setAttribute("value", fStringPool.toString(attDefaultValue)); 2275 } 2276 } 2277 } 2278 } 2279 catch (Exception e) { 2280 e.printStackTrace(System.err); 2281 } 2282 2283 } 2285 } 2287 2290 public void internalPEDecl(int entityNameIndex, int entityValueIndex) throws Exception { 2291 if (fDeferredDocumentImpl != null) { 2292 if (fGrammarAccess) { 2293 StringBuffer str = new StringBuffer (); 2294 str.append("<!ENTITY % "); 2295 str.append(fStringPool.toString(entityNameIndex)); 2296 str.append(" \""); 2297 str.append(fStringPool.toString(entityValueIndex)); 2298 str.append("\">"); 2299 int commentIndex = fStringPool.addString(str.toString()); 2300 int internalPEEntityIndex = fDeferredDocumentImpl.createComment(commentIndex); 2301 int schemaIndex = getFirstChildElement(fDocumentTypeIndex, "schema"); 2302 fDeferredDocumentImpl.appendChild(schemaIndex, internalPEEntityIndex); 2303 } 2304 } 2305 else if (fDocumentImpl != null) { 2306 if (fGrammarAccess) { 2307 StringBuffer str = new StringBuffer (); 2308 str.append("<!ENTITY % "); 2309 str.append(fStringPool.toString(entityNameIndex)); 2310 str.append(" \""); 2311 str.append(fStringPool.toString(entityValueIndex)); 2312 str.append("\">"); 2313 Node internalPEEntity = fDocumentImpl.createComment(str.toString()); 2314 Node schema = XUtil.getFirstChildElement(fDocumentType, "schema"); 2315 schema.appendChild(internalPEEntity); 2316 } 2317 } 2318 else { 2319 fStringPool.orphanString(entityValueIndex); 2320 } 2321 } 2322 2323 2326 public void externalPEDecl(int entityNameIndex, int publicIdIndex, int systemIdIndex) throws Exception { 2327 if (fDeferredDocumentImpl != null) { 2328 if (fGrammarAccess) { 2329 StringBuffer str = new StringBuffer (); 2330 str.append("<!ENTITY "); 2331 str.append(fStringPool.toString(entityNameIndex)); 2332 str.append(' '); 2333 if (publicIdIndex != -1) { 2334 str.append("PUBLIC \""); 2335 str.append(fStringPool.toString(publicIdIndex)); 2336 str.append('"'); 2337 if (systemIdIndex != -1) { 2338 str.append(" \""); 2339 str.append(fStringPool.toString(systemIdIndex)); 2340 str.append('"'); 2341 } 2342 } 2343 else if (systemIdIndex != -1) { 2344 str.append("SYSTEM \""); 2345 str.append(fStringPool.toString(systemIdIndex)); 2346 str.append('"'); 2347 } 2348 str.append('>'); 2349 int commentIndex = fStringPool.addString(str.toString()); 2350 int externalPEEntityIndex = fDeferredDocumentImpl.createComment(commentIndex); 2351 int schemaIndex = getFirstChildElement(fDocumentTypeIndex, "schema"); 2352 fDeferredDocumentImpl.appendChild(schemaIndex, externalPEEntityIndex); 2353 } 2354 } 2355 else if (fDocumentImpl != null) { 2356 if (fGrammarAccess) { 2357 StringBuffer str = new StringBuffer (); 2358 str.append("<!ENTITY "); 2359 str.append(fStringPool.toString(entityNameIndex)); 2360 str.append(' '); 2361 if (publicIdIndex != -1) { 2362 str.append("PUBLIC \""); 2363 str.append(fStringPool.toString(publicIdIndex)); 2364 str.append('"'); 2365 if (systemIdIndex != -1) { 2366 str.append(" \""); 2367 str.append(fStringPool.toString(systemIdIndex)); 2368 str.append('"'); 2369 } 2370 } 2371 else if (systemIdIndex != -1) { 2372 str.append("SYSTEM \""); 2373 str.append(fStringPool.toString(systemIdIndex)); 2374 str.append('"'); 2375 } 2376 str.append('>'); 2377 Node externalPEEntity = fDocumentImpl.createComment(str.toString()); 2378 Node schema = XUtil.getFirstChildElement(fDocumentType, "schema"); 2379 schema.appendChild(externalPEEntity); 2380 } 2381 } 2382 } 2383 2384 2387 public void internalEntityDecl(int entityNameIndex, int entityValueIndex) 2388 throws Exception { 2389 2390 if (fDeferredDocumentImpl != null) { 2392 2393 if (fDocumentTypeIndex == -1) return; 2395 2398 int newEntityIndex = fDeferredDocumentImpl.createEntity(entityNameIndex, -1, -1, -1); 2399 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, newEntityIndex); 2400 2401 if (fGrammarAccess) { 2404 StringBuffer str = new StringBuffer (); 2405 str.append("<!ENTITY "); 2406 str.append(fStringPool.toString(entityNameIndex)); 2407 str.append(" \""); 2408 str.append(fStringPool.toString(entityValueIndex)); 2409 str.append("\">"); 2410 int commentIndex = fStringPool.addString(str.toString()); 2411 int textEntityIndex = fDeferredDocumentImpl.createComment(commentIndex); 2412 int schemaIndex = getFirstChildElement(fDocumentTypeIndex, "schema"); 2413 fDeferredDocumentImpl.appendChild(schemaIndex, textEntityIndex); 2414 } 2415 } 2416 2417 else if (fDocumentImpl != null) { 2419 if (fDocumentType == null) return; 2421 2424 String entityName = fStringPool.toString(entityNameIndex); 2425 2426 Entity entity = fDocumentImpl.createEntity(entityName); 2427 fDocumentType.getEntities().setNamedItem(entity); 2428 2429 if (fGrammarAccess) { 2432 StringBuffer str = new StringBuffer (); 2433 str.append("<!ENTITY "); 2434 str.append(fStringPool.toString(entityNameIndex)); 2435 str.append(" \""); 2436 str.append(fStringPool.toString(entityValueIndex)); 2437 str.append("\">"); 2438 Node textEntity = fDocumentImpl.createComment(str.toString()); 2439 Node schema = XUtil.getFirstChildElement(fDocumentType, "schema"); 2440 schema.appendChild(textEntity); 2441 } 2442 } 2443 2444 } 2446 2449 public void externalEntityDecl(int entityNameIndex, int publicIdIndex, int systemIdIndex) 2450 throws Exception { 2451 2452 if (fDeferredDocumentImpl != null) { 2454 2455 2458 int newEntityIndex = fDeferredDocumentImpl.createEntity(entityNameIndex, publicIdIndex, systemIdIndex, -1); 2459 2460 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, newEntityIndex); 2461 2462 if (fGrammarAccess) { 2465 StringBuffer str = new StringBuffer (); 2466 str.append("<!ENTITY "); 2467 str.append(fStringPool.toString(entityNameIndex)); 2468 str.append(' '); 2469 if (publicIdIndex != -1) { 2470 str.append("PUBLIC \""); 2471 str.append(fStringPool.toString(publicIdIndex)); 2472 str.append('"'); 2473 if (systemIdIndex != -1) { 2474 str.append(" \""); 2475 str.append(fStringPool.toString(systemIdIndex)); 2476 str.append('"'); 2477 } 2478 } 2479 else if (systemIdIndex != -1) { 2480 str.append("SYSTEM \""); 2481 str.append(fStringPool.toString(systemIdIndex)); 2482 str.append('"'); 2483 } 2484 str.append('>'); 2485 int commentIndex = fStringPool.addString(str.toString()); 2486 int externalEntityIndex = fDeferredDocumentImpl.createComment(commentIndex); 2487 int schemaIndex = getFirstChildElement(fDocumentTypeIndex, "schema"); 2488 fDeferredDocumentImpl.appendChild(schemaIndex, externalEntityIndex); 2489 } 2490 } 2491 2492 else if (fDocumentImpl != null) { 2494 2495 2498 String entityName = fStringPool.toString(entityNameIndex); 2499 String publicId = fStringPool.toString(publicIdIndex); 2500 String systemId = fStringPool.toString(systemIdIndex); 2501 2502 EntityImpl entity = (EntityImpl)fDocumentImpl.createEntity(entityName); 2503 if (publicIdIndex != -1) { 2504 entity.setPublicId(publicId); 2505 } 2506 entity.setSystemId(systemId); 2507 fDocumentType.getEntities().setNamedItem(entity); 2508 2509 if (fGrammarAccess) { 2512 StringBuffer str = new StringBuffer (); 2513 str.append("<!ENTITY "); 2514 str.append(fStringPool.toString(entityNameIndex)); 2515 str.append(' '); 2516 if (publicIdIndex != -1) { 2517 str.append("PUBLIC \""); 2518 str.append(fStringPool.toString(publicIdIndex)); 2519 str.append('"'); 2520 if (systemIdIndex != -1) { 2521 str.append(" \""); 2522 str.append(fStringPool.toString(systemIdIndex)); 2523 str.append('"'); 2524 } 2525 } 2526 else if (systemIdIndex != -1) { 2527 str.append("SYSTEM \""); 2528 str.append(fStringPool.toString(systemIdIndex)); 2529 str.append('"'); 2530 } 2531 str.append('>'); 2532 Node externalEntity = fDocumentImpl.createComment(str.toString()); 2533 Node schema = XUtil.getFirstChildElement(fDocumentType, "schema"); 2534 schema.appendChild(externalEntity); 2535 } 2536 } 2537 2538 } 2540 2543 public void unparsedEntityDecl(int entityNameIndex, 2544 int publicIdIndex, int systemIdIndex, 2545 int notationNameIndex) throws Exception { 2546 2547 if (fDeferredDocumentImpl != null) { 2549 2550 2553 int newEntityIndex = fDeferredDocumentImpl.createEntity(entityNameIndex, publicIdIndex, systemIdIndex, notationNameIndex); 2554 2555 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, newEntityIndex); 2556 2557 if (fGrammarAccess) { 2560 StringBuffer str = new StringBuffer (); 2561 str.append("<!ENTITY "); 2562 str.append(fStringPool.toString(entityNameIndex)); 2563 str.append(' '); 2564 if (publicIdIndex != -1) { 2565 str.append("PUBLIC \""); 2566 str.append(fStringPool.toString(publicIdIndex)); 2567 str.append('"'); 2568 if (systemIdIndex != -1) { 2569 str.append(" \""); 2570 str.append(fStringPool.toString(systemIdIndex)); 2571 str.append('"'); 2572 } 2573 } 2574 else if (systemIdIndex != -1) { 2575 str.append("SYSTEM \""); 2576 str.append(fStringPool.toString(systemIdIndex)); 2577 str.append('"'); 2578 } 2579 str.append(" NDATA "); 2580 str.append(fStringPool.toString(notationNameIndex)); 2581 str.append('>'); 2582 int commentIndex = fStringPool.addString(str.toString()); 2583 int unparsedEntityIndex = fDeferredDocumentImpl.createComment(commentIndex); 2584 int schemaIndex = getFirstChildElement(fDocumentTypeIndex, "schema"); 2585 fDeferredDocumentImpl.appendChild(schemaIndex, unparsedEntityIndex); 2586 } 2587 } 2588 2589 else if (fDocumentImpl != null) { 2591 2592 2595 String entityName = fStringPool.toString(entityNameIndex); 2596 String publicId = fStringPool.toString(publicIdIndex); 2597 String systemId = fStringPool.toString(systemIdIndex); 2598 String notationName = fStringPool.toString(notationNameIndex); 2599 2600 EntityImpl entity = (EntityImpl)fDocumentImpl.createEntity(entityName); 2601 if (publicIdIndex != -1) { 2602 entity.setPublicId(publicId); 2603 } 2604 entity.setSystemId(systemId); 2605 entity.setNotationName(notationName); 2606 fDocumentType.getEntities().setNamedItem(entity); 2607 2608 if (fGrammarAccess) { 2611 StringBuffer str = new StringBuffer (); 2612 str.append("<!ENTITY "); 2613 str.append(fStringPool.toString(entityNameIndex)); 2614 str.append(' '); 2615 if (publicIdIndex != -1) { 2616 str.append("PUBLIC \""); 2617 str.append(fStringPool.toString(publicIdIndex)); 2618 str.append('"'); 2619 if (systemIdIndex != -1) { 2620 str.append(" \""); 2621 str.append(fStringPool.toString(systemIdIndex)); 2622 str.append('"'); 2623 } 2624 } 2625 else if (systemIdIndex != -1) { 2626 str.append("SYSTEM \""); 2627 str.append(fStringPool.toString(systemIdIndex)); 2628 str.append('"'); 2629 } 2630 str.append(" NDATA "); 2631 str.append(fStringPool.toString(notationNameIndex)); 2632 str.append('>'); 2633 Node unparsedEntity = fDocumentImpl.createComment(str.toString()); 2634 Node schema = XUtil.getFirstChildElement(fDocumentType, "schema"); 2635 schema.appendChild(unparsedEntity); 2636 } 2637 } 2638 2639 } 2641 2644 public void notationDecl(int notationNameIndex, int publicIdIndex, int systemIdIndex) 2645 throws Exception { 2646 2647 if (fDeferredDocumentImpl != null) { 2649 2650 2653 int newNotationIndex = fDeferredDocumentImpl.createNotation(notationNameIndex, publicIdIndex, systemIdIndex); 2654 2655 fDeferredDocumentImpl.appendChild(fDocumentTypeIndex, newNotationIndex); 2656 2657 if (fGrammarAccess) { 2659 int schemaIndex = getLastChildElement(fDocumentTypeIndex, "schema"); 2660 String notationName = fStringPool.toString(notationNameIndex); 2661 int notationIndex = getLastChildElement(schemaIndex, "notation", "name", notationName); 2662 if (notationIndex == -1) { 2663 int handle = fAttrList.startAttrList(); 2664 fAttrList.addAttr( 2665 fStringPool.addSymbol("name"), 2666 fStringPool.addString(notationName), 2667 fStringPool.addSymbol("NMTOKEN"), 2668 true, 2669 false); if (publicIdIndex != -1) { 2671 fAttrList.addAttr( 2672 fStringPool.addSymbol("public"), 2673 publicIdIndex, 2674 fStringPool.addSymbol("CDATA"), 2675 true, 2676 false); } 2678 if (systemIdIndex != -1) { 2679 fAttrList.addAttr( 2680 fStringPool.addSymbol("system"), 2681 systemIdIndex, 2682 fStringPool.addSymbol("CDATA"), 2683 true, 2684 false); } 2686 fAttrList.endAttrList(); 2687 notationIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol("notation"), fAttrList, handle); 2688 fDeferredDocumentImpl.appendChild(schemaIndex, notationIndex); 2689 } 2690 } 2691 } 2692 2693 else if (fDocumentImpl != null) { 2695 2696 2699 String notationName = fStringPool.toString(notationNameIndex); 2700 String publicId = fStringPool.toString(publicIdIndex); 2701 String systemId = fStringPool.toString(systemIdIndex); 2702 2703 NotationImpl notationImpl = (NotationImpl)fDocumentImpl.createNotation(notationName); 2704 notationImpl.setPublicId(publicId); 2705 if (systemIdIndex != -1) { 2706 notationImpl.setSystemId(systemId); 2707 } 2708 2709 fDocumentType.getNotations().setNamedItem(notationImpl); 2710 2711 if (fGrammarAccess) { 2713 Element schema = XUtil.getFirstChildElement(fDocumentType, "schema"); 2714 Element notation = XUtil.getFirstChildElement(schema, "notation", "name", notationName); 2715 if (notation == null) { 2716 notation = fDocument.createElement("notation"); 2717 notation.setAttribute("name", notationName); 2718 if (publicId != null) { 2721 notation.setAttribute("public", publicId); 2722 } 2723 if (systemIdIndex != -1) { 2724 notation.setAttribute("system", systemId); 2725 } 2726 schema.appendChild(notation); 2727 } 2728 } 2729 } 2730 2731 } 2733 2737 2738 private int getFirstChildElement(int nodeIndex) { 2739 int childIndex = getLastChildElement(nodeIndex); 2740 while (childIndex != -1) { 2741 int prevIndex = getPrevSiblingElement(childIndex); 2742 if (prevIndex == -1) { 2743 break; 2744 } 2745 childIndex = prevIndex; 2746 } 2747 return childIndex; 2748 } 2749 2750 2751 private int getFirstChildElement(int nodeIndex, String name) { 2752 int childIndex = getLastChildElement(nodeIndex); 2753 if (childIndex != -1) { 2754 int nameIndex = fStringPool.addSymbol(name); 2755 while (childIndex != -1) { 2756 if (fDeferredDocumentImpl.getNodeName(childIndex, false) == nameIndex) { 2757 break; 2758 } 2759 int prevIndex = getPrevSiblingElement(childIndex); 2760 childIndex = prevIndex; 2761 } 2762 } 2763 return childIndex; 2764 } 2765 2766 2767 private int getLastChildElement(int nodeIndex) { 2768 int childIndex = fDeferredDocumentImpl.getLastChild(nodeIndex, false); 2769 while (childIndex != -1) { 2770 if (fDeferredDocumentImpl.getNodeType(childIndex, false) == Node.ELEMENT_NODE) { 2771 return childIndex; 2772 } 2773 childIndex = fDeferredDocumentImpl.getPrevSibling(childIndex, false); 2774 } 2775 return -1; 2776 } 2777 2778 2779 private int getPrevSiblingElement(int nodeIndex) { 2780 int siblingIndex = fDeferredDocumentImpl.getPrevSibling(nodeIndex, false); 2781 while (siblingIndex != -1) { 2782 if (fDeferredDocumentImpl.getNodeType(siblingIndex, false) == Node.ELEMENT_NODE) { 2783 return siblingIndex; 2784 } 2785 siblingIndex = fDeferredDocumentImpl.getPrevSibling(siblingIndex, false); 2786 } 2787 return -1; 2788 } 2789 2790 2791 private int getLastChildElement(int nodeIndex, String elementName) { 2792 int childIndex = getLastChildElement(nodeIndex); 2793 if (childIndex != -1) { 2794 while (childIndex != -1) { 2795 String nodeName = fDeferredDocumentImpl.getNodeNameString(childIndex, false); 2796 if (nodeName.equals(elementName)) { 2797 return childIndex; 2798 } 2799 childIndex = getPrevSiblingElement(childIndex); 2800 } 2801 } 2802 return -1; 2803 } 2804 2805 2806 private int getPrevSiblingElement(int nodeIndex, String elementName) { 2807 int siblingIndex = getPrevSiblingElement(nodeIndex); 2808 if (siblingIndex != -1) { 2809 while (siblingIndex != -1) { 2810 String nodeName = fDeferredDocumentImpl.getNodeNameString(siblingIndex, false); 2811 if (nodeName.equals(elementName)) { 2812 return siblingIndex; 2813 } 2814 siblingIndex = getPrevSiblingElement(siblingIndex); 2815 } 2816 } 2817 return -1; 2818 } 2819 2820 2821 private int getLastChildElement(int nodeIndex, String elemName, String attrName, String attrValue) { 2822 int childIndex = getLastChildElement(nodeIndex, elemName); 2823 if (childIndex != -1) { 2824 while (childIndex != -1) { 2825 int attrIndex = fDeferredDocumentImpl.getNodeValue(childIndex, false); 2826 while (attrIndex != -1) { 2827 String nodeName = fDeferredDocumentImpl.getNodeNameString(attrIndex, false); 2828 if (nodeName.equals(attrName)) { 2829 int textIndex = fDeferredDocumentImpl.getLastChild(attrIndex, false); 2831 String nodeValue = fDeferredDocumentImpl.getNodeValueString(textIndex, false); 2832 if (nodeValue.equals(attrValue)) { 2833 return childIndex; 2834 } 2835 } 2836 attrIndex = fDeferredDocumentImpl.getPrevSibling(attrIndex, false); 2837 } 2838 childIndex = getPrevSiblingElement(childIndex, elemName); 2839 } 2840 } 2841 return -1; 2842 } 2843 2844 2845 private int getPrevSiblingElement(int nodeIndex, String elemName, String attrName, String attrValue) { 2846 int siblingIndex = getPrevSiblingElement(nodeIndex, elemName); 2847 if (siblingIndex != -1) { 2848 int attributeNameIndex = fStringPool.addSymbol(attrName); 2849 while (siblingIndex != -1) { 2850 int attrIndex = fDeferredDocumentImpl.getNodeValue(siblingIndex, false); 2851 while (attrIndex != -1) { 2852 int attrValueIndex = fDeferredDocumentImpl.getNodeValue(attrIndex, false); 2853 if (attrValue.equals(fStringPool.toString(attrValueIndex))) { 2854 return siblingIndex; 2855 } 2856 attrIndex = fDeferredDocumentImpl.getPrevSibling(attrIndex, false); 2857 } 2858 siblingIndex = getPrevSiblingElement(siblingIndex, elemName); 2859 } 2860 } 2861 return -1; 2862 } 2863 2864 2871 private void copyInto(Node src, int destIndex) throws Exception { 2872 2873 boolean domimpl = src != null && src instanceof DocumentImpl; 2875 2876 Node start = src; 2878 Node parent = src; 2879 Node place = src; 2880 2881 while (place != null) { 2883 2884 int nodeIndex = -1; 2886 short type = place.getNodeType(); 2887 switch (type) { 2888 case Node.CDATA_SECTION_NODE: { 2889 boolean ignorable = domimpl && ((TextImpl)place).isIgnorableWhitespace(); 2890 nodeIndex = fDeferredDocumentImpl.createCDATASection(fStringPool.addString(place.getNodeValue()), ignorable); 2891 break; 2892 } 2893 case Node.COMMENT_NODE: { 2894 nodeIndex = fDeferredDocumentImpl.createComment(fStringPool.addString(place.getNodeValue())); 2895 break; 2896 } 2897 case Node.ELEMENT_NODE: { 2898 XMLAttrList attrList = null; 2899 int handle = -1; 2900 NamedNodeMap attrs = place.getAttributes(); 2901 if (attrs != null) { 2902 int length = attrs.getLength(); 2903 if (length > 0) { 2904 handle = fAttrList.startAttrList(); 2905 for (int i = 0; i < length; i++) { 2906 Attr attr = (Attr )attrs.item(i); 2907 String attrName = attr.getNodeName(); 2908 String attrValue = attr.getNodeValue(); 2909 fAttrList.addAttr( 2910 fStringPool.addSymbol(attrName), 2911 fStringPool.addString(attrValue), 2912 fStringPool.addSymbol("CDATA"), attr.getSpecified(), 2914 false); } 2916 fAttrList.endAttrList(); 2917 attrList = fAttrList; 2918 } 2919 } 2920 nodeIndex = fDeferredDocumentImpl.createElement(fStringPool.addSymbol(place.getNodeName()), attrList, handle); 2921 break; 2922 } 2923 case Node.ENTITY_REFERENCE_NODE: { 2924 nodeIndex = fDeferredDocumentImpl.createEntityReference(fStringPool.addSymbol(place.getNodeName())); 2925 break; 2926 } 2927 case Node.PROCESSING_INSTRUCTION_NODE: { 2928 nodeIndex = fDeferredDocumentImpl.createProcessingInstruction(fStringPool.addSymbol(place.getNodeName()), fStringPool.addString(place.getNodeValue())); 2929 break; 2930 } 2931 case Node.TEXT_NODE: { 2932 boolean ignorable = domimpl && ((TextImpl)place).isIgnorableWhitespace(); 2933 nodeIndex = fDeferredDocumentImpl.createTextNode(fStringPool.addString(place.getNodeValue()), ignorable); 2934 break; 2935 } 2936 default: { 2937 throw new IllegalArgumentException ("PAR010 Can't copy node type, "+ 2938 type+" ("+ 2939 place.getNodeName()+')' 2940 +"\n"+type+"\t"+place.getNodeName()); 2941 } 2942 } 2943 fDeferredDocumentImpl.appendChild(destIndex, nodeIndex); 2944 2945 if (place.hasChildNodes()) { 2947 parent = place; 2948 place = place.getFirstChild(); 2949 destIndex = nodeIndex; 2950 } 2951 2952 else { 2954 place = place.getNextSibling(); 2955 while (place == null && parent != start) { 2956 place = parent.getNextSibling(); 2957 parent = parent.getParentNode(); 2958 destIndex = fDeferredDocumentImpl.getParentNode(destIndex, false); 2959 } 2960 } 2961 2962 } 2963 2964 } 2966 2970 private void setOccurrenceCount(Element model, int minOccur, int maxOccur) { 2971 2972 model.setAttribute("minOccurs", Integer.toString(minOccur)); 2974 if (minOccur == 1) { 2975 ((AttrImpl)model.getAttributeNode("minOccurs")).setSpecified(false); 2976 } 2977 2978 if (maxOccur == -1) { 2980 model.setAttribute("maxOccurs", "*"); 2981 } 2982 else if (maxOccur != 1) { 2983 model.setAttribute("maxOccurs", Integer.toString(maxOccur)); 2984 } 2985 2986 } 2988 2989 private Element createChildren(XMLContentSpec.Provider provider, 2990 int index, XMLContentSpec node, 2991 DocumentImpl factory, 2992 Element parent) throws Exception { 2993 2994 provider.getContentSpec(index, node); 2996 int occurs = -1; 2997 switch (node.type) { 2998 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE: { 2999 occurs = '+'; 3000 provider.getContentSpec(node.value, node); 3001 break; 3002 } 3003 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE: { 3004 occurs = '*'; 3005 provider.getContentSpec(node.value, node); 3006 break; 3007 } 3008 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE: { 3009 occurs = '?'; 3010 provider.getContentSpec(node.value, node); 3011 break; 3012 } 3013 } 3014 3015 int nodeType = node.type; 3017 switch (nodeType) { 3018 3019 case XMLContentSpec.CONTENTSPECNODE_CHOICE: 3021 case XMLContentSpec.CONTENTSPECNODE_SEQ: { 3022 3023 int leftIndex = node.value; 3025 int rightIndex = node.otherValue; 3026 Element left = createChildren(provider, leftIndex, node, 3027 factory, parent); 3028 3029 Element right = createChildren(provider, rightIndex, node, 3031 factory, null); 3032 3033 boolean choice = nodeType == XMLContentSpec.CONTENTSPECNODE_CHOICE; 3035 String type = choice ? "choice" : "sequence"; 3036 Element model = left; 3037 if (!left.getNodeName().equals(type)) { 3038 String minOccurs = left.getAttribute("minOccurs"); 3039 String maxOccurs = left.getAttribute("maxOccurs"); 3040 boolean min1 = minOccurs.length() == 0 || minOccurs.equals("1"); 3041 boolean max1 = maxOccurs.length() == 0 || maxOccurs.equals("1"); 3042 if (parent == null || (min1 && max1)) { 3043 model = factory.createElement(type); 3044 model.appendChild(left); 3045 } 3046 else { 3047 model = parent; 3048 } 3049 } 3050 3051 switch (occurs) { 3053 case '+': { 3054 model.setAttribute("maxOccurs", "unbounded"); 3055 break; 3056 } 3057 case '*': { 3058 model.setAttribute("minOccurs", "0"); 3059 model.setAttribute("maxOccurs", "unbounded"); 3060 break; 3061 } 3062 case '?': { 3063 model.setAttribute("minOccurs", "0"); 3064 break; 3065 } 3066 } 3067 3068 model.appendChild(right); 3070 3071 return model; 3073 } 3074 3075 case XMLContentSpec.CONTENTSPECNODE_LEAF: { 3077 Element leaf = factory.createElement("element"); 3078 leaf.setAttribute("ref", fStringPool.toString(node.value)); 3079 switch (occurs) { 3080 case '+': { 3081 leaf.setAttribute("maxOccurs", "unbounded"); 3082 break; 3083 } 3084 case '*': { 3085 leaf.setAttribute("minOccurs", "0"); 3086 leaf.setAttribute("maxOccurs", "unbounded"); 3087 break; 3088 } 3089 case '?': { 3090 leaf.setAttribute("minOccurs", "0"); 3091 break; 3092 } 3093 } 3094 return leaf; 3095 } 3096 3097 } 3099 return null; 3101 3102 } 3104 3105 private int createChildren(XMLContentSpec.Provider provider, 3106 int index, XMLContentSpec node, 3107 DeferredDocumentImpl factory, 3108 int parent) throws Exception { 3109 3110 provider.getContentSpec(index, node); 3112 int occurs = -1; 3113 switch (node.type) { 3114 case XMLContentSpec.CONTENTSPECNODE_ONE_OR_MORE: { 3115 occurs = '+'; 3116 provider.getContentSpec(node.value, node); 3117 break; 3118 } 3119 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_MORE: { 3120 occurs = '*'; 3121 provider.getContentSpec(node.value, node); 3122 break; 3123 } 3124 case XMLContentSpec.CONTENTSPECNODE_ZERO_OR_ONE: { 3125 occurs = '?'; 3126 provider.getContentSpec(node.value, node); 3127 break; 3128 } 3129 } 3130 3131 int nodeType = node.type; 3133 switch (nodeType) { 3134 3135 case XMLContentSpec.CONTENTSPECNODE_CHOICE: 3137 case XMLContentSpec.CONTENTSPECNODE_SEQ: { 3138 3139 int leftIndex = node.value; 3141 int rightIndex = node.otherValue; 3142 int left = createChildren(provider, leftIndex, node, 3143 factory, parent); 3144 3145 int right = createChildren(provider, rightIndex, node, 3147 factory, -1); 3148 3149 boolean choice = nodeType == XMLContentSpec.CONTENTSPECNODE_CHOICE; 3151 int type = fStringPool.addSymbol(choice ? "choice" : "sequence"); 3152 int model = left; 3153 if (factory.getNodeName(left, false) != type) { 3154 int minOccurs = factory.getAttribute(left, fStringPool.addSymbol("minOccurs")); 3155 int maxOccurs = factory.getAttribute(left, fStringPool.addSymbol("maxOccurs")); 3156 boolean min1 = minOccurs == -1 || fStringPool.toString(minOccurs).equals("1"); 3157 boolean max1 = maxOccurs == -1 || fStringPool.toString(maxOccurs).equals("1"); 3158 if (parent == -1 || (min1 && max1)) { 3159 model = factory.createElement(type, null, -1); 3160 factory.appendChild(model, left); 3161 } 3162 else { 3163 model = parent; 3164 } 3165 } 3166 3167 switch (occurs) { 3169 case '+': { 3170 int maxOccurs = factory.createAttribute(fStringPool.addSymbol("maxOccurs"), 3171 fStringPool.addString("unbounded"), 3172 true); 3173 factory.setAttributeNode(model, maxOccurs); 3174 break; 3175 } 3176 case '*': { 3177 int minOccurs = factory.createAttribute(fStringPool.addSymbol("minOccurs"), 3178 fStringPool.addString("0"), 3179 true); 3180 factory.setAttributeNode(model, minOccurs); 3181 int maxOccurs = factory.createAttribute(fStringPool.addSymbol("maxOccurs"), 3182 fStringPool.addString("unbounded"), 3183 true); 3184 factory.setAttributeNode(model, maxOccurs); 3185 break; 3186 } 3187 case '?': { 3188 int minOccurs = factory.createAttribute(fStringPool.addSymbol("minOccurs"), 3189 fStringPool.addString("0"), 3190 true); 3191 factory.setAttributeNode(model, minOccurs); 3192 break; 3193 } 3194 } 3195 3196 factory.appendChild(model, right); 3198 3199 return model; 3201 } 3202 3203 case XMLContentSpec.CONTENTSPECNODE_LEAF: { 3205 int handle = fAttrList.startAttrList(); 3206 fAttrList.addAttr( 3207 fStringPool.addSymbol("ref"), 3208 fStringPool.addString(fStringPool.toString(node.value)), 3209 fStringPool.addSymbol("NMTOKEN"), 3210 true, 3211 false); switch (occurs) { 3213 case '+': { 3214 fAttrList.addAttr( 3215 fStringPool.addSymbol("maxOccurs"), 3216 fStringPool.addString("unbounded"), 3217 fStringPool.addSymbol("CDATA"), 3218 true, 3219 false); break; 3221 } 3222 case '*': { 3223 fAttrList.addAttr( 3224 fStringPool.addSymbol("minOccurs"), 3225 fStringPool.addString("0"), 3226 fStringPool.addSymbol("NMTOKEN"), 3227 true, 3228 false); fAttrList.addAttr( 3230 fStringPool.addSymbol("maxOccurs"), 3231 fStringPool.addString("unbounded"), 3232 fStringPool.addSymbol("CDATA"), 3233 true, 3234 false); break; 3236 } 3237 case '?': { 3238 fAttrList.addAttr( 3239 fStringPool.addSymbol("minOccurs"), 3240 fStringPool.addString("0"), 3241 fStringPool.addSymbol("NMTOKEN"), 3242 true, 3243 false); break; 3245 } 3246 } 3247 fAttrList.endAttrList(); 3248 int leaf = factory.createElement(fStringPool.addSymbol("element"), fAttrList, handle); 3249 return leaf; 3250 } 3251 3252 } 3254 return -1; 3256 3257 } 3259} | Popular Tags |