jaxp/src/share/classes/org/xml/sax/helpers/XMLReaderFactory.java
author kvn
Fri, 20 Jun 2008 11:10:05 -0700
changeset 762 1b26adb5fea1
parent 6 7f561c08de6b
permissions -rw-r--r--
6715633: when matching a memory node the adr_type should not change Summary: verify the adr_type of a mach node was not changed Reviewed-by: rasbold, never
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
// XMLReaderFactory.java - factory for creating a new reader.
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
// http://www.saxproject.org
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
// Written by David Megginson
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
// and by David Brownell
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
// NO WARRANTY!  This class is in the Public Domain.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
// $Id: XMLReaderFactory.java,v 1.2.2.1 2005/07/31 22:48:08 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
package org.xml.sax.helpers;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import java.io.BufferedReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import java.io.InputStreamReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import org.xml.sax.SAXException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * Factory for creating an XML reader.
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * <blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * <em>This module, both source code and documentation, is in the
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * for further information.
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * </blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * <p>This class contains static methods for creating an XML reader
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * from an explicit class name, or based on runtime defaults:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 *   XMLReader myReader = XMLReaderFactory.createXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * } catch (SAXException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 *   System.err.println(e.getMessage());
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 * }
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
 * </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
 * <p><strong>Note to Distributions bundled with parsers:</strong>
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
 * You should modify the implementation of the no-arguments
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
 * <em>createXMLReader</em> to handle cases where the external
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
 * configuration mechanisms aren't set up.  That method should do its
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
 * best to return a parser when one is in the class path, even when
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * nothing bound its class name to <code>org.xml.sax.driver</code> so
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * those configuration mechanisms would see it.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 * @since SAX 2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * @author David Megginson, David Brownell
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
final public class XMLReaderFactory
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     * Private constructor.
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
     * <p>This constructor prevents the class from being instantiated.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    private XMLReaderFactory ()
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
    private static final String property = "org.xml.sax.driver";
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     * Attempt to create an XMLReader from system defaults.
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
     * In environments which can support it, the name of the XMLReader
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
     * class is determined by trying each these options in order, and
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * using the first one which succeeds:</p> <ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     * <li>If the system property <code>org.xml.sax.driver</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
     * has a value, that is used as an XMLReader class name. </li>
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     * <li>The JAR "Services API" is used to look for a class name
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     * in the <em>META-INF/services/org.xml.sax.driver</em> file in
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
     * jarfiles available to the runtime.</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * <li> SAX parser distributions are strongly encouraged to provide
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * a default XMLReader class name that will take effect only when
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     * previous options (on this list) are not successful.</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
     * <li>Finally, if {@link ParserFactory#makeParser()} can
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
     * return a system default SAX1 parser, that parser is wrapped in
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     * a {@link ParserAdapter}.  (This is a migration aid for SAX1
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
     * environments, where the <code>org.xml.sax.parser</code> system
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
     * property will often be usable.) </li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
     * </ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     * <p> In environments such as small embedded systems, which can not
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
     * support that flexibility, other mechanisms to determine the default
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
     * may be used. </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
     * <p>Note that many Java environments allow system properties to be
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     * initialized on a command line.  This means that <em>in most cases</em>
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
     * setting a good value for that property ensures that calls to this
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
     * method will succeed, except when security policies intervene.
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
     * This will also maximize application portability to older SAX
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * environments, with less robust implementations of this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     * @return A new XMLReader.
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     * @exception org.xml.sax.SAXException If no default XMLReader class
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     *            can be identified and instantiated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     * @see #createXMLReader(java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    public static XMLReader createXMLReader ()
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
        String          className = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        ClassLoader     loader = NewInstance.getClassLoader ();
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        // 1. try the JVM-instance-wide system property
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        try { className = System.getProperty (property); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        catch (RuntimeException e) { /* normally fails for applets */ }
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        // 2. if that fails, try META-INF/services/
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        if (className == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
                String          service = "META-INF/services/" + property;
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
                InputStream     in;
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
                BufferedReader  reader;
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
                if (loader == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
                    in = ClassLoader.getSystemResourceAsStream (service);
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
                else
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
                    in = loader.getResourceAsStream (service);
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
                if (in != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
                    reader = new BufferedReader (
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
                            new InputStreamReader (in, "UTF8"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
                    className = reader.readLine ();
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
                    in.close ();
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
            } catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        // 3. Distro-specific fallback
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
        if (className == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
// BEGIN DISTRIBUTION-SPECIFIC
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
            // EXAMPLE:
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
            // className = "com.example.sax.XmlReader";
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
            // or a $JAVA_HOME/jre/lib/*properties setting...
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
            className = "com.sun.org.apache.xerces.internal.parsers.SAXParser";
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
// END DISTRIBUTION-SPECIFIC
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
        // do we know the XMLReader implementation class yet?
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
        if (className != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
            return loadClass (loader, className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
        // 4. panic -- adapt any SAX1 parser
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
            return new ParserAdapter (ParserFactory.makeParser ());
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        } catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
            throw new SAXException ("Can't create default XMLReader; "
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
                    + "is system property org.xml.sax.driver set?");
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
     * Attempt to create an XML reader from a class name.
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
     * <p>Given a class name, this method attempts to load
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
     * and instantiate the class as an XML reader.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
     * <p>Note that this method will not be usable in environments where
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
     * the caller (perhaps an applet) is not permitted to load classes
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     * dynamically.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
     * @return A new XML reader.
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
     * @exception org.xml.sax.SAXException If the class cannot be
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
     *            loaded, instantiated, and cast to XMLReader.
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     * @see #createXMLReader()
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    public static XMLReader createXMLReader (String className)
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
        throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
        return loadClass (NewInstance.getClassLoader (), className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    private static XMLReader loadClass (ClassLoader loader, String className)
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    throws SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
            return (XMLReader) NewInstance.newInstance (loader, className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        } catch (ClassNotFoundException e1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
            throw new SAXException("SAX2 driver class " + className +
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                                   " not found", e1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        } catch (IllegalAccessException e2) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
            throw new SAXException("SAX2 driver class " + className +
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
                                   " found but cannot be loaded", e2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        } catch (InstantiationException e3) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
            throw new SAXException("SAX2 driver class " + className +
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
           " loaded but cannot be instantiated (no empty public constructor?)",
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
                                   e3);
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
        } catch (ClassCastException e4) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
            throw new SAXException("SAX2 driver class " + className +
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
                                   " does not implement XMLReader", e4);
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
}