mrglavas    2005/06/19 21:47:59

  Modified:    java/src/org/apache/xerces/jaxp SAXParserImpl.java
                        DocumentBuilderImpl.java
               java/src/org/apache/xerces/parsers DOMParser.java
  Added:       java/src/org/apache/xerces/jaxp
                        SchemaValidatorConfiguration.java
  Log:
  Initial support for SAXParserFactory.setSchema() and 
DocumentBuilderFactory.setSchema().
  
  Revision  Changes    Path
  1.31      +100 -1    
xml-xerces/java/src/org/apache/xerces/jaxp/SAXParserImpl.java
  
  Index: SAXParserImpl.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xerces/java/src/org/apache/xerces/jaxp/SAXParserImpl.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- SAXParserImpl.java        19 Jun 2005 20:48:43 -0000      1.30
  +++ SAXParserImpl.java        20 Jun 2005 04:47:59 -0000      1.31
  @@ -16,6 +16,7 @@
   
   package org.apache.xerces.jaxp;
   
  +import java.io.IOException;
   import java.util.Enumeration;
   import java.util.HashMap;
   import java.util.Hashtable;
  @@ -26,15 +27,21 @@
   import javax.xml.validation.Schema;
   
   import org.apache.xerces.impl.Constants;
  +import org.apache.xerces.impl.xs.XMLSchemaValidator;
  +import org.apache.xerces.impl.xs.XSMessageFormatter;
   import org.apache.xerces.jaxp.validation.XSGrammarPoolContainer;
   import org.apache.xerces.util.SAXMessageFormatter;
   import org.apache.xerces.util.SecurityManager;
  +import org.apache.xerces.xni.parser.XMLComponent;
  +import org.apache.xerces.xni.parser.XMLComponentManager;
  +import org.apache.xerces.xni.parser.XMLConfigurationException;
   import org.apache.xerces.xni.parser.XMLParserConfiguration;
   import org.apache.xerces.xs.AttributePSVI;
   import org.apache.xerces.xs.ElementPSVI;
   import org.apache.xerces.xs.PSVIProvider;
   import org.xml.sax.EntityResolver;
   import org.xml.sax.ErrorHandler;
  +import org.xml.sax.InputSource;
   import org.xml.sax.Parser;
   import org.xml.sax.SAXException;
   import org.xml.sax.SAXNotRecognizedException;
  @@ -81,6 +88,9 @@
       private String schemaLanguage = null;     // null means DTD
       private final Schema grammar;
       
  +    private XMLComponent fSchemaValidator;
  +    private XMLComponentManager fSchemaValidatorComponentManager;
  +    
       /** Initial ErrorHandler */
       private final ErrorHandler fInitErrorHandler;
       
  @@ -145,6 +155,19 @@
           
           // Get the Schema object from the factory
           this.grammar = spf.getSchema();
  +        if (grammar != null) {
  +            if (grammar instanceof XSGrammarPoolContainer) {
  +                XMLParserConfiguration config = 
xmlReader.getXMLParserConfiguration();
  +                XMLSchemaValidator validator = new XMLSchemaValidator();
  +                
config.addRecognizedFeatures(validator.getRecognizedFeatures());
  +                
config.addRecognizedProperties(validator.getRecognizedProperties());
  +                config.setDocumentHandler(validator);
  +                validator.setDocumentHandler(xmlReader);
  +                xmlReader.setDocumentSource(validator);
  +                fSchemaValidator = validator;
  +                fSchemaValidatorComponentManager = new 
SchemaValidatorConfiguration(config, (XSGrammarPoolContainer) grammar);
  +            }
  +        }
           
           // Initial EntityResolver
           fInitEntityResolver = xmlReader.getEntityResolver();
  @@ -327,6 +350,10 @@
                   boolean current = super.getFeature(name);
                   fInitFeatures.put(name, current ? Boolean.TRUE : 
Boolean.FALSE); 
               }
  +            /** Forward feature to the schema validator if there is one. **/
  +            if (fSAXParser != null && fSAXParser.fSchemaValidator != null) {
  +                setSchemaValidatorFeature(name, value);
  +            }
               super.setFeature(name, value);               
           }
           
  @@ -421,6 +448,10 @@
               if (!fInitProperties.containsKey(name)) {
                   fInitProperties.put(name, super.getProperty(name));
               }
  +            /** Forward property to the schema validator if there is one. **/
  +            if (fSAXParser != null && fSAXParser.fSchemaValidator != null) {
  +                setSchemaValidatorProperty(name, value);
  +            }
               super.setProperty(name, value);
           }
           
  @@ -462,6 +493,22 @@
               }
           }
           
  +        public void parse(InputSource inputSource)
  +            throws SAXException, IOException {
  +            if (fSAXParser != null && fSAXParser.fSchemaValidator != null) {
  +                resetSchemaValidator();
  +            }
  +            super.parse(inputSource);
  +        }
  +        
  +        public void parse(String systemId) 
  +            throws SAXException, IOException {
  +            if (fSAXParser != null && fSAXParser.fSchemaValidator != null) {
  +                resetSchemaValidator();
  +            }
  +            super.parse(systemId);
  +        }
  +        
           XMLParserConfiguration getXMLParserConfiguration() {
               return fConfiguration;
           }
  @@ -485,5 +532,57 @@
               throws SAXNotRecognizedException, SAXNotSupportedException {
               return super.getProperty(name);
           }
  +        
  +        private void setSchemaValidatorFeature(String name, boolean value)
  +            throws SAXNotRecognizedException, SAXNotSupportedException {
  +            try {
  +                fSAXParser.fSchemaValidator.setFeature(name, value);
  +            }
  +            // This should never be thrown from the schema validator.
  +            catch (XMLConfigurationException e) {
  +                String identifier = e.getIdentifier();
  +                if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) 
{
  +                    throw new SAXNotRecognizedException(
  +                        
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 
  +                        "feature-not-recognized", new Object [] 
{identifier}));
  +                }
  +                else {
  +                    throw new SAXNotSupportedException(
  +                        
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 
  +                        "feature-not-supported", new Object [] 
{identifier}));
  +                }
  +            }
  +        }
  +        
  +        private void setSchemaValidatorProperty(String name, Object value) 
  +            throws SAXNotRecognizedException, SAXNotSupportedException {
  +            try {
  +                fSAXParser.fSchemaValidator.setProperty(name, value);
  +            }
  +            // This should never be thrown from the schema validator.
  +            catch (XMLConfigurationException e) {
  +                String identifier = e.getIdentifier();
  +                if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) 
{
  +                    throw new SAXNotRecognizedException(
  +                        
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 
  +                        "property-not-recognized", new Object [] 
{identifier}));
  +                }
  +                else {
  +                    throw new SAXNotSupportedException(
  +                        
SAXMessageFormatter.formatMessage(fConfiguration.getLocale(), 
  +                        "property-not-supported", new Object [] 
{identifier}));
  +                }
  +            }
  +        }
  +        
  +        private void resetSchemaValidator() throws SAXException {
  +            try {
  +                
fSAXParser.fSchemaValidator.reset(fSAXParser.fSchemaValidatorComponentManager);
  +            }
  +            // This should never be thrown from the schema validator.
  +            catch (XMLConfigurationException e) {
  +                throw new SAXException(e);
  +            }
  +        }
       }
   }
  
  
  
  1.31      +36 -1     
