You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ne...@apache.org on 2001/11/28 16:26:48 UTC

cvs commit: xml-xerces/java/src/org/xml/sax/helpers NewInstance.java ParserFactory.java

neilg       01/11/28 07:26:48

  Modified:    java/src/org/xml/sax/helpers ParserFactory.java
  Added:       java/src/org/xml/sax/helpers NewInstance.java
  Log:
  make search for a SAXParser class by the ParserFactory more robust.  This is a clear bugfix to SAX level 2, and since the new class that is being added is package protected this should have no unexpected impact on users of the API.
  
  Revision  Changes    Path
  1.5       +8 -8      xml-xerces/java/src/org/xml/sax/helpers/ParserFactory.java
  
  Index: ParserFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/xml/sax/helpers/ParserFactory.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ParserFactory.java	2000/05/08 23:08:39	1.4
  +++ ParserFactory.java	2001/11/28 15:26:48	1.5
  @@ -1,6 +1,7 @@
   // SAX parser factory.
  +// http://www.saxproject.org
   // No warranty; no copyright -- use this as you will.
  -// $Id: ParserFactory.java,v 1.4 2000/05/05 17:50:13 david Exp $
  +// $Id: ParserFactory.java,v 1.6 2001/11/21 01:02:50 dbrownell Exp $
   
   package org.xml.sax.helpers;
   
  @@ -19,6 +20,8 @@
    * <blockquote>
    * <em>This module, both source code and documentation, is in the
    * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  + * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
  + * for further information.
    * </blockquote>
    *
    * <p><strong>Note:</strong> This class is designed to work with the now-deprecated
  @@ -40,11 +43,8 @@
    *             {@link org.xml.sax.Parser Parser}
    *             interface.
    * @since SAX 1.0
  - * @author David Megginson, 
  - *         <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  - * @version 2.0
  - * @see org.xml.sax.Parser
  - * @see java.lang.Class
  + * @author David Megginson
  + * @version 2.0r2pre3
    */
   public class ParserFactory {
       
  @@ -121,9 +121,9 @@
   	InstantiationException,
   	ClassCastException
       {
  -	return (Parser)(Class.forName(className).newInstance());
  +	return (Parser) NewInstance.newInstance (
  +		NewInstance.getClassLoader (), className);
       }
       
   }
   
  -// end of ParserFactory.java
  
  
  
  1.1                  xml-xerces/java/src/org/xml/sax/helpers/NewInstance.java
  
  Index: NewInstance.java
  ===================================================================
  // NewInstance.java - create a new instance of a class by name.
  // http://www.saxproject.org
  // Written by Edwin Goei, edwingo@apache.org
  // and by David Brownell, dbrownell@users.sourceforge.net
  // NO WARRANTY!  This class is in the Public Domain.
  // $Id: NewInstance.java,v 1.1 2001/11/28 15:26:48 neilg Exp $
  
  package org.xml.sax.helpers;
  
  import java.lang.reflect.Method;
  import java.lang.reflect.InvocationTargetException;
  
  /**
   * Create a new instance of a class by name.
   *
   * <blockquote>
   * <em>This module, both source code and documentation, is in the
   * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
   * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
   * for further information.
   * </blockquote>
   *
   * <p>This class contains a static method for creating an instance of a
   * class from an explicit class name.  It tries to use the thread's context
   * ClassLoader if possible and falls back to using
   * Class.forName(String).</p>
   *
   * <p>This code is designed to compile and run on JDK version 1.1 and later
   * including versions of Java 2.</p>
   *
   * @author Edwin Goei, David Brownell
   * @version 2.0r2pre3
   */
  class NewInstance {
  
      /**
       * Creates a new instance of the specified class name
       *
       * Package private so this code is not exposed at the API level.
       */
      static Object newInstance (ClassLoader classLoader, String className)
          throws ClassNotFoundException, IllegalAccessException,
              InstantiationException
      {
          Class driverClass;
          if (classLoader == null) {
              driverClass = Class.forName(className);
          } else {
              driverClass = classLoader.loadClass(className);
          }
          return driverClass.newInstance();
      }
  
      /**
       * Figure out which ClassLoader to use.  For JDK 1.2 and later use
       * the context ClassLoader.
       */           
      static ClassLoader getClassLoader ()
      {
          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
              return NewInstance.class.getClassLoader();
          }
  
          try {
              return (ClassLoader) m.invoke(Thread.currentThread(), null);
          } catch (IllegalAccessException e) {
              // assert(false)
              throw new UnknownError(e.getMessage());
          } catch (InvocationTargetException e) {
              // assert(e.getTargetException() instanceof SecurityException)
              throw new UnknownError(e.getMessage());
          }
      }
  }
  
  
  

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