1 /*
   2  * Copyright (c) 2000, 2006, 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 
  30 /**
  31  * Defines a factory API that enables applications to obtain a
  32  * parser that produces DOM object trees from XML documents.
  33  *
  34  * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  35  * @author <a href="mailto:Neeraj.Bajaj@sun.com">Neeraj Bajaj</a>
  36  *
  37  * @version $Revision: 1.10 $, $Date: 2010-11-01 04:36:09 $
  38 
  39  */
  40 
  41 public abstract class DocumentBuilderFactory {
  42 
  43     /** The default property name according to the JAXP spec */
  44     private static final String DEFAULT_PROPERTY_NAME = "javax.xml.parsers.DocumentBuilderFactory";
  45 
  46     private boolean validating = false;
  47     private boolean namespaceAware = false;
  48     private boolean whitespace = false;
  49     private boolean expandEntityRef = true;
  50     private boolean ignoreComments = false;
  51     private boolean coalescing = false;
  52 
  53     private boolean canonicalState = false;
  54 
  55     /**
  56      * <p>Protected constructor to prevent instantiation.
  57      * Use {@link #newInstance()}.</p>
  58      */
  59     protected DocumentBuilderFactory () {
  60     }
  61 
  62     /**
  63      * Obtain a new instance of a
  64      * <code>DocumentBuilderFactory</code>. This static method creates
  65      * a new factory instance.
  66      * This method uses the following ordered lookup procedure to determine
  67      * the <code>DocumentBuilderFactory</code> implementation class to
  68      * load:
  69      * <ul>
  70      * <li>
  71      * Use the <code>javax.xml.parsers.DocumentBuilderFactory</code> system
  72      * property.
  73      * </li>
  74      * <li>
  75      * Use the properties file "lib/jaxp.properties" in the JRE directory.
  76      * This configuration file is in standard <code>java.util.Properties
  77      * </code> format and contains the fully qualified name of the
  78      * implementation class with the key being the system property defined
  79      * above.
  80      *
  81      * The jaxp.properties file is read only once by the JAXP implementation
  82      * and it's values are then cached for future use.  If the file does not exist
  83      * when the first attempt is made to read from it, no further attempts are
  84      * made to check for its existence.  It is not possible to change the value
  85      * of any property in jaxp.properties after it has been read for the first time.
  86      * </li>
  87      * <li>
  88      * Uses the service-provider loading facilities, defined by the {@link java.util.ServiceLoader} class, to attempt 
  89      * to locate and load an implementation of the service. If there are providers other than the implementation specific
  90      * default located, then the first provider that is not the default is instantiated and returned; Otherwise
  91      * the default implementation is returned if it is on the classpath or installed as a module.
  92      * 
  93      * If a misconfigured provider is encountered and {@link java.util.ServiceConfigurationError} is thrown, the error will be wrapped 
  94      * in a {@link javax.xml.parsers.FactoryConfigurationException}.</p>
  95      * </li>
  96      * <li>
  97      * Platform default <code>DocumentBuilderFactory</code> instance.
  98      * </li>
  99      * </ul>
 100      *
 101      * Once an application has obtained a reference to a
 102      * <code>DocumentBuilderFactory</code> it can use the factory to
 103      * configure and obtain parser instances.
 104      *
 105      *
 106      * <h2>Tip for Trouble-shooting</h2>
 107      * <p>Setting the <code>jaxp.debug</code> system property will cause
 108      * this method to print a lot of debug messages
 109      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
 110      *
 111      * <p> If you have problems loading {@link DocumentBuilder}s, try:</p>
 112      * <pre>
 113      * java -Djaxp.debug=1 YourProgram ....
 114      * </pre>
 115      *
 116      * @return New instance of a <code>DocumentBuilderFactory</code>
 117      *
 118      * @throws FactoryConfigurationError if the implementation is not
 119      *   available or cannot be instantiated.
 120      */
 121     public static DocumentBuilderFactory newInstance() {
 122 //        try {
 123             return (DocumentBuilderFactory) FactoryFinder.find(DocumentBuilderFactory.class,
 124                     /* The default property name according to the JAXP spec */
 125                     "javax.xml.parsers.DocumentBuilderFactory",
 126                     /* The fallback implementation class name */
 127                     "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
 128 //        } catch (FactoryConfigurationError e) {
 129 //            throw e;
 130 //        }
 131     }
 132 
 133     /**
 134      * <p>Obtain a new instance of a <code>DocumentBuilderFactory</code> from class name.
 135      * This function is useful when there are multiple providers in the classpath.
 136      * It gives more control to the application as it can specify which provider
 137      * should be loaded.</p>
 138      *
 139      * <p>Once an application has obtained a reference to a <code>DocumentBuilderFactory</code>
 140      * it can use the factory to configure and obtain parser instances.</p>
 141      *
 142      *
 143      * <h2>Tip for Trouble-shooting</h2>
 144      * <p>Setting the <code>jaxp.debug</code> system property will cause
 145      * this method to print a lot of debug messages
 146      * to <code>System.err</code> about what it is doing and where it is looking at.</p>
 147      *
 148      * <p> If you have problems try:</p>
 149      * <pre>
 150      * java -Djaxp.debug=1 YourProgram ....
 151      * </pre>
 152      *
 153      * @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.parsers.DocumentBuilderFactory</code>.
 154      *
 155      * @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>
 156      *                     current <code>Thread</code>'s context classLoader is used to load the factory class.
 157      *
 158      * @return New instance of a <code>DocumentBuilderFactory</code>
 159      *
 160      * @throws FactoryConfigurationError if <code>factoryClassName</code> is <code>null</code>, or
 161      *                                   the factory class cannot be loaded, instantiated.
 162      *
 163      * @see #newInstance()
 164      *
 165      * @since 1.6
 166      */
 167     public static DocumentBuilderFactory newInstance(String factoryClassName, ClassLoader classLoader){
 168         try {
 169             //do not fallback if given classloader can't find the class, throw exception
 170             return (DocumentBuilderFactory) FactoryFinder.newInstance(factoryClassName, classLoader, false);
 171         } catch (FactoryConfigurationError e) {
 172             throw e;
 173         }
 174     }
 175 
 176     /**
 177      * Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}
 178      * using the currently configured parameters.
 179      *
 180      * @return A new instance of a DocumentBuilder.
 181      *
 182      * @throws ParserConfigurationException if a DocumentBuilder
 183      *   cannot be created which satisfies the configuration requested.
 184      */
 185 
 186     public abstract DocumentBuilder newDocumentBuilder()
 187         throws ParserConfigurationException;
 188 
 189 
 190     /**
 191      * Specifies that the parser produced by this code will
 192      * provide support for XML namespaces. By default the value of this is set
 193      * to <code>false</code>
 194      *
 195      * @param awareness true if the parser produced will provide support
 196      *                  for XML namespaces; false otherwise.
 197      */
 198 
 199     public void setNamespaceAware(boolean awareness) {
 200         this.namespaceAware = awareness;
 201     }
 202 
 203     /**
 204      * Specifies that the parser produced by this code will
 205      * validate documents as they are parsed. By default the value of this
 206      * is set to <code>false</code>.
 207      *
 208      * <p>
 209      * Note that "the validation" here means
 210      * <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating
 211      * parser</a> as defined in the XML recommendation.
 212      * In other words, it essentially just controls the DTD validation.
 213      * (except the legacy two properties defined in JAXP 1.2.)
 214      * </p>
 215      *
 216      * <p>
 217      * To use modern schema languages such as W3C XML Schema or
 218      * RELAX NG instead of DTD, you can configure your parser to be
 219      * a non-validating parser by leaving the {@link #setValidating(boolean)}
 220      * method <code>false</code>, then use the {@link #setSchema(Schema)}
 221      * method to associate a schema to a parser.
 222      * </p>
 223      *
 224      * @param validating true if the parser produced will validate documents
 225      *                   as they are parsed; false otherwise.
 226      */
 227 
 228     public void setValidating(boolean validating) {
 229         this.validating = validating;
 230     }
 231 
 232     /**
 233      * Specifies that the parsers created by this  factory must eliminate
 234      * whitespace in element content (sometimes known loosely as
 235      * 'ignorable whitespace') when parsing XML documents (see XML Rec
 236      * 2.10). Note that only whitespace which is directly contained within
 237      * element content that has an element only content model (see XML
 238      * Rec 3.2.1) will be eliminated. Due to reliance on the content model
 239      * this setting requires the parser to be in validating mode. By default
 240      * the value of this is set to <code>false</code>.
 241      *
 242      * @param whitespace true if the parser created must eliminate whitespace
 243      *                   in the element content when parsing XML documents;
 244      *                   false otherwise.
 245      */
 246 
 247     public void setIgnoringElementContentWhitespace(boolean whitespace) {
 248         this.whitespace = whitespace;
 249     }
 250 
 251     /**
 252      * Specifies that the parser produced by this code will
 253      * expand entity reference nodes. By default the value of this is set to
 254      * <code>true</code>
 255      *
 256      * @param expandEntityRef true if the parser produced will expand entity
 257      *                        reference nodes; false otherwise.
 258      */
 259 
 260     public void setExpandEntityReferences(boolean expandEntityRef) {
 261         this.expandEntityRef = expandEntityRef;
 262     }
 263 
 264     /**
 265      * <p>Specifies that the parser produced by this code will
 266      * ignore comments. By default the value of this is set to <code>false
 267      * </code>.</p>
 268      *
 269      * @param ignoreComments <code>boolean</code> value to ignore comments during processing
 270      */
 271 
 272     public void setIgnoringComments(boolean ignoreComments) {
 273         this.ignoreComments = ignoreComments;
 274     }
 275 
 276     /**
 277      * Specifies that the parser produced by this code will
 278      * convert CDATA nodes to Text nodes and append it to the
 279      * adjacent (if any) text node. By default the value of this is set to
 280      * <code>false</code>
 281      *
 282      * @param coalescing  true if the parser produced will convert CDATA nodes
 283      *                    to Text nodes and append it to the adjacent (if any)
 284      *                    text node; false otherwise.
 285      */
 286 
 287     public void setCoalescing(boolean coalescing) {
 288         this.coalescing = coalescing;
 289     }
 290 
 291     /**
 292      * Indicates whether or not the factory is configured to produce
 293      * parsers which are namespace aware.
 294      *
 295      * @return  true if the factory is configured to produce parsers which
 296      *          are namespace aware; false otherwise.
 297      */
 298 
 299     public boolean isNamespaceAware() {
 300         return namespaceAware;
 301     }
 302 
 303     /**
 304      * Indicates whether or not the factory is configured to produce
 305      * parsers which validate the XML content during parse.
 306      *
 307      * @return  true if the factory is configured to produce parsers
 308      *          which validate the XML content during parse; false otherwise.
 309      */
 310 
 311     public boolean isValidating() {
 312         return validating;
 313     }
 314 
 315     /**
 316      * Indicates whether or not the factory is configured to produce
 317      * parsers which ignore ignorable whitespace in element content.
 318      *
 319      * @return  true if the factory is configured to produce parsers
 320      *          which ignore ignorable whitespace in element content;
 321      *          false otherwise.
 322      */
 323 
 324     public boolean isIgnoringElementContentWhitespace() {
 325         return whitespace;
 326     }
 327 
 328     /**
 329      * Indicates whether or not the factory is configured to produce
 330      * parsers which expand entity reference nodes.
 331      *
 332      * @return  true if the factory is configured to produce parsers
 333      *          which expand entity reference nodes; false otherwise.
 334      */
 335 
 336     public boolean isExpandEntityReferences() {
 337         return expandEntityRef;
 338     }
 339 
 340     /**
 341      * Indicates whether or not the factory is configured to produce
 342      * parsers which ignores comments.
 343      *
 344      * @return  true if the factory is configured to produce parsers
 345      *          which ignores comments; false otherwise.
 346      */
 347 
 348     public boolean isIgnoringComments() {
 349         return ignoreComments;
 350     }
 351 
 352     /**
 353      * Indicates whether or not the factory is configured to produce
 354      * parsers which converts CDATA nodes to Text nodes and appends it to
 355      * the adjacent (if any) Text node.
 356      *
 357      * @return  true if the factory is configured to produce parsers
 358      *          which converts CDATA nodes to Text nodes and appends it to
 359      *          the adjacent (if any) Text node; false otherwise.
 360      */
 361 
 362     public boolean isCoalescing() {
 363         return coalescing;
 364     }
 365 
 366     /**
 367      * Allows the user to set specific attributes on the underlying
 368      * implementation.
 369      *
 370      * @param name The name of the attribute.
 371      * @param value The value of the attribute.
 372      *
 373      * @throws IllegalArgumentException thrown if the underlying
 374      *   implementation doesn't recognize the attribute.
 375      */
 376     public abstract void setAttribute(String name, Object value)
 377                 throws IllegalArgumentException;
 378 
 379     /**
 380      * Allows the user to retrieve specific attributes on the underlying
 381      * implementation.
 382      *
 383      * @param name The name of the attribute.
 384      *
 385      * @return value The value of the attribute.
 386      *
 387      * @throws IllegalArgumentException thrown if the underlying
 388      *   implementation doesn't recognize the attribute.
 389      */
 390     public abstract Object getAttribute(String name)
 391                 throws IllegalArgumentException;
 392 
 393         /**
 394          * <p>Set a feature for this <code>DocumentBuilderFactory</code> and <code>DocumentBuilder</code>s created by this factory.</p>
 395          *
 396          * <p>
 397          * Feature names are fully qualified {@link java.net.URI}s.
 398          * Implementations may define their own features.
 399          * A {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
 400          * <code>DocumentBuilder</code>s it creates cannot support the feature.
 401          * It is possible for a <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
 402          * </p>
 403          *
 404          * <p>
 405          * All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
 406          * When the feature is:</p>
 407          * <ul>
 408          *   <li>
 409          *     <code>true</code>: the implementation will limit XML processing to conform to implementation limits.
 410          *     Examples include enity expansion limits and XML Schema constructs that would consume large amounts of resources.
 411          *     If XML processing is limited for security reasons, it will be reported via a call to the registered
 412          *    {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.
 413          *     See {@link  DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler errorHandler)}.
 414          *   </li>
 415          *   <li>
 416          *     <code>false</code>: the implementation will processing XML according to the XML specifications without
 417          *     regard to possible implementation limits.
 418          *   </li>
 419          * </ul>
 420          *
 421          * @param name Feature name.
 422          * @param value Is feature state <code>true</code> or <code>false</code>.
 423          *
 424          * @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code> or the <code>DocumentBuilder</code>s
 425          *   it creates cannot support this feature.
 426      * @throws NullPointerException If the <code>name</code> parameter is null.
 427          */
 428         public abstract void setFeature(String name, boolean value)
 429                 throws ParserConfigurationException;
 430 
 431         /**
 432          * <p>Get the state of the named feature.</p>
 433          *
 434          * <p>
 435          * Feature names are fully qualified {@link java.net.URI}s.
 436          * Implementations may define their own features.
 437          * An {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the
 438          * <code>DocumentBuilder</code>s it creates cannot support the feature.
 439          * It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.
 440          * </p>
 441          *
 442          * @param name Feature name.
 443          *
 444          * @return State of the named feature.
 445          *
 446          * @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code>
 447          *   or the <code>DocumentBuilder</code>s it creates cannot support this feature.
 448          */
 449         public abstract boolean getFeature(String name)
 450                 throws ParserConfigurationException;
 451 
 452 
 453     /** <p>Get current state of canonicalization.</p>
 454      *
 455      * @return current state canonicalization control
 456      */
 457     /*
 458     public boolean getCanonicalization() {
 459         return canonicalState;
 460     }
 461     */
 462 
 463 
 464     /**
 465      * Gets the {@link Schema} object specified through
 466      * the {@link #setSchema(Schema schema)} method.
 467      *
 468      * @return
 469      *      the {@link Schema} object that was last set through
 470      *      the {@link #setSchema(Schema)} method, or null
 471      *      if the method was not invoked since a {@link DocumentBuilderFactory}
 472      *      is created.
 473      *
 474      * @throws UnsupportedOperationException When implementation does not
 475      *   override this method.
 476      *
 477      * @since 1.5
 478      */
 479     public Schema getSchema() {
 480         throw new UnsupportedOperationException(
 481             "This parser does not support specification \""
 482             + this.getClass().getPackage().getSpecificationTitle()
 483             + "\" version \""
 484             + this.getClass().getPackage().getSpecificationVersion()
 485             + "\""
 486             );
 487 
 488     }
 489 
 490     /* <p>Set canonicalization control to <code>true</code> or
 491      * </code>false</code>.</p>
 492      *
 493      * @param state of canonicalization
 494      */
 495     /*
 496     public void setCanonicalization(boolean state) {
 497         canonicalState = state;
 498     }
 499     */
 500 
 501     /**
 502      * <p>Set the {@link Schema} to be used by parsers created
 503      * from this factory.
 504      *
 505      * <p>
 506      * When a {@link Schema} is non-null, a parser will use a validator
 507      * created from it to validate documents before it passes information
 508      * down to the application.
 509      *
 510      * <p>When errors are found by the validator, the parser is responsible
 511      * to report them to the user-specified {@link org.xml.sax.ErrorHandler}
 512      * (or if the error handler is not set, ignore them or throw them), just
 513      * like any other errors found by the parser itself.
 514      * In other words, if the user-specified {@link org.xml.sax.ErrorHandler}
 515      * is set, it must receive those errors, and if not, they must be
 516      * treated according to the implementation specific
 517      * default error handling rules.
 518      *
 519      * <p>
 520      * A validator may modify the outcome of a parse (for example by
 521      * adding default values that were missing in documents), and a parser
 522      * is responsible to make sure that the application will receive
 523      * modified DOM trees.
 524      *
 525      * <p>
 526      * Initialy, null is set as the {@link Schema}.
 527      *
 528      * <p>
 529      * This processing will take effect even if
 530      * the {@link #isValidating()} method returns <code>false</code>.
 531      *
 532      * <p>It is an error to use
 533      * the <code>http://java.sun.com/xml/jaxp/properties/schemaSource</code>
 534      * property and/or the <code>http://java.sun.com/xml/jaxp/properties/schemaLanguage</code>
 535      * property in conjunction with a {@link Schema} object.
 536      * Such configuration will cause a {@link ParserConfigurationException}
 537      * exception when the {@link #newDocumentBuilder()} is invoked.</p>
 538      *
 539      *
 540      * <h4>Note for implmentors</h4>
 541      *
 542      * <p>
 543      * A parser must be able to work with any {@link Schema}
 544      * implementation. However, parsers and schemas are allowed
 545      * to use implementation-specific custom mechanisms
 546      * as long as they yield the result described in the specification.
 547      * </p>
 548      *
 549      * @param schema <code>Schema</code> to use or <code>null</code>
 550      *   to remove a schema.
 551      *
 552      * @throws UnsupportedOperationException When implementation does not
 553      *   override this method.
 554      *
 555      * @since 1.5
 556      */
 557     public void setSchema(Schema schema) {
 558         throw new UnsupportedOperationException(
 559             "This parser does not support specification \""
 560             + this.getClass().getPackage().getSpecificationTitle()
 561             + "\" version \""
 562             + this.getClass().getPackage().getSpecificationVersion()
 563             + "\""
 564             );
 565     }
 566 
 567 
 568 
 569     /**
 570      * <p>Set state of XInclude processing.</p>
 571      *
 572      * <p>If XInclude markup is found in the document instance, should it be
 573      * processed as specified in <a href="http://www.w3.org/TR/xinclude/">
 574      * XML Inclusions (XInclude) Version 1.0</a>.</p>
 575      *
 576      * <p>XInclude processing defaults to <code>false</code>.</p>
 577      *
 578      * @param state Set XInclude processing to <code>true</code> or
 579      *   <code>false</code>
 580      *
 581      * @throws UnsupportedOperationException When implementation does not
 582      *   override this method.
 583      *
 584      * @since 1.5
 585      */
 586     public void setXIncludeAware(final boolean state) {
 587         if (state) {
 588             throw new UnsupportedOperationException(" setXIncludeAware " +
 589                 "is not supported on this JAXP" +
 590                 " implementation or earlier: " + this.getClass());
 591         }
 592     }
 593 
 594     /**
 595      * <p>Get state of XInclude processing.</p>
 596      *
 597      * @return current state of XInclude processing
 598      *
 599      * @throws UnsupportedOperationException When implementation does not
 600      *   override this method.
 601      *
 602      * @since 1.5
 603      */
 604     public boolean isXIncludeAware() {
 605         throw new UnsupportedOperationException(
 606             "This parser does not support specification \""
 607             + this.getClass().getPackage().getSpecificationTitle()
 608             + "\" version \""
 609             + this.getClass().getPackage().getSpecificationVersion()
 610             + "\""
 611             );
 612     }
 613 }