Path: blob/jdk8u272-b10-aarch32-20201026/jaxp/src/javax/xml/parsers/DocumentBuilderFactory.java
48789 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.parsers;2627import javax.xml.validation.Schema;2829/**30* Defines a factory API that enables applications to obtain a31* parser that produces DOM object trees from XML documents.32*33* @author <a href="mailto:[email protected]">Jeff Suttor</a>34* @author <a href="mailto:[email protected]">Neeraj Bajaj</a>35*36* @version $Revision: 1.9 $, $Date: 2010/05/25 16:19:44 $3738*/3940public abstract class DocumentBuilderFactory {4142private boolean validating = false;43private boolean namespaceAware = false;44private boolean whitespace = false;45private boolean expandEntityRef = true;46private boolean ignoreComments = false;47private boolean coalescing = false;4849/**50* <p>Protected constructor to prevent instantiation.51* Use {@link #newInstance()}.</p>52*/53protected DocumentBuilderFactory () {54}5556/**57* Obtain a new instance of a58* <code>DocumentBuilderFactory</code>. This static method creates59* a new factory instance.60* This method uses the following ordered lookup procedure to determine61* the <code>DocumentBuilderFactory</code> implementation class to62* load:63* <ul>64* <li>65* Use the <code>javax.xml.parsers.DocumentBuilderFactory</code> system66* property.67* </li>68* <li>69* Use the properties file "lib/jaxp.properties" in the JRE directory.70* This configuration file is in standard <code>java.util.Properties71* </code> format and contains the fully qualified name of the72* implementation class with the key being the system property defined73* above.74*75* The jaxp.properties file is read only once by the JAXP implementation76* and it's values are then cached for future use. If the file does not exist77* when the first attempt is made to read from it, no further attempts are78* made to check for its existence. It is not possible to change the value79* of any property in jaxp.properties after it has been read for the first time.80* </li>81* <li>82* Uses the service-provider loading facilities, defined by the83* {@link java.util.ServiceLoader} class, to attempt to locate and load an84* implementation of the service using the {@linkplain85* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:86* the service-provider loading facility will use the {@linkplain87* java.lang.Thread#getContextClassLoader() current thread's context class loader}88* to attempt to load the service. If the context class89* loader is null, the {@linkplain90* ClassLoader#getSystemClassLoader() system class loader} will be used.91* </li>92* <li>93* Otherwise, the system-default implementation is returned.94* </li>95* </ul>96*97* Once an application has obtained a reference to a98* <code>DocumentBuilderFactory</code> it can use the factory to99* configure and obtain parser instances.100*101*102* <h2>Tip for Trouble-shooting</h2>103* <p>Setting the <code>jaxp.debug</code> system property will cause104* this method to print a lot of debug messages105* to <code>System.err</code> about what it is doing and where it is looking at.</p>106*107* <p> If you have problems loading {@link DocumentBuilder}s, try:</p>108* <pre>109* java -Djaxp.debug=1 YourProgram ....110* </pre>111*112* @return New instance of a <code>DocumentBuilderFactory</code>113*114* @throws FactoryConfigurationError in case of {@linkplain115* java.util.ServiceConfigurationError service configuration error} or if116* the implementation is not available or cannot be instantiated.117*/118public static DocumentBuilderFactory newInstance() {119return FactoryFinder.find(120/* The default property name according to the JAXP spec */121DocumentBuilderFactory.class, // "javax.xml.parsers.DocumentBuilderFactory"122/* The fallback implementation class name */123"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");124}125126/**127* <p>Obtain a new instance of a <code>DocumentBuilderFactory</code> from class name.128* This function is useful when there are multiple providers in the classpath.129* It gives more control to the application as it can specify which provider130* should be loaded.</p>131*132* <p>Once an application has obtained a reference to a <code>DocumentBuilderFactory</code>133* it can use the factory to configure and obtain parser instances.</p>134*135*136* <h2>Tip for Trouble-shooting</h2>137* <p>Setting the <code>jaxp.debug</code> system property will cause138* this method to print a lot of debug messages139* to <code>System.err</code> about what it is doing and where it is looking at.</p>140*141* <p> If you have problems try:</p>142* <pre>143* java -Djaxp.debug=1 YourProgram ....144* </pre>145*146* @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.parsers.DocumentBuilderFactory</code>.147*148* @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>149* current <code>Thread</code>'s context classLoader is used to load the factory class.150*151* @return New instance of a <code>DocumentBuilderFactory</code>152*153* @throws FactoryConfigurationError if <code>factoryClassName</code> is <code>null</code>, or154* the factory class cannot be loaded, instantiated.155*156* @see #newInstance()157*158* @since 1.6159*/160public static DocumentBuilderFactory newInstance(String factoryClassName, ClassLoader classLoader){161//do not fallback if given classloader can't find the class, throw exception162return FactoryFinder.newInstance(DocumentBuilderFactory.class,163factoryClassName, classLoader, false);164}165166/**167* Creates a new instance of a {@link javax.xml.parsers.DocumentBuilder}168* using the currently configured parameters.169*170* @return A new instance of a DocumentBuilder.171*172* @throws ParserConfigurationException if a DocumentBuilder173* cannot be created which satisfies the configuration requested.174*/175176public abstract DocumentBuilder newDocumentBuilder()177throws ParserConfigurationException;178179180/**181* Specifies that the parser produced by this code will182* provide support for XML namespaces. By default the value of this is set183* to <code>false</code>184*185* @param awareness true if the parser produced will provide support186* for XML namespaces; false otherwise.187*/188189public void setNamespaceAware(boolean awareness) {190this.namespaceAware = awareness;191}192193/**194* Specifies that the parser produced by this code will195* validate documents as they are parsed. By default the value of this196* is set to <code>false</code>.197*198* <p>199* Note that "the validation" here means200* <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating201* parser</a> as defined in the XML recommendation.202* In other words, it essentially just controls the DTD validation.203* (except the legacy two properties defined in JAXP 1.2.)204* </p>205*206* <p>207* To use modern schema languages such as W3C XML Schema or208* RELAX NG instead of DTD, you can configure your parser to be209* a non-validating parser by leaving the {@link #setValidating(boolean)}210* method <code>false</code>, then use the {@link #setSchema(Schema)}211* method to associate a schema to a parser.212* </p>213*214* @param validating true if the parser produced will validate documents215* as they are parsed; false otherwise.216*/217218public void setValidating(boolean validating) {219this.validating = validating;220}221222/**223* Specifies that the parsers created by this factory must eliminate224* whitespace in element content (sometimes known loosely as225* 'ignorable whitespace') when parsing XML documents (see XML Rec226* 2.10). Note that only whitespace which is directly contained within227* element content that has an element only content model (see XML228* Rec 3.2.1) will be eliminated. Due to reliance on the content model229* this setting requires the parser to be in validating mode. By default230* the value of this is set to <code>false</code>.231*232* @param whitespace true if the parser created must eliminate whitespace233* in the element content when parsing XML documents;234* false otherwise.235*/236237public void setIgnoringElementContentWhitespace(boolean whitespace) {238this.whitespace = whitespace;239}240241/**242* Specifies that the parser produced by this code will243* expand entity reference nodes. By default the value of this is set to244* <code>true</code>245*246* @param expandEntityRef true if the parser produced will expand entity247* reference nodes; false otherwise.248*/249250public void setExpandEntityReferences(boolean expandEntityRef) {251this.expandEntityRef = expandEntityRef;252}253254/**255* <p>Specifies that the parser produced by this code will256* ignore comments. By default the value of this is set to <code>false257* </code>.</p>258*259* @param ignoreComments <code>boolean</code> value to ignore comments during processing260*/261262public void setIgnoringComments(boolean ignoreComments) {263this.ignoreComments = ignoreComments;264}265266/**267* Specifies that the parser produced by this code will268* convert CDATA nodes to Text nodes and append it to the269* adjacent (if any) text node. By default the value of this is set to270* <code>false</code>271*272* @param coalescing true if the parser produced will convert CDATA nodes273* to Text nodes and append it to the adjacent (if any)274* text node; false otherwise.275*/276277public void setCoalescing(boolean coalescing) {278this.coalescing = coalescing;279}280281/**282* Indicates whether or not the factory is configured to produce283* parsers which are namespace aware.284*285* @return true if the factory is configured to produce parsers which286* are namespace aware; false otherwise.287*/288289public boolean isNamespaceAware() {290return namespaceAware;291}292293/**294* Indicates whether or not the factory is configured to produce295* parsers which validate the XML content during parse.296*297* @return true if the factory is configured to produce parsers298* which validate the XML content during parse; false otherwise.299*/300301public boolean isValidating() {302return validating;303}304305/**306* Indicates whether or not the factory is configured to produce307* parsers which ignore ignorable whitespace in element content.308*309* @return true if the factory is configured to produce parsers310* which ignore ignorable whitespace in element content;311* false otherwise.312*/313314public boolean isIgnoringElementContentWhitespace() {315return whitespace;316}317318/**319* Indicates whether or not the factory is configured to produce320* parsers which expand entity reference nodes.321*322* @return true if the factory is configured to produce parsers323* which expand entity reference nodes; false otherwise.324*/325326public boolean isExpandEntityReferences() {327return expandEntityRef;328}329330/**331* Indicates whether or not the factory is configured to produce332* parsers which ignores comments.333*334* @return true if the factory is configured to produce parsers335* which ignores comments; false otherwise.336*/337338public boolean isIgnoringComments() {339return ignoreComments;340}341342/**343* Indicates whether or not the factory is configured to produce344* parsers which converts CDATA nodes to Text nodes and appends it to345* the adjacent (if any) Text node.346*347* @return true if the factory is configured to produce parsers348* which converts CDATA nodes to Text nodes and appends it to349* the adjacent (if any) Text node; false otherwise.350*/351352public boolean isCoalescing() {353return coalescing;354}355356/**357* Allows the user to set specific attributes on the underlying358* implementation.359* <p>360* All implementations that implement JAXP 1.5 or newer are required to361* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and362* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} properties.363* </p>364* <ul>365* <li>366* <p>367* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property368* restricts the access to external DTDs, external Entity References to the369* protocols specified by the property.370* If access is denied during parsing due to the restriction of this property,371* {@link org.xml.sax.SAXException} will be thrown by the parse methods defined by372* {@link javax.xml.parsers.DocumentBuilder}.373* </p>374* <p>375* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property376* restricts the access to external Schema set by the schemaLocation attribute to377* the protocols specified by the property. If access is denied during parsing378* due to the restriction of this property, {@link org.xml.sax.SAXException}379* will be thrown by the parse methods defined by380* {@link javax.xml.parsers.DocumentBuilder}.381* </p>382* </li>383* </ul>384*385* @param name The name of the attribute.386* @param value The value of the attribute.387*388* @throws IllegalArgumentException thrown if the underlying389* implementation doesn't recognize the attribute.390*/391public abstract void setAttribute(String name, Object value)392throws IllegalArgumentException;393394/**395* Allows the user to retrieve specific attributes on the underlying396* implementation.397*398* @param name The name of the attribute.399*400* @return value The value of the attribute.401*402* @throws IllegalArgumentException thrown if the underlying403* implementation doesn't recognize the attribute.404*/405public abstract Object getAttribute(String name)406throws IllegalArgumentException;407408/**409* <p>Set a feature for this <code>DocumentBuilderFactory</code> and <code>DocumentBuilder</code>s created by this factory.</p>410*411* <p>412* Feature names are fully qualified {@link java.net.URI}s.413* Implementations may define their own features.414* A {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the415* <code>DocumentBuilder</code>s it creates cannot support the feature.416* It is possible for a <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.417* </p>418*419* <p>420* All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.421* When the feature is:</p>422* <ul>423* <li>424* <code>true</code>: the implementation will limit XML processing to conform to implementation limits.425* Examples include enity expansion limits and XML Schema constructs that would consume large amounts of resources.426* If XML processing is limited for security reasons, it will be reported via a call to the registered427* {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.428* See {@link DocumentBuilder#setErrorHandler(org.xml.sax.ErrorHandler errorHandler)}.429* </li>430* <li>431* <code>false</code>: the implementation will processing XML according to the XML specifications without432* regard to possible implementation limits.433* </li>434* </ul>435*436* @param name Feature name.437* @param value Is feature state <code>true</code> or <code>false</code>.438*439* @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code> or the <code>DocumentBuilder</code>s440* it creates cannot support this feature.441* @throws NullPointerException If the <code>name</code> parameter is null.442*/443public abstract void setFeature(String name, boolean value)444throws ParserConfigurationException;445446/**447* <p>Get the state of the named feature.</p>448*449* <p>450* Feature names are fully qualified {@link java.net.URI}s.451* Implementations may define their own features.452* An {@link ParserConfigurationException} is thrown if this <code>DocumentBuilderFactory</code> or the453* <code>DocumentBuilder</code>s it creates cannot support the feature.454* It is possible for an <code>DocumentBuilderFactory</code> to expose a feature value but be unable to change its state.455* </p>456*457* @param name Feature name.458*459* @return State of the named feature.460*461* @throws ParserConfigurationException if this <code>DocumentBuilderFactory</code>462* or the <code>DocumentBuilder</code>s it creates cannot support this feature.463*/464public abstract boolean getFeature(String name)465throws ParserConfigurationException;466467468/**469* Gets the {@link Schema} object specified through470* the {@link #setSchema(Schema schema)} method.471*472* @return473* the {@link Schema} object that was last set through474* the {@link #setSchema(Schema)} method, or null475* if the method was not invoked since a {@link DocumentBuilderFactory}476* is created.477*478* @throws UnsupportedOperationException When implementation does not479* override this method.480*481* @since 1.5482*/483public Schema getSchema() {484throw new UnsupportedOperationException(485"This parser does not support specification \""486+ this.getClass().getPackage().getSpecificationTitle()487+ "\" version \""488+ this.getClass().getPackage().getSpecificationVersion()489+ "\""490);491492}493494/**495* <p>Set the {@link Schema} to be used by parsers created496* from this factory.497*498* <p>499* When a {@link Schema} is non-null, a parser will use a validator500* created from it to validate documents before it passes information501* down to the application.502*503* <p>When errors are found by the validator, the parser is responsible504* to report them to the user-specified {@link org.xml.sax.ErrorHandler}505* (or if the error handler is not set, ignore them or throw them), just506* like any other errors found by the parser itself.507* In other words, if the user-specified {@link org.xml.sax.ErrorHandler}508* is set, it must receive those errors, and if not, they must be509* treated according to the implementation specific510* default error handling rules.511*512* <p>513* A validator may modify the outcome of a parse (for example by514* adding default values that were missing in documents), and a parser515* is responsible to make sure that the application will receive516* modified DOM trees.517*518* <p>519* Initialy, null is set as the {@link Schema}.520*521* <p>522* This processing will take effect even if523* the {@link #isValidating()} method returns <code>false</code>.524*525* <p>It is an error to use526* the <code>http://java.sun.com/xml/jaxp/properties/schemaSource</code>527* property and/or the <code>http://java.sun.com/xml/jaxp/properties/schemaLanguage</code>528* property in conjunction with a {@link Schema} object.529* Such configuration will cause a {@link ParserConfigurationException}530* exception when the {@link #newDocumentBuilder()} is invoked.</p>531*532*533* <h4>Note for implmentors</h4>534*535* <p>536* A parser must be able to work with any {@link Schema}537* implementation. However, parsers and schemas are allowed538* to use implementation-specific custom mechanisms539* as long as they yield the result described in the specification.540* </p>541*542* @param schema <code>Schema</code> to use or <code>null</code>543* to remove a schema.544*545* @throws UnsupportedOperationException When implementation does not546* override this method.547*548* @since 1.5549*/550public void setSchema(Schema schema) {551throw new UnsupportedOperationException(552"This parser does not support specification \""553+ this.getClass().getPackage().getSpecificationTitle()554+ "\" version \""555+ this.getClass().getPackage().getSpecificationVersion()556+ "\""557);558}559560561562/**563* <p>Set state of XInclude processing.</p>564*565* <p>If XInclude markup is found in the document instance, should it be566* processed as specified in <a href="http://www.w3.org/TR/xinclude/">567* XML Inclusions (XInclude) Version 1.0</a>.</p>568*569* <p>XInclude processing defaults to <code>false</code>.</p>570*571* @param state Set XInclude processing to <code>true</code> or572* <code>false</code>573*574* @throws UnsupportedOperationException When implementation does not575* override this method.576*577* @since 1.5578*/579public void setXIncludeAware(final boolean state) {580if (state) {581throw new UnsupportedOperationException(" setXIncludeAware " +582"is not supported on this JAXP" +583" implementation or earlier: " + this.getClass());584}585}586587/**588* <p>Get state of XInclude processing.</p>589*590* @return current state of XInclude processing591*592* @throws UnsupportedOperationException When implementation does not593* override this method.594*595* @since 1.5596*/597public boolean isXIncludeAware() {598throw new UnsupportedOperationException(599"This parser does not support specification \""600+ this.getClass().getPackage().getSpecificationTitle()601+ "\" version \""602+ this.getClass().getPackage().getSpecificationVersion()603+ "\""604);605}606}607608609