Source for org.xml.sax.helpers.ParserFactory

   1: // SAX parser factory.
   2: // http://www.saxproject.org
   3: // No warranty; no copyright -- use this as you will.
   4: // $Id: ParserFactory.java,v 1.1 2004/12/23 22:38:42 mark Exp $
   5: 
   6: package org.xml.sax.helpers;
   7: 
   8: import java.lang.ClassNotFoundException;
   9: import java.lang.IllegalAccessException;
  10: import java.lang.InstantiationException;
  11: import java.lang.SecurityException;
  12: import java.lang.ClassCastException;
  13: 
  14: import org.xml.sax.Parser;
  15: 
  16: 
  17: /**
  18:  * Java-specific class for dynamically loading SAX parsers.
  19:  *
  20:  * <blockquote>
  21:  * <em>This module, both source code and documentation, is in the
  22:  * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  23:  * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
  24:  * for further information.
  25:  * </blockquote>
  26:  *
  27:  * <p><strong>Note:</strong> This class is designed to work with the now-deprecated
  28:  * SAX1 {@link org.xml.sax.Parser Parser} class.  SAX2 applications should use
  29:  * {@link org.xml.sax.helpers.XMLReaderFactory XMLReaderFactory} instead.</p>
  30:  *
  31:  * <p>ParserFactory is not part of the platform-independent definition
  32:  * of SAX; it is an additional convenience class designed
  33:  * specifically for Java XML application writers.  SAX applications
  34:  * can use the static methods in this class to allocate a SAX parser
  35:  * dynamically at run-time based either on the value of the
  36:  * `org.xml.sax.parser' system property or on a string containing the class
  37:  * name.</p>
  38:  *
  39:  * <p>Note that the application still requires an XML parser that
  40:  * implements SAX1.</p>
  41:  *
  42:  * @deprecated This class works with the deprecated
  43:  *             {@link org.xml.sax.Parser Parser}
  44:  *             interface.
  45:  * @since SAX 1.0
  46:  * @author David Megginson
  47:  * @version 2.0.1 (sax2r2)
  48:  */
  49: public class ParserFactory {
  50:     
  51:     
  52:     /**
  53:      * Private null constructor.
  54:      */
  55:     private ParserFactory ()
  56:     {
  57:     }
  58:     
  59:     
  60:     /**
  61:      * Create a new SAX parser using the `org.xml.sax.parser' system property.
  62:      *
  63:      * <p>The named class must exist and must implement the
  64:      * {@link org.xml.sax.Parser Parser} interface.</p>
  65:      *
  66:      * @exception java.lang.NullPointerException There is no value
  67:      *            for the `org.xml.sax.parser' system property.
  68:      * @exception java.lang.ClassNotFoundException The SAX parser
  69:      *            class was not found (check your CLASSPATH).
  70:      * @exception IllegalAccessException The SAX parser class was
  71:      *            found, but you do not have permission to load
  72:      *            it.
  73:      * @exception InstantiationException The SAX parser class was
  74:      *            found but could not be instantiated.
  75:      * @exception java.lang.ClassCastException The SAX parser class
  76:      *            was found and instantiated, but does not implement
  77:      *            org.xml.sax.Parser.
  78:      * @see #makeParser(java.lang.String)
  79:      * @see org.xml.sax.Parser
  80:      */
  81:     public static Parser makeParser ()
  82:     throws ClassNotFoundException,
  83:     IllegalAccessException, 
  84:     InstantiationException,
  85:     NullPointerException,
  86:     ClassCastException
  87:     {
  88:     String className = System.getProperty("org.xml.sax.parser");
  89:     if (className == null) {
  90:         throw new NullPointerException("No value for sax.parser property");
  91:     } else {
  92:         return makeParser(className);
  93:     }
  94:     }
  95:     
  96:     
  97:     /**
  98:      * Create a new SAX parser object using the class name provided.
  99:      *
 100:      * <p>The named class must exist and must implement the
 101:      * {@link org.xml.sax.Parser Parser} interface.</p>
 102:      *
 103:      * @param className A string containing the name of the
 104:      *                  SAX parser class.
 105:      * @exception java.lang.ClassNotFoundException The SAX parser
 106:      *            class was not found (check your CLASSPATH).
 107:      * @exception IllegalAccessException The SAX parser class was
 108:      *            found, but you do not have permission to load
 109:      *            it.
 110:      * @exception InstantiationException The SAX parser class was
 111:      *            found but could not be instantiated.
 112:      * @exception java.lang.ClassCastException The SAX parser class
 113:      *            was found and instantiated, but does not implement
 114:      *            org.xml.sax.Parser.
 115:      * @see #makeParser()
 116:      * @see org.xml.sax.Parser
 117:      */
 118:     public static Parser makeParser (String className)
 119:     throws ClassNotFoundException,
 120:     IllegalAccessException, 
 121:     InstantiationException,
 122:     ClassCastException
 123:     {
 124:     return (Parser) NewInstance.newInstance (
 125:         NewInstance.getClassLoader (), className);
 126:     }
 127:     
 128: }