src/javax/xml/validation/SchemaFactory.java

Print this page




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.validation;
  27 
  28 import java.io.File;
  29 import java.net.URL;
  30 
  31 import javax.xml.transform.Source;
  32 import javax.xml.transform.stream.StreamSource;
  33 
  34 import org.w3c.dom.ls.LSResourceResolver;
  35 import org.xml.sax.ErrorHandler;
  36 import org.xml.sax.SAXException;
  37 import org.xml.sax.SAXNotRecognizedException;
  38 import org.xml.sax.SAXNotSupportedException;

  39 
  40 /**
  41  * Factory that creates {@link Schema} objects. Entry-point to
  42  * the validation API.
  43  *
  44  * <p>
  45  * {@link SchemaFactory} is a schema compiler. It reads external
  46  * representations of schemas and prepares them for validation.
  47  *
  48  * <p>
  49  * The {@link SchemaFactory} class is not thread-safe. In other words,
  50  * it is the application's responsibility to ensure that at most
  51  * one thread is using a {@link SchemaFactory} object at any
  52  * given moment. Implementations are encouraged to mark methods
  53  * as <code>synchronized</code> to protect themselves from broken clients.
  54  *
  55  * <p>
  56  * {@link SchemaFactory} is not re-entrant. While one of the
  57  * <code>newSchema</code> methods is being invoked, applications
  58  * may not attempt to recursively invoke the <code>newSchema</code> method,


  62  * <p>
  63  * This spec uses a namespace URI to designate a schema language.
  64  * The following table shows the values defined by this specification.
  65  * <p>
  66  * To be compliant with the spec, the implementation
  67  * is only required to support W3C XML Schema 1.0. However,
  68  * if it chooses to support other schema languages listed here,
  69  * it must conform to the relevant behaviors described in this spec.
  70  *
  71  * <p>
  72  * Schema languages not listed here are expected to
  73  * introduce their own URIs to represent themselves.
  74  * The {@link SchemaFactory} class is capable of locating other
  75  * implementations for other schema languages at run-time.
  76  *
  77  * <p>
  78  * Note that because the XML DTD is strongly tied to the parsing process
  79  * and has a significant effect on the parsing process, it is impossible
  80  * to define the DTD validation as a process independent from parsing.
  81  * For this reason, this specification does not define the semantics for
  82  * the XML DTD. This doesn't prohibit implentors from implementing it
  83  * in a way they see fit, but <em>users are warned that any DTD
  84  * validation implemented on this interface necessarily deviate from
  85  * the XML DTD semantics as defined in the XML 1.0</em>.
  86  *
  87  * <table border="1" cellpadding="2">
  88  *   <thead>
  89  *     <tr>
  90  *       <th>value</th>
  91  *       <th>language</th>
  92  *     </tr>
  93  *   </thead>
  94  *   <tbody>
  95  *     <tr>
  96  *       <td>{@link javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI} ("<code>http://www.w3.org/2001/XMLSchema</code>")</td>
  97  *       <td><a href="http://www.w3.org/TR/xmlschema-1">W3C XML Schema 1.0</a></td>
  98  *     </tr>
  99  *     <tr>
 100  *       <td>{@link javax.xml.XMLConstants#RELAXNG_NS_URI} ("<code>http://relaxng.org/ns/structure/1.0</code>")</td>
 101  *       <td><a href="http://www.relaxng.org/">RELAX NG 1.0</a></td>
 102  *     </tr>


 130      *
 131      * <p>To find a <code>SchemaFactory</code> object for a given schema language,
 132      * this method looks the following places in the following order
 133      * where "the class loader" refers to the context class loader:</p>
 134      * <ol>
 135      *  <li>
 136      *     If the system property
 137      *     <code>"javax.xml.validation.SchemaFactory:<i>schemaLanguage</i>"</code>
 138      *     is present (where <i>schemaLanguage</i> is the parameter
 139      *     to this method), then its value is read
 140      *     as a class name. The method will try to
 141      *     create a new instance of this class by using the class loader,
 142      *     and returns it if it is successfully created.
 143      *   </li>
 144      *   <li>
 145      *     <code>$java.home/lib/jaxp.properties</code> is read and
 146      *     the value associated with the key being the system property above
 147      *     is looked for. If present, the value is processed just like above.
 148      *   </li>
 149      *   <li>
 150      *     <p>The class loader is asked for service provider provider-configuration files matching
 151      *     <code>javax.xml.validation.SchemaFactory</code> in the resource directory META-INF/services.
 152      *     See the JAR File Specification for file format and parsing rules.
 153      *     Each potential service provider is required to implement the method:</p>
 154      *     <pre>
 155      *        {@link #isSchemaLanguageSupported(String schemaLanguage)}
 156      *     </pre>
 157      *     The first service provider found in class loader order that supports the specified schema language is returned.



 158      *   </li>
 159      *   <li>
 160      *     Platform default <code>SchemaFactory</code> is located
 161      *     in a implementation specific way. There must be a platform default
 162      *     <code>SchemaFactory</code> for W3C XML Schema.
 163      *   </li>
 164      * </ol>
 165      *
 166      * <p>If everything fails, {@link IllegalArgumentException} will be thrown.</p>
 167      *
 168      * <p><strong>Tip for Trouble-shooting:</strong></p>
 169      * <p>See {@link java.util.Properties#load(java.io.InputStream)} for
 170      * exactly how a property file is parsed. In particular, colons ':'
 171      * need to be escaped in a property file, so make sure schema language
 172      * URIs are properly escaped in it. For example:</p>
 173      * <pre>
 174      * http\://www.w3.org/2001/XMLSchema=org.acme.foo.XSSchemaFactory
 175      * </pre>
 176      *
 177      * @param schemaLanguage
 178      *      Specifies the schema language which the returned
 179      *      SchemaFactory will understand. See
 180      *      <a href="#schemaLanguage">the list of available
 181      *      schema languages</a> for the possible values.
 182      *
 183      * @return New instance of a <code>SchemaFactory</code>
 184      *
 185      * @throws IllegalArgumentException
 186      *      If no implementation of the schema language is available.
 187      * @throws NullPointerException
 188      *      If the <code>schemaLanguage</code> parameter is null.


 189      *
 190      * @see #newInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader)
 191      */
 192     public static final SchemaFactory newInstance(String schemaLanguage) {
 193         ClassLoader cl;
 194         cl = ss.getContextClassLoader();
 195 
 196         if (cl == null) {
 197             //cl = ClassLoader.getSystemClassLoader();
 198             //use the current class loader
 199             cl = SchemaFactory.class.getClassLoader();
 200         }
 201 
 202         SchemaFactory f = new SchemaFactoryFinder(cl).newFactory(schemaLanguage);
 203         if (f == null) {
 204             throw new IllegalArgumentException(
 205                     "No SchemaFactory"
 206                     + " that implements the schema language specified by: " + schemaLanguage
 207                     + " could be loaded");
 208         }
 209         return f;
 210     }
 211 
 212     /**




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.validation;
  27 
  28 import java.io.File;
  29 import java.net.URL;

  30 import javax.xml.transform.Source;
  31 import javax.xml.transform.stream.StreamSource;

  32 import org.w3c.dom.ls.LSResourceResolver;
  33 import org.xml.sax.ErrorHandler;
  34 import org.xml.sax.SAXException;
  35 import org.xml.sax.SAXNotRecognizedException;
  36 import org.xml.sax.SAXNotSupportedException;
  37 import org.xml.sax.SAXParseException;
  38 
  39 /**
  40  * Factory that creates {@link Schema} objects&#x2E; Entry-point to
  41  * the validation API.
  42  *
  43  * <p>
  44  * {@link SchemaFactory} is a schema compiler. It reads external
  45  * representations of schemas and prepares them for validation.
  46  *
  47  * <p>
  48  * The {@link SchemaFactory} class is not thread-safe. In other words,
  49  * it is the application's responsibility to ensure that at most
  50  * one thread is using a {@link SchemaFactory} object at any
  51  * given moment. Implementations are encouraged to mark methods
  52  * as <code>synchronized</code> to protect themselves from broken clients.
  53  *
  54  * <p>
  55  * {@link SchemaFactory} is not re-entrant. While one of the
  56  * <code>newSchema</code> methods is being invoked, applications
  57  * may not attempt to recursively invoke the <code>newSchema</code> method,


  61  * <p>
  62  * This spec uses a namespace URI to designate a schema language.
  63  * The following table shows the values defined by this specification.
  64  * <p>
  65  * To be compliant with the spec, the implementation
  66  * is only required to support W3C XML Schema 1.0. However,
  67  * if it chooses to support other schema languages listed here,
  68  * it must conform to the relevant behaviors described in this spec.
  69  *
  70  * <p>
  71  * Schema languages not listed here are expected to
  72  * introduce their own URIs to represent themselves.
  73  * The {@link SchemaFactory} class is capable of locating other
  74  * implementations for other schema languages at run-time.
  75  *
  76  * <p>
  77  * Note that because the XML DTD is strongly tied to the parsing process
  78  * and has a significant effect on the parsing process, it is impossible
  79  * to define the DTD validation as a process independent from parsing.
  80  * For this reason, this specification does not define the semantics for
  81  * the XML DTD. This doesn't prohibit implementors from implementing it
  82  * in a way they see fit, but <em>users are warned that any DTD
  83  * validation implemented on this interface necessarily deviate from
  84  * the XML DTD semantics as defined in the XML 1.0</em>.
  85  *
  86  * <table border="1" cellpadding="2">
  87  *   <thead>
  88  *     <tr>
  89  *       <th>value</th>
  90  *       <th>language</th>
  91  *     </tr>
  92  *   </thead>
  93  *   <tbody>
  94  *     <tr>
  95  *       <td>{@link javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI} ("<code>http://www.w3.org/2001/XMLSchema</code>")</td>
  96  *       <td><a href="http://www.w3.org/TR/xmlschema-1">W3C XML Schema 1.0</a></td>
  97  *     </tr>
  98  *     <tr>
  99  *       <td>{@link javax.xml.XMLConstants#RELAXNG_NS_URI} ("<code>http://relaxng.org/ns/structure/1.0</code>")</td>
 100  *       <td><a href="http://www.relaxng.org/">RELAX NG 1.0</a></td>
 101  *     </tr>


 129      *
 130      * <p>To find a <code>SchemaFactory</code> object for a given schema language,
 131      * this method looks the following places in the following order
 132      * where "the class loader" refers to the context class loader:</p>
 133      * <ol>
 134      *  <li>
 135      *     If the system property
 136      *     <code>"javax.xml.validation.SchemaFactory:<i>schemaLanguage</i>"</code>
 137      *     is present (where <i>schemaLanguage</i> is the parameter
 138      *     to this method), then its value is read
 139      *     as a class name. The method will try to
 140      *     create a new instance of this class by using the class loader,
 141      *     and returns it if it is successfully created.
 142      *   </li>
 143      *   <li>
 144      *     <code>$java.home/lib/jaxp.properties</code> is read and
 145      *     the value associated with the key being the system property above
 146      *     is looked for. If present, the value is processed just like above.
 147      *   </li>
 148      *   <li>
 149      *   Use the service-provider loading facilities, defined by the
 150      *   {@link java.util.ServiceLoader} class, to attempt to locate and load an
 151      *   implementation of the service.<br>
 152      *   Each potential service provider is required to implement the method
 153      *        {@link #isSchemaLanguageSupported(String schemaLanguage)}.
 154      *   <br>
 155      *   The first service provider found that supports the specified schema
 156      *   language is returned.
 157      *   <br>
 158      *   In case of {@link java.util.ServiceConfigurationError} a
 159      *   {@link SchemaFactoryConfigurationError} will be thrown.
 160      *   </li>
 161      *   <li>
 162      *     Platform default <code>SchemaFactory</code> is located
 163      *     in a implementation specific way. There must be a platform default
 164      *     <code>SchemaFactory</code> for W3C XML Schema.
 165      *   </li>
 166      * </ol>
 167      *
 168      * <p>If everything fails, {@link IllegalArgumentException} will be thrown.</p>
 169      *
 170      * <p><strong>Tip for Trouble-shooting:</strong></p>
 171      * <p>See {@link java.util.Properties#load(java.io.InputStream)} for
 172      * exactly how a property file is parsed. In particular, colons ':'
 173      * need to be escaped in a property file, so make sure schema language
 174      * URIs are properly escaped in it. For example:</p>
 175      * <pre>
 176      * http\://www.w3.org/2001/XMLSchema=org.acme.foo.XSSchemaFactory
 177      * </pre>
 178      *
 179      * @param schemaLanguage
 180      *      Specifies the schema language which the returned
 181      *      SchemaFactory will understand. See
 182      *      <a href="#schemaLanguage">the list of available
 183      *      schema languages</a> for the possible values.
 184      *
 185      * @return New instance of a <code>SchemaFactory</code>
 186      *
 187      * @throws IllegalArgumentException
 188      *      If no implementation of the schema language is available.
 189      * @throws NullPointerException
 190      *      If the <code>schemaLanguage</code> parameter is null.
 191      * @throws SchemaFactoryConfigurationError
 192      *      If a configuration error is encountered.
 193      *
 194      * @see #newInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader)
 195      */
 196     public static SchemaFactory newInstance(String schemaLanguage) {
 197         ClassLoader cl;
 198         cl = ss.getContextClassLoader();
 199 
 200         if (cl == null) {
 201             //cl = ClassLoader.getSystemClassLoader();
 202             //use the current class loader
 203             cl = SchemaFactory.class.getClassLoader();
 204         }
 205 
 206         SchemaFactory f = new SchemaFactoryFinder(cl).newFactory(schemaLanguage);
 207         if (f == null) {
 208             throw new IllegalArgumentException(
 209                     "No SchemaFactory"
 210                     + " that implements the schema language specified by: " + schemaLanguage
 211                     + " could be loaded");
 212         }
 213         return f;
 214     }
 215 
 216     /**