001// SAX parser factory.
002// No warranty; no copyright -- use this as you will.
003// $Id: ParserFactory.java,v 1.1 2001/03/05 21:40:06 jstrachan Exp $
004
005package org.xml.sax.helpers;
006
007import java.lang.ClassNotFoundException;
008import java.lang.IllegalAccessException;
009import java.lang.InstantiationException;
010import java.lang.SecurityException;
011import java.lang.ClassCastException;
012
013import org.xml.sax.Parser;
014
015
016/**
017 * Java-specific class for dynamically loading SAX parsers.
018 *
019 * <blockquote>
020 * <em>This module, both source code and documentation, is in the
021 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
022 * </blockquote>
023 *
024 * <p><strong>Note:</strong> This class is designed to work with the now-deprecated
025 * SAX1 {@link org.xml.sax.Parser Parser} class.  SAX2 applications should use
026 * {@link org.xml.sax.helpers.XMLReaderFactory XMLReaderFactory} instead.</p>
027 *
028 * <p>ParserFactory is not part of the platform-independent definition
029 * of SAX; it is an additional convenience class designed
030 * specifically for Java XML application writers.  SAX applications
031 * can use the static methods in this class to allocate a SAX parser
032 * dynamically at run-time based either on the value of the
033 * `org.xml.sax.parser' system property or on a string containing the class
034 * name.</p>
035 *
036 * <p>Note that the application still requires an XML parser that
037 * implements SAX1.</p>
038 *
039 * @deprecated This class works with the deprecated
040 *             {@link org.xml.sax.Parser Parser}
041 *             interface.
042 * @since SAX 1.0
043 * @author David Megginson, 
044 *         <a href="mailto:sax@megginson.com">sax@megginson.com</a>
045 * @version 2.0
046 * @see org.xml.sax.Parser
047 * @see java.lang.Class
048 */
049public class ParserFactory {
050    
051    
052    /**
053     * Private null constructor.
054     */
055    private ParserFactory ()
056    {
057    }
058    
059    
060    /**
061     * Create a new SAX parser using the `org.xml.sax.parser' system property.
062     *
063     * <p>The named class must exist and must implement the
064     * {@link org.xml.sax.Parser Parser} interface.</p>
065     *
066     * @exception java.lang.NullPointerException There is no value
067     *            for the `org.xml.sax.parser' system property.
068     * @exception java.lang.ClassNotFoundException The SAX parser
069     *            class was not found (check your CLASSPATH).
070     * @exception IllegalAccessException The SAX parser class was
071     *            found, but you do not have permission to load
072     *            it.
073     * @exception InstantiationException The SAX parser class was
074     *            found but could not be instantiated.
075     * @exception java.lang.ClassCastException The SAX parser class
076     *            was found and instantiated, but does not implement
077     *            org.xml.sax.Parser.
078     * @see #makeParser(java.lang.String)
079     * @see org.xml.sax.Parser
080     */
081    public static Parser makeParser ()
082        throws ClassNotFoundException,
083        IllegalAccessException, 
084        InstantiationException,
085        NullPointerException,
086        ClassCastException
087    {
088        String className = System.getProperty("org.xml.sax.parser");
089        if (className == null) {
090            throw new NullPointerException("No value for sax.parser property");
091        } else {
092            return makeParser(className);
093        }
094    }
095    
096    
097    /**
098     * Create a new SAX parser object using the class name provided.
099     *
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)(Class.forName(className).newInstance());
125    }
126    
127}
128
129// end of ParserFactory.java