1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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.parsers;
  27 
  28 import javax.xml.validation.Schema;
  29 import org.xml.sax.SAXException;
  30 import org.xml.sax.SAXNotRecognizedException;
  31 import org.xml.sax.SAXNotSupportedException;
  32 
  33 /**
  34  * Defines a factory API that enables applications to configure and
  35  * obtain a SAX based parser to parse XML documents.
  36  *
  37  * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  38  * @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a>
  39  *
  40  * @since 1.4
  41  */
  42 public abstract class SAXParserFactory {
  43 
  44     /**
  45      * Should Parsers be validating?
  46      */
  47     private boolean validating = false;
  48 
  49     /**
  50      * Should Parsers be namespace aware?
  51      */
  52     private boolean namespaceAware = false;
  53 
  54     /**
  55      * Protected constructor to force use of {@link #newInstance()}.
  56      */
  57     protected SAXParserFactory () {
  58 
  59     }
  60 
  61     /**
  62      * Obtain a new instance of a {@code SAXParserFactory}. This
  63      * static method creates a new factory instance
  64      * This method uses the following ordered lookup procedure to determine
  65      * the {@code SAXParserFactory} implementation class to
  66      * load:
  67      * <ul>
  68      * <li>
  69      * Use the {@code javax.xml.parsers.SAXParserFactory} system
  70      * property.
  71      * </li>
  72      * <li>
  73      * <p>
  74      * Use the configuration file "jaxp.properties". The file is in standard
  75      * {@link java.util.Properties} format and typically located in the
  76      * {@code conf} directory of the Java installation. It contains the fully qualified
  77      * name of the implementation class with the key being the system property
  78      * defined above.
  79      * <p>
  80      * The jaxp.properties file is read only once by the JAXP implementation
  81      * and its values are then cached for future use.  If the file does not exist
  82      * when the first attempt is made to read from it, no further attempts are
  83      * made to check for its existence.  It is not possible to change the value
  84      * of any property in jaxp.properties after it has been read for the first time.
  85      * </li>
  86      * <li>
  87      * <p>
  88      * Use the service-provider loading facility, defined by the
  89      * {@link java.util.ServiceLoader} class, to attempt to locate and load an
  90      * implementation of the service using the {@linkplain
  91      * java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:
  92      * the service-provider loading facility will use the {@linkplain
  93      * java.lang.Thread#getContextClassLoader() current thread's context class loader}
  94      * to attempt to load the service. If the context class
  95      * loader is null, the {@linkplain
  96      * ClassLoader#getSystemClassLoader() system class loader} will be used.
  97      * </li>
  98      * <li>
  99      * <p>
 100      * Otherwise the system-default implementation is returned.
 101      * </li>
 102      * </ul>
 103      *
 104      * <p>
 105      * Once an application has obtained a reference to a
 106      * {@code SAXParserFactory} it can use the factory to
 107      * configure and obtain parser instances.
 108      *
 109      *
 110      *
 111      * <h2>Tip for Trouble-shooting</h2>
 112      * <p>
 113      * Setting the {@code jaxp.debug} system property will cause
 114      * this method to print a lot of debug messages
 115      * to {@code System.err} about what it is doing and where it is looking at.
 116      *
 117      * <p>
 118      * If you have problems loading {@link SAXParser}s, try:
 119      * <pre>
 120      * java -Djaxp.debug=1 YourProgram ....
 121      * </pre>
 122      *
 123      *
 124      * @return A new instance of a SAXParserFactory.
 125      *
 126      * @throws FactoryConfigurationError in case of {@linkplain
 127      * java.util.ServiceConfigurationError service configuration error} or if
 128      * the implementation is not available or cannot be instantiated.
 129      */
 130 
 131     public static SAXParserFactory newInstance() {
 132         return FactoryFinder.find(
 133                 /* The default property name according to the JAXP spec */
 134                 SAXParserFactory.class,
 135                 /* The fallback implementation class name */
 136                 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
 137     }
 138 
 139     /**
 140      * Obtain a new instance of a {@code SAXParserFactory} from class name.
 141      * This function is useful when there are multiple providers in the classpath.
 142      * It gives more control to the application as it can specify which provider
 143      * should be loaded.
 144      *
 145      * <p>Once an application has obtained a reference to a {@code SAXParserFactory}
 146      * it can use the factory to configure and obtain parser instances.
 147      *
 148      *
 149      * <h2>Tip for Trouble-shooting</h2>
 150      * <p>Setting the {@code jaxp.debug} system property will cause
 151      * this method to print a lot of debug messages
 152      * to {@code System.err} about what it is doing and where it is looking at.
 153      *
 154      * <p>
 155      * If you have problems, try:
 156      * <pre>
 157      * java -Djaxp.debug=1 YourProgram ....
 158      * </pre>
 159      *
 160      * @param factoryClassName fully qualified factory class name that provides implementation of {@code javax.xml.parsers.SAXParserFactory}.
 161      *
 162      * @param classLoader {@code ClassLoader} used to load the factory class. If {@code null}
 163      *                     current {@code Thread}'s context classLoader is used to load the factory class.
 164      *
 165      * @return New instance of a {@code SAXParserFactory}
 166      *
 167      * @throws FactoryConfigurationError if {@code factoryClassName} is {@code null}, or
 168      *                                   the factory class cannot be loaded, instantiated.
 169      *
 170      * @see #newInstance()
 171      *
 172      * @since 1.6
 173      */
 174     public static SAXParserFactory newInstance(String factoryClassName, ClassLoader classLoader){
 175             //do not fallback if given classloader can't find the class, throw exception
 176             return FactoryFinder.newInstance(SAXParserFactory.class,
 177                     factoryClassName, classLoader, false);
 178     }
 179 
 180     /**
 181      * Creates a new instance of a SAXParser using the currently
 182      * configured factory parameters.
 183      *
 184      * @return A new instance of a SAXParser.
 185      *
 186      * @throws ParserConfigurationException if a parser cannot
 187      *   be created which satisfies the requested configuration.
 188      * @throws SAXException for SAX errors.
 189      */
 190 
 191     public abstract SAXParser newSAXParser()
 192         throws ParserConfigurationException, SAXException;
 193 
 194 
 195     /**
 196      * Specifies that the parser produced by this code will
 197      * provide support for XML namespaces. By default the value of this is set
 198      * to {@code false}.
 199      *
 200      * @param awareness true if the parser produced by this code will
 201      *                  provide support for XML namespaces; false otherwise.
 202      */
 203 
 204     public void setNamespaceAware(boolean awareness) {
 205         this.namespaceAware = awareness;
 206     }
 207 
 208     /**
 209      * Specifies that the parser produced by this code will
 210      * validate documents as they are parsed. By default the value of this is
 211      * set to {@code false}.
 212      *
 213      * <p>
 214      * Note that "the validation" here means
 215      * <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating
 216      * parser</a> as defined in the XML recommendation.
 217      * In other words, it essentially just controls the DTD validation.
 218      * (except the legacy two properties defined in JAXP 1.2.)
 219      *
 220      * <p>
 221      * To use modern schema languages such as W3C XML Schema or
 222      * RELAX NG instead of DTD, you can configure your parser to be
 223      * a non-validating parser by leaving the {@link #setValidating(boolean)}
 224      * method {@code false}, then use the {@link #setSchema(Schema)}
 225      * method to associate a schema to a parser.
 226      *
 227      * @param validating true if the parser produced by this code will
 228      *                   validate documents as they are parsed; false otherwise.
 229      */
 230 
 231     public void setValidating(boolean validating) {
 232         this.validating = validating;
 233     }
 234 
 235     /**
 236      * Indicates whether or not the factory is configured to produce
 237      * parsers which are namespace aware.
 238      *
 239      * @return true if the factory is configured to produce
 240      *         parsers which are namespace aware; false otherwise.
 241      */
 242 
 243     public boolean isNamespaceAware() {
 244         return namespaceAware;
 245     }
 246 
 247     /**
 248      * Indicates whether or not the factory is configured to produce
 249      * parsers which validate the XML content during parse.
 250      *
 251      * @return true if the factory is configured to produce parsers which validate
 252      *         the XML content during parse; false otherwise.
 253      */
 254 
 255     public boolean isValidating() {
 256         return validating;
 257     }
 258 
 259     /**
 260      * Sets the particular feature in the underlying implementation of
 261      * org.xml.sax.XMLReader.
 262      * A list of the core features and properties can be found at
 263      * <a href="http://www.saxproject.org/">http://www.saxproject.org/</a>
 264      *
 265      * <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
 266      * When the feature is
 267      * <ul>
 268      *   <li>
 269      *     {@code true}: the implementation will limit XML processing to conform to implementation limits.
 270      *     Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources.
 271      *     If XML processing is limited for security reasons, it will be reported via a call to the registered
 272      *     {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.
 273      *     See {@link SAXParser} {@code parse} methods for handler specification.
 274      *   </li>
 275      *   <li>
 276      *     When the feature is {@code false}, the implementation will processing XML according to the XML specifications without
 277      *     regard to possible implementation limits.
 278      *   </li>
 279      * </ul>
 280      *
 281      * @param name The name of the feature to be set.
 282      * @param value The value of the feature to be set.
 283      *
 284      * @throws ParserConfigurationException if a parser cannot
 285      *     be created which satisfies the requested configuration.
 286      * @throws SAXNotRecognizedException When the underlying XMLReader does
 287      *            not recognize the property name.
 288      * @throws SAXNotSupportedException When the underlying XMLReader
 289      *            recognizes the property name but doesn't support the
 290      *            property.
 291      * @throws NullPointerException If the {@code name} parameter is null.
 292      *
 293      * @see org.xml.sax.XMLReader#setFeature
 294      */
 295     public abstract void setFeature(String name, boolean value)
 296         throws ParserConfigurationException, SAXNotRecognizedException,
 297                 SAXNotSupportedException;
 298 
 299     /**
 300      *
 301      * Returns the particular property requested for in the underlying
 302      * implementation of org.xml.sax.XMLReader.
 303      *
 304      * @param name The name of the property to be retrieved.
 305      *
 306      * @return Value of the requested property.
 307      *
 308      * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
 309      * @throws SAXNotRecognizedException When the underlying XMLReader does not recognize the property name.
 310      * @throws SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support the property.
 311      *
 312      * @see org.xml.sax.XMLReader#getProperty
 313      */
 314     public abstract boolean getFeature(String name)
 315         throws ParserConfigurationException, SAXNotRecognizedException,
 316                 SAXNotSupportedException;
 317 
 318 
 319     /**
 320      * Gets the {@link Schema} object specified through
 321      * the {@link #setSchema(Schema schema)} method.
 322      *
 323      *
 324      * @throws UnsupportedOperationException When implementation does not
 325      *   override this method
 326      *
 327      * @return
 328      *      the {@link Schema} object that was last set through
 329      *      the {@link #setSchema(Schema)} method, or null
 330      *      if the method was not invoked since a {@link SAXParserFactory}
 331      *      is created.
 332      *
 333      * @since 1.5
 334      */
 335     public Schema getSchema() {
 336         throw new UnsupportedOperationException(
 337             "This parser does not support specification \""
 338             + this.getClass().getPackage().getSpecificationTitle()
 339             + "\" version \""
 340             + this.getClass().getPackage().getSpecificationVersion()
 341             + "\""
 342             );
 343     }
 344 
 345     /**
 346      * Set the {@link Schema} to be used by parsers created
 347      * from this factory.
 348      *
 349      * <p>When a {@link Schema} is non-null, a parser will use a validator
 350      * created from it to validate documents before it passes information
 351      * down to the application.
 352      *
 353      * <p>When warnings/errors/fatal errors are found by the validator, the parser must
 354      * handle them as if those errors were found by the parser itself.
 355      * In other words, if the user-specified {@link org.xml.sax.ErrorHandler}
 356      * is set, it must receive those errors, and if not, they must be
 357      * treated according to the implementation specific
 358      * default error handling rules.
 359      *
 360      * <p>A validator may modify the SAX event stream (for example by
 361      * adding default values that were missing in documents), and a parser
 362      * is responsible to make sure that the application will receive
 363      * those modified event stream.
 364      *
 365      * <p>Initially, {@code null} is set as the {@link Schema}.
 366      *
 367      * <p>This processing will take effect even if
 368      * the {@link #isValidating()} method returns {@code false}.
 369      *
 370      * <p>It is an error to use
 371      * the {@code http://java.sun.com/xml/jaxp/properties/schemaSource}
 372      * property and/or the {@code http://java.sun.com/xml/jaxp/properties/schemaLanguage}
 373      * property in conjunction with a non-null {@link Schema} object.
 374      * Such configuration will cause a {@link SAXException}
 375      * exception when those properties are set on a {@link SAXParser}.
 376      *
 377      * <h3>Note for implementors</h3>
 378      * <p>
 379      * A parser must be able to work with any {@link Schema}
 380      * implementation. However, parsers and schemas are allowed
 381      * to use implementation-specific custom mechanisms
 382      * as long as they yield the result described in the specification.
 383      * 
 384      *
 385      * @param schema {@code Schema} to use, {@code null} to remove a schema.
 386      *
 387      * @throws UnsupportedOperationException When implementation does not
 388      *   override this method
 389      *
 390      * @since 1.5
 391      */
 392     public void setSchema(Schema schema) {
 393         throw new UnsupportedOperationException(
 394             "This parser does not support specification \""
 395             + this.getClass().getPackage().getSpecificationTitle()
 396             + "\" version \""
 397             + this.getClass().getPackage().getSpecificationVersion()
 398             + "\""
 399             );
 400     }
 401 
 402     /**
 403      * Set state of XInclude processing.
 404      *
 405      * <p>If XInclude markup is found in the document instance, should it be
 406      * processed as specified in <a href="http://www.w3.org/TR/xinclude/">
 407      * XML Inclusions (XInclude) Version 1.0</a>.
 408      *
 409      * <p>XInclude processing defaults to {@code false}.
 410      *
 411      * @param state Set XInclude processing to {@code true} or
 412      *   {@code false}
 413      *
 414      * @throws UnsupportedOperationException When implementation does not
 415      *   override this method
 416      *
 417      * @since 1.5
 418      */
 419     public void setXIncludeAware(final boolean state) {
 420         if (state) {
 421             throw new UnsupportedOperationException(" setXIncludeAware " +
 422                 "is not supported on this JAXP"  +
 423                 " implementation or earlier: " + this.getClass());
 424         }
 425     }
 426 
 427     /**
 428      * Get state of XInclude processing.
 429      *
 430      * @return current state of XInclude processing
 431      *
 432      * @throws UnsupportedOperationException When implementation does not
 433      *   override this method
 434      *
 435      * @since 1.5
 436      */
 437     public boolean isXIncludeAware() {
 438         throw new UnsupportedOperationException(
 439             "This parser does not support specification \""
 440             + this.getClass().getPackage().getSpecificationTitle()
 441             + "\" version \""
 442             + this.getClass().getPackage().getSpecificationVersion()
 443             + "\""
 444             );
 445     }
 446 }