Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/helpers/XMLReaderFactory.java
48576 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425// XMLReaderFactory.java - factory for creating a new reader.26// http://www.saxproject.org27// Written by David Megginson28// and by David Brownell29// NO WARRANTY! This class is in the Public Domain.30// $Id: XMLReaderFactory.java,v 1.2.2.1 2005/07/31 22:48:08 jeffsuttor Exp $3132package org.xml.sax.helpers;33import java.io.BufferedReader;34import java.io.InputStream;35import java.io.InputStreamReader;36import org.xml.sax.XMLReader;37import org.xml.sax.SAXException;383940/**41* Factory for creating an XML reader.42*43* <blockquote>44* <em>This module, both source code and documentation, is in the45* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>46* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>47* for further information.48* </blockquote>49*50* <p>This class contains static methods for creating an XML reader51* from an explicit class name, or based on runtime defaults:</p>52*53* <pre>54* try {55* XMLReader myReader = XMLReaderFactory.createXMLReader();56* } catch (SAXException e) {57* System.err.println(e.getMessage());58* }59* </pre>60*61* <p><strong>Note to Distributions bundled with parsers:</strong>62* You should modify the implementation of the no-arguments63* <em>createXMLReader</em> to handle cases where the external64* configuration mechanisms aren't set up. That method should do its65* best to return a parser when one is in the class path, even when66* nothing bound its class name to <code>org.xml.sax.driver</code> so67* those configuration mechanisms would see it.</p>68*69* @since SAX 2.070* @author David Megginson, David Brownell71* @version 2.0.1 (sax2r2)72*/73final public class XMLReaderFactory74{75/**76* Private constructor.77*78* <p>This constructor prevents the class from being instantiated.</p>79*/80private XMLReaderFactory ()81{82}8384private static final String property = "org.xml.sax.driver";85private static SecuritySupport ss = new SecuritySupport();8687private static String _clsFromJar = null;88private static boolean _jarread = false;89/**90* Attempt to create an XMLReader from system defaults.91* In environments which can support it, the name of the XMLReader92* class is determined by trying each these options in order, and93* using the first one which succeeds:</p> <ul>94*95* <li>If the system property <code>org.xml.sax.driver</code>96* has a value, that is used as an XMLReader class name. </li>97*98* <li>The JAR "Services API" is used to look for a class name99* in the <em>META-INF/services/org.xml.sax.driver</em> file in100* jarfiles available to the runtime.</li>101*102* <li> SAX parser distributions are strongly encouraged to provide103* a default XMLReader class name that will take effect only when104* previous options (on this list) are not successful.</li>105*106* <li>Finally, if {@link ParserFactory#makeParser()} can107* return a system default SAX1 parser, that parser is wrapped in108* a {@link ParserAdapter}. (This is a migration aid for SAX1109* environments, where the <code>org.xml.sax.parser</code> system110* property will often be usable.) </li>111*112* </ul>113*114* <p> In environments such as small embedded systems, which can not115* support that flexibility, other mechanisms to determine the default116* may be used. </p>117*118* <p>Note that many Java environments allow system properties to be119* initialized on a command line. This means that <em>in most cases</em>120* setting a good value for that property ensures that calls to this121* method will succeed, except when security policies intervene.122* This will also maximize application portability to older SAX123* environments, with less robust implementations of this method.124* </p>125*126* @return A new XMLReader.127* @exception org.xml.sax.SAXException If no default XMLReader class128* can be identified and instantiated.129* @see #createXMLReader(java.lang.String)130*/131public static XMLReader createXMLReader ()132throws SAXException133{134String className = null;135ClassLoader cl = ss.getContextClassLoader();136137// 1. try the JVM-instance-wide system property138try {139className = ss.getSystemProperty(property);140}141catch (RuntimeException e) { /* continue searching */ }142143// 2. if that fails, try META-INF/services/144if (className == null) {145if (!_jarread) {146_jarread = true;147String service = "META-INF/services/" + property;148InputStream in;149BufferedReader reader;150151try {152if (cl != null) {153in = ss.getResourceAsStream(cl, service);154155// If no provider found then try the current ClassLoader156if (in == null) {157cl = null;158in = ss.getResourceAsStream(cl, service);159}160} else {161// No Context ClassLoader, try the current ClassLoader162in = ss.getResourceAsStream(cl, service);163}164165if (in != null) {166reader = new BufferedReader (new InputStreamReader (in, "UTF8"));167_clsFromJar = reader.readLine ();168in.close ();169}170} catch (Exception e) {171}172}173className = _clsFromJar;174}175176// 3. Distro-specific fallback177if (className == null) {178// BEGIN DISTRIBUTION-SPECIFIC179180// EXAMPLE:181// className = "com.example.sax.XmlReader";182// or a $JAVA_HOME/jre/lib/*properties setting...183className = "com.sun.org.apache.xerces.internal.parsers.SAXParser";184185// END DISTRIBUTION-SPECIFIC186}187188// do we know the XMLReader implementation class yet?189if (className != null)190return loadClass (cl, className);191192// 4. panic -- adapt any SAX1 parser193try {194return new ParserAdapter (ParserFactory.makeParser ());195} catch (Exception e) {196throw new SAXException ("Can't create default XMLReader; "197+ "is system property org.xml.sax.driver set?");198}199}200201202/**203* Attempt to create an XML reader from a class name.204*205* <p>Given a class name, this method attempts to load206* and instantiate the class as an XML reader.</p>207*208* <p>Note that this method will not be usable in environments where209* the caller (perhaps an applet) is not permitted to load classes210* dynamically.</p>211*212* @return A new XML reader.213* @exception org.xml.sax.SAXException If the class cannot be214* loaded, instantiated, and cast to XMLReader.215* @see #createXMLReader()216*/217public static XMLReader createXMLReader (String className)218throws SAXException219{220return loadClass (ss.getContextClassLoader(), className);221}222223private static XMLReader loadClass (ClassLoader loader, String className)224throws SAXException225{226try {227return (XMLReader) NewInstance.newInstance (loader, className);228} catch (ClassNotFoundException e1) {229throw new SAXException("SAX2 driver class " + className +230" not found", e1);231} catch (IllegalAccessException e2) {232throw new SAXException("SAX2 driver class " + className +233" found but cannot be loaded", e2);234} catch (InstantiationException e3) {235throw new SAXException("SAX2 driver class " + className +236" loaded but cannot be instantiated (no empty public constructor?)",237e3);238} catch (ClassCastException e4) {239throw new SAXException("SAX2 driver class " + className +240" does not implement XMLReader", e4);241}242}243}244245246