xml-xerces/java/src/org/apache/xerces/jaxp/DocumentBuilderImpl.java
  
  Index: DocumentBuilderImpl.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xerces/java/src/org/apache/xerces/jaxp/DocumentBuilderImpl.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- DocumentBuilderImpl.java  19 Jun 2005 21:29:41 -0000      1.30
  +++ DocumentBuilderImpl.java  20 Jun 2005 04:47:59 -0000      1.31
  @@ -26,8 +26,14 @@
   import org.apache.xerces.dom.DOMImplementationImpl;
   import org.apache.xerces.dom.DOMMessageFormatter;
   import org.apache.xerces.impl.Constants;
  +import org.apache.xerces.impl.xs.XMLSchemaValidator;
  +import org.apache.xerces.jaxp.validation.XSGrammarPoolContainer;
   import org.apache.xerces.parsers.DOMParser;
   import org.apache.xerces.util.SecurityManager;
  +import org.apache.xerces.xni.parser.XMLComponent;
  +import org.apache.xerces.xni.parser.XMLComponentManager;
  +import org.apache.xerces.xni.parser.XMLConfigurationException;
  +import org.apache.xerces.xni.parser.XMLParserConfiguration;
   import org.w3c.dom.DOMImplementation;
   import org.w3c.dom.Document;
   import org.xml.sax.EntityResolver;
  @@ -84,6 +90,9 @@
       private DOMParser domParser = null;
       private final Schema grammar;
       
  +    private XMLComponent fSchemaValidator;
  +    private XMLComponentManager fSchemaValidatorComponentManager;
  +    
       /** Initial ErrorHandler */
       private final ErrorHandler fInitErrorHandler;
       
  @@ -139,6 +148,19 @@
           }
           
           this.grammar = dbf.getSchema();
  +        if (grammar != null) {
  +            if (grammar instanceof XSGrammarPoolContainer) {
  +                XMLParserConfiguration config = 
domParser.getXMLParserConfiguration();
  +                XMLSchemaValidator validator = new XMLSchemaValidator();
  +                
config.addRecognizedFeatures(validator.getRecognizedFeatures());
  +                
config.addRecognizedProperties(validator.getRecognizedProperties());
  +                config.setDocumentHandler(validator);
  +                validator.setDocumentHandler(domParser);
  +                domParser.setDocumentSource(validator);
  +                fSchemaValidator = validator;
  +                fSchemaValidatorComponentManager = new 
SchemaValidatorConfiguration(config, (XSGrammarPoolContainer) grammar);
  +            }
  +        }
   
           // Set features
           setFeatures(features);
  @@ -234,6 +256,9 @@
                   
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 
                   "jaxp-null-input-source", null));
           }
  +        if (fSchemaValidator != null) {
  +            resetSchemaValidator();
  +        }
           domParser.parse(is);
           return domParser.getDocument();
       }
  @@ -296,4 +321,14 @@
       DOMParser getDOMParser() {
           return domParser;
       }
  +    
  +    private void resetSchemaValidator() throws SAXException {
  +        try {
  +            fSchemaValidator.reset(fSchemaValidatorComponentManager);
  +        }
  +        // This should never be thrown from the schema validator.
  +        catch (XMLConfigurationException e) {
  +            throw new SAXException(e);
  +        }
  +    }
   }
  
  
  
  1.1                  
