1 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import java.io.IOException ; 61 import java.util.Locale ; 62 63 import com.sun.org.apache.xerces.internal.impl.Constants; 64 import com.sun.org.apache.xerces.internal.xs.PSVIProvider; 65 import com.sun.org.apache.xerces.internal.util.EntityResolverWrapper; 66 import com.sun.org.apache.xerces.internal.util.EntityResolver2Wrapper; 67 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper; 68 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter; 69 import com.sun.org.apache.xerces.internal.util.SymbolHash; 70 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 71 import com.sun.org.apache.xerces.internal.xni.Augmentations; 72 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 73 import com.sun.org.apache.xerces.internal.xni.QName; 74 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 75 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 76 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 77 import com.sun.org.apache.xerces.internal.xni.XMLString; 78 import com.sun.org.apache.xerces.internal.xni.XNIException; 79 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 80 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 81 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 82 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 83 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException; 84 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 85 import com.sun.org.apache.xerces.internal.xs.AttributePSVI; 86 import com.sun.org.apache.xerces.internal.xs.ElementPSVI; 87 import org.xml.sax.AttributeList ; 88 import org.xml.sax.Attributes ; 89 import org.xml.sax.ext.Attributes2 ; 90 import org.xml.sax.ContentHandler ; 91 import org.xml.sax.DTDHandler ; 92 import org.xml.sax.DocumentHandler ; 93 import org.xml.sax.EntityResolver ; 94 import org.xml.sax.ErrorHandler ; 95 import org.xml.sax.InputSource ; 96 import org.xml.sax.Locator ; 97 import org.xml.sax.ext.Locator2Impl ; 98 import org.xml.sax.ext.Locator2 ; 99 import org.xml.sax.Parser ; 100 import org.xml.sax.SAXException ; 101 import org.xml.sax.SAXNotRecognizedException ; 102 import org.xml.sax.SAXNotSupportedException ; 103 import org.xml.sax.SAXParseException ; 104 import org.xml.sax.XMLReader ; 105 import org.xml.sax.ext.DeclHandler ; 106 import org.xml.sax.ext.EntityResolver2 ; 107 import org.xml.sax.ext.LexicalHandler ; 108 import org.xml.sax.helpers.LocatorImpl ; 109 110 120 public abstract class AbstractSAXParser 121 extends AbstractXMLDocumentParser 122 implements PSVIProvider, Parser , XMLReader { 125 126 130 132 133 protected static final String NAMESPACES = 134 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 135 136 137 protected static final String NAMESPACE_PREFIXES = 138 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE; 139 140 141 protected static final String STRING_INTERNING = 142 Constants.SAX_FEATURE_PREFIX + Constants.STRING_INTERNING_FEATURE; 143 144 145 146 protected static final String ALLOW_UE_AND_NOTATION_EVENTS = 149 Constants.SAX_FEATURE_PREFIX + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE; 150 151 152 private static final String [] RECOGNIZED_FEATURES = { 153 NAMESPACES, 154 NAMESPACE_PREFIXES, 155 STRING_INTERNING, 156 }; 157 158 160 161 protected static final String LEXICAL_HANDLER = 162 Constants.SAX_PROPERTY_PREFIX + Constants.LEXICAL_HANDLER_PROPERTY; 163 164 165 protected static final String DECLARATION_HANDLER = 166 Constants.SAX_PROPERTY_PREFIX + Constants.DECLARATION_HANDLER_PROPERTY; 167 168 169 protected static final String DOM_NODE = 170 Constants.SAX_PROPERTY_PREFIX + Constants.DOM_NODE_PROPERTY; 171 172 173 private static final String [] RECOGNIZED_PROPERTIES = { 174 LEXICAL_HANDLER, 175 DECLARATION_HANDLER, 176 DOM_NODE, 177 }; 178 179 183 185 186 protected boolean fNamespaces; 187 188 189 protected boolean fNamespacePrefixes = false; 190 191 192 protected boolean fLexicalHandlerParameterEntities = true; 193 194 196 197 protected ContentHandler fContentHandler; 198 199 200 protected DocumentHandler fDocumentHandler; 201 202 203 protected NamespaceContext fNamespaceContext; 204 205 206 protected org.xml.sax.DTDHandler fDTDHandler; 207 208 209 protected DeclHandler fDeclHandler; 210 211 212 protected LexicalHandler fLexicalHandler; 213 214 protected QName fQName = new QName(); 215 216 protected boolean resolve_dtd_uris = true; 217 protected boolean startDocumentCalled = false; 218 protected boolean resolverType = true; 219 protected boolean locatorType = false; 220 protected boolean attributeType = true; 221 protected boolean isStandalone = false; 222 224 229 protected boolean fParseInProgress = false; 230 231 protected String fVersion; 233 234 private final AttributesProxy fAttributesProxy = new AttributesProxy(); 236 private Augmentations fAugmentations = null; 237 238 private static final int BUFFER_SIZE = 20; 241 private char[] fCharBuffer = new char[BUFFER_SIZE]; 242 243 protected SymbolHash fDeclaredAttrs = null; 247 248 252 253 protected AbstractSAXParser(XMLParserConfiguration config) { 254 super(config); 255 256 config.addRecognizedFeatures(RECOGNIZED_FEATURES); 257 config.addRecognizedProperties(RECOGNIZED_PROPERTIES); 258 259 try { 260 config.setFeature(ALLOW_UE_AND_NOTATION_EVENTS, false); 261 } 262 catch (XMLConfigurationException e) { 263 } 265 } 267 271 297 public void startDocument(XMLLocator locator, String encoding, 298 NamespaceContext namespaceContext, Augmentations augs) 299 throws XNIException { 300 301 fNamespaceContext = namespaceContext; 302 startDocumentCalled = true; 303 try { 304 if (fDocumentHandler != null) { 306 if (locator != null) { 307 fDocumentHandler.setDocumentLocator(new LocatorProxy(locator)); 308 } 309 fDocumentHandler.startDocument(); 310 } 311 312 if (fContentHandler != null) { 314 if (locator != null) { 315 locatorType = true; 316 fContentHandler.setDocumentLocator(new LocatorProxy(locator)); 317 } 318 fContentHandler.startDocument(); 319 } 320 } 321 catch (SAXException e) { 322 throw new XNIException(e); 323 } 324 325 } 327 340 public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) 341 throws XNIException { 342 343 fVersion = version; 346 if(standalone != null && standalone.equalsIgnoreCase("yes")) 347 isStandalone = true; 348 else 349 isStandalone = false; 350 } 352 364 public void doctypeDecl(String rootElement, 365 String publicId, String systemId, Augmentations augs) 366 throws XNIException { 367 fInDTD = true; 368 369 try { 370 if (fLexicalHandler != null) { 372 fLexicalHandler.startDTD(rootElement, publicId, systemId); 373 } 374 } 375 catch (SAXException e) { 376 throw new XNIException(e); 377 } 378 379 if(fDeclHandler != null) { 381 fDeclaredAttrs = new SymbolHash(); 382 } 383 384 } 386 410 public void startGeneralEntity(String name, XMLResourceIdentifier identifier, 411 String encoding, Augmentations augs) 412 throws XNIException { 413 414 try { 415 if (augs != null && Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 417 if (fContentHandler != null) { 419 fContentHandler.skippedEntity(name); 420 } 421 } 422 else { 423 if (fLexicalHandler != null) { 425 fLexicalHandler.startEntity(name); 426 } 427 } 428 } 429 catch (SAXException e) { 430 throw new XNIException(e); 431 } 432 433 } 435 454 public void endGeneralEntity(String name, Augmentations augs) throws XNIException { 455 456 try { 457 if (augs == null || !Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 459 if (fLexicalHandler != null) { 461 fLexicalHandler.endEntity(name); 462 } 463 } 464 } 465 catch (SAXException e) { 466 throw new XNIException(e); 467 } 468 469 } 471 482 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 483 throws XNIException { 484 485 try { 486 if (fDocumentHandler != null) { 488 fAttributesProxy.setAttributes(attributes); 491 fDocumentHandler.startElement(element.rawname, fAttributesProxy); 492 } 493 494 if (fContentHandler != null) { 496 497 if (fNamespaces) { 498 startNamespaceMapping(); 500 501 int len = attributes.getLength(); 508 for (int i = len - 1; i >= 0; --i) { 509 attributes.getName(i, fQName); 510 if ((fQName.prefix == XMLSymbols.PREFIX_XMLNS) || 511 (fQName.rawname == XMLSymbols.PREFIX_XMLNS)) { 512 if (!fNamespacePrefixes) { 513 attributes.removeAttributeAt(i); 515 } 516 else { 517 fQName.prefix = ""; 520 fQName.uri = ""; 521 fQName.localpart = ""; 522 attributes.setName(i, fQName); 523 } 524 } 525 } 526 } 527 528 fAugmentations = augs; 529 530 String uri = element.uri != null ? element.uri : ""; 531 String localpart = fNamespaces ? element.localpart : ""; 532 fAttributesProxy.setAttributes(attributes); 533 fContentHandler.startElement(uri, localpart, element.rawname, 534 fAttributesProxy); 535 } 536 } 537 catch (SAXException e) { 538 throw new XNIException(e); 539 } 540 541 } 543 551 public void characters(XMLString text, Augmentations augs) throws XNIException { 552 553 if (text.length == 0) { 556 return; 557 } 558 559 560 try { 561 if (fDocumentHandler != null) { 563 fDocumentHandler.characters(text.ch, text.offset, text.length); 566 } 567 568 if (fContentHandler != null) { 570 fContentHandler.characters(text.ch, text.offset, text.length); 571 } 572 } 573 catch (SAXException e) { 574 throw new XNIException(e); 575 } 576 577 } 579 592 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 593 594 try { 595 if (fDocumentHandler != null) { 597 fDocumentHandler.ignorableWhitespace(text.ch, text.offset, text.length); 598 } 599 600 if (fContentHandler != null) { 602 fContentHandler.ignorableWhitespace(text.ch, text.offset, text.length); 603 } 604 } 605 catch (SAXException e) { 606 throw new XNIException(e); 607 } 608 609 } 611 619 public void endElement(QName element, Augmentations augs) throws XNIException { 620 621 622 try { 623 if (fDocumentHandler != null) { 625 fDocumentHandler.endElement(element.rawname); 626 } 627 628 if (fContentHandler != null) { 630 fAugmentations = augs; 631 String uri = element.uri != null ? element.uri : ""; 632 String localpart = fNamespaces ? element.localpart : ""; 633 fContentHandler.endElement(uri, localpart, 634 element.rawname); 635 if (fNamespaces) { 636 endNamespaceMapping(); 637 } 638 } 639 } 640 catch (SAXException e) { 641 throw new XNIException(e); 642 } 643 644 } 646 652 public void startCDATA(Augmentations augs) throws XNIException { 653 654 try { 655 if (fLexicalHandler != null) { 657 fLexicalHandler.startCDATA(); 658 } 659 } 660 catch (SAXException e) { 661 throw new XNIException(e); 662 } 663 664 } 666 672 public void endCDATA(Augmentations augs) throws XNIException { 673 674 try { 675 if (fLexicalHandler != null) { 677 fLexicalHandler.endCDATA(); 678 } 679 } 680 catch (SAXException e) { 681 throw new XNIException(e); 682 } 683 684 } 686 694 public void comment(XMLString text, Augmentations augs) throws XNIException { 695 696 try { 697 if (fLexicalHandler != null) { 699 fLexicalHandler.comment(text.ch, 0, text.length); 700 } 701 } 702 catch (SAXException e) { 703 throw new XNIException(e); 704 } 705 706 } 708 725 public void processingInstruction(String target, XMLString data, Augmentations augs) 726 throws XNIException { 727 728 734 try { 735 if (fDocumentHandler != null) { 737 fDocumentHandler.processingInstruction(target, 738 data.toString()); 739 } 740 741 if (fContentHandler != null) { 743 fContentHandler.processingInstruction(target, data.toString()); 744 } 745 } 746 catch (SAXException e) { 747 throw new XNIException(e); 748 } 749 750 } 752 753 759 public void endDocument(Augmentations augs) throws XNIException { 760 761 try { 762 if (fDocumentHandler != null) { 764 fDocumentHandler.endDocument(); 765 } 766 767 if (fContentHandler != null) { 769 fContentHandler.endDocument(); 770 } 771 } 772 catch (SAXException e) { 773 throw new XNIException(e); 774 } 775 776 } 778 782 790 public void startExternalSubset(XMLResourceIdentifier identifier, 791 Augmentations augs) throws XNIException { 792 startParameterEntity("[dtd]", null, null, augs); 793 } 794 795 803 public void endExternalSubset(Augmentations augs) throws XNIException { 804 endParameterEntity("[dtd]", augs); 805 } 806 807 832 public void startParameterEntity(String name, 833 XMLResourceIdentifier identifier, 834 String encoding, Augmentations augs) 835 throws XNIException { 836 837 try { 838 if (augs != null && Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 840 if (fContentHandler != null) { 842 fContentHandler.skippedEntity(name); 843 } 844 } 845 else { 846 if (fLexicalHandler != null && fLexicalHandlerParameterEntities) { 848 fLexicalHandler.startEntity(name); 849 } 850 } 851 } 852 catch (SAXException e) { 853 throw new XNIException(e); 854 } 855 856 } 858 878 public void endParameterEntity(String name, Augmentations augs) throws XNIException { 879 880 try { 881 if (augs == null || !Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 883 if (fLexicalHandler != null && fLexicalHandlerParameterEntities) { 885 fLexicalHandler.endEntity(name); 886 } 887 } 888 } 889 catch (SAXException e) { 890 throw new XNIException(e); 891 } 892 893 } 895 906 public void elementDecl(String name, String contentModel, Augmentations augs) 907 throws XNIException { 908 909 try { 910 if (fDeclHandler != null) { 912 fDeclHandler.elementDecl(name, contentModel); 913 } 914 } 915 catch (SAXException e) { 916 throw new XNIException(e); 917 } 918 919 } 921 947 public void attributeDecl(String elementName, String attributeName, 948 String type, String [] enumeration, 949 String defaultType, XMLString defaultValue, 950 XMLString nonNormalizedDefaultValue, Augmentations augs) throws XNIException { 951 952 try { 953 if (fDeclHandler != null) { 955 String elemAttr = new StringBuffer (elementName).append("<").append(attributeName).toString(); 957 if(fDeclaredAttrs.get(elemAttr) != null) { 958 return; 960 } 961 fDeclaredAttrs.put(elemAttr, Boolean.TRUE); 962 if (type.equals("NOTATION") || 963 type.equals("ENUMERATION")) { 964 965 StringBuffer str = new StringBuffer (); 966 if (type.equals("NOTATION")) { 967 str.append(type); 968 str.append(" ("); 969 } 970 else { 971 str.append("("); 972 } 973 for (int i = 0; i < enumeration.length; i++) { 974 str.append(enumeration[i]); 975 if (i < enumeration.length - 1) { 976 str.append('|'); 977 } 978 } 979 str.append(')'); 980 type = str.toString(); 981 } 982 String value = (defaultValue==null) ? null : defaultValue.toString(); 983 fDeclHandler.attributeDecl(elementName, attributeName, 984 type, defaultType, value); 985 } 986 } 987 catch (SAXException e) { 988 throw new XNIException(e); 989 } 990 991 } 993 1010 public void internalEntityDecl(String name, XMLString text, 1011 XMLString nonNormalizedText, 1012 Augmentations augs) throws XNIException { 1013 1014 try { 1015 if (fDeclHandler != null) { 1017 fDeclHandler.internalEntityDecl(name, text.toString()); 1018 } 1019 } 1020 catch (SAXException e) { 1021 throw new XNIException(e); 1022 } 1023 1024 } 1026 1039 public void externalEntityDecl(String name, XMLResourceIdentifier identifier, 1040 Augmentations augs) throws XNIException { 1041 1042 String publicId = identifier.getPublicId(); 1043 String literalSystemId = identifier.getLiteralSystemId(); 1044 String expandedSystemId = identifier.getExpandedSystemId(); 1045 try { 1046 if (fDeclHandler != null) { 1048 fDeclHandler.externalEntityDecl(name, publicId, ((resolve_dtd_uris) ? expandedSystemId : literalSystemId)); 1049 } 1050 } 1051 catch (SAXException e) { 1052 throw new XNIException(e); 1053 } 1054 1055 } 1057 1070 public void unparsedEntityDecl(String name, XMLResourceIdentifier identifier, 1071 String notation, 1072 Augmentations augs) throws XNIException { 1073 1074 String publicId = identifier.getPublicId(); 1075 String expandedSystemId = identifier.getExpandedSystemId(); 1076 String literalSystemId = identifier.getLiteralSystemId(); 1077 try { 1078 if (fDTDHandler != null) { 1080 fDTDHandler.unparsedEntityDecl(name, publicId, 1081 ((resolve_dtd_uris) ? expandedSystemId : literalSystemId), notation); 1082 } 1083 } 1084 catch (SAXException e) { 1085 throw new XNIException(e); 1086 } 1087 1088 } 1090 1101 public void notationDecl(String name, XMLResourceIdentifier identifier, 1102 Augmentations augs) throws XNIException { 1103 1104 String publicId = identifier.getPublicId(); 1105 String expandedSystemId = identifier.getExpandedSystemId(); 1106 String literalSystemId = identifier.getLiteralSystemId(); 1107 try { 1108 if (fDTDHandler != null) { 1110 fDTDHandler.notationDecl(name, publicId, ((resolve_dtd_uris) ? expandedSystemId : literalSystemId)); 1111 } 1112 } 1113 catch (SAXException e) { 1114 throw new XNIException(e); 1115 } 1116 1117 } 1119 1127 public void endDTD(Augmentations augs) throws XNIException { 1128 fInDTD = false; 1129 1130 try { 1131 if (fLexicalHandler != null) { 1133 fLexicalHandler.endDTD(); 1134 } 1135 } 1136 catch (SAXException e) { 1137 throw new XNIException(e); 1138 } 1139 if(fDeclaredAttrs != null) { 1140 fDeclaredAttrs.clear(); 1142 } 1143 1144 } 1146 1150 1163 public void parse(String systemId) throws SAXException , IOException { 1164 1165 XMLInputSource source = new XMLInputSource(null, systemId, null); 1167 try { 1168 parse(source); 1169 } 1170 1171 catch (XMLParseException e) { 1173 Exception ex = e.getException(); 1174 if (ex == null) { 1175 locatorType = true; 1178 LocatorImpl locatorImpl = new LocatorImpl (){ 1179 public String getXMLVersion() { 1180 return fVersion; 1181 } 1182 public String getEncoding() { 1188 return null; 1189 } 1190 }; 1191 locatorImpl.setPublicId(e.getPublicId()); 1192 locatorImpl.setSystemId(e.getExpandedSystemId()); 1193 locatorImpl.setLineNumber(e.getLineNumber()); 1194 locatorImpl.setColumnNumber(e.getColumnNumber()); 1195 throw new SAXParseException (e.getMessage(), locatorImpl); 1196 } 1197 if (ex instanceof SAXException ) { 1198 throw (SAXException )ex; 1200 } 1201 if (ex instanceof IOException ) { 1202 throw (IOException )ex; 1203 } 1204 throw new SAXException (ex); 1205 } 1206 catch (XNIException e) { 1207 Exception ex = e.getException(); 1208 if (ex == null) { 1209 throw new SAXException (e.getMessage()); 1210 } 1211 if (ex instanceof SAXException ) { 1212 throw (SAXException )ex; 1213 } 1214 if (ex instanceof IOException ) { 1215 throw (IOException )ex; 1216 } 1217 throw new SAXException (ex); 1218 } 1219 1220 } 1222 1230 public void parse(InputSource inputSource) 1231 throws SAXException , IOException { 1232 1233 try { 1235 XMLInputSource xmlInputSource = 1236 new XMLInputSource(inputSource.getPublicId(), 1237 inputSource.getSystemId(), 1238 null); 1239 xmlInputSource.setByteStream(inputSource.getByteStream()); 1240 xmlInputSource.setCharacterStream(inputSource.getCharacterStream()); 1241 xmlInputSource.setEncoding(inputSource.getEncoding()); 1242 parse(xmlInputSource); 1243 } 1244 1245 catch (XMLParseException e) { 1247 Exception ex = e.getException(); 1248 if (ex == null) { 1249 locatorType = true; 1252 LocatorImpl locatorImpl = new LocatorImpl () { 1253 public String getXMLVersion() { 1254 return fVersion; 1255 } 1256 public String getEncoding() { 1262 return null; 1263 } 1264 }; 1265 locatorImpl.setPublicId(e.getPublicId()); 1266 locatorImpl.setSystemId(e.getExpandedSystemId()); 1267 locatorImpl.setLineNumber(e.getLineNumber()); 1268 locatorImpl.setColumnNumber(e.getColumnNumber()); 1269 throw new SAXParseException (e.getMessage(), locatorImpl); 1270 } 1271 if (ex instanceof SAXException ) { 1272 throw (SAXException )ex; 1274 } 1275 if (ex instanceof IOException ) { 1276 throw (IOException )ex; 1277 } 1278 throw new SAXException (ex); 1279 } 1280 catch (XNIException e) { 1281 Exception ex = e.getException(); 1282 if (ex == null) { 1283 throw new SAXException (e.getMessage()); 1284 } 1285 if (ex instanceof SAXException ) { 1286 throw (SAXException )ex; 1287 } 1288 if (ex instanceof IOException ) { 1289 throw (IOException )ex; 1290 } 1291 throw new SAXException (ex); 1292 } 1293 1294 } 1296 1303 public void setEntityResolver(EntityResolver resolver) { 1304 1305 try { 1306 if(resolver instanceof EntityResolver2 ){ 1307 resolverType = true; 1308 fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolver2Wrapper((EntityResolver2 )resolver)); 1309 }else 1310 fConfiguration.setProperty(ENTITY_RESOLVER, new EntityResolverWrapper(resolver)); 1311 } 1312 catch (XMLConfigurationException e) { 1313 } 1315 1316 } 1318 1325 public EntityResolver getEntityResolver() { 1326 1327 EntityResolver entityResolver = null; 1328 try { 1329 XMLEntityResolver xmlEntityResolver = 1330 (XMLEntityResolver)fConfiguration.getProperty(ENTITY_RESOLVER); 1331 if (xmlEntityResolver != null){ 1332 if( xmlEntityResolver instanceof EntityResolverWrapper) { 1333 entityResolver = ((EntityResolverWrapper)xmlEntityResolver).getEntityResolver(); 1334 }else if(xmlEntityResolver instanceof EntityResolver2Wrapper){ 1335 entityResolver = ((EntityResolver2Wrapper)xmlEntityResolver).getEntityResolver(); 1336 } 1337 } 1338 }catch (XMLConfigurationException e) { 1339 } 1341 return entityResolver; 1342 1343 } 1345 1361 public void setErrorHandler(ErrorHandler errorHandler) { 1362 1363 try { 1364 fConfiguration.setProperty(ERROR_HANDLER, 1365 new ErrorHandlerWrapper(errorHandler)); 1366 } 1367 catch (XMLConfigurationException e) { 1368 } 1370 1371 } 1373 1380 public ErrorHandler getErrorHandler() { 1381 1382 ErrorHandler errorHandler = null; 1383 try { 1384 XMLErrorHandler xmlErrorHandler = 1385 (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER); 1386 if (xmlErrorHandler != null && 1387 xmlErrorHandler instanceof ErrorHandlerWrapper) { 1388 errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler(); 1389 } 1390 } 1391 catch (XMLConfigurationException e) { 1392 } 1394 return errorHandler; 1395 1396 } 1398 1408 public void setLocale(Locale locale) throws SAXException { 1409 fConfiguration.setLocale(locale); 1412 1413 } 1415 1430 public void setDTDHandler(DTDHandler dtdHandler) { 1431 fDTDHandler = dtdHandler; 1432 } 1434 1438 1452 public void setDocumentHandler(DocumentHandler documentHandler) { 1453 fDocumentHandler = documentHandler; 1454 } 1456 1460 1475 public void setContentHandler(ContentHandler contentHandler) { 1476 fContentHandler = contentHandler; 1477 } 1479 1487 public ContentHandler getContentHandler() { 1488 return fContentHandler; 1489 } 1491 1498 public DTDHandler getDTDHandler() { 1499 return fDTDHandler; 1500 } 1502 1516 public void setFeature(String featureId, boolean state) 1517 throws SAXNotRecognizedException , SAXNotSupportedException { 1518 1519 try { 1520 1524 if (featureId.startsWith(Constants.SAX_FEATURE_PREFIX)) { 1525 final int suffixLength = featureId.length() - Constants.SAX_FEATURE_PREFIX.length(); 1526 1527 if (suffixLength == Constants.NAMESPACES_FEATURE.length() && 1529 featureId.endsWith(Constants.NAMESPACES_FEATURE)) { 1530 fConfiguration.setFeature(featureId, state); 1531 fNamespaces = state; 1532 return; 1533 } 1534 if (suffixLength == Constants.NAMESPACE_PREFIXES_FEATURE.length() && 1541 featureId.endsWith(Constants.NAMESPACE_PREFIXES_FEATURE)) { 1542 fConfiguration.setFeature(featureId, state); 1543 fNamespacePrefixes = state; 1544 return; 1545 } 1546 if (suffixLength == Constants.STRING_INTERNING_FEATURE.length() && 1551 featureId.endsWith(Constants.STRING_INTERNING_FEATURE)) { 1552 if (!state) { 1553 throw new SAXNotSupportedException ( 1554 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1555 "false-not-supported", new Object [] {featureId})); 1556 } 1557 return; 1558 } 1559 if (suffixLength == Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE.length() && 1564 featureId.endsWith(Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE)) { 1565 fLexicalHandlerParameterEntities = state; 1566 return; 1567 } 1568 1569 if(suffixLength == Constants.RESOLVE_DTD_URIS_FEATURE.length() && featureId.endsWith(Constants.RESOLVE_DTD_URIS_FEATURE)){ 1570 resolve_dtd_uris = state; 1571 fConfiguration.setFeature(featureId, state); 1572 return; 1573 } 1574 1575 if((suffixLength == Constants.XML_11_FEATURE.length() && 1576 featureId.endsWith(Constants.XML_11_FEATURE)) || 1577 (suffixLength == Constants.USE_ENTITY_RESOLVER2_FEATURE.length() && 1578 featureId.endsWith(Constants.USE_ENTITY_RESOLVER2_FEATURE))|| 1579 (suffixLength == Constants.USE_LOCATOR2_FEATURE.length() && 1580 featureId.endsWith(Constants.USE_LOCATOR2_FEATURE))|| 1581 (suffixLength == Constants.USE_ATTRIBUTES2_FEATURE.length() && 1582 featureId.endsWith(Constants.USE_ATTRIBUTES2_FEATURE))|| 1583 (suffixLength == Constants.IS_STANDALONE_FEATURE.length() && 1584 featureId.endsWith(Constants.IS_STANDALONE_FEATURE)) 1585 ){ 1586 throw new SAXNotSupportedException ( 1587 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1588 "feature-read-only", new Object [] {featureId})); 1589 } 1590 1591 1592 } 1596 1597 1601 1609 1610 1614 fConfiguration.setFeature(featureId, state); 1615 } 1616 catch (XMLConfigurationException e) { 1617 String identifier = e.getIdentifier(); 1618 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1619 throw new SAXNotRecognizedException ( 1620 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1621 "feature-not-recognized", new Object [] {identifier})); 1622 } 1623 else { 1624 throw new SAXNotSupportedException ( 1625 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1626 "feature-not-supported", new Object [] {identifier})); 1627 } 1628 } 1629 1630 } 1632 1646 public boolean getFeature(String featureId) 1647 throws SAXNotRecognizedException , SAXNotSupportedException { 1648 1649 try { 1650 1654 if (featureId.startsWith(Constants.SAX_FEATURE_PREFIX)) { 1655 final int suffixLength = featureId.length() - Constants.SAX_FEATURE_PREFIX.length(); 1656 1657 if (suffixLength == Constants.NAMESPACE_PREFIXES_FEATURE.length() && 1664 featureId.endsWith(Constants.NAMESPACE_PREFIXES_FEATURE)) { 1665 boolean state = fConfiguration.getFeature(featureId); 1666 return state; 1667 } 1668 if (suffixLength == Constants.STRING_INTERNING_FEATURE.length() && 1673 featureId.endsWith(Constants.STRING_INTERNING_FEATURE)) { 1674 return true; 1675 } 1676 1677 if (suffixLength == Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE.length() && 1682 featureId.endsWith(Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE)) { 1683 return fLexicalHandlerParameterEntities; 1684 } 1685 1686 if(suffixLength == Constants.USE_ENTITY_RESOLVER2_FEATURE.length() && 1687 featureId.endsWith(Constants.USE_ENTITY_RESOLVER2_FEATURE)){ 1688 return resolverType; 1689 } 1690 1691 if(suffixLength == Constants.USE_LOCATOR2_FEATURE.length() && featureId.endsWith(Constants.USE_LOCATOR2_FEATURE)){ 1692 return locatorType; 1693 1694 } 1695 if(suffixLength == Constants.USE_ATTRIBUTES2_FEATURE.length() && featureId.endsWith(Constants.USE_ATTRIBUTES2_FEATURE)){ 1696 return attributeType; 1697 } 1698 1699 if(suffixLength == Constants.IS_STANDALONE_FEATURE.length() && featureId.endsWith(Constants.IS_STANDALONE_FEATURE)){ 1700 return isStandalone; 1701 } 1702 1703 } 1707 1708 1712 1719 1720 return fConfiguration.getFeature(featureId); 1721 } 1722 catch (XMLConfigurationException e) { 1723 String identifier = e.getIdentifier(); 1724 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1725 throw new SAXNotRecognizedException ( 1726 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1727 "feature-not-recognized", new Object [] {identifier})); 1728 } 1729 else { 1730 throw new SAXNotSupportedException ( 1731 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1732 "feature-not-supported", new Object [] {identifier})); 1733 } 1734 } 1735 1736 } 1738 1753 public void setProperty(String propertyId, Object value) 1754 throws SAXNotRecognizedException , SAXNotSupportedException { 1755 1756 try { 1757 1761 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 1762 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 1763 1764 if (suffixLength == Constants.LEXICAL_HANDLER_PROPERTY.length() && 1771 propertyId.endsWith(Constants.LEXICAL_HANDLER_PROPERTY)) { 1772 try { 1773 setLexicalHandler((LexicalHandler )value); 1774 } 1775 catch (ClassCastException e) { 1776 throw new SAXNotSupportedException ( 1777 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1778 "incompatible-class", new Object [] {propertyId, "org.xml.sax.ext.LexicalHandler"})); 1779 } 1780 return; 1781 } 1782 if (suffixLength == Constants.DECLARATION_HANDLER_PROPERTY.length() && 1789 propertyId.endsWith(Constants.DECLARATION_HANDLER_PROPERTY)) { 1790 try { 1791 setDeclHandler((DeclHandler )value); 1792 } 1793 catch (ClassCastException e) { 1794 throw new SAXNotSupportedException ( 1795 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1796 "incompatible-class", new Object [] {propertyId, "org.xml.sax.ext.DeclHandler"})); 1797 } 1798 return; 1799 } 1800 if (suffixLength == Constants.DOM_NODE_PROPERTY.length() && 1811 propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) { 1812 throw new SAXNotSupportedException ( 1813 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1814 "property-read-only", new Object [] {propertyId})); 1815 } 1816 1817 if(suffixLength == Constants.DOCUMENT_XML_VERSION_PROPERTY.length() && propertyId.endsWith(Constants.DOCUMENT_XML_VERSION_PROPERTY)){ 1818 throw new SAXNotSupportedException ( 1819 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1820 "property-read-only", new Object [] {propertyId})); 1821 } 1822 1823 } 1827 1828 1832 1839 1840 1844 fConfiguration.setProperty(propertyId, value); 1845 } 1846 catch (XMLConfigurationException e) { 1847 String identifier = e.getIdentifier(); 1848 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1849 throw new SAXNotRecognizedException ( 1850 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1851 "property-not-recognized", new Object [] {identifier})); 1852 } 1853 else { 1854 throw new SAXNotSupportedException ( 1855 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1856 "property-not-supported", new Object [] {identifier})); 1857 } 1858 } 1859 1860 } 1862 1876 public Object getProperty(String propertyId) 1877 throws SAXNotRecognizedException , SAXNotSupportedException { 1878 1879 try { 1880 1884 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 1885 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 1886 1887 if (suffixLength == Constants.LEXICAL_HANDLER_PROPERTY.length() && 1894 propertyId.endsWith(Constants.LEXICAL_HANDLER_PROPERTY)) { 1895 return getLexicalHandler(); 1896 } 1897 if (suffixLength == Constants.DECLARATION_HANDLER_PROPERTY.length() && 1904 propertyId.endsWith(Constants.DECLARATION_HANDLER_PROPERTY)) { 1905 return getDeclHandler(); 1906 } 1907 if (suffixLength == Constants.DOM_NODE_PROPERTY.length() && 1918 propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) { 1919 throw new SAXNotSupportedException ( 1921 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1922 "dom-node-read-not-supported", null)); 1923 } 1924 if(suffixLength == Constants.DOCUMENT_XML_VERSION_PROPERTY.length() && 1925 propertyId.endsWith(Constants.DOCUMENT_XML_VERSION_PROPERTY)){ 1926 if(startDocumentCalled){ 1927 return fVersion; 1928 }else{ 1929 throw new SAXNotSupportedException ( 1930 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1931 "start-document-not-called", new Object [] {propertyId})); 1932 } 1933 1934 } 1935 } 1939 1940 1944 1951 1952 1956 return fConfiguration.getProperty(propertyId); 1957 } 1958 catch (XMLConfigurationException e) { 1959 String identifier = e.getIdentifier(); 1960 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1961 throw new SAXNotRecognizedException ( 1962 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1963 "property-not-recognized", new Object [] {identifier})); 1964 } 1965 else { 1966 throw new SAXNotSupportedException ( 1967 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1968 "property-not-supported", new Object [] {identifier})); 1969 } 1970 } 1971 1972 } 1974 1978 1980 1993 protected void setDeclHandler(DeclHandler handler) 1994 throws SAXNotRecognizedException , SAXNotSupportedException { 1995 1996 if (fParseInProgress) { 1997 throw new SAXNotSupportedException ( 1998 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1999 "property-not-parsing-supported", 2000 new Object [] {"http://xml.org/sax/properties/declaration-handler"})); 2001 } 2002 fDeclHandler = handler; 2003 2004 } 2006 2011 protected DeclHandler getDeclHandler() 2012 throws SAXNotRecognizedException , SAXNotSupportedException { 2013 return fDeclHandler; 2014 } 2016 2029 protected void setLexicalHandler(LexicalHandler handler) 2030 throws SAXNotRecognizedException , SAXNotSupportedException { 2031 2032 if (fParseInProgress) { 2033 throw new SAXNotSupportedException ( 2034 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 2035 "property-not-parsing-supported", 2036 new Object [] {"http://xml.org/sax/properties/lexical-handler"})); 2037 } 2038 fLexicalHandler = handler; 2039 2040 } 2042 2047 protected LexicalHandler getLexicalHandler() 2048 throws SAXNotRecognizedException , SAXNotSupportedException { 2049 return fLexicalHandler; 2050 } 2052 2055 protected final void startNamespaceMapping() throws SAXException { 2056 int count = fNamespaceContext.getDeclaredPrefixCount(); 2057 if (count > 0) { 2058 String prefix = null; 2059 String uri = null; 2060 for (int i = 0; i < count; i++) { 2061 prefix = fNamespaceContext.getDeclaredPrefixAt(i); 2062 uri = fNamespaceContext.getURI(prefix); 2063 fContentHandler.startPrefixMapping(prefix, 2064 (uri == null) ? "" : uri); 2065 } 2066 } 2067 } 2068 2069 2072 protected final void endNamespaceMapping() throws SAXException { 2073 int count = fNamespaceContext.getDeclaredPrefixCount(); 2074 if (count > 0) { 2075 for (int i = 0; i < count; i++) { 2076 fContentHandler.endPrefixMapping(fNamespaceContext.getDeclaredPrefixAt(i)); 2077 } 2078 } 2079 } 2080 2081 2085 2090 public void reset() throws XNIException { 2091 super.reset(); 2092 2093 fInDTD = false; 2095 fVersion = "1.0"; 2096 2097 fNamespaces = fConfiguration.getFeature(NAMESPACES); 2099 fNamespacePrefixes = fConfiguration.getFeature(NAMESPACE_PREFIXES); 2100 fAugmentations = null; 2101 fDeclaredAttrs = null; 2102 2103 } 2105 2109 protected class LocatorProxy 2110 implements Locator2 { 2111 2112 2116 2117 protected XMLLocator fLocator; 2118 2119 2123 2124 public LocatorProxy(XMLLocator locator) { 2125 fLocator = locator; 2126 } 2127 2128 2132 2133 public String getPublicId() { 2134 return fLocator.getPublicId(); 2135 } 2136 2137 2138 public String getSystemId() { 2139 return fLocator.getExpandedSystemId(); 2140 } 2141 2142 public int getLineNumber() { 2143 return fLocator.getLineNumber(); 2144 } 2145 2146 2147 public int getColumnNumber() { 2148 return fLocator.getColumnNumber(); 2149 } 2150 2151 public String getXMLVersion() { 2153 return fVersion; 2154 } 2155 2156 public String getEncoding() { 2157 return fLocator.getEncoding(); 2158 } 2159 2160 } 2162 protected static final class AttributesProxy 2163 implements AttributeList , Attributes2 { 2164 2165 2169 2170 protected XMLAttributes fAttributes; 2171 2172 2176 2177 public void setAttributes(XMLAttributes attributes) { 2178 fAttributes = attributes; 2179 } 2181 public int getLength() { 2182 return fAttributes.getLength(); 2183 } 2184 2185 public String getName(int i) { 2186 return fAttributes.getQName(i); 2187 } 2188 2189 public String getQName(int index) { 2190 return fAttributes.getQName(index); 2191 } 2192 2193 public String getURI(int index) { 2194 String uri= fAttributes.getURI(index); 2198 return uri != null ? uri : ""; 2199 } 2200 2201 public String getLocalName(int index) { 2202 return fAttributes.getLocalName(index); 2203 } 2204 2205 public String getType(int i) { 2206 return fAttributes.getType(i); 2207 } 2208 2209 public String getType(String name) { 2210 return fAttributes.getType(name); 2211 } 2212 2213 public String getType(String uri, String localName) { 2214 return uri.equals("") ? fAttributes.getType(null, localName) : 2215 fAttributes.getType(uri, localName); 2216 } 2217 2218 public String getValue(int i) { 2219 return fAttributes.getValue(i); 2220 } 2221 2222 public String getValue(String name) { 2223 return fAttributes.getValue(name); 2224 } 2225 2226 public String getValue(String uri, String localName) { 2227 return uri.equals("") ? fAttributes.getValue(null, localName) : 2228 fAttributes.getValue(uri, localName); 2229 } 2230 2231 public int getIndex(String qName) { 2232 return fAttributes.getIndex(qName); 2233 } 2234 2235 public int getIndex(String uri, String localPart) { 2236 return uri.equals("") ? fAttributes.getIndex(null, localPart) : 2237 fAttributes.getIndex(uri, localPart); 2238 } 2239 2240 public boolean isDeclared(int index) { 2243 if (index < 0 || index >= fAttributes.getLength()) { 2244 throw new ArrayIndexOutOfBoundsException (index); 2245 } 2246 return Boolean.TRUE.equals( 2247 fAttributes.getAugmentations(index).getItem( 2248 Constants.ATTRIBUTE_DECLARED)); 2249 } 2250 2251 public boolean isDeclared(String qName) { 2252 int index = getIndex(qName); 2253 if (index == -1) { 2254 throw new IllegalArgumentException (qName); 2255 } 2256 return Boolean.TRUE.equals( 2257 fAttributes.getAugmentations(index).getItem( 2258 Constants.ATTRIBUTE_DECLARED)); 2259 } 2260 2261 public boolean isDeclared(String uri, String localName) { 2262 int index = getIndex(uri, localName); 2263 if (index == -1) { 2264 throw new IllegalArgumentException (localName); 2265 } 2266 return Boolean.TRUE.equals( 2267 fAttributes.getAugmentations(index).getItem( 2268 Constants.ATTRIBUTE_DECLARED)); 2269 } 2270 2271 public boolean isSpecified(int index) { 2272 if (index < 0 || index >= fAttributes.getLength()) { 2273 throw new ArrayIndexOutOfBoundsException (index); 2274 } 2275 return fAttributes.isSpecified(index); 2276 } 2277 2278 public boolean isSpecified(String qName) { 2279 int index = getIndex(qName); 2280 if (index == -1) { 2281 throw new IllegalArgumentException (qName); 2282 } 2283 return fAttributes.isSpecified(index); 2284 } 2285 2286 public boolean isSpecified(String uri, String localName) { 2287 int index = getIndex(uri, localName); 2288 if (index == -1) { 2289 throw new IllegalArgumentException (localName); 2290 } 2291 return fAttributes.isSpecified(index); 2292 } 2293 2294 } 2296 2297 2299 public ElementPSVI getElementPSVI(){ 2300 return (fAugmentations != null)?(ElementPSVI)fAugmentations.getItem(Constants.ELEMENT_PSVI):null; 2301 } 2302 2303 2304 public AttributePSVI getAttributePSVI(int index){ 2305 2306 return (AttributePSVI)fAttributesProxy.fAttributes.getAugmentations(index).getItem(Constants.ATTRIBUTE_PSVI); 2307 } 2308 2309 2310 public AttributePSVI getAttributePSVIByName(String uri, 2311 String localname){ 2312 return (AttributePSVI)fAttributesProxy.fAttributes.getAugmentations(uri, localname).getItem(Constants.ATTRIBUTE_PSVI); 2313 } 2314 2315} | Popular Tags |