Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/transform/Transformer.java
32285 views
/*1* Copyright (c) 2000, 2005, 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;2627import java.util.Properties;2829/**30* An instance of this abstract class can transform a31* source tree into a result tree.32*33* <p>An instance of this class can be obtained with the34* {@link TransformerFactory#newTransformer TransformerFactory.newTransformer}35* method. This instance may then be used to process XML from a36* variety of sources and write the transformation output to a37* variety of sinks.</p>38*39* <p>An object of this class may not be used in multiple threads40* running concurrently. Different Transformers may be used41* concurrently by different threads.</p>42*43* <p>A <code>Transformer</code> may be used multiple times. Parameters and44* output properties are preserved across transformations.</p>45*46* @author <a href="[email protected]">Jeff Suttor</a>47*/48public abstract class Transformer {4950/**51* Default constructor is protected on purpose.52*/53protected Transformer() { }5455/**56* <p>Reset this <code>Transformer</code> to its original configuration.</p>57*58* <p><code>Transformer</code> is reset to the same state as when it was created with59* {@link TransformerFactory#newTransformer()},60* {@link TransformerFactory#newTransformer(Source source)} or61* {@link Templates#newTransformer()}.62* <code>reset()</code> is designed to allow the reuse of existing <code>Transformer</code>s63* thus saving resources associated with the creation of new <code>Transformer</code>s.</p>64*65* <p>The reset <code>Transformer</code> is not guaranteed to have the same {@link URIResolver}66* or {@link ErrorListener} <code>Object</code>s, e.g. {@link Object#equals(Object obj)}.67* It is guaranteed to have a functionally equal <code>URIResolver</code>68* and <code>ErrorListener</code>.</p>69*70* @throws UnsupportedOperationException When implementation does not71* override this method.72*73* @since 1.574*/75public void reset() {7677// implementors should override this method78throw new UnsupportedOperationException(79"This Transformer, \"" + this.getClass().getName() + "\", does not support the reset functionality."80+ " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""81+ " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""82);83}8485/**86* <p>Transform the XML <code>Source</code> to a <code>Result</code>.87* Specific transformation behavior is determined by the settings of the88* <code>TransformerFactory</code> in effect when the89* <code>Transformer</code> was instantiated and any modifications made to90* the <code>Transformer</code> instance.</p>91*92* <p>An empty <code>Source</code> is represented as an empty document93* as constructed by {@link javax.xml.parsers.DocumentBuilder#newDocument()}.94* The result of transforming an empty <code>Source</code> depends on95* the transformation behavior; it is not always an empty96* <code>Result</code>.</p>97*98* @param xmlSource The XML input to transform.99* @param outputTarget The <code>Result</code> of transforming the100* <code>xmlSource</code>.101*102* @throws TransformerException If an unrecoverable error occurs103* during the course of the transformation.104*/105public abstract void transform(Source xmlSource, Result outputTarget)106throws TransformerException;107108/**109* Add a parameter for the transformation.110*111* <p>Pass a qualified name as a two-part string, the namespace URI112* enclosed in curly braces ({}), followed by the local name. If the113* name has a null URL, the String only contain the local name. An114* application can safely check for a non-null URI by testing to see if the115* first character of the name is a '{' character.</p>116* <p>For example, if a URI and local name were obtained from an element117* defined with <xyz:foo118* xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,119* then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".120* Note that no prefix is used.</p>121*122* @param name The name of the parameter, which may begin with a123* namespace URI in curly braces ({}).124* @param value The value object. This can be any valid Java object. It is125* up to the processor to provide the proper object coersion or to simply126* pass the object on for use in an extension.127*128* @throws NullPointerException If value is null.129*/130public abstract void setParameter(String name, Object value);131132/**133* Get a parameter that was explicitly set with setParameter.134*135* <p>This method does not return a default parameter value, which136* cannot be determined until the node context is evaluated during137* the transformation process.138*139* @param name of <code>Object</code> to get140*141* @return A parameter that has been set with setParameter.142*/143public abstract Object getParameter(String name);144145/**146* <p>Set a list of parameters.</p>147*148* <p>Note that the list of parameters is specified as a149* <code>Properties</code> <code>Object</code> which limits the parameter150* values to <code>String</code>s. Multiple calls to151* {@link #setParameter(String name, Object value)} should be used when the152* desired values are non-<code>String</code> <code>Object</code>s.153* The parameter names should conform as specified in154* {@link #setParameter(String name, Object value)}.155* An <code>IllegalArgumentException</code> is thrown if any names do not156* conform.</p>157*158* <p>New parameters in the list are added to any existing parameters.159* If the name of a new parameter is equal to the name of an existing160* parameter as determined by {@link java.lang.Object#equals(Object obj)},161* the existing parameter is set to the new value.</p>162*163* @param params Parameters to set.164*165* @throws IllegalArgumentException If any parameter names do not conform166* to the naming rules.167*/168169/**170* Clear all parameters set with setParameter.171*/172public abstract void clearParameters();173174/**175* Set an object that will be used to resolve URIs used in176* document().177*178* <p>If the resolver argument is null, the URIResolver value will179* be cleared and the transformer will no longer have a resolver.</p>180*181* @param resolver An object that implements the URIResolver interface,182* or null.183*/184public abstract void setURIResolver(URIResolver resolver);185186/**187* Get an object that will be used to resolve URIs used in188* document().189*190* @return An object that implements the URIResolver interface,191* or null.192*/193public abstract URIResolver getURIResolver();194195/**196* Set the output properties for the transformation. These197* properties will override properties set in the Templates198* with xsl:output.199*200* <p>If argument to this function is null, any properties201* previously set are removed, and the value will revert to the value202* defined in the templates object.</p>203*204* <p>Pass a qualified property key name as a two-part string, the namespace205* URI enclosed in curly braces ({}), followed by the local name. If the206* name has a null URL, the String only contain the local name. An207* application can safely check for a non-null URI by testing to see if the208* first character of the name is a '{' character.</p>209* <p>For example, if a URI and local name were obtained from an element210* defined with <xyz:foo211* xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,212* then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".213* Note that no prefix is used.</p>214* An <code>IllegalArgumentException</code> is thrown if any of the215* argument keys are not recognized and are not namespace qualified.216*217* @param oformat A set of output properties that will be218* used to override any of the same properties in affect219* for the transformation.220*221* @throws IllegalArgumentException When keys are not recognized and222* are not namespace qualified.223*224* @see javax.xml.transform.OutputKeys225* @see java.util.Properties226*227*/228public abstract void setOutputProperties(Properties oformat);229230/**231* <p>Get a copy of the output properties for the transformation.</p>232*233* <p>The properties returned should contain properties set by the user,234* and properties set by the stylesheet, and these properties235* are "defaulted" by default properties specified by236* <a href="http://www.w3.org/TR/xslt#output">section 16 of the237* XSL Transformations (XSLT) W3C Recommendation</a>. The properties that238* were specifically set by the user or the stylesheet should be in the base239* Properties list, while the XSLT default properties that were not240* specifically set should be the default Properties list. Thus,241* getOutputProperties().getProperty(String key) will obtain any242* property in that was set by {@link #setOutputProperty},243* {@link #setOutputProperties}, in the stylesheet, <em>or</em> the default244* properties, while245* getOutputProperties().get(String key) will only retrieve properties246* that were explicitly set by {@link #setOutputProperty},247* {@link #setOutputProperties}, or in the stylesheet.</p>248*249* <p>Note that mutation of the Properties object returned will not250* effect the properties that the transformer contains.</p>251*252* <p>If any of the argument keys are not recognized and are not253* namespace qualified, the property will be ignored and not returned.254* In other words the behaviour is not orthogonal with255* {@link #setOutputProperties setOutputProperties}.</p>256*257* @return A copy of the set of output properties in effect for258* the next transformation.259*260* @see javax.xml.transform.OutputKeys261* @see java.util.Properties262* @see <a href="http://www.w3.org/TR/xslt#output">263* XSL Transformations (XSLT) Version 1.0</a>264*/265public abstract Properties getOutputProperties();266267/**268* Set an output property that will be in effect for the269* transformation.270*271* <p>Pass a qualified property name as a two-part string, the namespace URI272* enclosed in curly braces ({}), followed by the local name. If the273* name has a null URL, the String only contain the local name. An274* application can safely check for a non-null URI by testing to see if the275* first character of the name is a '{' character.</p>276* <p>For example, if a URI and local name were obtained from an element277* defined with <xyz:foo278* xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>,279* then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo".280* Note that no prefix is used.</p>281*282* <p>The Properties object that was passed to {@link #setOutputProperties}283* won't be effected by calling this method.</p>284*285* @param name A non-null String that specifies an output286* property name, which may be namespace qualified.287* @param value The non-null string value of the output property.288*289* @throws IllegalArgumentException If the property is not supported, and is290* not qualified with a namespace.291*292* @see javax.xml.transform.OutputKeys293*/294public abstract void setOutputProperty(String name, String value)295throws IllegalArgumentException;296297/**298* <p>Get an output property that is in effect for the transformer.</p>299*300* <p>If a property has been set using {@link #setOutputProperty},301* that value will be returned. Otherwise, if a property is explicitly302* specified in the stylesheet, that value will be returned. If303* the value of the property has been defaulted, that is, if no304* value has been set explicitly either with {@link #setOutputProperty} or305* in the stylesheet, the result may vary depending on306* implementation and input stylesheet.</p>307*308* @param name A non-null String that specifies an output309* property name, which may be namespace qualified.310*311* @return The string value of the output property, or null312* if no property was found.313*314* @throws IllegalArgumentException If the property is not supported.315*316* @see javax.xml.transform.OutputKeys317*/318public abstract String getOutputProperty(String name)319throws IllegalArgumentException;320321/**322* Set the error event listener in effect for the transformation.323*324* @param listener The new error listener.325*326* @throws IllegalArgumentException if listener is null.327*/328public abstract void setErrorListener(ErrorListener listener)329throws IllegalArgumentException;330331/**332* Get the error event handler in effect for the transformation.333* Implementations must provide a default error listener.334*335* @return The current error handler, which should never be null.336*/337public abstract ErrorListener getErrorListener();338}339340341