1 16 17 package org.apache.xerces.parsers; 18 19 import java.io.IOException ; 20 import java.util.Locale ; 21 22 import org.apache.xerces.impl.Constants; 23 import org.apache.xerces.xs.PSVIProvider; 24 import org.apache.xerces.util.EntityResolverWrapper; 25 import org.apache.xerces.util.EntityResolver2Wrapper; 26 import org.apache.xerces.util.ErrorHandlerWrapper; 27 import org.apache.xerces.util.SAXMessageFormatter; 28 import org.apache.xerces.util.SymbolHash; 29 import org.apache.xerces.util.XMLSymbols; 30 import org.apache.xerces.xni.Augmentations; 31 import org.apache.xerces.xni.NamespaceContext; 32 import org.apache.xerces.xni.QName; 33 import org.apache.xerces.xni.XMLAttributes; 34 import org.apache.xerces.xni.XMLLocator; 35 import org.apache.xerces.xni.XMLResourceIdentifier; 36 import org.apache.xerces.xni.XMLString; 37 import org.apache.xerces.xni.XNIException; 38 import org.apache.xerces.xni.parser.XMLConfigurationException; 39 import org.apache.xerces.xni.parser.XMLEntityResolver; 40 import org.apache.xerces.xni.parser.XMLErrorHandler; 41 import org.apache.xerces.xni.parser.XMLInputSource; 42 import org.apache.xerces.xni.parser.XMLParseException; 43 import org.apache.xerces.xni.parser.XMLParserConfiguration; 44 import org.apache.xerces.xs.AttributePSVI; 45 import org.apache.xerces.xs.ElementPSVI; 46 import org.xml.sax.AttributeList ; 47 import org.xml.sax.Attributes ; 48 import org.xml.sax.ContentHandler ; 49 import org.xml.sax.DTDHandler ; 50 import org.xml.sax.DocumentHandler ; 51 import org.xml.sax.EntityResolver ; 52 import org.xml.sax.ErrorHandler ; 53 import org.xml.sax.InputSource ; 54 import org.xml.sax.Locator ; 55 import org.xml.sax.Parser ; 56 import org.xml.sax.SAXException ; 57 import org.xml.sax.SAXNotRecognizedException ; 58 import org.xml.sax.SAXNotSupportedException ; 59 import org.xml.sax.SAXParseException ; 60 import org.xml.sax.XMLReader ; 61 import org.xml.sax.ext.Attributes2 ; 62 import org.xml.sax.ext.DeclHandler ; 63 import org.xml.sax.ext.EntityResolver2 ; 64 import org.xml.sax.ext.LexicalHandler ; 65 import org.xml.sax.ext.Locator2 ; 66 import org.xml.sax.helpers.LocatorImpl ; 67 68 78 public abstract class AbstractSAXParser 79 extends AbstractXMLDocumentParser 80 implements PSVIProvider, Parser , XMLReader { 83 84 88 90 91 protected static final String NAMESPACES = 92 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 93 94 95 protected static final String NAMESPACE_PREFIXES = 96 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE; 97 98 99 protected static final String STRING_INTERNING = 100 Constants.SAX_FEATURE_PREFIX + Constants.STRING_INTERNING_FEATURE; 101 102 103 protected static final String ALLOW_UE_AND_NOTATION_EVENTS = 106 Constants.SAX_FEATURE_PREFIX + Constants.ALLOW_DTD_EVENTS_AFTER_ENDDTD_FEATURE; 107 108 109 private static final String [] RECOGNIZED_FEATURES = { 110 NAMESPACES, 111 NAMESPACE_PREFIXES, 112 STRING_INTERNING, 113 }; 114 115 117 118 protected static final String LEXICAL_HANDLER = 119 Constants.SAX_PROPERTY_PREFIX + Constants.LEXICAL_HANDLER_PROPERTY; 120 121 122 protected static final String DECLARATION_HANDLER = 123 Constants.SAX_PROPERTY_PREFIX + Constants.DECLARATION_HANDLER_PROPERTY; 124 125 126 protected static final String DOM_NODE = 127 Constants.SAX_PROPERTY_PREFIX + Constants.DOM_NODE_PROPERTY; 128 129 130 private static final String [] RECOGNIZED_PROPERTIES = { 131 LEXICAL_HANDLER, 132 DECLARATION_HANDLER, 133 DOM_NODE, 134 }; 135 136 140 142 143 protected boolean fNamespaces; 144 145 146 protected boolean fNamespacePrefixes = false; 147 148 149 protected boolean fLexicalHandlerParameterEntities = true; 150 151 152 protected boolean fStandalone; 153 154 155 protected boolean fResolveDTDURIs = true; 156 157 158 protected boolean fUseEntityResolver2 = true; 159 160 164 protected boolean fXMLNSURIs = false; 165 166 168 169 protected ContentHandler fContentHandler; 170 171 172 protected DocumentHandler fDocumentHandler; 173 174 175 protected NamespaceContext fNamespaceContext; 176 177 178 protected org.xml.sax.DTDHandler fDTDHandler; 179 180 181 protected DeclHandler fDeclHandler; 182 183 184 protected LexicalHandler fLexicalHandler; 185 186 protected QName fQName = new QName(); 187 188 190 195 protected boolean fParseInProgress = false; 196 197 protected String fVersion; 199 200 private final AttributesProxy fAttributesProxy = new AttributesProxy(); 202 private Augmentations fAugmentations = null; 203 204 205 private static final int BUFFER_SIZE = 20; 208 private char[] fCharBuffer = new char[BUFFER_SIZE]; 209 210 protected SymbolHash fDeclaredAttrs = null; 214 215 219 220 protected AbstractSAXParser(XMLParserConfiguration config) { 221 super(config); 222 223 config.addRecognizedFeatures(RECOGNIZED_FEATURES); 224 config.addRecognizedProperties(RECOGNIZED_PROPERTIES); 225 226 try { 227 config.setFeature(ALLOW_UE_AND_NOTATION_EVENTS, false); 228 } 229 catch (XMLConfigurationException e) { 230 } 232 } 234 238 264 public void startDocument(XMLLocator locator, String encoding, 265 NamespaceContext namespaceContext, Augmentations augs) 266 throws XNIException { 267 268 fNamespaceContext = namespaceContext; 269 270 try { 271 if (fDocumentHandler != null) { 273 if (locator != null) { 274 fDocumentHandler.setDocumentLocator(new LocatorProxy(locator)); 275 } 276 fDocumentHandler.startDocument(); 277 } 278 279 if (fContentHandler != null) { 281 if (locator != null) { 282 fContentHandler.setDocumentLocator(new LocatorProxy(locator)); 283 } 284 fContentHandler.startDocument(); 285 } 286 } 287 catch (SAXException e) { 288 throw new XNIException(e); 289 } 290 291 } 293 306 public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) 307 throws XNIException { 308 fVersion = version; 311 fStandalone = "yes".equals(standalone); 312 } 314 326 public void doctypeDecl(String rootElement, 327 String publicId, String systemId, Augmentations augs) 328 throws XNIException { 329 fInDTD = true; 330 331 try { 332 if (fLexicalHandler != null) { 334 fLexicalHandler.startDTD(rootElement, publicId, systemId); 335 } 336 } 337 catch (SAXException e) { 338 throw new XNIException(e); 339 } 340 341 if(fDeclHandler != null) { 343 fDeclaredAttrs = new SymbolHash(); 344 } 345 346 } 348 372 public void startGeneralEntity(String name, XMLResourceIdentifier identifier, 373 String encoding, Augmentations augs) 374 throws XNIException { 375 376 try { 377 if (augs != null && Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 379 if (fContentHandler != null) { 381 fContentHandler.skippedEntity(name); 382 } 383 } 384 else { 385 if (fLexicalHandler != null) { 387 fLexicalHandler.startEntity(name); 388 } 389 } 390 } 391 catch (SAXException e) { 392 throw new XNIException(e); 393 } 394 395 } 397 416 public void endGeneralEntity(String name, Augmentations augs) throws XNIException { 417 418 try { 419 if (augs == null || !Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 421 if (fLexicalHandler != null) { 423 fLexicalHandler.endEntity(name); 424 } 425 } 426 } 427 catch (SAXException e) { 428 throw new XNIException(e); 429 } 430 431 } 433 444 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 445 throws XNIException { 446 447 try { 448 if (fDocumentHandler != null) { 450 fAttributesProxy.setAttributes(attributes); 453 fDocumentHandler.startElement(element.rawname, fAttributesProxy); 454 } 455 456 if (fContentHandler != null) { 458 459 if (fNamespaces) { 460 startNamespaceMapping(); 462 463 int len = attributes.getLength(); 470 if (!fNamespacePrefixes) { 471 for (int i = len - 1; i >= 0; --i) { 472 attributes.getName(i, fQName); 473 if ((fQName.prefix == XMLSymbols.PREFIX_XMLNS) || 474 (fQName.rawname == XMLSymbols.PREFIX_XMLNS)) { 475 attributes.removeAttributeAt(i); 477 } 478 } 479 } 480 else if (!fXMLNSURIs) { 481 for (int i = len - 1; i >= 0; --i) { 482 attributes.getName(i, fQName); 483 if ((fQName.prefix == XMLSymbols.PREFIX_XMLNS) || 484 (fQName.rawname == XMLSymbols.PREFIX_XMLNS)) { 485 fQName.prefix = ""; 488 fQName.uri = ""; 489 fQName.localpart = ""; 490 attributes.setName(i, fQName); 491 } 492 } 493 } 494 } 495 496 fAugmentations = augs; 497 498 String uri = element.uri != null ? element.uri : ""; 499 String localpart = fNamespaces ? element.localpart : ""; 500 fAttributesProxy.setAttributes(attributes); 501 fContentHandler.startElement(uri, localpart, element.rawname, 502 fAttributesProxy); 503 } 504 } 505 catch (SAXException e) { 506 throw new XNIException(e); 507 } 508 509 } 511 519 public void characters(XMLString text, Augmentations augs) throws XNIException { 520 521 if (text.length == 0) { 524 return; 525 } 526 527 528 try { 529 if (fDocumentHandler != null) { 531 fDocumentHandler.characters(text.ch, text.offset, text.length); 534 } 535 536 if (fContentHandler != null) { 538 fContentHandler.characters(text.ch, text.offset, text.length); 539 } 540 } 541 catch (SAXException e) { 542 throw new XNIException(e); 543 } 544 545 } 547 560 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 561 562 try { 563 if (fDocumentHandler != null) { 565 fDocumentHandler.ignorableWhitespace(text.ch, text.offset, text.length); 566 } 567 568 if (fContentHandler != null) { 570 fContentHandler.ignorableWhitespace(text.ch, text.offset, text.length); 571 } 572 } 573 catch (SAXException e) { 574 throw new XNIException(e); 575 } 576 577 } 579 587 public void endElement(QName element, Augmentations augs) throws XNIException { 588 589 590 try { 591 if (fDocumentHandler != null) { 593 fDocumentHandler.endElement(element.rawname); 594 } 595 596 if (fContentHandler != null) { 598 fAugmentations = augs; 599 String uri = element.uri != null ? element.uri : ""; 600 String localpart = fNamespaces ? element.localpart : ""; 601 fContentHandler.endElement(uri, localpart, 602 element.rawname); 603 if (fNamespaces) { 604 endNamespaceMapping(); 605 } 606 } 607 } 608 catch (SAXException e) { 609 throw new XNIException(e); 610 } 611 612 } 614 620 public void startCDATA(Augmentations augs) throws XNIException { 621 622 try { 623 if (fLexicalHandler != null) { 625 fLexicalHandler.startCDATA(); 626 } 627 } 628 catch (SAXException e) { 629 throw new XNIException(e); 630 } 631 632 } 634 640 public void endCDATA(Augmentations augs) throws XNIException { 641 642 try { 643 if (fLexicalHandler != null) { 645 fLexicalHandler.endCDATA(); 646 } 647 } 648 catch (SAXException e) { 649 throw new XNIException(e); 650 } 651 652 } 654 662 public void comment(XMLString text, Augmentations augs) throws XNIException { 663 664 try { 665 if (fLexicalHandler != null) { 667 fLexicalHandler.comment(text.ch, 0, text.length); 668 } 669 } 670 catch (SAXException e) { 671 throw new XNIException(e); 672 } 673 674 } 676 693 public void processingInstruction(String target, XMLString data, Augmentations augs) 694 throws XNIException { 695 696 702 try { 703 if (fDocumentHandler != null) { 705 fDocumentHandler.processingInstruction(target, 706 data.toString()); 707 } 708 709 if (fContentHandler != null) { 711 fContentHandler.processingInstruction(target, data.toString()); 712 } 713 } 714 catch (SAXException e) { 715 throw new XNIException(e); 716 } 717 718 } 720 721 727 public void endDocument(Augmentations augs) throws XNIException { 728 729 try { 730 if (fDocumentHandler != null) { 732 fDocumentHandler.endDocument(); 733 } 734 735 if (fContentHandler != null) { 737 fContentHandler.endDocument(); 738 } 739 } 740 catch (SAXException e) { 741 throw new XNIException(e); 742 } 743 744 } 746 750 758 public void startExternalSubset(XMLResourceIdentifier identifier, 759 Augmentations augs) throws XNIException { 760 startParameterEntity("[dtd]", null, null, augs); 761 } 762 763 771 public void endExternalSubset(Augmentations augs) throws XNIException { 772 endParameterEntity("[dtd]", augs); 773 } 774 775 800 public void startParameterEntity(String name, 801 XMLResourceIdentifier identifier, 802 String encoding, Augmentations augs) 803 throws XNIException { 804 805 try { 806 if (augs != null && Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 808 if (fContentHandler != null) { 810 fContentHandler.skippedEntity(name); 811 } 812 } 813 else { 814 if (fLexicalHandler != null && fLexicalHandlerParameterEntities) { 816 fLexicalHandler.startEntity(name); 817 } 818 } 819 } 820 catch (SAXException e) { 821 throw new XNIException(e); 822 } 823 824 } 826 846 public void endParameterEntity(String name, Augmentations augs) throws XNIException { 847 848 try { 849 if (augs == null || !Boolean.TRUE.equals(augs.getItem(Constants.ENTITY_SKIPPED))) { 851 if (fLexicalHandler != null && fLexicalHandlerParameterEntities) { 853 fLexicalHandler.endEntity(name); 854 } 855 } 856 } 857 catch (SAXException e) { 858 throw new XNIException(e); 859 } 860 861 } 863 874 public void elementDecl(String name, String contentModel, Augmentations augs) 875 throws XNIException { 876 877 try { 878 if (fDeclHandler != null) { 880 fDeclHandler.elementDecl(name, contentModel); 881 } 882 } 883 catch (SAXException e) { 884 throw new XNIException(e); 885 } 886 887 } 889 915 public void attributeDecl(String elementName, String attributeName, 916 String type, String [] enumeration, 917 String defaultType, XMLString defaultValue, 918 XMLString nonNormalizedDefaultValue, Augmentations augs) throws XNIException { 919 920 try { 921 if (fDeclHandler != null) { 923 String elemAttr = new StringBuffer (elementName).append("<").append(attributeName).toString(); 925 if(fDeclaredAttrs.get(elemAttr) != null) { 926 return; 928 } 929 fDeclaredAttrs.put(elemAttr, Boolean.TRUE); 930 if (type.equals("NOTATION") || 931 type.equals("ENUMERATION")) { 932 933 StringBuffer str = new StringBuffer (); 934 if (type.equals("NOTATION")) { 935 str.append(type); 936 str.append(" ("); 937 } 938 else { 939 str.append("("); 940 } 941 for (int i = 0; i < enumeration.length; i++) { 942 str.append(enumeration[i]); 943 if (i < enumeration.length - 1) { 944 str.append('|'); 945 } 946 } 947 str.append(')'); 948 type = str.toString(); 949 } 950 String value = (defaultValue==null) ? null : defaultValue.toString(); 951 fDeclHandler.attributeDecl(elementName, attributeName, 952 type, defaultType, value); 953 } 954 } 955 catch (SAXException e) { 956 throw new XNIException(e); 957 } 958 959 } 961 978 public void internalEntityDecl(String name, XMLString text, 979 XMLString nonNormalizedText, 980 Augmentations augs) throws XNIException { 981 982 try { 983 if (fDeclHandler != null) { 985 fDeclHandler.internalEntityDecl(name, text.toString()); 986 } 987 } 988 catch (SAXException e) { 989 throw new XNIException(e); 990 } 991 992 } 994 1007 public void externalEntityDecl(String name, XMLResourceIdentifier identifier, 1008 Augmentations augs) throws XNIException { 1009 try { 1010 if (fDeclHandler != null) { 1012 String publicId = identifier.getPublicId(); 1013 String systemId = fResolveDTDURIs ? 1014 identifier.getExpandedSystemId() : identifier.getLiteralSystemId(); 1015 fDeclHandler.externalEntityDecl(name, publicId, systemId); 1016 } 1017 } 1018 catch (SAXException e) { 1019 throw new XNIException(e); 1020 } 1021 1022 } 1024 1037 public void unparsedEntityDecl(String name, XMLResourceIdentifier identifier, 1038 String notation, 1039 Augmentations augs) throws XNIException { 1040 try { 1041 if (fDTDHandler != null) { 1043 String publicId = identifier.getPublicId(); 1044 String systemId = fResolveDTDURIs ? 1045 identifier.getExpandedSystemId() : identifier.getLiteralSystemId(); 1046 fDTDHandler.unparsedEntityDecl(name, publicId, systemId, notation); 1047 } 1048 } 1049 catch (SAXException e) { 1050 throw new XNIException(e); 1051 } 1052 1053 } 1055 1066 public void notationDecl(String name, XMLResourceIdentifier identifier, 1067 Augmentations augs) throws XNIException { 1068 try { 1069 if (fDTDHandler != null) { 1071 String publicId = identifier.getPublicId(); 1072 String systemId = fResolveDTDURIs ? 1073 identifier.getExpandedSystemId() : identifier.getLiteralSystemId(); 1074 fDTDHandler.notationDecl(name, publicId, systemId); 1075 } 1076 } 1077 catch (SAXException e) { 1078 throw new XNIException(e); 1079 } 1080 1081 } 1083 1091 public void endDTD(Augmentations augs) throws XNIException { 1092 fInDTD = false; 1093 1094 try { 1095 if (fLexicalHandler != null) { 1097 fLexicalHandler.endDTD(); 1098 } 1099 } 1100 catch (SAXException e) { 1101 throw new XNIException(e); 1102 } 1103 if(fDeclaredAttrs != null) { 1104 fDeclaredAttrs.clear(); 1106 } 1107 1108 } 1110 1114 1127 public void parse(String systemId) throws SAXException , IOException { 1128 1129 XMLInputSource source = new XMLInputSource(null, systemId, null); 1131 try { 1132 parse(source); 1133 } 1134 1135 catch (XMLParseException e) { 1137 Exception ex = e.getException(); 1138 if (ex == null) { 1139 LocatorImpl locatorImpl = new LocatorImpl (){ 1142 public String getXMLVersion() { 1143 return fVersion; 1144 } 1145 public String getEncoding() { 1151 return null; 1152 } 1153 }; 1154 locatorImpl.setPublicId(e.getPublicId()); 1155 locatorImpl.setSystemId(e.getExpandedSystemId()); 1156 locatorImpl.setLineNumber(e.getLineNumber()); 1157 locatorImpl.setColumnNumber(e.getColumnNumber()); 1158 throw new SAXParseException (e.getMessage(), locatorImpl); 1159 } 1160 if (ex instanceof SAXException ) { 1161 throw (SAXException )ex; 1163 } 1164 if (ex instanceof IOException ) { 1165 throw (IOException )ex; 1166 } 1167 throw new SAXException (ex); 1168 } 1169 catch (XNIException e) { 1170 Exception ex = e.getException(); 1171 if (ex == null) { 1172 throw new SAXException (e.getMessage()); 1173 } 1174 if (ex instanceof SAXException ) { 1175 throw (SAXException )ex; 1176 } 1177 if (ex instanceof IOException ) { 1178 throw (IOException )ex; 1179 } 1180 throw new SAXException (ex); 1181 } 1182 1183 } 1185 1193 public void parse(InputSource inputSource) 1194 throws SAXException , IOException { 1195 1196 try { 1198 XMLInputSource xmlInputSource = 1199 new XMLInputSource(inputSource.getPublicId(), 1200 inputSource.getSystemId(), 1201 null); 1202 xmlInputSource.setByteStream(inputSource.getByteStream()); 1203 xmlInputSource.setCharacterStream(inputSource.getCharacterStream()); 1204 xmlInputSource.setEncoding(inputSource.getEncoding()); 1205 parse(xmlInputSource); 1206 } 1207 1208 catch (XMLParseException e) { 1210 Exception ex = e.getException(); 1211 if (ex == null) { 1212 LocatorImpl locatorImpl = new LocatorImpl () { 1215 public String getXMLVersion() { 1216 return fVersion; 1217 } 1218 public String getEncoding() { 1224 return null; 1225 } 1226 }; 1227 locatorImpl.setPublicId(e.getPublicId()); 1228 locatorImpl.setSystemId(e.getExpandedSystemId()); 1229 locatorImpl.setLineNumber(e.getLineNumber()); 1230 locatorImpl.setColumnNumber(e.getColumnNumber()); 1231 throw new SAXParseException (e.getMessage(), locatorImpl); 1232 } 1233 if (ex instanceof SAXException ) { 1234 throw (SAXException )ex; 1236 } 1237 if (ex instanceof IOException ) { 1238 throw (IOException )ex; 1239 } 1240 throw new SAXException (ex); 1241 } 1242 catch (XNIException e) { 1243 Exception ex = e.getException(); 1244 if (ex == null) { 1245 throw new SAXException (e.getMessage()); 1246 } 1247 if (ex instanceof SAXException ) { 1248 throw (SAXException )ex; 1249 } 1250 if (ex instanceof IOException ) { 1251 throw (IOException )ex; 1252 } 1253 throw new SAXException (ex); 1254 } 1255 1256 } 1258 1265 public void setEntityResolver(EntityResolver resolver) { 1266 1267 try { 1268 XMLEntityResolver xer = (XMLEntityResolver) fConfiguration.getProperty(ENTITY_RESOLVER); 1269 if (fUseEntityResolver2 && resolver instanceof EntityResolver2 ) { 1270 if (xer instanceof EntityResolver2Wrapper) { 1271 EntityResolver2Wrapper er2w = (EntityResolver2Wrapper) xer; 1272 er2w.setEntityResolver((EntityResolver2 ) resolver); 1273 } 1274 else { 1275 fConfiguration.setProperty(ENTITY_RESOLVER, 1276 new EntityResolver2Wrapper((EntityResolver2 ) resolver)); 1277 } 1278 } 1279 else { 1280 if (xer instanceof EntityResolverWrapper) { 1281 EntityResolverWrapper erw = (EntityResolverWrapper) xer; 1282 erw.setEntityResolver(resolver); 1283 } 1284 else { 1285 fConfiguration.setProperty(ENTITY_RESOLVER, 1286 new EntityResolverWrapper(resolver)); 1287 } 1288 } 1289 } 1290 catch (XMLConfigurationException e) { 1291 } 1293 1294 } 1296 1303 public EntityResolver getEntityResolver() { 1304 1305 EntityResolver entityResolver = null; 1306 try { 1307 XMLEntityResolver xmlEntityResolver = 1308 (XMLEntityResolver)fConfiguration.getProperty(ENTITY_RESOLVER); 1309 if (xmlEntityResolver != null) { 1310 if (xmlEntityResolver instanceof EntityResolverWrapper) { 1311 entityResolver = 1312 ((EntityResolverWrapper) xmlEntityResolver).getEntityResolver(); 1313 } 1314 else if (xmlEntityResolver instanceof EntityResolver2Wrapper) { 1315 entityResolver = 1316 ((EntityResolver2Wrapper) xmlEntityResolver).getEntityResolver(); 1317 } 1318 } 1319 } 1320 catch (XMLConfigurationException e) { 1321 } 1323 return entityResolver; 1324 1325 } 1327 1343 public void setErrorHandler(ErrorHandler errorHandler) { 1344 1345 try { 1346 XMLErrorHandler xeh = (XMLErrorHandler) fConfiguration.getProperty(ERROR_HANDLER); 1347 if (xeh instanceof ErrorHandlerWrapper) { 1348 ErrorHandlerWrapper ehw = (ErrorHandlerWrapper) xeh; 1349 ehw.setErrorHandler(errorHandler); 1350 } 1351 else { 1352 fConfiguration.setProperty(ERROR_HANDLER, 1353 new ErrorHandlerWrapper(errorHandler)); 1354 } 1355 } 1356 catch (XMLConfigurationException e) { 1357 } 1359 1360 } 1362 1369 public ErrorHandler getErrorHandler() { 1370 1371 ErrorHandler errorHandler = null; 1372 try { 1373 XMLErrorHandler xmlErrorHandler = 1374 (XMLErrorHandler)fConfiguration.getProperty(ERROR_HANDLER); 1375 if (xmlErrorHandler != null && 1376 xmlErrorHandler instanceof ErrorHandlerWrapper) { 1377 errorHandler = ((ErrorHandlerWrapper)xmlErrorHandler).getErrorHandler(); 1378 } 1379 } 1380 catch (XMLConfigurationException e) { 1381 } 1383 return errorHandler; 1384 1385 } 1387 1397 public void setLocale(Locale locale) throws SAXException { 1398 fConfiguration.setLocale(locale); 1401 1402 } 1404 1419 public void setDTDHandler(DTDHandler dtdHandler) { 1420 fDTDHandler = dtdHandler; 1421 } 1423 1427 1441 public void setDocumentHandler(DocumentHandler documentHandler) { 1442 fDocumentHandler = documentHandler; 1443 } 1445 1449 1464 public void setContentHandler(ContentHandler contentHandler) { 1465 fContentHandler = contentHandler; 1466 } 1468 1476 public ContentHandler getContentHandler() { 1477 return fContentHandler; 1478 } 1480 1487 public DTDHandler getDTDHandler() { 1488 return fDTDHandler; 1489 } 1491 1505 public void setFeature(String featureId, boolean state) 1506 throws SAXNotRecognizedException , SAXNotSupportedException { 1507 1508 try { 1509 1513 if (featureId.startsWith(Constants.SAX_FEATURE_PREFIX)) { 1514 final int suffixLength = featureId.length() - Constants.SAX_FEATURE_PREFIX.length(); 1515 1516 if (suffixLength == Constants.NAMESPACES_FEATURE.length() && 1518 featureId.endsWith(Constants.NAMESPACES_FEATURE)) { 1519 fConfiguration.setFeature(featureId, state); 1520 fNamespaces = state; 1521 return; 1522 } 1523 1524 if (suffixLength == Constants.NAMESPACE_PREFIXES_FEATURE.length() && 1531 featureId.endsWith(Constants.NAMESPACE_PREFIXES_FEATURE)) { 1532 fConfiguration.setFeature(featureId, state); 1533 fNamespacePrefixes = state; 1534 return; 1535 } 1536 1537 if (suffixLength == Constants.STRING_INTERNING_FEATURE.length() && 1542 featureId.endsWith(Constants.STRING_INTERNING_FEATURE)) { 1543 if (!state) { 1544 throw new SAXNotSupportedException ( 1545 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1546 "false-not-supported", new Object [] {featureId})); 1547 } 1548 return; 1549 } 1550 1551 if (suffixLength == Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE.length() && 1556 featureId.endsWith(Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE)) { 1557 fLexicalHandlerParameterEntities = state; 1558 return; 1559 } 1560 1561 if (suffixLength == Constants.RESOLVE_DTD_URIS_FEATURE.length() && 1566 featureId.endsWith(Constants.RESOLVE_DTD_URIS_FEATURE)) { 1567 fResolveDTDURIs = state; 1568 return; 1569 } 1570 1571 if (suffixLength == Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE.length() && 1576 featureId.endsWith(Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE)) { 1577 if (state) { 1580 throw new SAXNotSupportedException ( 1581 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1582 "true-not-supported", new Object [] {featureId})); 1583 } 1584 return; 1585 } 1586 1587 if (suffixLength == Constants.XMLNS_URIS_FEATURE.length() && 1592 featureId.endsWith(Constants.XMLNS_URIS_FEATURE)) { 1593 fXMLNSURIs = state; 1594 return; 1595 } 1596 1597 if (suffixLength == Constants.USE_ENTITY_RESOLVER2_FEATURE.length() && 1602 featureId.endsWith(Constants.USE_ENTITY_RESOLVER2_FEATURE)) { 1603 if (state != fUseEntityResolver2) { 1604 fUseEntityResolver2 = state; 1605 setEntityResolver(getEntityResolver()); 1607 } 1608 return; 1609 } 1610 1611 1615 if ((suffixLength == Constants.IS_STANDALONE_FEATURE.length() && 1626 featureId.endsWith(Constants.IS_STANDALONE_FEATURE)) || 1627 (suffixLength == Constants.USE_ATTRIBUTES2_FEATURE.length() && 1628 featureId.endsWith(Constants.USE_ATTRIBUTES2_FEATURE)) || 1629 (suffixLength == Constants.USE_LOCATOR2_FEATURE.length() && 1630 featureId.endsWith(Constants.USE_LOCATOR2_FEATURE)) || 1631 (suffixLength == Constants.XML_11_FEATURE.length() && 1632 featureId.endsWith(Constants.XML_11_FEATURE))) { 1633 throw new SAXNotSupportedException ( 1634 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1635 "feature-read-only", new Object [] {featureId})); 1636 } 1637 1638 1639 } 1643 1644 1648 1656 1657 1661 fConfiguration.setFeature(featureId, state); 1662 } 1663 catch (XMLConfigurationException e) { 1664 String identifier = e.getIdentifier(); 1665 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1666 throw new SAXNotRecognizedException ( 1667 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1668 "feature-not-recognized", new Object [] {identifier})); 1669 } 1670 else { 1671 throw new SAXNotSupportedException ( 1672 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1673 "feature-not-supported", new Object [] {identifier})); 1674 } 1675 } 1676 1677 } 1679 1693 public boolean getFeature(String featureId) 1694 throws SAXNotRecognizedException , SAXNotSupportedException { 1695 1696 try { 1697 1701 if (featureId.startsWith(Constants.SAX_FEATURE_PREFIX)) { 1702 final int suffixLength = featureId.length() - Constants.SAX_FEATURE_PREFIX.length(); 1703 1704 if (suffixLength == Constants.NAMESPACE_PREFIXES_FEATURE.length() && 1711 featureId.endsWith(Constants.NAMESPACE_PREFIXES_FEATURE)) { 1712 boolean state = fConfiguration.getFeature(featureId); 1713 return state; 1714 } 1715 if (suffixLength == Constants.STRING_INTERNING_FEATURE.length() && 1720 featureId.endsWith(Constants.STRING_INTERNING_FEATURE)) { 1721 return true; 1722 } 1723 1724 if (suffixLength == Constants.IS_STANDALONE_FEATURE.length() && 1728 featureId.endsWith(Constants.IS_STANDALONE_FEATURE)) { 1729 return fStandalone; 1730 } 1731 1732 if (suffixLength == Constants.XML_11_FEATURE.length() && 1736 featureId.endsWith(Constants.XML_11_FEATURE)) { 1737 return (fConfiguration instanceof XML11Configurable); 1738 } 1739 1740 if (suffixLength == Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE.length() && 1745 featureId.endsWith(Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE)) { 1746 return fLexicalHandlerParameterEntities; 1747 } 1748 1749 if (suffixLength == Constants.RESOLVE_DTD_URIS_FEATURE.length() && 1753 featureId.endsWith(Constants.RESOLVE_DTD_URIS_FEATURE)) { 1754 return fResolveDTDURIs; 1755 } 1756 1757 if (suffixLength == Constants.XMLNS_URIS_FEATURE.length() && 1762 featureId.endsWith(Constants.XMLNS_URIS_FEATURE)) { 1763 return fXMLNSURIs; 1764 } 1765 1766 if (suffixLength == Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE.length() && 1771 featureId.endsWith(Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE)) { 1772 return false; 1775 } 1776 1777 if (suffixLength == Constants.USE_ENTITY_RESOLVER2_FEATURE.length() && 1782 featureId.endsWith(Constants.USE_ENTITY_RESOLVER2_FEATURE)) { 1783 return fUseEntityResolver2; 1784 } 1785 1786 if ((suffixLength == Constants.USE_ATTRIBUTES2_FEATURE.length() && 1794 featureId.endsWith(Constants.USE_ATTRIBUTES2_FEATURE)) || 1795 (suffixLength == Constants.USE_LOCATOR2_FEATURE.length() && 1796 featureId.endsWith(Constants.USE_LOCATOR2_FEATURE))) { 1797 return true; 1798 } 1799 1800 1801 } 1805 1806 1810 1817 1818 return fConfiguration.getFeature(featureId); 1819 } 1820 catch (XMLConfigurationException e) { 1821 String identifier = e.getIdentifier(); 1822 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1823 throw new SAXNotRecognizedException ( 1824 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1825 "feature-not-recognized", new Object [] {identifier})); 1826 } 1827 else { 1828 throw new SAXNotSupportedException ( 1829 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1830 "feature-not-supported", new Object [] {identifier})); 1831 } 1832 } 1833 1834 } 1836 1851 public void setProperty(String propertyId, Object value) 1852 throws SAXNotRecognizedException , SAXNotSupportedException { 1853 1854 try { 1855 1859 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 1860 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 1861 1862 if (suffixLength == Constants.LEXICAL_HANDLER_PROPERTY.length() && 1869 propertyId.endsWith(Constants.LEXICAL_HANDLER_PROPERTY)) { 1870 try { 1871 setLexicalHandler((LexicalHandler )value); 1872 } 1873 catch (ClassCastException e) { 1874 throw new SAXNotSupportedException ( 1875 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1876 "incompatible-class", new Object [] {propertyId, "org.xml.sax.ext.LexicalHandler"})); 1877 } 1878 return; 1879 } 1880 if (suffixLength == Constants.DECLARATION_HANDLER_PROPERTY.length() && 1887 propertyId.endsWith(Constants.DECLARATION_HANDLER_PROPERTY)) { 1888 try { 1889 setDeclHandler((DeclHandler )value); 1890 } 1891 catch (ClassCastException e) { 1892 throw new SAXNotSupportedException ( 1893 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1894 "incompatible-class", new Object [] {propertyId, "org.xml.sax.ext.DeclHandler"})); 1895 } 1896 return; 1897 } 1898 if ((suffixLength == Constants.DOM_NODE_PROPERTY.length() && 1913 propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) || 1914 (suffixLength == Constants.DOCUMENT_XML_VERSION_PROPERTY.length() && 1915 propertyId.endsWith(Constants.DOCUMENT_XML_VERSION_PROPERTY))) { 1916 throw new SAXNotSupportedException ( 1917 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1918 "property-read-only", new Object [] {propertyId})); 1919 } 1920 } 1924 1925 1929 1936 1937 1941 fConfiguration.setProperty(propertyId, value); 1942 } 1943 catch (XMLConfigurationException e) { 1944 String identifier = e.getIdentifier(); 1945 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1946 throw new SAXNotRecognizedException ( 1947 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1948 "property-not-recognized", new Object [] {identifier})); 1949 } 1950 else { 1951 throw new SAXNotSupportedException ( 1952 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 1953 "property-not-supported", new Object [] {identifier})); 1954 } 1955 } 1956 1957 } 1959 1973 public Object getProperty(String propertyId) 1974 throws SAXNotRecognizedException , SAXNotSupportedException { 1975 1976 try { 1977 1981 if (propertyId.startsWith(Constants.SAX_PROPERTY_PREFIX)) { 1982 final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); 1983 1984 if (suffixLength == Constants.DOCUMENT_XML_VERSION_PROPERTY.length() && 1991 propertyId.endsWith(Constants.DOCUMENT_XML_VERSION_PROPERTY)) { 1992 return fVersion; 1993 } 1994 1995 if (suffixLength == Constants.LEXICAL_HANDLER_PROPERTY.length() && 2002 propertyId.endsWith(Constants.LEXICAL_HANDLER_PROPERTY)) { 2003 return getLexicalHandler(); 2004 } 2005 if (suffixLength == Constants.DECLARATION_HANDLER_PROPERTY.length() && 2012 propertyId.endsWith(Constants.DECLARATION_HANDLER_PROPERTY)) { 2013 return getDeclHandler(); 2014 } 2015 2016 if (suffixLength == Constants.DOM_NODE_PROPERTY.length() && 2027 propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) { 2028 throw new SAXNotSupportedException ( 2030 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 2031 "dom-node-read-not-supported", null)); 2032 } 2033 2034 } 2038 2039 2043 2050 2051 2055 return fConfiguration.getProperty(propertyId); 2056 } 2057 catch (XMLConfigurationException e) { 2058 String identifier = e.getIdentifier(); 2059 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 2060 throw new SAXNotRecognizedException ( 2061 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 2062 "property-not-recognized", new Object [] {identifier})); 2063 } 2064 else { 2065 throw new SAXNotSupportedException ( 2066 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 2067 "property-not-supported", new Object [] {identifier})); 2068 } 2069 } 2070 2071 } 2073 2077 2079 2092 protected void setDeclHandler(DeclHandler handler) 2093 throws SAXNotRecognizedException , SAXNotSupportedException { 2094 2095 if (fParseInProgress) { 2096 throw new SAXNotSupportedException ( 2097 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 2098 "property-not-parsing-supported", 2099 new Object [] {"http://xml.org/sax/properties/declaration-handler"})); 2100 } 2101 fDeclHandler = handler; 2102 2103 } 2105 2110 protected DeclHandler getDeclHandler() 2111 throws SAXNotRecognizedException , SAXNotSupportedException { 2112 return fDeclHandler; 2113 } 2115 2128 protected void setLexicalHandler(LexicalHandler handler) 2129 throws SAXNotRecognizedException , SAXNotSupportedException { 2130 2131 if (fParseInProgress) { 2132 throw new SAXNotSupportedException ( 2133 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 2134 "property-not-parsing-supported", 2135 new Object [] {"http://xml.org/sax/properties/lexical-handler"})); 2136 } 2137 fLexicalHandler = handler; 2138 2139 } 2141 2146 protected LexicalHandler getLexicalHandler() 2147 throws SAXNotRecognizedException , SAXNotSupportedException { 2148 return fLexicalHandler; 2149 } 2151 2154 protected final void startNamespaceMapping() throws SAXException { 2155 int count = fNamespaceContext.getDeclaredPrefixCount(); 2156 if (count > 0) { 2157 String prefix = null; 2158 String uri = null; 2159 for (int i = 0; i < count; i++) { 2160 prefix = fNamespaceContext.getDeclaredPrefixAt(i); 2161 uri = fNamespaceContext.getURI(prefix); 2162 fContentHandler.startPrefixMapping(prefix, 2163 (uri == null) ? "" : uri); 2164 } 2165 } 2166 } 2167 2168 2171 protected final void endNamespaceMapping() throws SAXException { 2172 int count = fNamespaceContext.getDeclaredPrefixCount(); 2173 if (count > 0) { 2174 for (int i = 0; i < count; i++) { 2175 fContentHandler.endPrefixMapping(fNamespaceContext.getDeclaredPrefixAt(i)); 2176 } 2177 } 2178 } 2179 2180 2184 2189 public void reset() throws XNIException { 2190 super.reset(); 2191 2192 fInDTD = false; 2194 fVersion = "1.0"; 2195 fStandalone = false; 2196 2197 fNamespaces = fConfiguration.getFeature(NAMESPACES); 2199 fNamespacePrefixes = fConfiguration.getFeature(NAMESPACE_PREFIXES); 2200 fAugmentations = null; 2201 fDeclaredAttrs = null; 2202 2203 } 2205 2209 protected class LocatorProxy 2210 implements Locator2 { 2211 2212 2216 2217 protected XMLLocator fLocator; 2218 2219 2223 2224 public LocatorProxy(XMLLocator locator) { 2225 fLocator = locator; 2226 } 2227 2228 2232 2233 public String getPublicId() { 2234 return fLocator.getPublicId(); 2235 } 2236 2237 2238 public String getSystemId() { 2239 return fLocator.getExpandedSystemId(); 2240 } 2241 2242 public int getLineNumber() { 2243 return fLocator.getLineNumber(); 2244 } 2245 2246 2247 public int getColumnNumber() { 2248 return fLocator.getColumnNumber(); 2249 } 2250 2251 public String getXMLVersion() { 2253 return fLocator.getXMLVersion(); 2254 } 2255 2256 public String getEncoding() { 2257 return fLocator.getEncoding(); 2258 } 2259 2260 } 2262 protected static final class AttributesProxy 2263 implements AttributeList , Attributes2 { 2264 2265 2269 2270 protected XMLAttributes fAttributes; 2271 2272 2276 2277 public void setAttributes(XMLAttributes attributes) { 2278 fAttributes = attributes; 2279 } 2281 public int getLength() { 2282 return fAttributes.getLength(); 2283 } 2284 2285 public String getName(int i) { 2286 return fAttributes.getQName(i); 2287 } 2288 2289 public String getQName(int index) { 2290 return fAttributes.getQName(index); 2291 } 2292 2293 public String getURI(int index) { 2294 String uri= fAttributes.getURI(index); 2298 return uri != null ? uri : ""; 2299 } 2300 2301 public String getLocalName(int index) { 2302 return fAttributes.getLocalName(index); 2303 } 2304 2305 public String getType(int i) { 2306 return fAttributes.getType(i); 2307 } 2308 2309 public String getType(String name) { 2310 return fAttributes.getType(name); 2311 } 2312 2313 public String getType(String uri, String localName) { 2314 return uri.equals("") ? fAttributes.getType(null, localName) : 2315 fAttributes.getType(uri, localName); 2316 } 2317 2318 public String getValue(int i) { 2319 return fAttributes.getValue(i); 2320 } 2321 2322 public String getValue(String name) { 2323 return fAttributes.getValue(name); 2324 } 2325 2326 public String getValue(String uri, String localName) { 2327 return uri.equals("") ? fAttributes.getValue(null, localName) : 2328 fAttributes.getValue(uri, localName); 2329 } 2330 2331 public int getIndex(String qName) { 2332 return fAttributes.getIndex(qName); 2333 } 2334 2335 public int getIndex(String uri, String localPart) { 2336 return uri.equals("") ? fAttributes.getIndex(null, localPart) : 2337 fAttributes.getIndex(uri, localPart); 2338 } 2339 2340 public boolean isDeclared(int index) { 2343 if (index < 0 || index >= fAttributes.getLength()) { 2344 throw new ArrayIndexOutOfBoundsException (index); 2345 } 2346 return Boolean.TRUE.equals( 2347 fAttributes.getAugmentations(index).getItem( 2348 Constants.ATTRIBUTE_DECLARED)); 2349 } 2350 2351 public boolean isDeclared(String qName) { 2352 int index = getIndex(qName); 2353 if (index == -1) { 2354 throw new IllegalArgumentException (qName); 2355 } 2356 return Boolean.TRUE.equals( 2357 fAttributes.getAugmentations(index).getItem( 2358 Constants.ATTRIBUTE_DECLARED)); 2359 } 2360 2361 public boolean isDeclared(String uri, String localName) { 2362 int index = getIndex(uri, localName); 2363 if (index == -1) { 2364 throw new IllegalArgumentException (localName); 2365 } 2366 return Boolean.TRUE.equals( 2367 fAttributes.getAugmentations(index).getItem( 2368 Constants.ATTRIBUTE_DECLARED)); 2369 } 2370 2371 public boolean isSpecified(int index) { 2372 if (index < 0 || index >= fAttributes.getLength()) { 2373 throw new ArrayIndexOutOfBoundsException (index); 2374 } 2375 return fAttributes.isSpecified(index); 2376 } 2377 2378 public boolean isSpecified(String qName) { 2379 int index = getIndex(qName); 2380 if (index == -1) { 2381 throw new IllegalArgumentException (qName); 2382 } 2383 return fAttributes.isSpecified(index); 2384 } 2385 2386 public boolean isSpecified(String uri, String localName) { 2387 int index = getIndex(uri, localName); 2388 if (index == -1) { 2389 throw new IllegalArgumentException (localName); 2390 } 2391 return fAttributes.isSpecified(index); 2392 } 2393 2394 } 2396 2397 2399 public ElementPSVI getElementPSVI(){ 2400 return (fAugmentations != null)?(ElementPSVI)fAugmentations.getItem(Constants.ELEMENT_PSVI):null; 2401 } 2402 2403 2404 public AttributePSVI getAttributePSVI(int index){ 2405 2406 return (AttributePSVI)fAttributesProxy.fAttributes.getAugmentations(index).getItem(Constants.ATTRIBUTE_PSVI); 2407 } 2408 2409 2410 public AttributePSVI getAttributePSVIByName(String uri, 2411 String localname){ 2412 return (AttributePSVI)fAttributesProxy.fAttributes.getAugmentations(uri, localname).getItem(Constants.ATTRIBUTE_PSVI); 2413 } 2414 2415} | Popular Tags |