public abstract class Transformer extends Object
An instance of this class can be obtained with the
TransformerFactory.newTransformer
method. This instance may then be used to process XML from a
variety of sources and write the transformation output to a
variety of sinks.
An object of this class may not be used in multiple threads running concurrently. Different Transformers may be used concurrently by different threads.
A Transformer
may be used multiple times. Parameters and
output properties are preserved across transformations.
Modifier | Constructor and Description |
---|---|
protected |
Transformer()
Default constructor is protected on purpose.
|
Modifier and Type | Method and Description |
---|---|
abstract void |
clearParameters()
Clear all parameters set with setParameter.
|
abstract ErrorListener |
getErrorListener()
Get the error event handler in effect for the transformation.
|
abstract Properties |
getOutputProperties()
Get a copy of the output properties for the transformation.
|
abstract String |
getOutputProperty(String name)
Get an output property that is in effect for the transformer.
|
abstract Object |
getParameter(String name)
Get a parameter that was explicitly set with setParameter.
|
abstract URIResolver |
getURIResolver()
Get an object that will be used to resolve URIs used in
document().
|
void |
reset()
Reset this
Transformer to its original configuration. |
abstract void |
setErrorListener(ErrorListener listener)
Set the error event listener in effect for the transformation.
|
abstract void |
setOutputProperties(Properties oformat)
Set the output properties for the transformation.
|
abstract void |
setOutputProperty(String name,
String value)
Set an output property that will be in effect for the
transformation.
|
abstract void |
setParameter(String name,
Object value)
Add a parameter for the transformation.
|
abstract void |
setURIResolver(URIResolver resolver)
Set an object that will be used to resolve URIs used in
document().
|
abstract void |
transform(Source xmlSource,
Result outputTarget)
Transform the XML
Source to a Result . |
public void reset()
Reset this Transformer
to its original configuration.
Transformer
is reset to the same state as when it was created with
TransformerFactory.newTransformer()
,
TransformerFactory.newTransformer(Source source)
or
Templates.newTransformer()
.
reset()
is designed to allow the reuse of existing Transformer
s
thus saving resources associated with the creation of new Transformer
s.
The reset Transformer
is not guaranteed to have the same URIResolver
or ErrorListener
Object
s, e.g. Object.equals(Object obj)
.
It is guaranteed to have a functionally equal URIResolver
and ErrorListener
.
UnsupportedOperationException
- When implementation does not
override this method.public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException
Transform the XML Source
to a Result
.
Specific transformation behavior is determined by the settings of the
TransformerFactory
in effect when the
Transformer
was instantiated and any modifications made to
the Transformer
instance.
An empty Source
is represented as an empty document
as constructed by DocumentBuilder.newDocument()
.
The result of transforming an empty Source
depends on
the transformation behavior; it is not always an empty
Result
.
xmlSource
- The XML input to transform.outputTarget
- The Result
of transforming the
xmlSource
.TransformerException
- If an unrecoverable error occurs
during the course of the transformation.public abstract void setParameter(String name, Object value)
Pass a qualified name as a two-part string, the namespace URI enclosed in curly braces ({}), followed by the local name. If the name has a null URL, the String only contain the local name. An application can safely check for a non-null URI by testing to see if the first character of the name is a '{' character.
For example, if a URI and local name were obtained from an element defined with <xyz:foo xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo". Note that no prefix is used.
name
- The name of the parameter, which may begin with a
namespace URI in curly braces ({}).value
- The value object. This can be any valid Java object. It is
up to the processor to provide the proper object coersion or to simply
pass the object on for use in an extension.NullPointerException
- If value is null.public abstract Object getParameter(String name)
This method does not return a default parameter value, which cannot be determined until the node context is evaluated during the transformation process.
name
- of Object
to getpublic abstract void clearParameters()
public abstract void setURIResolver(URIResolver resolver)
If the resolver argument is null, the URIResolver value will be cleared and the transformer will no longer have a resolver.
resolver
- An object that implements the URIResolver interface,
or null.public abstract URIResolver getURIResolver()
public abstract void setOutputProperties(Properties oformat)
If argument to this function is null, any properties previously set are removed, and the value will revert to the value defined in the templates object.
Pass a qualified property key name as a two-part string, the namespace URI enclosed in curly braces ({}), followed by the local name. If the name has a null URL, the String only contain the local name. An application can safely check for a non-null URI by testing to see if the first character of the name is a '{' character.
For example, if a URI and local name were obtained from an element defined with <xyz:foo xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo". Note that no prefix is used.
AnIllegalArgumentException
is thrown if any of the
argument keys are not recognized and are not namespace qualified.oformat
- A set of output properties that will be
used to override any of the same properties in affect
for the transformation.IllegalArgumentException
- When keys are not recognized and
are not namespace qualified.OutputKeys
,
Properties
public abstract Properties getOutputProperties()
Get a copy of the output properties for the transformation.
The properties returned should contain properties set by the user,
and properties set by the stylesheet, and these properties
are "defaulted" by default properties specified by
section 16 of the
XSL Transformations (XSLT) W3C Recommendation. The properties that
were specifically set by the user or the stylesheet should be in the base
Properties list, while the XSLT default properties that were not
specifically set should be the default Properties list. Thus,
getOutputProperties().getProperty(String key) will obtain any
property in that was set by setOutputProperty(java.lang.String, java.lang.String)
,
setOutputProperties(java.util.Properties)
, in the stylesheet, or the default
properties, while
getOutputProperties().get(String key) will only retrieve properties
that were explicitly set by setOutputProperty(java.lang.String, java.lang.String)
,
setOutputProperties(java.util.Properties)
, or in the stylesheet.
Note that mutation of the Properties object returned will not effect the properties that the transformer contains.
If any of the argument keys are not recognized and are not
namespace qualified, the property will be ignored and not returned.
In other words the behaviour is not orthogonal with
setOutputProperties
.
OutputKeys
,
Properties
,
XSL Transformations (XSLT) Version 1.0public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException
Pass a qualified property name as a two-part string, the namespace URI enclosed in curly braces ({}), followed by the local name. If the name has a null URL, the String only contain the local name. An application can safely check for a non-null URI by testing to see if the first character of the name is a '{' character.
For example, if a URI and local name were obtained from an element defined with <xyz:foo xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo". Note that no prefix is used.
The Properties object that was passed to setOutputProperties(java.util.Properties)
won't be effected by calling this method.
name
- A non-null String that specifies an output
property name, which may be namespace qualified.value
- The non-null string value of the output property.IllegalArgumentException
- If the property is not supported, and is
not qualified with a namespace.OutputKeys
public abstract String getOutputProperty(String name) throws IllegalArgumentException
Get an output property that is in effect for the transformer.
If a property has been set using setOutputProperty(java.lang.String, java.lang.String)
,
that value will be returned. Otherwise, if a property is explicitly
specified in the stylesheet, that value will be returned. If
the value of the property has been defaulted, that is, if no
value has been set explicitly either with setOutputProperty(java.lang.String, java.lang.String)
or
in the stylesheet, the result may vary depending on
implementation and input stylesheet.
name
- A non-null String that specifies an output
property name, which may be namespace qualified.IllegalArgumentException
- If the property is not supported.OutputKeys
public abstract void setErrorListener(ErrorListener listener) throws IllegalArgumentException
listener
- The new error listener.IllegalArgumentException
- if listener is null.public abstract ErrorListener getErrorListener()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2024, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.