Source Home >> Java Source 1.6.0 >> org.xml.sax.helpers.XMLReaderFactory V 0.09
  • 001// XMLReaderFactory.java - factory for creating a new reader.
  • 002// http://www.saxproject.org
  • 003// Written by David Megginson
  • 004// and by David Brownell
  • 005// NO WARRANTY! This class is in the Public Domain.
  • 006// $Id: XMLReaderFactory.java,v 1.2.2.1 2005/07/31 22:48:08 jeffsuttor Exp $
  • 007
  • 008package org.xml.sax.helpers;
  • 009import java.io.BufferedReader;
  • 010import java.io.InputStream;
  • 011import java.io.InputStreamReader;
  • 012import org.xml.sax.XMLReader;
  • 013import org.xml.sax.SAXException;
  • 014
  • 015
  • 016/**
  • 017 * Factory for creating an XML reader.
  • 018 *
  • 019 * <blockquote>
  • 020 * <em>This module, both source code and documentation, is in the
  • 021 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  • 022 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
  • 023 * for further information.
  • 024 * </blockquote>
  • 025 *
  • 026 * <p>This class contains static methods for creating an XML reader
  • 027 * from an explicit class name, or based on runtime defaults:</p>
  • 028 *
  • 029 * <pre>
  • 030 * try {
  • 031 * XMLReader myReader = XMLReaderFactory.createXMLReader();
  • 032 * } catch (SAXException e) {
  • 033 * System.err.println(e.getMessage());
  • 034 * }
  • 035 * </pre>
  • 036 *
  • 037 * <p><strong>Note to Distributions bundled with parsers:</strong>
  • 038 * You should modify the implementation of the no-arguments
  • 039 * <em>createXMLReader</em> to handle cases where the external
  • 040 * configuration mechanisms aren't set up. That method should do its
  • 041 * best to return a parser when one is in the class path, even when
  • 042 * nothing bound its class name to <code>org.xml.sax.driver</code> so
  • 043 * those configuration mechanisms would see it.</p>
  • 044 *
  • 045 * @since SAX 2.0
  • 046 * @author David Megginson, David Brownell
  • 047 * @version 2.0.1 (sax2r2)
  • 048 */
  • 049final public class XMLReaderFactory
  • 050{
  • 051 /**
  • 052 * Private constructor.
  • 053 *
  • 054 * <p>This constructor prevents the class from being instantiated.</p>
  • 055 */
  • 056 private XMLReaderFactory ()
  • 057 {
  • 058 }
  • 059
  • 060 private static final String property = "org.xml.sax.driver";
  • 061
  • 062 /**
  • 063 * Attempt to create an XMLReader from system defaults.
  • 064 * In environments which can support it, the name of the XMLReader
  • 065 * class is determined by trying each these options in order, and
  • 066 * using the first one which succeeds:</p> <ul>
  • 067 *
  • 068 * <li>If the system property <code>org.xml.sax.driver</code>
  • 069 * has a value, that is used as an XMLReader class name. </li>
  • 070 *
  • 071 * <li>The JAR "Services API" is used to look for a class name
  • 072 * in the <em>META-INF/services/org.xml.sax.driver</em> file in
  • 073 * jarfiles available to the runtime.</li>
  • 074 *
  • 075 * <li> SAX parser distributions are strongly encouraged to provide
  • 076 * a default XMLReader class name that will take effect only when
  • 077 * previous options (on this list) are not successful.</li>
  • 078 *
  • 079 * <li>Finally, if {@link ParserFactory#makeParser()} can
  • 080 * return a system default SAX1 parser, that parser is wrapped in
  • 081 * a {@link ParserAdapter}. (This is a migration aid for SAX1
  • 082 * environments, where the <code>org.xml.sax.parser</code> system
  • 083 * property will often be usable.) </li>
  • 084 *
  • 085 * </ul>
  • 086 *
  • 087 * <p> In environments such as small embedded systems, which can not
  • 088 * support that flexibility, other mechanisms to determine the default
  • 089 * may be used. </p>
  • 090 *
  • 091 * <p>Note that many Java environments allow system properties to be
  • 092 * initialized on a command line. This means that <em>in most cases</em>
  • 093 * setting a good value for that property ensures that calls to this
  • 094 * method will succeed, except when security policies intervene.
  • 095 * This will also maximize application portability to older SAX
  • 096 * environments, with less robust implementations of this method.
  • 097 * </p>
  • 098 *
  • 099 * @return A new XMLReader.
  • 100 * @exception org.xml.sax.SAXException If no default XMLReader class
  • 101 * can be identified and instantiated.
  • 102 * @see #createXMLReader(java.lang.String)
  • 103 */
  • 104 public static XMLReader createXMLReader ()
  • 105 throws SAXException
  • 106 {
  • 107 String className = null;
  • 108 ClassLoader loader = NewInstance.getClassLoader ();
  • 109
  • 110 // 1. try the JVM-instance-wide system property
  • 111 try { className = System.getProperty (property); }
  • 112 catch (RuntimeException e) { /* normally fails for applets */}
  • 113
  • 114 // 2. if that fails, try META-INF/services/
  • 115 if (className == null) {
  • 116 try {
  • 117 String service = "META-INF/services/" + property;
  • 118 InputStream in;
  • 119 BufferedReader reader;
  • 120
  • 121 if (loader == null)
  • 122 in = ClassLoader.getSystemResourceAsStream (service);
  • 123 else
  • 124 in = loader.getResourceAsStream (service);
  • 125
  • 126 if (in != null) {
  • 127 reader = new BufferedReader (
  • 128 new InputStreamReader (in, "UTF8"));
  • 129 className = reader.readLine ();
  • 130 in.close ();
  • 131 }
  • 132 } catch (Exception e) {
  • 133 }
  • 134 }
  • 135
  • 136 // 3. Distro-specific fallback
  • 137 if (className == null) {
  • 138// BEGIN DISTRIBUTION-SPECIFIC
  • 139
  • 140 // EXAMPLE:
  • 141 // className = "com.example.sax.XmlReader";
  • 142 // or a $JAVA_HOME/jre/lib/*properties setting...
  • 143 className = "com.sun.org.apache.xerces.internal.parsers.SAXParser";
  • 144
  • 145// END DISTRIBUTION-SPECIFIC
  • 146 }
  • 147
  • 148 // do we know the XMLReader implementation class yet?
  • 149 if (className != null)
  • 150 return loadClass (loader, className);
  • 151
  • 152 // 4. panic -- adapt any SAX1 parser
  • 153 try {
  • 154 return new ParserAdapter (ParserFactory.makeParser ());
  • 155 } catch (Exception e) {
  • 156 throw new SAXException ("Can't create default XMLReader; "
  • 157 + "is system property org.xml.sax.driver set?");
  • 158 }
  • 159 }
  • 160
  • 161
  • 162 /**
  • 163 * Attempt to create an XML reader from a class name.
  • 164 *
  • 165 * <p>Given a class name, this method attempts to load
  • 166 * and instantiate the class as an XML reader.</p>
  • 167 *
  • 168 * <p>Note that this method will not be usable in environments where
  • 169 * the caller (perhaps an applet) is not permitted to load classes
  • 170 * dynamically.</p>
  • 171 *
  • 172 * @return A new XML reader.
  • 173 * @exception org.xml.sax.SAXException If the class cannot be
  • 174 * loaded, instantiated, and cast to XMLReader.
  • 175 * @see #createXMLReader()
  • 176 */
  • 177 public static XMLReader createXMLReader (String className)
  • 178 throws SAXException
  • 179 {
  • 180 return loadClass (NewInstance.getClassLoader (), className);
  • 181 }
  • 182
  • 183 private static XMLReader loadClass (ClassLoader loader, String className)
  • 184 throws SAXException
  • 185 {
  • 186 try {
  • 187 return (XMLReader) NewInstance.newInstance (loader, className);
  • 188 } catch (ClassNotFoundException e1) {
  • 189 throw new SAXException("SAX2 driver class " + className +
  • 190 " not found", e1);
  • 191 } catch (IllegalAccessException e2) {
  • 192 throw new SAXException("SAX2 driver class " + className +
  • 193 " found but cannot be loaded", e2);
  • 194 } catch (InstantiationException e3) {
  • 195 throw new SAXException("SAX2 driver class " + className +
  • 196 " loaded but cannot be instantiated (no empty public constructor?)",
  • 197 e3);
  • 198 } catch (ClassCastException e4) {
  • 199 throw new SAXException("SAX2 driver class " + className +
  • 200 " does not implement XMLReader", e4);
  • 201 }
  • 202 }
  • 203}

文件:XMLReaderFactory.java
包名:org.xml.sax.helpers
类名:XMLReaderFactory
继承:
接口: