Class TransformerFactory
- Direct Known Subclasses:
- SAXTransformerFactory
A TransformerFactory instance can be used to create
 Transformer and
 Templates objects.
 
The system property that determines which Factory implementation
 to create is named "javax.xml.transform.TransformerFactory".
 This property names a concrete subclass of the
 TransformerFactory abstract class. If the property is not
 defined, a platform default is be used.
- Since:
- 1.5
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedDefault constructor is protected on purpose.
- 
Method SummaryModifier and TypeMethodDescriptionabstract SourcegetAssociatedStylesheet(Source source, String media, String title, String charset) Get the stylesheet specification(s) associated with the XMLSourcedocument via the xml-stylesheet processing instruction that match the given criteria.abstract ObjectgetAttribute(String name) Allows the user to retrieve specific attributes on the underlying implementation.abstract ErrorListenerGet the error event handler for the TransformerFactory.abstract booleangetFeature(String name) Look up the value of a feature.abstract URIResolverGet the object that is used by default during the transformation to resolve URIs used in document(), xsl:import, or xsl:include.static TransformerFactoryCreates a new instance of theTransformerFactorybuiltin system-default implementation.static TransformerFactoryObtains a new instance of aTransformerFactory.static TransformerFactorynewInstance(String factoryClassName, ClassLoader classLoader) Obtain a new instance of aTransformerFactoryfrom factory class name.abstract TemplatesnewTemplates(Source source) Process the Source into a Templates object, which is a a compiled representation of the source.abstract TransformerCreate a newTransformerthat performs a copy of theSourceto theResult, i.e. the "identity transform".abstract TransformernewTransformer(Source source) Process theSourceinto aTransformerObject.abstract voidsetAttribute(String name, Object value) Allows the user to set specific attributes on the underlying implementation.abstract voidsetErrorListener(ErrorListener listener) Set the error event listener for the TransformerFactory, which is used for the processing of transformation instructions, and not for the transformation itself.abstract voidsetFeature(String name, boolean value) Set a feature for thisTransformerFactoryandTransformers orTemplates created by this factory.abstract voidsetURIResolver(URIResolver resolver) Set an object that is used by default during the transformation to resolve URIs used in document(), xsl:import, or xsl:include.
- 
Constructor Details- 
TransformerFactoryprotected TransformerFactory()Default constructor is protected on purpose.
 
- 
- 
Method Details- 
newDefaultInstanceCreates a new instance of theTransformerFactorybuiltin system-default implementation.- Returns:
- A new instance of the TransformerFactorybuiltin system-default implementation.
- Since:
- 9
 
- 
newInstanceObtains a new instance of aTransformerFactory. This method uses the JAXP Lookup Mechanism to determine theTransformerFactoryimplementation class to load.Once an application has obtained a reference to a TransformerFactory, it can use the factory to configure and obtain transformer instances.- Returns:
- new TransformerFactory instance, never null.
- Throws:
- TransformerFactoryConfigurationError- Thrown in case of service configuration error or if the implementation is not available or cannot be instantiated.
 
- 
newInstancepublic static TransformerFactory newInstance(String factoryClassName, ClassLoader classLoader) throws TransformerFactoryConfigurationError Obtain a new instance of aTransformerFactoryfrom factory class name. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded.Once an application has obtained a reference to a TransformerFactoryit can use the factory to configure and obtain transformer instances.Tip for Trouble-shootingSetting the jaxp.debugsystem property will cause this method to print a lot of debug messages toSystem.errabout what it is doing and where it is looking at.If you have problems try: java -Djaxp.debug=1 YourProgram .... - Parameters:
- factoryClassName- fully qualified factory class name that provides implementation of- javax.xml.transform.TransformerFactory.
- classLoader-- ClassLoaderused to load the factory class. If- nullcurrent- Thread's context classLoader is used to load the factory class.
- Returns:
- new TransformerFactory instance, never null.
- Throws:
- TransformerFactoryConfigurationError- if- factoryClassNameis- null, or the factory class cannot be loaded, instantiated.
- Since:
- 1.6
- See Also:
 
- 
newTransformerProcess theSourceinto aTransformerObject. TheSourceis an XSLT document that conforms to XSL Transformations (XSLT) Version 1.0. Care must be taken not to use thisTransformerin multipleThreads running concurrently. DifferentTransformerFactoriescan be used concurrently by differentThreads.- Parameters:
- source-- Sourceof XSLT document used to create- Transformer. Examples of XML- Sources include- DOMSource,- SAXSource, and- StreamSource.
- Returns:
- A Transformerobject that may be used to perform a transformation in a singleThread, nevernull.
- Throws:
- TransformerConfigurationException- Thrown if there are errors when parsing the- Sourceor it is not possible to create a- Transformerinstance.
- See Also:
 
- 
newTransformerCreate a newTransformerthat performs a copy of theSourceto theResult, i.e. the "identity transform".- Returns:
- A Transformer object that may be used to perform a transformation in a single thread, never null.
- Throws:
- TransformerConfigurationException- When it is not possible to create a- Transformerinstance.
 
- 
newTemplatesProcess the Source into a Templates object, which is a a compiled representation of the source. This Templates object may then be used concurrently across multiple threads. Creating a Templates object allows the TransformerFactory to do detailed performance optimization of transformation instructions, without penalizing runtime transformation.- Parameters:
- source- An object that holds a URL, input stream, etc.
- Returns:
- A Templates object capable of being used for transformation
   purposes, never null.
