001// SAX parser factory.
002// http://www.saxproject.org
003// No warranty; no copyright -- use this as you will.
004// $Id: ParserFactory.java,v 1.1 2004/12/23 22:38:42 mark Exp $
005
006package org.xml.sax.helpers;
007
008import java.lang.ClassNotFoundException;
009import java.lang.IllegalAccessException;
010import java.lang.InstantiationException;
011import java.lang.SecurityException;
012import java.lang.ClassCastException;
013
014import org.xml.sax.Parser;
015
016
017/**
018 * Java-specific class for dynamically loading SAX parsers.
019 *
020 * <blockquote>
021 * <em>This module, both source code and documentation, is in the
022 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
023 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
024 * for further information.
025 * </blockquote>
026 *
027 * <p><strong>Note:</strong> This class is designed to work with the now-deprecated
028 * SAX1 {@link org.xml.sax.Parser Parser} class.  SAX2 applications should use
029 * {@link org.xml.sax.helpers.XMLReaderFactory XMLReaderFactory} instead.</p>
030 *
031 * <p>ParserFactory is not part of the platform-independent definition
032 * of SAX; it is an additional convenience class designed
033 * specifically for Java XML application writers.  SAX applications
034 * can use the static methods in this class to allocate a SAX parser
035 * dynamically at run-time based either on the value of the
036 * `org.xml.sax.parser' system property or on a string containing the class
037 * name.</p>
038 *
039 * <p>Note that the application still requires an XML parser that
040 * implements SAX1.</p>
041 *
042 * @deprecated This class works with the deprecated
043 *             {@link org.xml.sax.Parser Parser}
044 *             interface.
045 * @since SAX 1.0
046 * @author David Megginson
047 * @version 2.0.1 (sax2r2)
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) NewInstance.newInstance (
125                NewInstance.getClassLoader (), className);
126    }
127
128}