xml-xerces/java/src/org/apache/xerces/jaxp/SchemaValidatorConfiguration.java
  
  Index: SchemaValidatorConfiguration.java
  ===================================================================
  /*
   * Copyright 2005 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.xerces.jaxp;
  
  import org.apache.xerces.impl.Constants;
  import org.apache.xerces.impl.XMLErrorReporter;
  import org.apache.xerces.impl.xs.XSMessageFormatter;
  import org.apache.xerces.jaxp.validation.XSGrammarPoolContainer;
  import org.apache.xerces.xni.grammars.XMLGrammarPool;
  import org.apache.xerces.xni.parser.XMLComponentManager;
  import org.apache.xerces.xni.parser.XMLConfigurationException;
  
  /**
   * <p>Parser configuration for Xerces' XMLSchemaValidator.</p>
   * 
   * @version $Id: SchemaValidatorConfiguration.java,v 1.1 2005/06/20 04:47:59 
mrglavas Exp $
   */
  class SchemaValidatorConfiguration implements XMLComponentManager {
      
      // feature identifiers
      
      /** Feature identifier: schema validation. */
      private static final String SCHEMA_VALIDATION =
          Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE;
      
      /** Feature identifier: validation. */
      private static final String VALIDATION =
          Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
      
      /** Feature identifier: use grammar pool only. */
      private static final String USE_GRAMMAR_POOL_ONLY =
          Constants.XERCES_FEATURE_PREFIX + 
Constants.USE_GRAMMAR_POOL_ONLY_FEATURE;
      
      /** Feature identifier: parser settings. */
      private static final String PARSER_SETTINGS = 
          Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
      
      // property identifiers
      
      /** Property identifier: error reporter. */
      private static final String ERROR_REPORTER =
          Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
      
      /** Property identifier: grammar pool. */
      private static final String XMLGRAMMAR_POOL =
          Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
      
      //
      // Data
      //
      
      /** Parent component manager. **/
      private final XMLComponentManager fParentComponentManager;
      
      /** The Schema's grammar pool. **/
      private final XMLGrammarPool fGrammarPool;  
  
      /** 
       * Tracks whether the validator should use components from 
       * the grammar pool to the exclusion of all others.
       */
      private final boolean fUseGrammarPoolOnly;
      
      public SchemaValidatorConfiguration(XMLComponentManager parentManager, 
              XSGrammarPoolContainer grammarContainer) {
          fParentComponentManager = parentManager;
          fGrammarPool = grammarContainer.getGrammarPool();
          fUseGrammarPoolOnly = grammarContainer.isFullyComposed();
          // add schema message formatter to error reporter
          try {
              XMLErrorReporter errorReporter = (XMLErrorReporter) 
fParentComponentManager.getProperty(ERROR_REPORTER);
              if (errorReporter != null) {
                  
errorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new 
XSMessageFormatter());
              }
          }
          // Ignore exception.
          catch (XMLConfigurationException exc) {}
      }
      
      /**
       * Returns the state of a feature.
       * 
       * @param featureId The feature identifier.
       * @return true if the feature is supported
       * 
       * @throws XMLConfigurationException Thrown for configuration error.
       *                                   In general, components should
       *                                   only throw this exception if
       *                                   it is <strong>really</strong>
       *                                   a critical error.
       */
      public boolean getFeature(String featureId)
              throws XMLConfigurationException {
          if (PARSER_SETTINGS.equals(featureId)) {
              return fParentComponentManager.getFeature(featureId);
          }
          else if (VALIDATION.equals(featureId) || 
SCHEMA_VALIDATION.equals(featureId)) {
              return true;
          }
          else if (USE_GRAMMAR_POOL_ONLY.equals(featureId)) {
              return fUseGrammarPoolOnly;
          }
          return fParentComponentManager.getFeature(featureId);
      }
  
      /**
       * Returns the value of a property.
       * 
       * @param propertyId The property identifier.
       * @return the value of the property
       * 
       * @throws XMLConfigurationException Thrown for configuration error.
       *                                   In general, components should
       *                                   only throw this exception if
       *                                   it is <strong>really</strong>
       *                                   a critical error.
       */
      public Object getProperty(String propertyId)
              throws XMLConfigurationException {
          if (XMLGRAMMAR_POOL.equals(propertyId)) {
              return fGrammarPool;
          }
          return fParentComponentManager.getProperty(propertyId);
      }
  }
  
  
  
  1.76      +8 -1      
xml-xerces/java/src/org/apache/xerces/parsers/DOMParser.java
  
  Index: DOMParser.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/DOMParser.java,v
  retrieving revision 1.75
  retrieving revision 1.76
  diff -u -r1.75 -r1.76
  --- DOMParser.java    19 Jun 2005 21:43:49 -0000      1.75
  +++ DOMParser.java    20 Jun 2005 04:47:59 -0000      1.76
  @@ -564,5 +564,12 @@
           }
   
       } // getProperty(String):Object
  +    
  +    /** 
  +     * Returns this parser's XMLParserConfiguration.
  +     */
  +    public XMLParserConfiguration getXMLParserConfiguration() {
  +        return fConfiguration;
  +    } // getXMLParserConfiguration():XMLParserConfiguration
   
   } // class DOMParser
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to