jfarcand    2003/10/27 12:49:00

  Modified:    catalina/src/share/org/apache/catalina/startup
                        TldConfig.java ContextConfig.java
  Added:       catalina/src/share/org/apache/catalina/startup
                        DigesterFactory.java
  Log:
  Merge digester's initialization logic into 1 class and add schema supports for 
parser other that Xerces ( someday I will find one ).
  
  Please review.
  
  Revision  Changes    Path
  1.33      +4 -107    
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/TldConfig.java
  
  Index: TldConfig.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/TldConfig.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- TldConfig.java    6 Oct 2003 23:51:46 -0000       1.32
  +++ TldConfig.java    27 Oct 2003 20:48:59 -0000      1.33
  @@ -86,7 +86,6 @@
   import javax.naming.NamingException;
   import javax.naming.directory.DirContext;
   import javax.servlet.ServletException;
  -import javax.xml.parsers.ParserConfigurationException;
   
   import org.apache.catalina.Context;
   import org.apache.catalina.Globals;
  @@ -95,8 +94,6 @@
   import org.apache.catalina.util.StringManager;
   import org.apache.commons.digester.Digester;
   import org.xml.sax.InputSource;
  -import org.xml.sax.SAXNotRecognizedException;
  -import org.xml.sax.SAXNotSupportedException;
   
   /**
    * Startup event listener for a <b>Context</b> that configures the properties
  @@ -452,112 +449,12 @@
        */
       private static Digester createTldDigester() {
   
  -        URL url = null;
  -        Digester tldDigester = new Digester();
  -        tldDigester.setNamespaceAware(xmlNamespaceAware);
  -        tldDigester.setValidating(xmlValidation);
  -        
  -        if (tldDigester.getFactory().getClass()
  -                    .getName().indexOf("xerces")!=-1) {
  -            tldDigester = patchXerces(tldDigester);
  -        }
  -        // Set the schemaLocation
  -        SchemaResolver tldEntityResolver = new SchemaResolver( tldDigester);
  -
  -        if (xmlValidation) {
  -            if (tldDigester.getFactory().getClass()
  -                            .getName().indexOf("xerces")!=-1) {
  -                try{
  -                    tldDigester.setFeature(
  -                        "http://apache.org/xml/features/validation/dynamic";,
  -                        true);
  -                    tldDigester.setFeature(
  -                        "http://apache.org/xml/features/validation/schema";,
  -                        true);
  -                } catch(ParserConfigurationException e){
  -                        // log("contextConfig.registerLocalSchema", e);
  -                } catch(SAXNotRecognizedException e){
  -                        // log("contextConfig.registerLocalSchema", e);
  -                } catch(SAXNotSupportedException e){
  -                        // log("contextConfig.registerLocalSchema", e);
  -                }
  +        return DigesterFactory.newDigester(xmlValidation, 
  +                                           xmlNamespaceAware, 
  +                                           new TldRuleSet());
   
  -            }
  -        }
  -        
  -        url = TldConfig.class.getResource(Constants.TldDtdResourcePath_11);
  -        tldEntityResolver.register(Constants.TldDtdPublicId_11,
  -                                   url.toString());
  -        
  -        url = TldConfig.class.getResource(Constants.TldDtdResourcePath_12);
  -        tldEntityResolver.register(Constants.TldDtdPublicId_12,
  -                                   url.toString());
  -        
  -        tldEntityResolver = registerLocalSchema(tldEntityResolver);
  -        
  -        tldDigester.setEntityResolver(tldEntityResolver);
  -        tldDigester.addRuleSet(new TldRuleSet());
  -        return (tldDigester);
  -
  -    }
  -
  -    
  -    private static Digester patchXerces(Digester digester){
  -        // This feature is needed for backward compatibility with old DDs
  -        // which used Java encoding names such as ISO8859_1 etc.
  -        // with Crimson (bug 4701993). By default, Xerces does not
  -        // support ISO8859_1.
  -        try{
  -            digester.setFeature(
  -                "http://apache.org/xml/features/allow-java-encodings";, true);
  -        } catch(ParserConfigurationException e){
  -                // log("contextConfig.registerLocalSchema", e);
  -        } catch(SAXNotRecognizedException e){
  -                // log("contextConfig.registerLocalSchema", e);
  -        } catch(SAXNotSupportedException e){
  -                // log("contextConfig.registerLocalSchema", e);
  -        }
  -        return digester;
  -    }
  -
  -    /**
  -     * Utilities used to force the parser to use local schema, when available,
  -     * instead of the <code>schemaLocation</code> XML element.
  -     * @param entityResolver The instance on which properties are set.
  -     * @return an instance ready to parse XML schema.
  -     */
  -    protected static SchemaResolver registerLocalSchema(SchemaResolver 
entityResolver){
  -
  -        URL url = TldConfig.class.getResource(Constants.J2eeSchemaResourcePath_14);
  -        entityResolver.register(Constants.J2eeSchemaPublicId_14,
  -                                url.toString());
  -
  -        url = TldConfig.class.getResource(Constants.W3cSchemaResourcePath_10);
  -        entityResolver.register(Constants.W3cSchemaPublicId_10,
  -                                url.toString());
  -
  -        url = TldConfig.class.getResource(Constants.JspSchemaResourcePath_20);
  -        entityResolver.register(Constants.JspSchemaPublicId_20,
  -                                url.toString());
  -
  -        url = TldConfig.class.getResource(Constants.TldSchemaResourcePath_20);
  -        entityResolver.register(Constants.TldSchemaPublicId_20,
  -                                url.toString());
  -        
  -        url = TldConfig.class.getResource(Constants.WebSchemaResourcePath_24);
  -        entityResolver.register(Constants.WebSchemaPublicId_24,
  -                                url.toString());
  -        
  -        url = 
TldConfig.class.getResource(Constants.J2eeWebServiceSchemaResourcePath_11);
  -        entityResolver.register(Constants.J2eeWebServiceSchemaPublicId_11,
  -                                url.toString());
  -
  -        url = 
TldConfig.class.getResource(Constants.J2eeWebServiceClientSchemaResourcePath_11);
  -        entityResolver.register(Constants.J2eeWebServiceClientSchemaPublicId_11,
  -                                url.toString());
  -        
  -        return entityResolver;
       }
  +
   
       /**
        * Scan the JAR file at the specified resource path for TLDs in the
  
  
  
  1.38      +7 -108    
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/ContextConfig.java
  
  Index: ContextConfig.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/ContextConfig.java,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- ContextConfig.java        6 Oct 2003 23:51:46 -0000       1.37
  +++ ContextConfig.java        27 Oct 2003 20:48:59 -0000      1.38
  @@ -70,7 +70,6 @@
   import java.util.Properties;
   
   import javax.servlet.ServletContext;
  -import javax.xml.parsers.ParserConfigurationException;
   
   import org.apache.catalina.Authenticator;
   import org.apache.catalina.Container;
  @@ -97,8 +96,6 @@
   import org.apache.catalina.util.StringManager;
   import org.apache.commons.digester.Digester;
   import org.xml.sax.InputSource;
  -import org.xml.sax.SAXNotRecognizedException;
  -import org.xml.sax.SAXNotSupportedException;
   import org.xml.sax.SAXParseException;
   
   /**
  @@ -451,88 +448,29 @@
   
       }
   
  -    private static Digester patchXerces(Digester digester){
  -        // This feature is needed for backward compatibility with old DDs
  -        // which used Java encoding names such as ISO8859_1 etc.
  -        // with Crimson (bug 4701993). By default, Xerces does not
  -        // support ISO8859_1.
  -        try{
  -            digester.setFeature(
  -                "http://apache.org/xml/features/allow-java-encodings";, true);
  -        } catch(ParserConfigurationException e){
  -                // log("contextConfig.registerLocalSchema", e);
  -        } catch(SAXNotRecognizedException e){
  -                // log("contextConfig.registerLocalSchema", e);
  -        } catch(SAXNotSupportedException e){
  -                // log("contextConfig.registerLocalSchema", e);
  -        }
  -        return digester;
  -    }
  -    
   
       /**
        * Create (if necessary) and return a Digester configured to process the
        * web application deployment descriptor (web.xml).
        */
       private static Digester createWebDigester() {
  -        Digester webDigester = 
  +        Digester webDigester =
               createWebXmlDigester(xmlNamespaceAware, xmlValidation);
  -        webDigester.addRuleSet(webRuleSet);
           return webDigester;
       }
   
   
  -    /**
  +    /*
        * Create (if necessary) and return a Digester configured to process the
        * web application deployment descriptor (web.xml).
        */
       public static Digester createWebXmlDigester(boolean namespaceAware,
                                                   boolean validation) {
  -        URL url = null;
  -        Digester webDigester = new Digester();
  -        webDigester.setNamespaceAware(namespaceAware);
  -        webDigester.setValidating(validation);
  -       
  -        if (webDigester.getFactory().getClass()
  -                .getName().indexOf("xerces")!=-1) {
  -            webDigester = patchXerces(webDigester);
  -        }
  -
  -        SchemaResolver webEntityResolver = new SchemaResolver( webDigester);
  -
  -        if (validation) {
  -            if (webDigester.getFactory().getClass()
  -                            .getName().indexOf("xerces")!=-1) {
  -                try{
  -                    webDigester.setFeature(
  -                        "http://apache.org/xml/features/validation/dynamic";,
  -                        true);
  -                    webDigester.setFeature(
  -                        "http://apache.org/xml/features/validation/schema";,
  -                        true);
  -                } catch(ParserConfigurationException e){
  -                        // log("contextConfig.registerLocalSchema", e);
  -                } catch(SAXNotRecognizedException e){
  -                        // log("contextConfig.registerLocalSchema", e);
  -                } catch(SAXNotSupportedException e){
  -                        // log("contextConfig.registerLocalSchema", e);
  -                }
  -
  -            }
  -        }
  -        
  -        url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_22);
  -        webEntityResolver.register(Constants.WebDtdPublicId_22,
  -                                   url.toString());
           
  -        url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_23);
  -        webEntityResolver.register(Constants.WebDtdPublicId_23,
  -                                   url.toString());
  -
  -        webEntityResolver = registerLocalSchema(webEntityResolver);
  -
  -        webDigester.setEntityResolver(webEntityResolver);
  -        return (webDigester);
  +        Digester webDigester =  DigesterFactory.newDigester(xmlValidation,
  +                                                            xmlNamespaceAware,
  +                                                            webRuleSet);
  +        return webDigester;
       }
   
       private String getBaseDir() {
  @@ -672,45 +610,6 @@
               log.error( message, throwable );
           }
   
  -    }
  -
  -    /**
  -     * Utilities used to force the parser to use local schema, when available,
  -     * instead of the <code>schemaLocation</code> XML element.
  -     * @param digester The instance on which properties are set.
  -     * @return an instance ready to parse XML schema.
  -     */
  -    protected static SchemaResolver registerLocalSchema(SchemaResolver 
entityResolver){
  -
  -        URL url = 
ContextConfig.class.getResource(Constants.J2eeSchemaResourcePath_14);
  -        entityResolver.register(Constants.J2eeSchemaPublicId_14,
  -                                url.toString());
  -
  -        url = ContextConfig.class.getResource(Constants.W3cSchemaResourcePath_10);
  -        entityResolver.register(Constants.W3cSchemaPublicId_10,
  -                                url.toString());
  -
  -        url = ContextConfig.class.getResource(Constants.JspSchemaResourcePath_20);
  -        entityResolver.register(Constants.JspSchemaPublicId_20,
  -                                url.toString());
  -
  -        url = ContextConfig.class.getResource(Constants.TldSchemaResourcePath_20);
  -        entityResolver.register(Constants.TldSchemaPublicId_20,
  -                                url.toString());
  -        
  -        url = ContextConfig.class.getResource(Constants.WebSchemaResourcePath_24);
  -        entityResolver.register(Constants.WebSchemaPublicId_24,
  -                                url.toString());
  -        
  -        url = 
ContextConfig.class.getResource(Constants.J2eeWebServiceSchemaResourcePath_11);
  -        entityResolver.register(Constants.J2eeWebServiceSchemaPublicId_11,
  -                                url.toString());
  -
  -        url = 
ContextConfig.class.getResource(Constants.J2eeWebServiceClientSchemaResourcePath_11);
  -        entityResolver.register(Constants.J2eeWebServiceClientSchemaPublicId_11,
  -                                url.toString());
  -        
  -        return entityResolver;
       }
   
   
  
  
  
  1.1                  
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/startup/DigesterFactory.java
  
  Index: DigesterFactory.java
  ===================================================================
  /*
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.catalina.startup;
  
  import java.net.URL;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.apache.catalina.util.SchemaResolver;
  import org.apache.catalina.util.StringManager;
  import org.apache.commons.digester.Digester;
  import org.apache.commons.digester.RuleSet;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXNotRecognizedException;
  import org.xml.sax.SAXNotSupportedException;
  
  /**
   * Wrapper class around the Digester that hide Digester's initialization details
   *
   * @author Jean-Francois Arcand
   */
  
  public class DigesterFactory{
  
      /**
       * The XML entiry resolver used by the Digester.
       */
      private static SchemaResolver schemaResolver;
  
  
      /**
       * Create a <code>Digester</code> parser with no <code>Rule</code>
       * associated and XML validation turned off.
       */
      public static Digester newDigester(){
          return newDigester(false, false, null);
      }
  
      
      /**
       * Create a <code>Digester</code> parser with XML validation turned off.
      * @param rule an instance of <code>Rule</code↔ used for parsing the xml.
       */
      public static Digester newDigester(RuleSet rule){
          return newDigester(false,false,rule);
      }
  
      
      /**
       * Create a <code>Digester</code> parser.
       * @param xmlValidation turn on/off xml validation
       * @param xmlNamespaceAware turn on/off namespace validation
       * @param rule an instance of <code>Rule</code↔ used for parsing the xml.
       */
      public static Digester newDigester(boolean xmlValidation,
                                         boolean xmlNamespaceAware,
                                         RuleSet rule) {
  
          URL url = null;
          Digester digester = new Digester();
          digester.setNamespaceAware(xmlNamespaceAware);
          digester.setValidating(xmlValidation);
          
          String parserName = 
                  digester.getFactory().getClass().getName();
          if (parserName.indexOf("xerces")!=-1) {
              digester = patchXerces(digester);
          }
  
          schemaResolver = new SchemaResolver(digester);
          if (xmlValidation) {
              // Xerces 2.3 and up has a special way to turn on validation
              // for both DTD and Schema
              if (parserName.indexOf("xerces")!=-1) {
                  turnOnXercesValidation(digester);
              } else {
                  turnOnValidation(digester);
              }
          }
          registerLocalSchema();
          
          digester.setEntityResolver(schemaResolver);
          if ( rule != null )
              digester.addRuleSet(rule);
  
          return (digester);
      }
  
  
      /**
       * Patch Xerces for backward compatibility.
       */
      private static Digester patchXerces(Digester digester){
          // This feature is needed for backward compatibility with old DDs
          // which used Java encoding names such as ISO8859_1 etc.
          // with Crimson (bug 4701993). By default, Xerces does not
          // support ISO8859_1.
          try{
              digester.setFeature(
                  "http://apache.org/xml/features/allow-java-encodings";, true);
          } catch(ParserConfigurationException e){
                  // log("contextConfig.registerLocalSchema", e);
          } catch(SAXNotRecognizedException e){
                  // log("contextConfig.registerLocalSchema", e);
          } catch(SAXNotSupportedException e){
                  // log("contextConfig.registerLocalSchema", e);
          }
          return digester;
      }
  
  
      /**
       * Utilities used to force the parser to use local schema, when available,
       * instead of the <code>schemaLocation</code> XML element.
       * @param The instance on which properties are set.
       * @return an instance ready to parse XML schema.
       */
      protected static void registerLocalSchema(){
          // J2EE
          register(Constants.J2eeSchemaResourcePath_14,
                   Constants.J2eeSchemaPublicId_14);
          // W3C
          register(Constants.W3cSchemaResourcePath_10,
                   Constants.W3cSchemaPublicId_10);
          // JSP
          register(Constants.JspSchemaResourcePath_20,
                   Constants.JspSchemaPublicId_20);
          // TLD
          register(Constants.TldDtdResourcePath_11,  
                   Constants.TldDtdPublicId_11);
          
          register(Constants.TldDtdResourcePath_12,
                   Constants.TldDtdPublicId_12);
  
          register(Constants.TldSchemaResourcePath_20,
                   Constants.TldSchemaPublicId_20);
  
          // web.xml    
          register(Constants.WebDtdResourcePath_22,
                   Constants.WebDtdPublicId_22);
  
          register(Constants.WebDtdResourcePath_23,
                   Constants.WebDtdPublicId_23);
  
          register(Constants.WebSchemaResourcePath_24,
                   Constants.WebSchemaPublicId_24);
  
          // Web Service
          register(Constants.J2eeWebServiceSchemaResourcePath_11,
                   Constants.J2eeWebServiceSchemaPublicId_11);
  
          register(Constants.J2eeWebServiceClientSchemaResourcePath_11,
                   Constants.J2eeWebServiceClientSchemaPublicId_11);
  
      }
  
  
      /**
       * Load the resource and add it to the 
       */
      protected static void register(String resourceURL, String resourcePublicId){
  
          URL url = DigesterFactory.class.getResource(resourceURL);
          schemaResolver.register(resourcePublicId , url.toString() );
  
      }
  
  
      /**
       * Turn on DTD and/or validation (based on the parser implementation)
       */
      protected static void turnOnValidation(Digester digester){
          URL url = DigesterFactory.class
                          .getResource(Constants.WebSchemaResourcePath_24);
          digester.setSchema(url.toString());     
      }
  
  
      /** 
       * Turn on schema AND DTD validation on Xerces parser.
       */
      protected static void turnOnXercesValidation(Digester digester){
          try{
              digester.setFeature(
                  "http://apache.org/xml/features/validation/dynamic";,
                  true);
              digester.setFeature(
                  "http://apache.org/xml/features/validation/schema";,
                  true);
          } catch(ParserConfigurationException e){
                  // log("contextConfig.registerLocalSchema", e);
          } catch(SAXNotRecognizedException e){
                  // log("contextConfig.registerLocalSchema", e);
          } catch(SAXNotSupportedException e){
                  // log("contextConfig.registerLocalSchema", e);
          }
      }
  }
  
  
  

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

Reply via email to