ParserFactory.java revision 917:6390a20678d6
1/*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26// SAX parser factory.
27// http://www.saxproject.org
28// No warranty; no copyright -- use this as you will.
29// $Id: ParserFactory.java,v 1.2 2004/11/03 22:53:09 jsuttor Exp $
30
31package org.xml.sax.helpers;
32
33
34/**
35 * Java-specific class for dynamically loading SAX parsers.
36 *
37 * <blockquote>
38 * <em>This module, both source code and documentation, is in the
39 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
40 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
41 * for further information.
42 * </blockquote>
43 *
44 * <p><strong>Note:</strong> This class is designed to work with the now-deprecated
45 * SAX1 {@link org.xml.sax.Parser Parser} class.  SAX2 applications should use
46 * {@link org.xml.sax.helpers.XMLReaderFactory XMLReaderFactory} instead.</p>
47 *
48 * <p>ParserFactory is not part of the platform-independent definition
49 * of SAX; it is an additional convenience class designed
50 * specifically for Java XML application writers.  SAX applications
51 * can use the static methods in this class to allocate a SAX parser
52 * dynamically at run-time based either on the value of the
53 * `org.xml.sax.parser' system property or on a string containing the class
54 * name.</p>
55 *
56 * <p>Note that the application still requires an XML parser that
57 * implements SAX1.</p>
58 *
59 * @deprecated This class works with the deprecated
60 *             {@link org.xml.sax.Parser Parser}
61 *             interface.
62 * @since 1.4, SAX 1.0
63 * @author David Megginson
64 * @version 2.0.1 (sax2r2)
65 */
66@SuppressWarnings( "deprecation" )
67@Deprecated(since="5")
68public class ParserFactory {
69    private static SecuritySupport ss = new SecuritySupport();
70
71    /**
72     * Private null constructor.
73     */
74    private ParserFactory ()
75    {
76    }
77
78
79    /**
80     * Create a new SAX parser using the `org.xml.sax.parser' system property.
81     *
82     * <p>The named class must exist and must implement the
83     * {@link org.xml.sax.Parser Parser} interface.</p>
84     *
85     * @exception java.lang.NullPointerException There is no value
86     *            for the `org.xml.sax.parser' system property.
87     * @exception java.lang.ClassNotFoundException The SAX parser
88     *            class was not found (check your CLASSPATH).
89     * @exception IllegalAccessException The SAX parser class was
90     *            found, but you do not have permission to load
91     *            it.
92     * @exception InstantiationException The SAX parser class was
93     *            found but could not be instantiated.
94     * @exception java.lang.ClassCastException The SAX parser class
95     *            was found and instantiated, but does not implement
96     *            org.xml.sax.Parser.
97     * @see #makeParser(java.lang.String)
98     * @see org.xml.sax.Parser
99     */
100    public static org.xml.sax.Parser makeParser ()
101        throws ClassNotFoundException,
102        IllegalAccessException,
103        InstantiationException,
104        NullPointerException,
105        ClassCastException
106    {
107        String className = ss.getSystemProperty("org.xml.sax.parser");
108        if (className == null) {
109            throw new NullPointerException("No value for sax.parser property");
110        } else {
111            return makeParser(className);
112        }
113    }
114
115
116    /**
117     * Create a new SAX parser object using the class name provided.
118     *
119     * <p>The named class must exist and must implement the
120     * {@link org.xml.sax.Parser Parser} interface.</p>
121     *
122     * @param className A string containing the name of the
123     *                  SAX parser class.
124     * @exception java.lang.ClassNotFoundException The SAX parser
125     *            class was not found (check your CLASSPATH).
126     * @exception IllegalAccessException The SAX parser class was
127     *            found, but you do not have permission to load
128     *            it.
129     * @exception InstantiationException The SAX parser class was
130     *            found but could not be instantiated.
131     * @exception java.lang.ClassCastException The SAX parser class
132     *            was found and instantiated, but does not implement
133     *            org.xml.sax.Parser.
134     * @see #makeParser()
135     * @see org.xml.sax.Parser
136     */
137    public static org.xml.sax.Parser makeParser (String className)
138        throws ClassNotFoundException,
139        IllegalAccessException,
140        InstantiationException,
141        ClassCastException
142    {
143        return NewInstance.newInstance (org.xml.sax.Parser.class, ss.getClassLoader(), className);
144    }
145
146}
147