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