Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/TransformerFactory.java
32285 views
/*1* Copyright (c) 2000, 2017, 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.transform;2627/**28* <p>A TransformerFactory instance can be used to create29* {@link javax.xml.transform.Transformer} and30* {@link javax.xml.transform.Templates} objects.</p>31*32* <p>The system property that determines which Factory implementation33* to create is named <code>"javax.xml.transform.TransformerFactory"</code>.34* This property names a concrete subclass of the35* <code>TransformerFactory</code> abstract class. If the property is not36* defined, a platform default is be used.</p>37*38* @author <a href="mailto:[email protected]">Jeff Suttor</a>39* @author <a href="mailto:[email protected]">Neeraj Bajaj</a>40*41* @since 1.542*/43public abstract class TransformerFactory {4445/**46* Default constructor is protected on purpose.47*/48protected TransformerFactory() { }49505152/**53* <p>Obtain a new instance of a <code>TransformerFactory</code>.54* This static method creates a new factory instance.</p>55* <p>This method uses the following ordered lookup procedure to determine56* the <code>TransformerFactory</code> implementation class to57* load:</p>58* <ul>59* <li>60* Use the <code>javax.xml.transform.TransformerFactory</code> system61* property.62* </li>63* <li>64* Use the properties file "lib/jaxp.properties" in the JRE directory.65* This configuration file is in standard <code>java.util.Properties66* </code> format and contains the fully qualified name of the67* implementation class with the key being the system property defined68* above.69* <br>70* The jaxp.properties file is read only once by the JAXP implementation71* and it's values are then cached for future use. If the file does not exist72* when the first attempt is made to read from it, no further attempts are73* made to check for its existence. It is not possible to change the value74* of any property in jaxp.properties after it has been read for the first time.75* </li>76* <li>77* Use the service-provider loading facilities, defined by the78* {@link java.util.ServiceLoader} class, to attempt to locate and load an79* implementation of the service using the {@linkplain80* java.util.ServiceLoader#load(java.lang.Class) default loading mechanism}:81* the service-provider loading facility will use the {@linkplain82* java.lang.Thread#getContextClassLoader() current thread's context class loader}83* to attempt to load the service. If the context class84* loader is null, the {@linkplain85* ClassLoader#getSystemClassLoader() system class loader} will be used.86* </li>87* <li>88* Otherwise, the system-default implementation is returned.89* </li>90* </ul>91*92* <p>Once an application has obtained a reference to a <code>93* TransformerFactory</code> it can use the factory to configure94* and obtain transformer instances.</p>95*96* @return new TransformerFactory instance, never null.97*98* @throws TransformerFactoryConfigurationError Thrown in case of {@linkplain99* java.util.ServiceConfigurationError service configuration error} or if100* the implementation is not available or cannot be instantiated.101*/102public static TransformerFactory newInstance()103throws TransformerFactoryConfigurationError {104105return FactoryFinder.find(106/* The default property name according to the JAXP spec */107TransformerFactory.class,108/* The fallback implementation class name, XSLTC */109"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");110}111112/**113* <p>Obtain a new instance of a <code>TransformerFactory</code> from factory class name.114* This function is useful when there are multiple providers in the classpath.115* It gives more control to the application as it can specify which provider116* should be loaded.</p>117*118* <p>Once an application has obtained a reference to a <code>119* TransformerFactory</code> it can use the factory to configure120* and obtain transformer instances.</p>121*122* <h2>Tip for Trouble-shooting</h2>123* <p>Setting the <code>jaxp.debug</code> system property will cause124* this method to print a lot of debug messages125* to <code>System.err</code> about what it is doing and where it is looking at.</p>126*127* <p> If you have problems try:</p>128* <pre>129* java -Djaxp.debug=1 YourProgram ....130* </pre>131*132* @param factoryClassName fully qualified factory class name that provides implementation of <code>javax.xml.transform.TransformerFactory</code>.133*134* @param classLoader <code>ClassLoader</code> used to load the factory class. If <code>null</code>135* current <code>Thread</code>'s context classLoader is used to load the factory class.136*137* @return new TransformerFactory instance, never null.138*139* @throws TransformerFactoryConfigurationError140* if <code>factoryClassName</code> is <code>null</code>, or141* the factory class cannot be loaded, instantiated.142*143* @see #newInstance()144*145* @since 1.6146*/147public static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader)148throws TransformerFactoryConfigurationError{149150//do not fallback if given classloader can't find the class, throw exception151return FactoryFinder.newInstance(TransformerFactory.class,152factoryClassName, classLoader, false);153}154/**155* <p>Process the <code>Source</code> into a <code>Transformer</code>156* <code>Object</code>. The <code>Source</code> is an XSLT document that157* conforms to <a href="http://www.w3.org/TR/xslt">158* XSL Transformations (XSLT) Version 1.0</a>. Care must159* be taken not to use this <code>Transformer</code> in multiple160* <code>Thread</code>s running concurrently.161* Different <code>TransformerFactories</code> can be used concurrently by162* different <code>Thread</code>s.</p>163*164* @param source <code>Source </code> of XSLT document used to create165* <code>Transformer</code>.166* Examples of XML <code>Source</code>s include167* {@link javax.xml.transform.dom.DOMSource DOMSource},168* {@link javax.xml.transform.sax.SAXSource SAXSource}, and169* {@link javax.xml.transform.stream.StreamSource StreamSource}.170*171* @return A <code>Transformer</code> object that may be used to perform172* a transformation in a single <code>Thread</code>, never173* <code>null</code>.174*175* @throws TransformerConfigurationException Thrown if there are errors when176* parsing the <code>Source</code> or it is not possible to create a177* <code>Transformer</code> instance.178*179* @see <a href="http://www.w3.org/TR/xslt">180* XSL Transformations (XSLT) Version 1.0</a>181*/182public abstract Transformer newTransformer(Source source)183throws TransformerConfigurationException;184185/**186* <p>Create a new <code>Transformer</code> that performs a copy187* of the <code>Source</code> to the <code>Result</code>.188* i.e. the "<em>identity transform</em>".</p>189*190* @return A Transformer object that may be used to perform a transformation191* in a single thread, never null.192*193* @throws TransformerConfigurationException When it is not194* possible to create a <code>Transformer</code> instance.195*/196public abstract Transformer newTransformer()197throws TransformerConfigurationException;198199/**200* Process the Source into a Templates object, which is a201* a compiled representation of the source. This Templates object202* may then be used concurrently across multiple threads. Creating203* a Templates object allows the TransformerFactory to do detailed204* performance optimization of transformation instructions, without205* penalizing runtime transformation.206*207* @param source An object that holds a URL, input stream, etc.208*209* @return A Templates object capable of being used for transformation210* purposes, never <code>null</code>.211*212* @throws TransformerConfigurationException When parsing to213* construct the Templates object fails.214*/215public abstract Templates newTemplates(Source source)216throws TransformerConfigurationException;217218/**219* <p>Get the stylesheet specification(s) associated with the220* XML <code>Source</code> document via the221* <a href="http://www.w3.org/TR/xml-stylesheet/">222* xml-stylesheet processing instruction</a> that match the given criteria.223* Note that it is possible to return several stylesheets, in which case224* they are applied as if they were a list of imports or cascades in a225* single stylesheet.</p>226*227* @param source The XML source document.228* @param media The media attribute to be matched. May be null, in which229* case the prefered templates will be used (i.e. alternate = no).230* @param title The value of the title attribute to match. May be null.231* @param charset The value of the charset attribute to match. May be null.232*233* @return A <code>Source</code> <code>Object</code> suitable for passing234* to the <code>TransformerFactory</code>.235*236* @throws TransformerConfigurationException An <code>Exception</code>237* is thrown if an error occurings during parsing of the238* <code>source</code>.239*240* @see <a href="http://www.w3.org/TR/xml-stylesheet/">241* Associating Style Sheets with XML documents Version 1.0</a>242*/243public abstract Source getAssociatedStylesheet(244Source source,245String media,246String title,247String charset)248throws TransformerConfigurationException;249250/**251* Set an object that is used by default during the transformation252* to resolve URIs used in document(), xsl:import, or xsl:include.253*254* @param resolver An object that implements the URIResolver interface,255* or null.256*/257public abstract void setURIResolver(URIResolver resolver);258259/**260* Get the object that is used by default during the transformation261* to resolve URIs used in document(), xsl:import, or xsl:include.262*263* @return The URIResolver that was set with setURIResolver.264*/265public abstract URIResolver getURIResolver();266267//======= CONFIGURATION METHODS =======268269/**270* <p>Set a feature for this <code>TransformerFactory</code> and <code>Transformer</code>s271* or <code>Template</code>s created by this factory.</p>272*273* <p>274* Feature names are fully qualified {@link java.net.URI}s.275* Implementations may define their own features.276* An {@link TransformerConfigurationException} is thrown if this <code>TransformerFactory</code> or the277* <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.278* It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.279* </p>280*281* <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.282* When the feature is:</p>283* <ul>284* <li>285* <code>true</code>: the implementation will limit XML processing to conform to implementation limits286* and behave in a secure fashion as defined by the implementation.287* Examples include resolving user defined style sheets and functions.288* If XML processing is limited for security reasons, it will be reported via a call to the registered289* {@link ErrorListener#fatalError(TransformerException exception)}.290* See {@link #setErrorListener(ErrorListener listener)}.291* </li>292* <li>293* <code>false</code>: the implementation will processing XML according to the XML specifications without294* regard to possible implementation limits.295* </li>296* </ul>297*298* @param name Feature name.299* @param value Is feature state <code>true</code> or <code>false</code>.300*301* @throws TransformerConfigurationException if this <code>TransformerFactory</code>302* or the <code>Transformer</code>s or <code>Template</code>s it creates cannot support this feature.303* @throws NullPointerException If the <code>name</code> parameter is null.304*/305public abstract void setFeature(String name, boolean value)306throws TransformerConfigurationException;307308/**309* Look up the value of a feature.310*311* <p>312* Feature names are fully qualified {@link java.net.URI}s.313* Implementations may define their own features.314* <code>false</code> is returned if this <code>TransformerFactory</code> or the315* <code>Transformer</code>s or <code>Template</code>s it creates cannot support the feature.316* It is possible for an <code>TransformerFactory</code> to expose a feature value but be unable to change its state.317* </p>318*319* @param name Feature name.320*321* @return The current state of the feature, <code>true</code> or <code>false</code>.322*323* @throws NullPointerException If the <code>name</code> parameter is null.324*/325public abstract boolean getFeature(String name);326327/**328* Allows the user to set specific attributes on the underlying329* implementation. An attribute in this context is defined to330* be an option that the implementation provides.331* An <code>IllegalArgumentException</code> is thrown if the underlying332* implementation doesn't recognize the attribute.333* <p>334* All implementations that implement JAXP 1.5 or newer are required to335* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and336* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} properties.337* </p>338* <ul>339* <li>340* <p>341* Access to external DTDs in the source file is restricted to the protocols342* specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.343* If access is denied during transformation due to the restriction of this property,344* {@link javax.xml.transform.TransformerException} will be thrown by345* {@link javax.xml.transform.Transformer#transform(Source, Result)}.346* </p>347* <p>348* Access to external DTDs in the stylesheet is restricted to the protocols349* specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.350* If access is denied during the creation of a new transformer due to the351* restriction of this property,352* {@link javax.xml.transform.TransformerConfigurationException} will be thrown353* by the {@link #newTransformer(Source)} method.354* </p>355* <p>356* Access to external reference set by the stylesheet processing instruction,357* Import and Include element is restricted to the protocols specified by the358* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_STYLESHEET} property.359* If access is denied during the creation of a new transformer due to the360* restriction of this property,361* {@link javax.xml.transform.TransformerConfigurationException} will be thrown362* by the {@link #newTransformer(Source)} method.363* </p>364* <p>365* Access to external document through XSLT document function is restricted366* to the protocols specified by the property. If access is denied during367* the transformation due to the restriction of this property,368* {@link javax.xml.transform.TransformerException} will be thrown by the369* {@link javax.xml.transform.Transformer#transform(Source, Result)} method.370* </p>371* </li>372* </ul>373*374* @param name The name of the attribute.375* @param value The value of the attribute.376*377* @throws IllegalArgumentException When implementation does not378* recognize the attribute.379*/380public abstract void setAttribute(String name, Object value);381382/**383* Allows the user to retrieve specific attributes on the underlying384* implementation.385* An <code>IllegalArgumentException</code> is thrown if the underlying386* implementation doesn't recognize the attribute.387*388* @param name The name of the attribute.389*390* @return value The value of the attribute.391*392* @throws IllegalArgumentException When implementation does not393* recognize the attribute.394*/395public abstract Object getAttribute(String name);396397/**398* Set the error event listener for the TransformerFactory, which399* is used for the processing of transformation instructions,400* and not for the transformation itself.401* An <code>IllegalArgumentException</code> is thrown if the402* <code>ErrorListener</code> listener is <code>null</code>.403*404* @param listener The new error listener.405*406* @throws IllegalArgumentException When <code>listener</code> is407* <code>null</code>408*/409public abstract void setErrorListener(ErrorListener listener);410411/**412* Get the error event handler for the TransformerFactory.413*414* @return The current error handler, which should never be null.415*/416public abstract ErrorListener getErrorListener();417418}419420421