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.1 2004/12/23 22:38:42 mark Exp $
007
008 package org.xml.sax.helpers;
009 import java.io.BufferedReader;
010 import java.io.InputStream;
011 import java.io.InputStreamReader;
012 import org.xml.sax.XMLReader;
013 import 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 */
049 final 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 // CLASSPATH LOCAL: have to code in the backup.
141 // Among other things, see PR 31303, and this thread:
142 // http://gcc.gnu.org/ml/java-patches/2007-q1/msg00661.html
143 className = "gnu.xml.stream.SAXParser";
144
145 // EXAMPLE:
146 // className = "com.example.sax.XmlReader";
147 // or a $JAVA_HOME/jre/lib/*properties setting...
148
149 // END DISTRIBUTION-SPECIFIC
150 }
151
152 // do we know the XMLReader implementation class yet?
153 if (className != null)
154 return loadClass (loader, className);
155
156 // 4. panic -- adapt any SAX1 parser
157 try {
158 return new ParserAdapter (ParserFactory.makeParser ());
159 } catch (Exception e) {
160 throw new SAXException ("Can't create default XMLReader; "
161 + "is system property org.xml.sax.driver set?");
162 }
163 }
164
165
166 /**
167 * Attempt to create an XML reader from a class name.
168 *
169 * <p>Given a class name, this method attempts to load
170 * and instantiate the class as an XML reader.</p>
171 *
172 * <p>Note that this method will not be usable in environments where
173 * the caller (perhaps an applet) is not permitted to load classes
174 * dynamically.</p>
175 *
176 * @return A new XML reader.
177 * @exception org.xml.sax.SAXException If the class cannot be
178 * loaded, instantiated, and cast to XMLReader.
179 * @see #createXMLReader()
180 */
181 public static XMLReader createXMLReader (String className)
182 throws SAXException
183 {
184 return loadClass (NewInstance.getClassLoader (), className);
185 }
186
187 private static XMLReader loadClass (ClassLoader loader, String className)
188 throws SAXException
189 {
190 try {
191 return (XMLReader) NewInstance.newInstance (loader, className);
192 } catch (ClassNotFoundException e1) {
193 throw new SAXException("SAX2 driver class " + className +
194 " not found", e1);
195 } catch (IllegalAccessException e2) {
196 throw new SAXException("SAX2 driver class " + className +
197 " found but cannot be loaded", e2);
198 } catch (InstantiationException e3) {
199 throw new SAXException("SAX2 driver class " + className +
200 " loaded but cannot be instantiated (no empty public constructor?)",
201 e3);
202 } catch (ClassCastException e4) {
203 throw new SAXException("SAX2 driver class " + className +
204 " does not implement XMLReader", e4);
205 }
206 }
207 }