Path: blob/master/src/java.xml/share/classes/javax/xml/transform/package-info.java
40948 views
/*1* Copyright (c) 2015, 2019, 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*/2425/**26* Defines the generic APIs for processing transformation instructions,27* and performing a transformation from source to result. These interfaces have no28* dependencies on SAX or the DOM standard, and try to make as few assumptions as29* possible about the details of the source and result of a transformation. It30* achieves this by defining {@link javax.xml.transform.Source} and31* {@link javax.xml.transform.Result} interfaces.32*33* <p>34* To provide concrete classes for the user, the API defines specializations35* of the interfaces found at the root level. These interfaces are found in36* {@link javax.xml.transform.sax}, {@link javax.xml.transform.dom},37* {@link javax.xml.transform.stax}, and {@link javax.xml.transform.stream}.38*39*40* <h2>Creating Objects</h2>41*42* <p>43* The API allows a concrete {@link javax.xml.transform.TransformerFactory}44* object to be created from the static function45* {@link javax.xml.transform.TransformerFactory#newInstance}.46*47*48* <h2>Specification of Inputs and Outputs</h2>49*50* <p>51* This API defines two interface objects called {@link javax.xml.transform.Source}52* and {@link javax.xml.transform.Result}. In order to pass Source and Result53* objects to the interfaces, concrete classes must be used. The following concrete54* representations are defined for each of these objects:55* {@link javax.xml.transform.stream.StreamSource} and56* {@link javax.xml.transform.stream.StreamResult},57* {@link javax.xml.transform.stax.StAXSource} and58* {@link javax.xml.transform.stax.StAXResult}, and59* {@link javax.xml.transform.sax.SAXSource} and60* {@link javax.xml.transform.sax.SAXResult}, and61* {@link javax.xml.transform.dom.DOMSource} and62* {@link javax.xml.transform.dom.DOMResult}. Each of these objects defines a63* FEATURE string (which is in the form of a URL), which can be passed into64* {@link javax.xml.transform.TransformerFactory#getFeature} to see if the given65* type of Source or Result object is supported. For instance, to test if a66* DOMSource and a StreamResult is supported, you can apply the following test.67*68* <pre>69* <code>70* TransformerFactory tfactory = TransformerFactory.newInstance();71* if (tfactory.getFeature(DOMSource.FEATURE) &&72* tfactory.getFeature(StreamResult.FEATURE)) {73* ...74* }75* </code>76* </pre>77*78*79* <h2><a id="qname-delimiter">Qualified Name Representation</a></h2>80*81* <p>82* <a href="http://www.w3.org/TR/REC-xml-names">Namespaces</a> present something83* of a problem area when dealing with XML objects. Qualified Names appear in XML84* markup as prefixed names. But the prefixes themselves do not hold identity.85* Rather, it is the URIs that they contextually map to that hold the identity.86* Therefore, when passing a Qualified Name like "xyz:foo" among Java programs,87* one must provide a means to map "xyz" to a namespace.88*89* <p>90* One solution has been to create a "QName" object that holds the namespace URI,91* as well as the prefix and local name, but this is not always an optimal solution,92* as when, for example, you want to use unique strings as keys in a dictionary93* object. Not having a string representation also makes it difficult to specify94* a namespaced identity outside the context of an XML document.95*96* <p>97* In order to pass namespaced values to transformations, for instance when setting98* a property or a parameter on a {@link javax.xml.transform.Transformer} object,99* this specification defines that a String "qname" object parameter be passed as100* two-part string, the namespace URI enclosed in curly braces ({}), followed by101* the local name. If the qname has a null URI, then the String object only102* contains the local name. An application can safely check for a non-null URI by103* testing to see if the first character of the name is a '{' character.104*105* <p>106* For example, if a URI and local name were obtained from an element defined with107* <xyz:foo xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, then the108* Qualified Name would be "{http://xyz.foo.com/yada/baz.html}foo". Note that the109* prefix is lost.110*111*112* <h2>Result Tree Serialization</h2>113*114* <p>115* Serialization of the result tree to a stream can be controlled with the116* {@link javax.xml.transform.Transformer#setOutputProperties} and the117* {@link javax.xml.transform.Transformer#setOutputProperty} methods.118* These properties only apply to stream results, they have no effect when119* the result is a DOM tree or SAX event stream.120*121* <p>122* Strings that match the <a href="http://www.w3.org/TR/xslt#output">XSLT123* specification for xsl:output attributes</a> can be referenced from the124* {@link javax.xml.transform.OutputKeys} class. Other strings can be125* specified as well.126* If the transformer does not recognize an output key, a127* {@link java.lang.IllegalArgumentException} is thrown, unless the key name128* is <a href="#qname-delimiter">namespace qualified</a>. Output key names129* that are namespace qualified are always allowed, although they may be130* ignored by some implementations.131*132* <p>133* If all that is desired is the simple identity transformation of a134* source to a result, then {@link javax.xml.transform.TransformerFactory}135* provides a136* {@link javax.xml.transform.TransformerFactory#newTransformer()} method137* with no arguments. This method creates a Transformer that effectively copies138* the source to the result. This method may be used to create a DOM from SAX139* events or to create an XML or HTML stream from a DOM or SAX events.140*141* <h2>Exceptions and Error Reporting</h2>142*143* <p>144* The transformation API throw three types of specialized exceptions. A145* {@link javax.xml.transform.TransformerFactoryConfigurationError} is parallel to146* the {@link javax.xml.parsers.FactoryConfigurationError}, and is thrown147* when a configuration problem with the TransformerFactory exists. This error148* will typically be thrown when the transformation factory class specified with149* the "javax.xml.transform.TransformerFactory" system property cannot be found or150* instantiated.151*152* <p>153* A {@link javax.xml.transform.TransformerConfigurationException}154* may be thrown if for any reason a Transformer can not be created. A155* TransformerConfigurationException may be thrown if there is a syntax error in156* the transformation instructions, for example when157* {@link javax.xml.transform.TransformerFactory#newTransformer} is158* called.159*160* <p>161* {@link javax.xml.transform.TransformerException} is a general162* exception that occurs during the course of a transformation. A transformer163* exception may wrap another exception, and if any of the164* {@link javax.xml.transform.TransformerException#printStackTrace()}165* methods are called on it, it will produce a list of stack dumps, starting from166* the most recent. The transformer exception also provides a167* {@link javax.xml.transform.SourceLocator} object which indicates where168* in the source tree or transformation instructions the error occurred.169* {@link javax.xml.transform.TransformerException#getMessageAndLocation()}170* may be called to get an error message with location info, and171* {@link javax.xml.transform.TransformerException#getLocationAsString()}172* may be called to get just the location string.173*174* <p>175* Transformation warnings and errors are sent to an176* {@link javax.xml.transform.ErrorListener}, at which point the application may177* decide to report the error or warning, and may decide to throw an178* <code>Exception</code> for a non-fatal error. The <code>ErrorListener</code>179* may be set via {@link javax.xml.transform.TransformerFactory#setErrorListener}180* for reporting errors that have to do with syntax errors in the transformation181* instructions, or via {@link javax.xml.transform.Transformer#setErrorListener}182* to report errors that occur during the transformation. The <code>ErrorListener</code>183* on both objects will always be valid and non-<code>null</code>, whether set by184* the application or a default implementation provided by the processor.185*186*187* <h2>Resolution of URIs within a transformation</h2>188*189* <p>190* The API provides a way for URIs referenced from within the stylesheet191* instructions or within the transformation to be resolved by the calling192* application. This can be done by creating a class that implements the193* {@link javax.xml.transform.URIResolver} interface, with its one method,194* {@link javax.xml.transform.URIResolver#resolve}, and use this class to195* set the URI resolution for the transformation instructions or transformation196* with {@link javax.xml.transform.TransformerFactory#setURIResolver} or197* {@link javax.xml.transform.Transformer#setURIResolver}. The198* <code>URIResolver.resolve</code> method takes two String arguments, the URI199* found in the stylesheet instructions or built as part of the transformation200* process, and the base URI against which the first argument will be made absolute201* if the absolute URI is required.202* The returned {@link javax.xml.transform.Source} object must be usable by203* the transformer, as specified in its implemented features.204*205* @since 1.5206*/207208package javax.xml.transform;209210211