Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/parsers/SAXParserFactory.java
48505 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;28import org.xml.sax.SAXException;29import org.xml.sax.SAXNotRecognizedException;30import org.xml.sax.SAXNotSupportedException;3132/**33* Defines a factory API that enables applications to configure and34* obtain a SAX based parser to parse XML documents.35*36* @author <a href="mailto:[email protected]">Jeff Suttor</a>37* @author <a href="mailto:[email protected]">Neeraj Bajaj</a>38*39* @version $Revision: 1.9 $, $Date: 2010/05/25 16:19:44 $40*41*/42public abstract class SAXParserFactory {4344/**45* <p>Should Parsers be validating?</p>46*/47private boolean validating = false;4849/**50* <p>Should Parsers be namespace aware?</p>51*/52private boolean namespaceAware = false;5354/**55* <p>Protected constructor to force use of {@link #newInstance()}.</p>56*/57protected SAXParserFactory () {5859}6061/**62* Obtain a new instance of a <code>SAXParserFactory</code>. This63* static method creates a new factory instance64* This method uses the following ordered lookup procedure to determine65* the <code>SAXParserFactory</code> implementation class to66* load:67* <ul>68* <li>69* Use the <code>javax.xml.parsers.SAXParserFactory</code> system70* property.71* </li>72* <li>73* Use the properties file "lib/jaxp.properties" in the JRE directory.74* This configuration file is in standard <code>java.util.Properties75* </code> format and contains the fully qualified name of the76* implementation class with the key being the system property defined77* above.78*79* The jaxp.properties file is read only once by the JAXP implementation80* and it's values are then cached for future use. If the file does not exist81* when the first attempt is made to read from it, no further attempts are82* made to check for its existence. It is not possible to change the value83* of any property in jaxp.properties after it has been read for the first time.84* </li>85* <li>86* Use the service-provider loading facilities, defined by the87* {@link java.util.ServiceLoader} class, to attempt to locate and load an88* implementation of the service using the {@linkplain89* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:90* the service-provider loading facility will use the {@linkplain91* java.lang.Thread#getContextClassLoader() current thread's context class loader}92* to attempt to load the service. If the context class93* loader is null, the {@linkplain94* ClassLoader#getSystemClassLoader() system class loader} will be used.95* </li>96* <li>97* Otherwise the system-default implementation is returned.98* </li>99* </ul>100*101* Once an application has obtained a reference to a102* <code>SAXParserFactory</code> it can use the factory to103* configure and obtain parser instances.104*105*106*107* <h2>Tip for Trouble-shooting</h2>108* <p>Setting the <code>jaxp.debug</code> system property will cause109* this method to print a lot of debug messages110* to <code>System.err</code> about what it is doing and where it is looking at.</p>111*112* <p> If you have problems loading {@link SAXParser}s, try:</p>113* <pre>114* java -Djaxp.debug=1 YourProgram ....115* </pre>116*117*118* @return A new instance of a SAXParserFactory.119*120* @throws FactoryConfigurationError in case of {@linkplain121* java.util.ServiceConfigurationError service configuration error} or if122* the implementation is not available or cannot be instantiated.123*/124125public static SAXParserFactory newInstance() {126return FactoryFinder.find(127/* The default property name according to the JAXP spec */128SAXParserFactory.class,129/* The fallback implementation class name */130"com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");131}132133/**134* <p>Obtain a new instance of a <code>SAXParserFactory</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 provider137* should be loaded.</p>138*139* <p>Once an application has obtained a reference to a <code>SAXParserFactory</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 cause145* this method to print a lot of debug messages146* 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.SAXParserFactory</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>SAXParserFactory</code>159*160* @throws FactoryConfigurationError if <code>factoryClassName</code> is <code>null</code>, or161* the factory class cannot be loaded, instantiated.162*163* @see #newInstance()164*165* @since 1.6166*/167public static SAXParserFactory newInstance(String factoryClassName, ClassLoader classLoader){168//do not fallback if given classloader can't find the class, throw exception169return FactoryFinder.newInstance(SAXParserFactory.class,170factoryClassName, classLoader, false);171}172173/**174* <p>Creates a new instance of a SAXParser using the currently175* configured factory parameters.</p>176*177* @return A new instance of a SAXParser.178*179* @throws ParserConfigurationException if a parser cannot180* be created which satisfies the requested configuration.181* @throws SAXException for SAX errors.182*/183184public abstract SAXParser newSAXParser()185throws ParserConfigurationException, SAXException;186187188/**189* Specifies that the parser produced by this code will190* provide support for XML namespaces. By default the value of this is set191* to <code>false</code>.192*193* @param awareness true if the parser produced by this code will194* provide support for XML namespaces; false otherwise.195*/196197public void setNamespaceAware(boolean awareness) {198this.namespaceAware = awareness;199}200201/**202* Specifies that the parser produced by this code will203* validate documents as they are parsed. By default the value of this is204* set to <code>false</code>.205*206* <p>207* Note that "the validation" here means208* <a href="http://www.w3.org/TR/REC-xml#proc-types">a validating209* parser</a> as defined in the XML recommendation.210* In other words, it essentially just controls the DTD validation.211* (except the legacy two properties defined in JAXP 1.2.)212* </p>213*214* <p>215* To use modern schema languages such as W3C XML Schema or216* RELAX NG instead of DTD, you can configure your parser to be217* a non-validating parser by leaving the {@link #setValidating(boolean)}218* method <code>false</code>, then use the {@link #setSchema(Schema)}219* method to associate a schema to a parser.220* </p>221*222* @param validating true if the parser produced by this code will223* validate documents as they are parsed; false otherwise.224*/225226public void setValidating(boolean validating) {227this.validating = validating;228}229230/**231* Indicates whether or not the factory is configured to produce232* parsers which are namespace aware.233*234* @return true if the factory is configured to produce235* parsers which are namespace aware; false otherwise.236*/237238public boolean isNamespaceAware() {239return namespaceAware;240}241242/**243* Indicates whether or not the factory is configured to produce244* parsers which validate the XML content during parse.245*246* @return true if the factory is configured to produce parsers which validate247* the XML content during parse; false otherwise.248*/249250public boolean isValidating() {251return validating;252}253254/**255*256* <p>Sets the particular feature in the underlying implementation of257* org.xml.sax.XMLReader.258* A list of the core features and properties can be found at259* <a href="http://www.saxproject.org/">http://www.saxproject.org/</a></p>260*261* <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.262* When the feature is</p>263* <ul>264* <li>265* <code>true</code>: the implementation will limit XML processing to conform to implementation limits.266* Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources.267* If XML processing is limited for security reasons, it will be reported via a call to the registered268* {@link org.xml.sax.ErrorHandler#fatalError(SAXParseException exception)}.269* See {@link SAXParser} <code>parse</code> methods for handler specification.270* </li>271* <li>272* When the feature is <code>false</code>, the implementation will processing XML according to the XML specifications without273* regard to possible implementation limits.274* </li>275* </ul>276*277* @param name The name of the feature to be set.278* @param value The value of the feature to be set.279*280* @throws ParserConfigurationException if a parser cannot281* be created which satisfies the requested configuration.282* @throws SAXNotRecognizedException When the underlying XMLReader does283* not recognize the property name.284* @throws SAXNotSupportedException When the underlying XMLReader285* recognizes the property name but doesn't support the286* property.287* @throws NullPointerException If the <code>name</code> parameter is null.288*289* @see org.xml.sax.XMLReader#setFeature290*/291public abstract void setFeature(String name, boolean value)292throws ParserConfigurationException, SAXNotRecognizedException,293SAXNotSupportedException;294295/**296*297* <p>Returns the particular property requested for in the underlying298* implementation of org.xml.sax.XMLReader.</p>299*300* @param name The name of the property to be retrieved.301*302* @return Value of the requested property.303*304* @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.305* @throws SAXNotRecognizedException When the underlying XMLReader does not recognize the property name.306* @throws SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support the property.307*308* @see org.xml.sax.XMLReader#getProperty309*/310public abstract boolean getFeature(String name)311throws ParserConfigurationException, SAXNotRecognizedException,312SAXNotSupportedException;313314315/**316* Gets the {@link Schema} object specified through317* the {@link #setSchema(Schema schema)} method.318*319*320* @throws UnsupportedOperationException When implementation does not321* override this method322*323* @return324* the {@link Schema} object that was last set through325* the {@link #setSchema(Schema)} method, or null326* if the method was not invoked since a {@link SAXParserFactory}327* is created.328*329* @since 1.5330*/331public Schema getSchema() {332throw new UnsupportedOperationException(333"This parser does not support specification \""334+ this.getClass().getPackage().getSpecificationTitle()335+ "\" version \""336+ this.getClass().getPackage().getSpecificationVersion()337+ "\""338);339}340341/**342* <p>Set the {@link Schema} to be used by parsers created343* from this factory.</p>344*345* <p>When a {@link Schema} is non-null, a parser will use a validator346* created from it to validate documents before it passes information347* down to the application.</p>348*349* <p>When warnings/errors/fatal errors are found by the validator, the parser must350* handle them as if those errors were found by the parser itself.351* In other words, if the user-specified {@link org.xml.sax.ErrorHandler}352* is set, it must receive those errors, and if not, they must be353* treated according to the implementation specific354* default error handling rules.355*356* <p>A validator may modify the SAX event stream (for example by357* adding default values that were missing in documents), and a parser358* is responsible to make sure that the application will receive359* those modified event stream.</p>360*361* <p>Initialy, <code>null</code> is set as the {@link Schema}.</p>362*363* <p>This processing will take effect even if364* the {@link #isValidating()} method returns <code>false</code>.365*366* <p>It is an error to use367* the <code>http://java.sun.com/xml/jaxp/properties/schemaSource</code>368* property and/or the <code>http://java.sun.com/xml/jaxp/properties/schemaLanguage</code>369* property in conjunction with a non-null {@link Schema} object.370* Such configuration will cause a {@link SAXException}371* exception when those properties are set on a {@link SAXParser}.</p>372*373* <h4>Note for implementors</h4>374* <p>375* A parser must be able to work with any {@link Schema}376* implementation. However, parsers and schemas are allowed377* to use implementation-specific custom mechanisms378* as long as they yield the result described in the specification.379* </p>380*381* @param schema <code>Schema</code> to use, <code>null</code> to remove a schema.382*383* @throws UnsupportedOperationException When implementation does not384* override this method385*386* @since 1.5387*/388public void setSchema(Schema schema) {389throw new UnsupportedOperationException(390"This parser does not support specification \""391+ this.getClass().getPackage().getSpecificationTitle()392+ "\" version \""393+ this.getClass().getPackage().getSpecificationVersion()394+ "\""395);396}397398/**399* <p>Set state of XInclude processing.</p>400*401* <p>If XInclude markup is found in the document instance, should it be402* processed as specified in <a href="http://www.w3.org/TR/xinclude/">403* XML Inclusions (XInclude) Version 1.0</a>.</p>404*405* <p>XInclude processing defaults to <code>false</code>.</p>406*407* @param state Set XInclude processing to <code>true</code> or408* <code>false</code>409*410* @throws UnsupportedOperationException When implementation does not411* override this method412*413* @since 1.5414*/415public void setXIncludeAware(final boolean state) {416if (state) {417throw new UnsupportedOperationException(" setXIncludeAware " +418"is not supported on this JAXP" +419" implementation or earlier: " + this.getClass());420}421}422423/**424* <p>Get state of XInclude processing.</p>425*426* @return current state of XInclude processing427*428* @throws UnsupportedOperationException When implementation does not429* override this method430*431* @since 1.5432*/433public boolean isXIncludeAware() {434throw new UnsupportedOperationException(435"This parser does not support specification \""436+ this.getClass().getPackage().getSpecificationTitle()437+ "\" version \""438+ this.getClass().getPackage().getSpecificationVersion()439+ "\""440);441}442}443444445