src/com/sun/org/apache/xerces/internal/jaxp/SAXParserImpl.java

Print this page




  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.jaxp;
  22 
  23 import java.io.IOException;
  24 import java.util.HashMap;
  25 import java.util.Hashtable;
  26 import java.util.Iterator;
  27 import java.util.Map;
  28 
  29 import javax.xml.XMLConstants;
  30 import javax.xml.validation.Schema;
  31 
  32 import com.sun.org.apache.xerces.internal.impl.Constants;
  33 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  34 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
  35 import com.sun.org.apache.xerces.internal.jaxp.validation.XSGrammarPoolContainer;
  36 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;
  37 import com.sun.org.apache.xerces.internal.util.SecurityManager;
  38 import com.sun.org.apache.xerces.internal.util.Status;

  39 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
  40 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
  41 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  42 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  43 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
  44 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
  45 import com.sun.org.apache.xerces.internal.xs.AttributePSVI;
  46 import com.sun.org.apache.xerces.internal.xs.ElementPSVI;
  47 import com.sun.org.apache.xerces.internal.xs.PSVIProvider;
  48 import org.xml.sax.EntityResolver;
  49 import org.xml.sax.ErrorHandler;
  50 import org.xml.sax.HandlerBase;
  51 import org.xml.sax.InputSource;
  52 import org.xml.sax.Parser;
  53 import org.xml.sax.SAXException;
  54 import org.xml.sax.SAXNotRecognizedException;
  55 import org.xml.sax.SAXNotSupportedException;
  56 import org.xml.sax.XMLReader;
  57 import org.xml.sax.helpers.DefaultHandler;
  58 


  75     /** Feature identifier: namespace prefixes. */
  76     private static final String NAMESPACE_PREFIXES_FEATURE =
  77         Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;
  78 
  79     /** Feature identifier: validation. */
  80     private static final String VALIDATION_FEATURE =
  81         Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
  82 
  83     /** Feature identifier: XML Schema validation */
  84     private static final String XMLSCHEMA_VALIDATION_FEATURE =
  85         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
  86 
  87     /** Feature identifier: XInclude processing */
  88     private static final String XINCLUDE_FEATURE =
  89         Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE;
  90 
  91     /** Property identifier: security manager. */
  92     private static final String SECURITY_MANAGER =
  93         Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
  94 
  95     /** property identifier: access external dtd. */
  96     public static final String ACCESS_EXTERNAL_DTD = XMLConstants.ACCESS_EXTERNAL_DTD;

  97 
  98     /** Property identifier: access to external schema */
  99     public static final String ACCESS_EXTERNAL_SCHEMA = XMLConstants.ACCESS_EXTERNAL_SCHEMA;
 100 
 101     private final JAXPSAXParser xmlReader;
 102     private String schemaLanguage = null;     // null means DTD
 103     private final Schema grammar;
 104 
 105     private final XMLComponent fSchemaValidator;
 106     private final XMLComponentManager fSchemaValidatorComponentManager;
 107     private final ValidationManager fSchemaValidationManager;
 108     private final UnparsedEntityHandler fUnparsedEntityHandler;
 109 
 110     /** Initial ErrorHandler */
 111     private final ErrorHandler fInitErrorHandler;
 112 
 113     /** Initial EntityResolver */
 114     private final EntityResolver fInitEntityResolver;
 115 


 116     /**
 117      * Create a SAX parser with the associated features
 118      * @param features Hashtable of SAX features, may be null
 119      */
 120     SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features)
 121         throws SAXException {
 122         this(spf, features, false);
 123     }
 124 
 125     /**
 126      * Create a SAX parser with the associated features
 127      * @param features Hashtable of SAX features, may be null
 128      */
 129     SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features, boolean secureProcessing)
 130         throws SAXException
 131     {
 132         // Instantiate a SAXParser directly and not through SAX so that we use the right ClassLoader
 133         xmlReader = new JAXPSAXParser(this);
 134 
 135         // JAXP "namespaceAware" == SAX Namespaces feature
 136         // Note: there is a compatibility problem here with default values:
 137         // JAXP default is false while SAX 2 default is true!
 138         xmlReader.setFeature0(NAMESPACES_FEATURE, spf.isNamespaceAware());
 139 
 140         // SAX "namespaces" and "namespace-prefixes" features should not
 141         // both be false.  We make them opposite for backward compatibility
 142         // since JAXP 1.0 apps may want to receive xmlns* attributes.
 143         xmlReader.setFeature0(NAMESPACE_PREFIXES_FEATURE, !spf.isNamespaceAware());
 144 
 145         // Avoid setting the XInclude processing feature if the value is false.
 146         // This will keep the configuration from throwing an exception if it
 147         // does not support XInclude.
 148         if (spf.isXIncludeAware()) {
 149             xmlReader.setFeature0(XINCLUDE_FEATURE, true);
 150         }
 151 



 152         // If the secure processing feature is on set a security manager.
 153         if (secureProcessing) {
 154             xmlReader.setProperty0(SECURITY_MANAGER, new SecurityManager());
 155             /**
 156              * By default, secure processing is set, no external access is allowed.
 157              * However, we need to check if it is actively set on the factory since we
 158              * allow the use of the System Property or jaxp.properties to override
 159              * the default value
 160              */
 161             if (features != null) {
 162                 Object temp = features.get(XMLConstants.FEATURE_SECURE_PROCESSING);
 163                 if (temp != null) {
 164                     boolean value = ((Boolean) temp).booleanValue();
 165                     if (value) {
 166                         xmlReader.setProperty0(ACCESS_EXTERNAL_DTD, Constants.EXTERNAL_ACCESS_DEFAULT_FSP);
 167                         xmlReader.setProperty0(ACCESS_EXTERNAL_SCHEMA, Constants.EXTERNAL_ACCESS_DEFAULT_FSP);



 168                     }
 169                 }
 170             }
 171         }
 172 
 173         // Set application's features, followed by validation features.
 174         setFeatures(features);
 175 
 176         // If validating, provide a default ErrorHandler that prints
 177         // validation errors with a warning telling the user to set an
 178         // ErrorHandler.
 179         if (spf.isValidating()) {
 180             fInitErrorHandler = new DefaultValidationErrorHandler();
 181             xmlReader.setErrorHandler(fInitErrorHandler);
 182         }
 183         else {
 184             fInitErrorHandler = xmlReader.getErrorHandler();
 185         }
 186         xmlReader.setFeature0(VALIDATION_FEATURE, spf.isValidating());
 187 


 513                     if (fSAXParser.grammar != null) {
 514                         throw new SAXNotSupportedException(
 515                                 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-already-specified", new Object[] {name}));
 516                     }
 517                     String val = (String)getProperty(JAXP_SCHEMA_LANGUAGE);
 518                     if ( val != null && W3C_XML_SCHEMA.equals(val) ) {
 519                         if (!fInitProperties.containsKey(JAXP_SCHEMA_SOURCE)) {
 520                             fInitProperties.put(JAXP_SCHEMA_SOURCE, super.getProperty(JAXP_SCHEMA_SOURCE));
 521                         }
 522                         super.setProperty(name, value);
 523                     }
 524                     else {
 525                         throw new SAXNotSupportedException(
 526                             SAXMessageFormatter.formatMessage(fConfiguration.getLocale(),
 527                             "jaxp-order-not-supported",
 528                             new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE}));
 529                     }
 530                     return;
 531                 }
 532             }
 533             if (!fInitProperties.containsKey(name)) {
 534                 fInitProperties.put(name, super.getProperty(name));
 535             }
 536             /** Forward property to the schema validator if there is one. **/
 537             if (fSAXParser != null && fSAXParser.fSchemaValidator != null) {
 538                 setSchemaValidatorProperty(name, value);
 539             }









 540             super.setProperty(name, value);
 541         }

 542 
 543         public synchronized Object getProperty(String name)
 544             throws SAXNotRecognizedException, SAXNotSupportedException {
 545             if (name == null) {
 546                 // TODO: Add localized error message.
 547                 throw new NullPointerException();
 548             }
 549             if (fSAXParser != null && JAXP_SCHEMA_LANGUAGE.equals(name)) {
 550                 // JAXP 1.2 support
 551                 return fSAXParser.schemaLanguage;
 552             }





 553             return super.getProperty(name);
 554         }
 555 
 556         synchronized void restoreInitState()
 557             throws SAXNotRecognizedException, SAXNotSupportedException {
 558             Iterator iter;
 559             if (!fInitFeatures.isEmpty()) {
 560                 iter = fInitFeatures.entrySet().iterator();
 561                 while (iter.hasNext()) {
 562                     Map.Entry entry = (Map.Entry) iter.next();
 563                     String name = (String) entry.getKey();
 564                     boolean value = ((Boolean) entry.getValue()).booleanValue();
 565                     super.setFeature(name, value);
 566                 }
 567                 fInitFeatures.clear();
 568             }
 569             if (!fInitProperties.isEmpty()) {
 570                 iter = fInitProperties.entrySet().iterator();
 571                 while (iter.hasNext()) {
 572                     Map.Entry entry = (Map.Entry) iter.next();




  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.jaxp;
  22 
  23 import java.io.IOException;
  24 import java.util.HashMap;
  25 import java.util.Hashtable;
  26 import java.util.Iterator;
  27 import java.util.Map;
  28 
  29 import javax.xml.XMLConstants;
  30 import javax.xml.validation.Schema;
  31 
  32 import com.sun.org.apache.xerces.internal.impl.Constants;
  33 import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;
  34 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator;
  35 import com.sun.org.apache.xerces.internal.jaxp.validation.XSGrammarPoolContainer;
  36 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;
  37 import com.sun.org.apache.xerces.internal.util.SecurityManager;
  38 import com.sun.org.apache.xerces.internal.util.Status;
  39 import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager;
  40 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
  41 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
  42 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
  43 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
  44 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
  45 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
  46 import com.sun.org.apache.xerces.internal.xs.AttributePSVI;
  47 import com.sun.org.apache.xerces.internal.xs.ElementPSVI;
  48 import com.sun.org.apache.xerces.internal.xs.PSVIProvider;
  49 import org.xml.sax.EntityResolver;
  50 import org.xml.sax.ErrorHandler;
  51 import org.xml.sax.HandlerBase;
  52 import org.xml.sax.InputSource;
  53 import org.xml.sax.Parser;
  54 import org.xml.sax.SAXException;
  55 import org.xml.sax.SAXNotRecognizedException;
  56 import org.xml.sax.SAXNotSupportedException;
  57 import org.xml.sax.XMLReader;
  58 import org.xml.sax.helpers.DefaultHandler;
  59 


  76     /** Feature identifier: namespace prefixes. */
  77     private static final String NAMESPACE_PREFIXES_FEATURE =
  78         Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE;
  79 
  80     /** Feature identifier: validation. */
  81     private static final String VALIDATION_FEATURE =
  82         Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
  83 
  84     /** Feature identifier: XML Schema validation */
  85     private static final String XMLSCHEMA_VALIDATION_FEATURE =
  86         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
  87 
  88     /** Feature identifier: XInclude processing */
  89     private static final String XINCLUDE_FEATURE =
  90         Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE;
  91 
  92     /** Property identifier: security manager. */
  93     private static final String SECURITY_MANAGER =
  94         Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
  95 
  96     /** Property identifier: Security property manager. */
  97     private static final String XML_SECURITY_PROPERTY_MANAGER =
  98             Constants.XML_SECURITY_PROPERTY_MANAGER;
  99 



 100     private final JAXPSAXParser xmlReader;
 101     private String schemaLanguage = null;     // null means DTD
 102     private final Schema grammar;
 103 
 104     private final XMLComponent fSchemaValidator;
 105     private final XMLComponentManager fSchemaValidatorComponentManager;
 106     private final ValidationManager fSchemaValidationManager;
 107     private final UnparsedEntityHandler fUnparsedEntityHandler;
 108 
 109     /** Initial ErrorHandler */
 110     private final ErrorHandler fInitErrorHandler;
 111 
 112     /** Initial EntityResolver */
 113     private final EntityResolver fInitEntityResolver;
 114 
 115     private XMLSecurityPropertyManager fSecurityPropertyMgr;
 116 
 117     /**
 118      * Create a SAX parser with the associated features
 119      * @param features Hashtable of SAX features, may be null
 120      */
 121     SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features)
 122         throws SAXException {
 123         this(spf, features, false);
 124     }
 125 
 126     /**
 127      * Create a SAX parser with the associated features
 128      * @param features Hashtable of SAX features, may be null
 129      */
 130     SAXParserImpl(SAXParserFactoryImpl spf, Hashtable features, boolean secureProcessing)
 131         throws SAXException
 132     {
 133         // Instantiate a SAXParser directly and not through SAX so that we use the right ClassLoader
 134         xmlReader = new JAXPSAXParser(this);
 135 
 136         // JAXP "namespaceAware" == SAX Namespaces feature
 137         // Note: there is a compatibility problem here with default values:
 138         // JAXP default is false while SAX 2 default is true!
 139         xmlReader.setFeature0(NAMESPACES_FEATURE, spf.isNamespaceAware());
 140 
 141         // SAX "namespaces" and "namespace-prefixes" features should not
 142         // both be false.  We make them opposite for backward compatibility
 143         // since JAXP 1.0 apps may want to receive xmlns* attributes.
 144         xmlReader.setFeature0(NAMESPACE_PREFIXES_FEATURE, !spf.isNamespaceAware());
 145 
 146         // Avoid setting the XInclude processing feature if the value is false.
 147         // This will keep the configuration from throwing an exception if it
 148         // does not support XInclude.
 149         if (spf.isXIncludeAware()) {
 150             xmlReader.setFeature0(XINCLUDE_FEATURE, true);
 151         }
 152 
 153         fSecurityPropertyMgr = new XMLSecurityPropertyManager();
 154         xmlReader.setProperty0(XML_SECURITY_PROPERTY_MANAGER, fSecurityPropertyMgr);
 155 
 156         // If the secure processing feature is on set a security manager.
 157         if (secureProcessing) {
 158             xmlReader.setProperty0(SECURITY_MANAGER, new SecurityManager());
 159             /**
 160              * By default, secure processing is set, no external access is allowed.
 161              * However, we need to check if it is actively set on the factory since we
 162              * allow the use of the System Property or jaxp.properties to override
 163              * the default value
 164              */
 165             if (features != null) {
 166                 Object temp = features.get(XMLConstants.FEATURE_SECURE_PROCESSING);
 167                 if (temp != null) {
 168                     boolean value = ((Boolean) temp).booleanValue();
 169                     if (value && Constants.IS_JDK8_OR_ABOVE) {
 170                         fSecurityPropertyMgr.setValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD, 
 171                                 XMLSecurityPropertyManager.State.FSP, Constants.EXTERNAL_ACCESS_DEFAULT_FSP);
 172                         fSecurityPropertyMgr.setValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_SCHEMA, 
 173                                 XMLSecurityPropertyManager.State.FSP, Constants.EXTERNAL_ACCESS_DEFAULT_FSP);
 174 
 175                     }                    
 176                 }
 177             }
 178         }
 179 
 180         // Set application's features, followed by validation features.
 181         setFeatures(features);
 182 
 183         // If validating, provide a default ErrorHandler that prints
 184         // validation errors with a warning telling the user to set an
 185         // ErrorHandler.
 186         if (spf.isValidating()) {
 187             fInitErrorHandler = new DefaultValidationErrorHandler();
 188             xmlReader.setErrorHandler(fInitErrorHandler);
 189         }
 190         else {
 191             fInitErrorHandler = xmlReader.getErrorHandler();
 192         }
 193         xmlReader.setFeature0(VALIDATION_FEATURE, spf.isValidating());
 194 


 520                     if (fSAXParser.grammar != null) {
 521                         throw new SAXNotSupportedException(
 522                                 SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), "schema-already-specified", new Object[] {name}));
 523                     }
 524                     String val = (String)getProperty(JAXP_SCHEMA_LANGUAGE);
 525                     if ( val != null && W3C_XML_SCHEMA.equals(val) ) {
 526                         if (!fInitProperties.containsKey(JAXP_SCHEMA_SOURCE)) {
 527                             fInitProperties.put(JAXP_SCHEMA_SOURCE, super.getProperty(JAXP_SCHEMA_SOURCE));
 528                         }
 529                         super.setProperty(name, value);
 530                     }
 531                     else {
 532                         throw new SAXNotSupportedException(
 533                             SAXMessageFormatter.formatMessage(fConfiguration.getLocale(),
 534                             "jaxp-order-not-supported",
 535                             new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE}));
 536                     }
 537                     return;
 538                 }
 539             }



 540             /** Forward property to the schema validator if there is one. **/
 541             if (fSAXParser != null && fSAXParser.fSchemaValidator != null) {
 542                 setSchemaValidatorProperty(name, value);
 543             }
 544             /** Check to see if the property is managed by the property manager **/
 545             int index = fSAXParser.fSecurityPropertyMgr.getIndex(name);
 546             if (index > -1) {
 547                 fSAXParser.fSecurityPropertyMgr.setValue(index, 
 548                         XMLSecurityPropertyManager.State.APIPROPERTY, (String)value);                    
 549             } else {
 550                 if (!fInitProperties.containsKey(name)) {
 551                     fInitProperties.put(name, super.getProperty(name));
 552                 }
 553                 super.setProperty(name, value);
 554             }
 555         }
 556 
 557         public synchronized Object getProperty(String name)
 558             throws SAXNotRecognizedException, SAXNotSupportedException {
 559             if (name == null) {
 560                 // TODO: Add localized error message.
 561                 throw new NullPointerException();
 562             }
 563             if (fSAXParser != null && JAXP_SCHEMA_LANGUAGE.equals(name)) {
 564                 // JAXP 1.2 support
 565                 return fSAXParser.schemaLanguage;
 566             }
 567             int index = fSAXParser.fSecurityPropertyMgr.getIndex(name);
 568             if (index > -1) {
 569                 return fSAXParser.fSecurityPropertyMgr.getValueByIndex(index);
 570             }
 571 
 572             return super.getProperty(name);
 573         }
 574 
 575         synchronized void restoreInitState()
 576             throws SAXNotRecognizedException, SAXNotSupportedException {
 577             Iterator iter;
 578             if (!fInitFeatures.isEmpty()) {
 579                 iter = fInitFeatures.entrySet().iterator();
 580                 while (iter.hasNext()) {
 581                     Map.Entry entry = (Map.Entry) iter.next();
 582                     String name = (String) entry.getKey();
 583                     boolean value = ((Boolean) entry.getValue()).booleanValue();
 584                     super.setFeature(name, value);
 585                 }
 586                 fInitFeatures.clear();
 587             }
 588             if (!fInitProperties.isEmpty()) {
 589                 iter = fInitProperties.entrySet().iterator();
 590                 while (iter.hasNext()) {
 591                     Map.Entry entry = (Map.Entry) iter.next();