You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2002/08/05 22:48:49 UTC

cvs commit: jakarta-commons/digester/src/java/org/apache/commons/digester Digester.java

craigmcc    2002/08/05 13:48:49

  Modified:    digester/src/java/org/apache/commons/digester Digester.java
  Log:
  Add support for public getFeature() and setFeature() methods to configure
  the SAXParserFactory.  Also added getProperty() and setProperty() methods
  to do the same sort of thing for SAXParser properties.  (I changed the
  original suggested patch to return SAXNotRecognizedException and
  SAXNotSupportedException errors to the caller).
  
  The current implementation still tries to set the schema location and source
  properties, even on a JAXP/1.1 parser that does not recognize them.  Shouldn't
  this be the responsibility of the caller?  Note that if they are removed, we
  should definitely add an example to the o.a.c.digester package description
  because this will likely be a common use case in a JAXP/1.2 world.
  
  Submitted by:	Jean-francois Arcand <jeanfrancois.arcand at sun.com>
  
  Revision  Changes    Path
  1.59      +224 -85   jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java
  
  Index: Digester.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
  retrieving revision 1.58
  retrieving revision 1.59
  diff -u -r1.58 -r1.59
  --- Digester.java	31 Jul 2002 17:23:55 -0000	1.58
  +++ Digester.java	5 Aug 2002 20:48:49 -0000	1.59
  @@ -78,6 +78,7 @@
   import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
  +import javax.xml.parsers.ParserConfigurationException;
   import javax.xml.parsers.SAXParser;
   import javax.xml.parsers.SAXParserFactory;
   
  @@ -220,7 +221,7 @@
       /**
        * The SAXParserFactory that is created the first time we need it.
        */
  -    protected static SAXParserFactory factory = null;
  +    protected SAXParserFactory factory = null;
   
   
       /**
  @@ -360,7 +361,7 @@
       
   
       
  -    // ----------------------------------------------------------- Properties
  +    // ------------------------------------------------------------- Properties
   
       /**
        * Return the currently mapped namespace URI for the specified prefix,
  @@ -502,6 +503,75 @@
   
   
       /**
  +     * Return the SAXParserFactory we will use, creating one if necessary.
  +     */
  +    public SAXParserFactory getFactory() {
  +
  +        if (factory == null) {
  +            factory = SAXParserFactory.newInstance();
  +            factory.setNamespaceAware(namespaceAware);
  +            factory.setValidating(validating);
  +        }
  +        return (factory);
  +
  +    }
  +
  +
  +    /**
  +     * Returns a flag indicating whether the requested feature is supported
  +     * by the underlying implementation of <code>org.xml.sax.XMLReader</code>.
  +     * See <a href="http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description"
  +     * http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description</a>
  +     * for information about the standard SAX2 feature flags.
  +     *
  +     * @param feature Name of the feature to inquire about
  +     *
  +     * @exception ParserConfigurationException if a parser configuration error
  +     *  occurs
  +     * @exception SAXNotRecognizedException if the property name is
  +     *  not recognized
  +     * @exception SAXNotSupportedException if the property name is
  +     *  recognized but not supported
  +     */
  +    public boolean getFeature(String feature)
  +        throws ParserConfigurationException, SAXNotRecognizedException,
  +        SAXNotSupportedException {
  +
  +        return (getFactory().getFeature(feature));
  +
  +    }
  +
  +
  +    /**
  +     * Sets a flag indicating whether the requested feature is supported
  +     * by the underlying implementation of <code>org.xml.sax.XMLReader</code>.
  +     * See <a href="http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description"
  +     * http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description</a>
  +     * for information about the standard SAX2 feature flags.  In order to be
  +     * effective, this method must be called <strong>before</strong> the
  +     * <code>getParser()</code> method is called for the first time, either
  +     * directly or indirectly.
  +     *
  +     * @param feature Name of the feature to set the status for
  +     * @param value The new value for this feature
  +     *
  +     * @exception ParserConfigurationException if a parser configuration error
  +     *  occurs
  +     * @exception SAXNotRecognizedException if the property name is
  +     *  not recognized
  +     * @exception SAXNotSupportedException if the property name is
  +     *  recognized but not supported
  +     */
  +    public void setFeature(String feature, boolean value)
  +        throws ParserConfigurationException, SAXNotRecognizedException,
  +        SAXNotSupportedException {
  +
  +        getFactory().setFeature(feature, value);
  +
  +    }
  +
  +
  +    /**
        * Return the current Logger associated with this instance of the Digester
        */
       public Log getLogger() {
  @@ -520,6 +590,7 @@
   
       }
   
  +
       /**
        * Return the current rule match path
        */
  @@ -529,10 +600,12 @@
   
       }
   
  +
       /**
        * Return the "namespace aware" flag for parsers we create.
        */
       public boolean getNamespaceAware() {
  +
           return (this.namespaceAware);
   
       }
  @@ -544,7 +617,9 @@
        * @param namespaceAware The new "namespace aware" flag
        */
       public void setNamespaceAware(boolean namespaceAware) {
  +
           this.namespaceAware = namespaceAware;
  +
       }
   
   
  @@ -590,31 +665,83 @@
        * is a problem creating the parser, return <code>null</code>.
        */
       public SAXParser getParser() {
  +
           // Return the parser we already created (if any)
  -        if (parser != null){
  -            setJAXPProperties();
  +        if (parser != null) {
               return (parser);
           }
   
  -        // Create and return a new parser
  -        synchronized (this) {
  -            try {
  -                if (factory == null) {
  -                    factory = SAXParserFactory.newInstance();
  -                }
  -                factory.setNamespaceAware(namespaceAware);
  -                factory.setValidating(validating);
  -                    
  -                parser = factory.newSAXParser();         
  -                setJAXPProperties();
  -                return parser;                                                
  -            } catch (Exception e) {
  -                log.error("Digester.getParser: ", e);
  -                return (null);
  +        // Create a new parser
  +        try {
  +            parser = getFactory().newSAXParser();         
  +        } catch (Exception e) {
  +            log.error("Digester.getParser: ", e);
  +            return (null);
  +        }
  +
  +        // Configure standard properties and return the new instance
  +        try {
  +            setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
  +        } catch (Exception e) {
  +            log.warn("" + e);
  +        }
  +        try {
  +            if (schemaLocation != null) {
  +                setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
               }
  +        } catch (Exception e) {
  +            log.warn("" + e);
           }
  +        return (parser);
   
       }
  +
  +
  +    /**
  +     * Return the current value of the specified property for the underlying
  +     * <code>XMLReader</code> implementation.
  +     * See <a href="http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description"
  +     * http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description</a>
  +     * for information about the standard SAX2 properties.
  +     *
  +     * @param property Property name to be retrieved
  +     *
  +     * @exception SAXNotRecognizedException if the property name is
  +     *  not recognized
  +     * @exception SAXNotSupportedException if the property name is
  +     *  recognized but not supported
  +     */
  +    public Object getProperty(String property)
  +        throws SAXNotRecognizedException, SAXNotSupportedException {
  +
  +        return (getParser().getProperty(property));
  +
  +    }
  +
  +
  +    /**
  +     * Set the current value of the specified property for the underlying
  +     * <code>XMLReader</code> implementation.
  +     * See <a href="http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description"
  +     * http://www.saxproject.org/apidoc/xml/sax/package-summary.html#package-description</a>
  +     * for information about the standard SAX2 properties.
  +     *
  +     * @param property Property name to be set
  +     * @param value Property value to be set
  +     *
  +     * @exception SAXNotRecognizedException if the property name is
  +     *  not recognized
  +     * @exception SAXNotSupportedException if the property name is
  +     *  recognized but not supported
  +     */
  +    public void setProperty(String property, Object value)
  +        throws SAXNotRecognizedException, SAXNotSupportedException {
  +
  +        getParser().setProperty(property, value);
  +
  +    }
  +
  +
       /**
        * By setting the reader in the constructor, you can bypass JAXP and
        * be able to use digester in Weblogic 6.0.  
  @@ -623,6 +750,7 @@
        *  SAXException if the reader cannot be instantiated
        */
       public XMLReader getReader() {
  +
           try {
               return (getXMLReader());
           } catch (SAXException e) {
  @@ -634,27 +762,6 @@
   
   
       /**
  -     * Return the XMLReader to be used for parsing the input document.
  -     *
  -     * FIX ME: there is a bug in JAXP/XERCES that prevent the use of a 
  -     * parser that contains a schema with a DTD.
  -     * @exception SAXException if no XMLReader can be instantiated
  -     */
  -    public synchronized XMLReader getXMLReader() throws SAXException {
  -        if (reader == null){
  -            reader = getParser().getXMLReader();
  -        }        
  -                               
  -        reader.setDTDHandler(this);           
  -        reader.setContentHandler(this);        
  -        
  -        reader.setEntityResolver(this);
  -        reader.setErrorHandler(this);
  -        return reader;
  -    }
  -
  -
  -    /**
        * Return the <code>Rules</code> implementation object containing our
        * rules collection and associated matching policy.  If none has been
        * established, a default implementation will be created and returned.
  @@ -671,24 +778,6 @@
   
       
       /**
  -     * Set the JAXP 1.2 XML Schema support.
  -     */
  -    private void setJAXPProperties(){
  -        try{
  -            parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
  -            if (schemaLocation != null){
  -                parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
  -            }         
  -        } catch (SAXNotRecognizedException e){
  -            log.warn("Error: JAXP SAXParser property not recognized: "
  -                + JAXP_SCHEMA_LANGUAGE);          
  -        } catch (SAXNotSupportedException e){
  -            log.warn("Error: JAXP SAXParser property not recognized: "
  -                + JAXP_SCHEMA_LANGUAGE);   
  -        }
  -    }    
  -    
  -    /**
        * Set the <code>Rules</code> implementation object containing our
        * rules collection and associated matching policy.
        *
  @@ -703,52 +792,58 @@
   
   
       /**
  -     * Return the validating parser flag.
  +     * Return the XML Schema URI used for validating an XML instance.
        */
  -    public boolean getValidating() {
  +    public String getSchema() {
   
  -        return (this.validating);
  +        return (this.schemaLocation);
   
       }
   
   
       /**
  -     * Set the validating parser flag.  This must be called before
  -     * <code>parse()</code> is called the first time.
  +     * Set the XML Schema URI used for validating a XML Instance.
        *
  -     * @param validating The new validating parser flag.
  +     * @param schemaLocation a URI to the schema.
        */
  -    public void setValidating(boolean validating) {
  +    public void setSchema(String schemaLocation){
   
  -        this.validating = validating;
  -
  -    }
  +        schemaLocation = schemaLocation;
   
  +    }   
  +    
   
       /**
  -     * Return the boolean as to whether the context classloader should be used.
  +     * Return the XML Schema language used when parsing.
        */
  -    public boolean getUseContextClassLoader() {
  +    public String getSchemaLanguage() {
   
  -        return useContextClassLoader;
  +        return (this.schemaLanguage);
   
       }
   
  +
       /**
  -     * Set the XML Schema URI used for validating a XML Instance.
  -     * @param schemaURI a URI to the schema.
  +     * Set the XML Schema language used when parsing. By default, we use W3C.
  +     *
  +     * @param schemaLanguage a URI to the schema language.
        */
  -    public void setSchema(String schemaURI){
  -        schemaLocation = schemaURI;    
  +    public void setSchemaLanguage(String schemaLanguage){
  +
  +        schemaLanguage = schemaLanguage;
  +
       }   
  -    
  +
  +
       /**
  -     * Set the XML Schema language used when parsing. By default, we use W3C.
  -     * @param schemaURI a URI to the schema.
  +     * Return the boolean as to whether the context classloader should be used.
        */
  -    public void setSchemaLanguage(String schemaLanguageURI){
  -        schemaLanguage = schemaLanguageURI;    
  -    }   
  +    public boolean getUseContextClassLoader() {
  +
  +        return useContextClassLoader;
  +
  +    }
  +
   
       /**
        * Determine whether to use the Context ClassLoader (the one found by
  @@ -763,6 +858,50 @@
   
           useContextClassLoader = use;
   
  +    }
  +
  +
  +    /**
  +     * Return the validating parser flag.
  +     */
  +    public boolean getValidating() {
  +
  +        return (this.validating);
  +
  +    }
  +
  +
  +    /**
  +     * Set the validating parser flag.  This must be called before
  +     * <code>parse()</code> is called the first time.
  +     *
  +     * @param validating The new validating parser flag.
  +     */
  +    public void setValidating(boolean validating) {
  +
  +        this.validating = validating;
  +
  +    }
  +
  +
  +    /**
  +     * Return the XMLReader to be used for parsing the input document.
  +     *
  +     * FIX ME: there is a bug in JAXP/XERCES that prevent the use of a 
  +     * parser that contains a schema with a DTD.
  +     * @exception SAXException if no XMLReader can be instantiated
  +     */
  +    public XMLReader getXMLReader() throws SAXException {
  +        if (reader == null){
  +            reader = getParser().getXMLReader();
  +        }        
  +                               
  +        reader.setDTDHandler(this);           
  +        reader.setContentHandler(this);        
  +        
  +        reader.setEntityResolver(this);
  +        reader.setErrorHandler(this);
  +        return reader;
       }
   
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>