mrglavas 2004/05/10 18:06:55 Modified: java/src/org/apache/xerces/parsers AbstractSAXParser.java java/src/org/apache/xerces/impl/msg SAXMessages.properties Log: Feature/Property Support for SAX2 Extensions 1.1 & SAX 2.0.2.
SAX2 Extensions 1.1: - is-standalone - resolve-dtd-uris - use-attributes2 - use-locator2 - use-entity-resolver2 SAX 2.0.2: - unicode-normalization-checking - xmlns-uris - xml-1.1 - document-xml-version (property) Still need to link with the SAX 2.0.2 API. Until then, returning false for use-attributes2 and use-locator2. Getting and setting of use-entity-resolver2 currently commented out. Revision Changes Path 1.55 +235 -29 xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java Index: AbstractSAXParser.java =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java,v retrieving revision 1.54 retrieving revision 1.55 diff -u -r1.54 -r1.55 --- AbstractSAXParser.java 7 Apr 2004 15:42:05 -0000 1.54 +++ AbstractSAXParser.java 11 May 2004 01:06:55 -0000 1.55 @@ -22,6 +22,8 @@ import org.apache.xerces.impl.Constants; import org.apache.xerces.xs.PSVIProvider; import org.apache.xerces.util.EntityResolverWrapper; +import org.apache.xerces.util.EntityResolver2Wrapper; +import org.apache.xerces.util.EntityResolver2Wrapper.EntityResolver2; import org.apache.xerces.util.ErrorHandlerWrapper; import org.apache.xerces.util.SAXMessageFormatter; import org.apache.xerces.util.SymbolHash; @@ -143,6 +145,21 @@ /** Lexical handler parameter entities. */ protected boolean fLexicalHandlerParameterEntities = true; + + /** Standalone document declaration. */ + protected boolean fStandalone; + + /** Resolve DTD URIs. */ + protected boolean fResolveDTDURIs = true; + + /** Use EntityResolver2. */ + protected boolean fUseEntityResolver2 = true; + + /** + * XMLNS URIs: Namespace declarations in the + * http://www.w3.org/2000/xmlns/ namespace. + */ + protected boolean fXMLNSURIs = false; // parser handlers @@ -286,10 +303,10 @@ */ public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) throws XNIException { - // the version need only be set once; if // document's XML 1.0|1.1, that's how it'll stay fVersion = version; + fStandalone = "yes".equals(standalone); } // xmlDecl(String,String,String) /** @@ -448,15 +465,21 @@ // we cannot use the declared prefix count for the current context // to skip this section. -- mrglavas int len = attributes.getLength(); - for (int i = len - 1; i >= 0; --i) { - attributes.getName(i, fQName); - if ((fQName.prefix == XMLSymbols.PREFIX_XMLNS) || - (fQName.rawname == XMLSymbols.PREFIX_XMLNS)) { - if (!fNamespacePrefixes) { + if (!fNamespacePrefixes) { + for (int i = len - 1; i >= 0; --i) { + attributes.getName(i, fQName); + if ((fQName.prefix == XMLSymbols.PREFIX_XMLNS) || + (fQName.rawname == XMLSymbols.PREFIX_XMLNS)) { // remove namespace declaration attributes attributes.removeAttributeAt(i); } - else { + } + } + else if (!fXMLNSURIs) { + for (int i = len - 1; i >= 0; --i) { + attributes.getName(i, fQName); + if ((fQName.prefix == XMLSymbols.PREFIX_XMLNS) || + (fQName.rawname == XMLSymbols.PREFIX_XMLNS)) { // localpart should be empty string as per SAX documentation: // http://www.saxproject.org/?selected=namespaces fQName.prefix = ""; @@ -981,13 +1004,13 @@ */ public void externalEntityDecl(String name, XMLResourceIdentifier identifier, Augmentations augs) throws XNIException { - - String publicId = identifier.getPublicId(); - String literalSystemId = identifier.getLiteralSystemId(); try { // SAX2 extension if (fDeclHandler != null) { - fDeclHandler.externalEntityDecl(name, publicId, literalSystemId); + String publicId = identifier.getPublicId(); + String systemId = fResolveDTDURIs ? + identifier.getExpandedSystemId() : identifier.getLiteralSystemId(); + fDeclHandler.externalEntityDecl(name, publicId, systemId); } } catch (SAXException e) { @@ -1012,14 +1035,13 @@ public void unparsedEntityDecl(String name, XMLResourceIdentifier identifier, String notation, Augmentations augs) throws XNIException { - - String publicId = identifier.getPublicId(); - String expandedSystemId = identifier.getExpandedSystemId(); try { // SAX2 extension if (fDTDHandler != null) { - fDTDHandler.unparsedEntityDecl(name, publicId, - expandedSystemId, notation); + String publicId = identifier.getPublicId(); + String systemId = fResolveDTDURIs ? + identifier.getExpandedSystemId() : identifier.getLiteralSystemId(); + fDTDHandler.unparsedEntityDecl(name, publicId, systemId, notation); } } catch (SAXException e) { @@ -1041,13 +1063,13 @@ */ public void notationDecl(String name, XMLResourceIdentifier identifier, Augmentations augs) throws XNIException { - - String publicId = identifier.getPublicId(); - String expandedSystemId = identifier.getExpandedSystemId(); try { // SAX1 and SAX2 if (fDTDHandler != null) { - fDTDHandler.notationDecl(name, publicId, expandedSystemId); + String publicId = identifier.getPublicId(); + String systemId = fResolveDTDURIs ? + identifier.getExpandedSystemId() : identifier.getLiteralSystemId(); + fDTDHandler.notationDecl(name, publicId, systemId); } } catch (SAXException e) { @@ -1241,8 +1263,14 @@ public void setEntityResolver(EntityResolver resolver) { try { - fConfiguration.setProperty(ENTITY_RESOLVER, - new EntityResolverWrapper(resolver)); + if (fUseEntityResolver2 && resolver instanceof EntityResolver2) { + fConfiguration.setProperty(ENTITY_RESOLVER, + new EntityResolver2Wrapper((EntityResolver2) resolver)); + } + else { + fConfiguration.setProperty(ENTITY_RESOLVER, + new EntityResolverWrapper(resolver)); + } } catch (XMLConfigurationException e) { // do nothing @@ -1263,9 +1291,15 @@ try { XMLEntityResolver xmlEntityResolver = (XMLEntityResolver)fConfiguration.getProperty(ENTITY_RESOLVER); - if (xmlEntityResolver != null && - xmlEntityResolver instanceof EntityResolverWrapper) { - entityResolver = ((EntityResolverWrapper)xmlEntityResolver).getEntityResolver(); + if (xmlEntityResolver != null) { + if (xmlEntityResolver instanceof EntityResolverWrapper) { + entityResolver = + ((EntityResolverWrapper) xmlEntityResolver).getEntityResolver(); + } + else if (xmlEntityResolver instanceof EntityResolver2Wrapper) { + entityResolver = + ((EntityResolver2Wrapper) xmlEntityResolver).getEntityResolver(); + } } } catch (XMLConfigurationException e) { @@ -1464,6 +1498,7 @@ fNamespaces = state; return; } + // http://xml.org/sax/features/namespace-prefixes // controls the reporting of raw prefixed names and Namespace // declarations (xmlns* attributes): when this feature is false @@ -1476,6 +1511,7 @@ fNamespacePrefixes = state; return; } + // http://xml.org/sax/features/string-interning // controls the use of java.lang.String#intern() for strings // passed to SAX handlers. @@ -1489,6 +1525,7 @@ } return; } + // http://xml.org/sax/features/lexical-handler/parameter-entities // controls whether the beginning and end of parameter entities // will be reported to the LexicalHandler. @@ -1498,6 +1535,85 @@ fLexicalHandlerParameterEntities = state; return; } + + // http://xml.org/sax/features/resolve-dtd-uris + // controls whether system identifiers will be absolutized relative to + // their base URIs before reporting. + // + if (suffixLength == Constants.RESOLVE_DTD_URIS_FEATURE.length() && + featureId.endsWith(Constants.RESOLVE_DTD_URIS_FEATURE)) { + fResolveDTDURIs = state; + return; + } + + // http://xml.org/sax/features/unicode-normalization-checking + // controls whether Unicode normalization checking is performed + // as per Appendix B of the XML 1.1 specification + // + if (suffixLength == Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE.length() && + featureId.endsWith(Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE)) { + // REVISIT: Allow this feature to be set once Unicode normalization + // checking is supported -- mrglavas. + if (state) { + throw new SAXNotSupportedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "true-not-supported", new Object [] {featureId})); + } + return; + } + + // http://xml.org/sax/features/xmlns-uris + // controls whether the parser reports that namespace declaration + // attributes as being in the namespace: http://www.w3.org/2000/xmlns/ + // + if (suffixLength == Constants.XMLNS_URIS_FEATURE.length() && + featureId.endsWith(Constants.XMLNS_URIS_FEATURE)) { + fXMLNSURIs = state; + return; + } + + // http://xml.org/sax/features/use-entity-resolver2 + // controls whether the methods of an object implementing + // org.xml.sax.ext.EntityResolver2 will be used by the parser. + // + // TODO: Uncomment once SAX 2.0.2 is available. -- mrglavas + /**if (suffixLength == Constants.USE_ENTITY_RESOLVER2_FEATURE.length() && + featureId.endsWith(Constants.USE_ENTITY_RESOLVER2_FEATURE)) { + if (state != fUseEntityResolver2) { + fUseEntityResolver2 = state; + // Refresh EntityResolver wrapper. + setEntityResolver(getEntityResolver()); + } + return; + }**/ + + // + // Read only features. + // + + // http://xml.org/sax/features/is-standalone + // reports whether the document specified a standalone document declaration. + // http://xml.org/sax/features/use-attributes2 + // reports whether Attributes objects passed to startElement also implement + // the org.xml.sax.ext.Attributes2 interface. + // http://xml.org/sax/features/use-locator2 + // reports whether Locator objects passed to setDocumentLocator also implement + // the org.xml.sax.ext.Locator2 interface. + // http://xml.org/sax/features/xml-1.1 + // reports whether the parser supports both XML 1.1 and XML 1.0. + if ((suffixLength == Constants.IS_STANDALONE_FEATURE.length() && + featureId.endsWith(Constants.IS_STANDALONE_FEATURE)) || + (suffixLength == Constants.USE_ATTRIBUTES2_FEATURE.length() && + featureId.endsWith(Constants.USE_ATTRIBUTES2_FEATURE)) || + (suffixLength == Constants.USE_LOCATOR2_FEATURE.length() && + featureId.endsWith(Constants.USE_LOCATOR2_FEATURE)) || + (suffixLength == Constants.XML_11_FEATURE.length() && + featureId.endsWith(Constants.XML_11_FEATURE))) { + throw new SAXNotSupportedException( + SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), + "feature-read-only", new Object [] {featureId})); + } + // // Drop through and perform default processing @@ -1584,14 +1700,84 @@ return true; } + // http://xml.org/sax/features/is-standalone + // reports whether the document specified a standalone document declaration. + // + if (suffixLength == Constants.IS_STANDALONE_FEATURE.length() && + featureId.endsWith(Constants.IS_STANDALONE_FEATURE)) { + return fStandalone; + } + + // http://xml.org/sax/features/xml-1.1 + // reports whether the parser supports both XML 1.1 and XML 1.0. + // + if (suffixLength == Constants.XML_11_FEATURE.length() && + featureId.endsWith(Constants.XML_11_FEATURE)) { + return (fConfiguration instanceof XML11Configuration); + } + // http://xml.org/sax/features/lexical-handler/parameter-entities // controls whether the beginning and end of parameter entities // will be reported to the LexicalHandler. // if (suffixLength == Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE.length() && featureId.endsWith(Constants.LEXICAL_HANDLER_PARAMETER_ENTITIES_FEATURE)) { - return fLexicalHandlerParameterEntities; + return fLexicalHandlerParameterEntities; + } + + // http://xml.org/sax/features/resolve-dtd-uris + // controls whether system identifiers will be absolutized relative to + // their base URIs before reporting. + if (suffixLength == Constants.RESOLVE_DTD_URIS_FEATURE.length() && + featureId.endsWith(Constants.RESOLVE_DTD_URIS_FEATURE)) { + return fResolveDTDURIs; } + + // http://xml.org/sax/features/xmlns-uris + // controls whether the parser reports that namespace declaration + // attributes as being in the namespace: http://www.w3.org/2000/xmlns/ + // + if (suffixLength == Constants.XMLNS_URIS_FEATURE.length() && + featureId.endsWith(Constants.XMLNS_URIS_FEATURE)) { + return fXMLNSURIs; + } + + // http://xml.org/sax/features/unicode-normalization-checking + // controls whether Unicode normalization checking is performed + // as per Appendix B of the XML 1.1 specification + // + if (suffixLength == Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE.length() && + featureId.endsWith(Constants.UNICODE_NORMALIZATION_CHECKING_FEATURE)) { + // REVISIT: Allow this feature to be set once Unicode normalization + // checking is supported -- mrglavas. + return false; + } + + // http://xml.org/sax/features/use-entity-resolver2 + // controls whether the methods of an object implementing + // org.xml.sax.ext.EntityResolver2 will be used by the parser. + // + // TODO: Uncomment once SAX 2.0.2 is available. -- mrglavas + /** if (suffixLength == Constants.USE_ENTITY_RESOLVER2_FEATURE.length() && + featureId.endsWith(Constants.USE_ENTITY_RESOLVER2_FEATURE)) { + return fUseEntityResolver2; + }**/ + + // http://xml.org/sax/features/use-attributes2 + // reports whether Attributes objects passed to startElement also implement + // the org.xml.sax.ext.Attributes2 interface. + // http://xml.org/sax/features/use-locator2 + // reports whether Locator objects passed to setDocumentLocator also implement + // the org.xml.sax.ext.Locator2 interface. + // + if ((suffixLength == Constants.USE_ATTRIBUTES2_FEATURE.length() && + featureId.endsWith(Constants.USE_ATTRIBUTES2_FEATURE)) || + (suffixLength == Constants.USE_LOCATOR2_FEATURE.length() && + featureId.endsWith(Constants.USE_LOCATOR2_FEATURE))) { + // REVISIT: Return true once SAX 2.0.2 interfaces are available. -- mrglavas + return false; + } + // // Drop through and perform default processing @@ -1699,9 +1885,15 @@ // supports this property but is not currently visiting a DOM // node, it should return null (this is a good way to check for // availability before the parse begins). + // http://xml.org/sax/properties/document-xml-version + // Value type: java.lang.String + // Access: read-only + // The literal string describing the actual XML version of the document. // - if (suffixLength == Constants.DOM_NODE_PROPERTY.length() && - propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) { + if ((suffixLength == Constants.DOM_NODE_PROPERTY.length() && + propertyId.endsWith(Constants.DOM_NODE_PROPERTY)) || + (suffixLength == Constants.DOCUMENT_XML_VERSION_PROPERTY.length() && + propertyId.endsWith(Constants.DOCUMENT_XML_VERSION_PROPERTY))) { throw new SAXNotSupportedException( SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "property-read-only", new Object [] {propertyId})); @@ -1771,6 +1963,17 @@ final int suffixLength = propertyId.length() - Constants.SAX_PROPERTY_PREFIX.length(); // + // http://xml.org/sax/properties/document-xml-version + // Value type: java.lang.String + // Access: read-only + // The literal string describing the actual XML version of the document. + // + if (suffixLength == Constants.DOCUMENT_XML_VERSION_PROPERTY.length() && + propertyId.endsWith(Constants.DOCUMENT_XML_VERSION_PROPERTY)) { + return fVersion; + } + + // // http://xml.org/sax/properties/lexical-handler // Value type: org.xml.sax.ext.LexicalHandler // Access: read/write, pre-parse only @@ -1790,6 +1993,7 @@ propertyId.endsWith(Constants.DECLARATION_HANDLER_PROPERTY)) { return getDeclHandler(); } + // // http://xml.org/sax/properties/dom-node // Value type: DOM Node @@ -1807,6 +2011,7 @@ SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "dom-node-read-not-supported", null)); } + // // Drop through and perform default processing // @@ -1968,6 +2173,7 @@ // reset state fInDTD = false; fVersion = "1.0"; + fStandalone = false; // features fNamespaces = fConfiguration.getFeature(NAMESPACES); 1.2 +2 -1 xml-xerces/java/src/org/apache/xerces/impl/msg/SAXMessages.properties Index: SAXMessages.properties =================================================================== RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/impl/msg/SAXMessages.properties,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SAXMessages.properties 5 Feb 2004 21:30:09 -0000 1.1 +++ SAXMessages.properties 11 May 2004 01:06:55 -0000 1.2 @@ -17,6 +17,7 @@ feature-not-recognized = Feature ''{0}'' is not recognized. true-not-supported = True state for feature ''{0}'' is not supported. false-not-supported = False state for feature ''{0}'' is not supported. +feature-read-only = Feature ''{0}'' is read only. # property messages property-not-supported = Property ''{0}'' is not supported. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]