You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by an...@apache.org on 2002/01/12 03:58:00 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/util ObjectFactory.java

andyc       02/01/11 18:58:00

  Modified:    java     build.xml
               java/docs releases.xml
               java/src/org/apache/xerces/parsers DOMParser.java
                        SAXParser.java XMLDocumentParser.java
  Added:       java/src/org/apache/xerces/parsers
                        org.apache.xerces.xni.parser.XMLParserConfiguration
               java/src/org/apache/xerces/util ObjectFactory.java
  Log:
  Added ability to override default parser configuration.
  
  Revision  Changes    Path
  1.70      +4 -1      xml-xerces/java/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/build.xml,v
  retrieving revision 1.69
  retrieving revision 1.70
  diff -u -r1.69 -r1.70
  --- build.xml	2 Jan 2002 22:28:16 -0000	1.69
  +++ build.xml	12 Jan 2002 02:57:59 -0000	1.70
  @@ -12,7 +12,7 @@
   Copyright:
     Copyright (c) 2000 The Apache Software Foundation.
   
  -   $Id: build.xml,v 1.69 2002/01/02 22:28:16 neilg Exp $
  +   $Id: build.xml,v 1.70 2002/01/12 02:57:59 andyc Exp $
   
   ==================================================================== -->
   
  @@ -139,6 +139,9 @@
   
       <copy file="${src.dir}/org/apache/xerces/parsers/org.xml.sax.driver"
         tofile="${build.dest}/META-INF/services/org.xml.sax.driver"/>
  +
  +    <copy file="${src.dir}/org/apache/xerces/parsers/org.apache.xerces.xni.parser.XMLParserConfiguration"
  +      tofile="${build.dest}/META-INF/services/org.apache.xerces.xni.parser.XMLParserConfiguration"/>
   
       <!-- copy src files -->
       <copy todir="${build.src}">
  
  
  
  1.97      +7 -1      xml-xerces/java/docs/releases.xml
  
  Index: releases.xml
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/docs/releases.xml,v
  retrieving revision 1.96
  retrieving revision 1.97
  diff -u -r1.96 -r1.97
  --- releases.xml	4 Jan 2002 23:27:49 -0000	1.96
  +++ releases.xml	12 Jan 2002 02:57:59 -0000	1.97
  @@ -1,11 +1,17 @@
   <?xml version='1.0' encoding='UTF-8'?>
  -<!-- $Id: releases.xml,v 1.96 2002/01/04 23:27:49 neilg Exp $ -->
  +<!-- $Id: releases.xml,v 1.97 2002/01/12 02:57:59 andyc Exp $ -->
   <!DOCTYPE releases SYSTEM 'dtd/releases.dtd'>
   <releases>
    <release version='NOT YET RELEASED'>
     <desc>
     </desc>
     <changes>
  +   <add>
  +    <note>
  +     Added ability to override default parser configuration.
  +    </note>
  +    <submitter name='Andy Clark'/>
  +   </add>
      <add>
       <note>Implemented support for UCS-4 and UCS-2 encodings.</note>
       <submitter name='Neil Graham'/>
  
  
  
  1.56      +8 -4      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.55
  retrieving revision 1.56
  diff -u -r1.55 -r1.56
  --- DOMParser.java	13 Dec 2001 22:59:20 -0000	1.55
  +++ DOMParser.java	12 Jan 2002 02:57:59 -0000	1.56
  @@ -2,7 +2,7 @@
    * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 2000,2001 The Apache Software Foundation.  All rights 
  + * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights 
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -64,6 +64,7 @@
   import org.apache.xerces.impl.validation.GrammarPool;
   import org.apache.xerces.util.EntityResolverWrapper;
   import org.apache.xerces.util.ErrorHandlerWrapper;
  +import org.apache.xerces.util.ObjectFactory;
   import org.apache.xerces.util.SymbolTable;
   import org.apache.xerces.xni.XNIException;
   import org.apache.xerces.xni.parser.XMLParseException;
  @@ -90,7 +91,7 @@
    * @author Arnaud  Le Hors, IBM
    * @author Andy Clark, IBM
    *
  - * @version $Id: DOMParser.java,v 1.55 2001/12/13 22:59:20 elena Exp $ 
  + * @version $Id: DOMParser.java,v 1.56 2002/01/12 02:57:59 andyc Exp $ 
    */
   public class DOMParser
       extends AbstractDOMParser {
  @@ -103,8 +104,11 @@
        * Constructs a DOM parser using the dtd/xml schema parser configuration.
        */
       public DOMParser() {
  -        super(new StandardParserConfiguration());
  -    } // <init>
  +        super((XMLParserConfiguration)ObjectFactory.createObject(
  +            "org.apache.xerces.xni.parser.XMLParserConfiguration",
  +            "org.apache.xerces.parsers.StandardParserConfiguration"
  +            ));
  +    } // <init>()
   
       /**
        * Constructs a DOM parser using the specified parser configuration.
  
  
  
  1.27      +7 -3      xml-xerces/java/src/org/apache/xerces/parsers/SAXParser.java
  
  Index: SAXParser.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/SAXParser.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- SAXParser.java	13 Dec 2001 22:59:20 -0000	1.26
  +++ SAXParser.java	12 Jan 2002 02:57:59 -0000	1.27
  @@ -2,7 +2,7 @@
    * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 2000,2001 The Apache Software Foundation.  All rights 
  + * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights 
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,6 +58,7 @@
   package org.apache.xerces.parsers;
   
   import org.apache.xerces.impl.validation.GrammarPool;
  +import org.apache.xerces.util.ObjectFactory;
   import org.apache.xerces.util.SymbolTable;
   import org.apache.xerces.xni.parser.XMLParserConfiguration;
   
  @@ -69,7 +70,7 @@
    * @author Arnaud  Le Hors, IBM
    * @author Andy Clark, IBM
    *
  - * @version $Id: SAXParser.java,v 1.26 2001/12/13 22:59:20 elena Exp $ 
  + * @version $Id: SAXParser.java,v 1.27 2002/01/12 02:57:59 andyc Exp $ 
    */
   public class SAXParser
       extends AbstractSAXParser {
  @@ -82,7 +83,10 @@
        * Constructs a SAX parser using the dtd/xml schema parser configuration.
        */
       public SAXParser() {
  -        super(new StandardParserConfiguration());
  +        super((XMLParserConfiguration)ObjectFactory.createObject(
  +            "org.apache.xerces.xni.parser.XMLParserConfiguration",
  +            "org.apache.xerces.parsers.StandardParserConfiguration"
  +            ));
       } // <init>()
   
       /**
  
  
  
  1.3       +9 -4      xml-xerces/java/src/org/apache/xerces/parsers/XMLDocumentParser.java
  
  Index: XMLDocumentParser.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/XMLDocumentParser.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XMLDocumentParser.java	23 Aug 2001 00:35:31 -0000	1.2
  +++ XMLDocumentParser.java	12 Jan 2002 02:57:59 -0000	1.3
  @@ -2,7 +2,7 @@
    * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights 
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights 
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -58,6 +58,7 @@
   package org.apache.xerces.parsers;
   
   import org.apache.xerces.impl.validation.GrammarPool;
  +import org.apache.xerces.util.ObjectFactory;
   import org.apache.xerces.util.SymbolTable;
   import org.apache.xerces.xni.parser.XMLParserConfiguration;
   
  @@ -70,7 +71,8 @@
    * @author Arnaud  Le Hors, IBM
    * @author Andy Clark, IBM
    *
  - * @version $Id: XMLDocumentParser.java,v 1.2 2001/08/23 00:35:31 lehors Exp $ */
  + * @version $Id: XMLDocumentParser.java,v 1.3 2002/01/12 02:57:59 andyc Exp $ 
  + */
   public class XMLDocumentParser
       extends AbstractXMLDocumentParser {
   
  @@ -83,8 +85,11 @@
        * configuration.
        */
       public XMLDocumentParser() {
  -        super(new StandardParserConfiguration());
  -    } // <init>
  +        super((XMLParserConfiguration)ObjectFactory.createObject(
  +            "org.apache.xerces.xni.parser.XMLParserConfiguration",
  +            "org.apache.xerces.parsers.StandardParserConfiguration"
  +            ));
  +    } // <init>()
   
       /**
        * Constructs a document parser using the specified parser configuration.
  
  
  
  1.1                  xml-xerces/java/src/org/apache/xerces/parsers/org.apache.xerces.xni.parser.XMLParserConfiguration
  
  Index: org.apache.xerces.xni.parser.XMLParserConfiguration
  ===================================================================
  org.apache.xerces.parsers.StandardParserConfiguration
  
  
  
  1.1                  xml-xerces/java/src/org/apache/xerces/util/ObjectFactory.java
  
  Index: ObjectFactory.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001-2002 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 acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "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 apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * 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 and was
   * originally based on software copyright (c) 1999-2001, Sun Microsystems,
   * Inc., http://www.sun.com.  For more information on the Apache Software
   * Foundation, please see <http://www.apache.org/>.
   */
  
  package org.apache.xerces.util;
  
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.File;
  import java.io.FileInputStream;
  
  import java.util.Properties;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  
  import java.lang.reflect.Method;
  import java.lang.reflect.InvocationTargetException;
  
  /**
   * This class is duplicated for each JAXP subpackage so keep it in sync.
   * It is package private and therefore is not exposed as part of the JAXP
   * API.
   * <p>
   * This code is designed to implement the JAXP 1.1 spec pluggability
   * feature and is designed to both compile and run on JDK version 1.1 and
   * later.  The code also runs both as part of an unbundled jar file and
   * when bundled as part of the JDK.
   * <p>
   * This class was moved from the <code>javax.xml.parsers.FactoryFinder</code>
   * class and modified to be used as a general utility for creating objects 
   * dynamically.
   *
   * @version $Id: ObjectFactory.java,v 1.1 2002/01/12 02:58:00 andyc Exp $
   */
  public class ObjectFactory {
  
      //
      // Constants
      //
  
      /** Set to true for debugging */
      private static final boolean DEBUG = false;
  
      //
      // Public static methods
      //
  
      /**
       * Finds the implementation Class object in the specified order.  The
       * specified order is the following:
       * <ol>
       *  <li>query the system property using <code>System.getProperty</code>
       *  <li>read <code>META-INF/services/<i>factoryId</i></code> file
       *  <li>use fallback classname
       * </ol>
       *
       * @return Class object of factory, never null
       *
       * @param factoryId             Name of the factory to find, same as
       *                              a property name
       * @param fallbackClassName     Implementation class name, if nothing else
       *                              is found.  Use null to mean no fallback.
       *
       * @exception ObjectFactory.ConfigurationError
       */
      public static Object createObject(String factoryId, String fallbackClassName)
          throws ConfigurationError {
          return createObject(factoryId, null, fallbackClassName);
      } // createObject(String,String):Object
  
      /**
       * Finds the implementation Class object in the specified order.  The
       * specified order is the following:
       * <ol>
       *  <li>query the system property using <code>System.getProperty</code>
       *  <li>read <code>$java.home/lib/<i>propertiesFilename</i></code> file
       *  <li>read <code>META-INF/services/<i>factoryId</i></code> file
       *  <li>use fallback classname
       * </ol>
       *
       * @return Class object of factory, never null
       *
       * @param factoryId             Name of the factory to find, same as
       *                              a property name
       * @param propertiesFilename The filename in the $java.home/lib directory
       *                           of the properties file.
       * @param fallbackClassName     Implementation class name, if nothing else
       *                              is found.  Use null to mean no fallback.
       *
       * @exception ObjectFactory.ConfigurationError
       */
      public static Object createObject(String factoryId, 
                                        String propertiesFilename,
                                        String fallbackClassName)
          throws ConfigurationError
      {
          debugPrintln("debug is on");
  
          ClassLoader classLoader = findClassLoader();
  
          // Use the system property first
          try {
              String systemProp =
                  System.getProperty( factoryId );
              if( systemProp!=null) {
                  debugPrintln("found system property " + systemProp);
                  return newInstance(systemProp, classLoader);
              }
          } catch (SecurityException se) {
          }
  
          // try to read from $java.home/lib/xml.properties
          if (propertiesFilename != null) {
              try {
                  String javah=System.getProperty( "java.home" );
                  String configFile = javah + File.separator +
                      "lib" + File.separator + propertiesFilename;
                  File f=new File( configFile );
                  if( f.exists()) {
                      Properties props=new Properties();
                      props.load( new FileInputStream(f));
                      String factoryClassName = props.getProperty(factoryId);
                      debugPrintln("found java.home property " + factoryClassName);
                      return newInstance(factoryClassName, classLoader);
                  }
              } catch(Exception ex ) {
                  if( DEBUG ) ex.printStackTrace();
              }
          }
  
          // try to find services in CLASSPATH
          String serviceId = "META-INF/services/" + factoryId;
          try {
              InputStream is=null;
              if (classLoader == null) {
                  is=ClassLoader.getSystemResourceAsStream( serviceId );
              } else {
                  is=classLoader.getResourceAsStream( serviceId );
              }
          
              if( is!=null ) {
                  debugPrintln("found " + serviceId);
  
                  // Read the service provider name in UTF-8 as specified in
                  // the jar spec.  Unfortunately this fails in Microsoft
                  // VJ++, which does not implement the UTF-8
                  // encoding. Theoretically, we should simply let it fail in
                  // that case, since the JVM is obviously broken if it
                  // doesn't support such a basic standard.  But since there
                  // are still some users attempting to use VJ++ for
                  // development, we have dropped in a fallback which makes a
                  // second attempt using the platform's default encoding. In
                  // VJ++ this is apparently ASCII, which is a subset of
                  // UTF-8... and since the strings we'll be reading here are
                  // also primarily limited to the 7-bit ASCII range (at
                  // least, in English versions), this should work well
                  // enough to keep us on the air until we're ready to
                  // officially decommit from VJ++. [Edited comment from
                  // jkesselm]
                  BufferedReader rd;
                  try {
                      rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                  } catch (java.io.UnsupportedEncodingException e) {
                      rd = new BufferedReader(new InputStreamReader(is));
                  }
          
                  String factoryClassName = rd.readLine();
                  rd.close();
  
                  if (factoryClassName != null &&
                      ! "".equals(factoryClassName)) {
                      debugPrintln("loaded from services: " + factoryClassName);
                      return newInstance(factoryClassName, classLoader);
                  }
              }
          } catch( Exception ex ) {
              if( DEBUG ) ex.printStackTrace();
          }
  
          if (fallbackClassName == null) {
              throw new ConfigurationError(
                  "Provider for " + factoryId + " cannot be found", null);
          }
  
          debugPrintln("loaded from fallback value: " + fallbackClassName);
          return newInstance(fallbackClassName, classLoader);
  
      } // createObject(String,String,String):Object
  
      //
      // Private static methods
      //
  
      /** Prints a message to standard error if debugging is enabled. */
      private static void debugPrintln(String msg) {
          if (DEBUG) {
              System.err.println("JAXP: " + msg);
          }
      } // debugPrintln(String)
  
      /**
       * Figure out which ClassLoader to use.  For JDK 1.2 and later use
       * the context ClassLoader.
       */           
      private static ClassLoader findClassLoader()
          throws ConfigurationError
      {
          Method m = null;
  
          try {
              m = Thread.class.getMethod("getContextClassLoader", null);
          } catch (NoSuchMethodException e) {
              // Assume that we are running JDK 1.1, use the current ClassLoader
              debugPrintln("assuming JDK 1.1");
              return ObjectFactory.class.getClassLoader();
          }
  
          try {
              return (ClassLoader) m.invoke(Thread.currentThread(), null);
          } catch (IllegalAccessException e) {
              // assert(false)
              throw new ConfigurationError("Unexpected IllegalAccessException",
                                           e);
          } catch (InvocationTargetException e) {
              // assert(e.getTargetException() instanceof SecurityException)
              throw new ConfigurationError("Unexpected InvocationTargetException",
                                           e);
          }
      } // findClassLoader():ClassLoader
  
      /**
       * Create an instance of a class using the specified ClassLoader
       */
      private static Object newInstance(String className,
                                        ClassLoader classLoader)
          throws ConfigurationError
      {
          try {
              Class spiClass;
              if (classLoader == null) {
                  spiClass = Class.forName(className);
              } else {
                  spiClass = classLoader.loadClass(className);
              }
              return spiClass.newInstance();
          } catch (ClassNotFoundException x) {
              throw new ConfigurationError(
                  "Provider " + className + " not found", x);
          } catch (Exception x) {
              throw new ConfigurationError(
                  "Provider " + className + " could not be instantiated: " + x,
                  x);
          }
      } // newInstance(String,ClassLoader):Object
  
      //
      // Classes
      //
  
      /**
       * A configuration error.
       */
      public static class ConfigurationError 
          extends Error {
  
          //
          // Data
          //
  
          /** Exception. */
          private Exception exception;
  
          //
          // Constructors
          //
  
          /**
           * Construct a new instance with the specified detail string and
           * exception.
           */
          public ConfigurationError(String msg, Exception x) {
              super(msg);
              this.exception = x;
          } // <init>(String,Exception)
  
          //
          // Public methods
          //
  
          /** Returns the exception associated to this error. */
          public Exception getException() {
              return exception;
          } // getException():Exception
  
      } // class ConfigurationError
  
  } // class ObjectFactory
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org