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