- Throws:
- TransformerConfigurationException- When parsing to construct the Templates object fails.
 
- 
getAssociatedStylesheetpublic abstract Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException Get the stylesheet specification(s) associated with the XMLSourcedocument via the xml-stylesheet processing instruction that match the given criteria. Note that it is possible to return several stylesheets, in which case they are applied as if they were a list of imports or cascades in a single stylesheet.- Parameters:
- source- The XML source document.
- media- The media attribute to be matched. May be null, in which case the preferred templates will be used (i.e. alternate = no).
- title- The value of the title attribute to match. May be null.
- charset- The value of the charset attribute to match. May be null.
- Returns:
- A SourceObjectsuitable for passing to theTransformerFactory.
- Throws:
- TransformerConfigurationException- An- Exceptionis thrown if an error occurings during parsing of the- source.
- See Also:
 
- 
setURIResolverSet an object that is used by default during the transformation to resolve URIs used in document(), xsl:import, or xsl:include.- Parameters:
- resolver- An object that implements the URIResolver interface, or null.
 
- 
getURIResolverGet the object that is used by default during the transformation to resolve URIs used in document(), xsl:import, or xsl:include.- Returns:
- The URIResolver that was set with setURIResolver.
 
- 
setFeaturepublic abstract void setFeature(String name, boolean value) throws TransformerConfigurationException Set a feature for this TransformerFactoryandTransformers orTemplates created by this factory.Feature names are fully qualified URIs. Implementations may define their own features. AnTransformerConfigurationExceptionis thrown if thisTransformerFactoryor theTransformers orTemplates it creates cannot support the feature. It is possible for anTransformerFactoryto expose a feature value but be unable to change its state.All implementations are required to support the XMLConstants.FEATURE_SECURE_PROCESSINGfeature. When the feature is:- 
     true: the implementation will limit XML processing to conform to implementation limits and behave in a secure fashion as defined by the implementation. Examples include resolving user defined style sheets and functions. If XML processing is limited for security reasons, it will be reported via a call to the registeredErrorListener.fatalError(TransformerException exception). SeesetErrorListener(ErrorListener listener).
- 
     false: the implementation will processing XML according to the XML specifications without regard to possible implementation limits.
 - Parameters:
- name- Feature name.
- value- Is feature state- trueor- false.
- Throws:
- TransformerConfigurationException- if this- TransformerFactoryor the- Transformers or- Templates it creates cannot support this feature.
- NullPointerException- If the- nameparameter is null.
 
- 
     
- 
getFeatureLook up the value of a feature.Feature names are fully qualified URIs. Implementations may define their own features.falseis returned if thisTransformerFactoryor theTransformers orTemplates it creates cannot support the feature. It is possible for anTransformerFactoryto expose a feature value but be unable to change its state.- Parameters:
- name- Feature name.
- Returns:
- The current state of the feature, trueorfalse.
- Throws:
- NullPointerException- If the- nameparameter is null.
 
- 
setAttributeAllows the user to set specific attributes on the underlying implementation. An attribute in this context is defined to be an option that the implementation provides. AnIllegalArgumentExceptionis thrown if the underlying implementation doesn't recognize the attribute.All implementations that implement JAXP 1.5 or newer are required to support the XMLConstants.ACCESS_EXTERNAL_DTDandXMLConstants.ACCESS_EXTERNAL_STYLESHEETproperties.- 
      Access to external DTDs in the source file is restricted to the protocols specified by the XMLConstants.ACCESS_EXTERNAL_DTDproperty. If access is denied during transformation due to the restriction of this property,TransformerExceptionwill be thrown byTransformer.transform(Source, Result).Access to external DTDs in the stylesheet is restricted to the protocols specified by the XMLConstants.ACCESS_EXTERNAL_DTDproperty. If access is denied during the creation of a new transformer due to the restriction of this property,TransformerConfigurationExceptionwill be thrown by thenewTransformer(Source)method.Access to external reference set by the stylesheet processing instruction, Import and Include element is restricted to the protocols specified by the XMLConstants.ACCESS_EXTERNAL_STYLESHEETproperty. If access is denied during the creation of a new transformer due to the restriction of this property,TransformerConfigurationExceptionwill be thrown by thenewTransformer(Source)method.Access to external document through XSLT document function is restricted to the protocols specified by the property. If access is denied during the transformation due to the restriction of this property, TransformerExceptionwill be thrown by theTransformer.transform(Source, Result)method.
 - Parameters:
- name- The name of the attribute.
- value- The value of the attribute.
- Throws:
- IllegalArgumentException- When implementation does not recognize the attribute.
 
- 
      
- 
getAttributeAllows the user to retrieve specific attributes on the underlying implementation. AnIllegalArgumentExceptionis thrown if the underlying implementation doesn't recognize the attribute.- Parameters:
- name- The name of the attribute.
- Returns:
- value The value of the attribute.
- Throws:
- IllegalArgumentException- When implementation does not recognize the attribute.
 
- 
setErrorListenerSet the error event listener for the TransformerFactory, which is used for the processing of transformation instructions, and not for the transformation itself. AnIllegalArgumentExceptionis thrown if theErrorListenerlistener isnull.- Parameters:
- listener- The new error listener.
- Throws:
- IllegalArgumentException- When- listeneris- null
 
- 
getErrorListenerGet the error event handler for the TransformerFactory.- Returns:
- The current error handler, which should never be null.
 
 
-