Top Description Fields Constructors Methods
org.springframework.web.servlet.view.xslt

public Class XsltView

extends AbstractUrlBasedView
Class Inheritance
Imports
java.io.IOException, .InputStream, .Reader, java.util.Enumeration, .Map, .Properties, javax.xml.transform.ErrorListener, .OutputKeys, .Result, .Source, .Templates, .Transformer, .TransformerConfigurationException, .TransformerFactory, .TransformerFactoryConfigurationError, .URIResolver, javax.xml.transform.dom.DOMSource, javax.xml.transform.stream.StreamResult, .StreamSource, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.w3c.dom.Document, .Node, org.springframework.beans.BeansException, org.springframework.context.ApplicationContextException, org.springframework.core.io.Resource, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .ReflectionUtils, .StringUtils, org.springframework.util.xml.SimpleTransformErrorListener, .TransformerUtils, org.springframework.web.servlet.view.AbstractUrlBasedView, org.springframework.web.util.WebUtils

XSLT-driven View that allows for response context to be rendered as the result of an XSLT transformation.

The XSLT Source object is supplied as a parameter in the model and then detected during response rendering. Users can either specify a specific entry in the model via the sourceKey property or have Spring locate the Source object. This class also provides basic conversion of objects into Source implementations. See here for more details.

All model parameters are passed to the XSLT Transformer as parameters. In addition the user can configure output properties to be passed to the Transformer.

Authors
Rob Harrop, Juergen Hoeller
Since
2.0

Field Summary

Modifier and TypeField and Description
private Templates
private boolean
private ErrorListener
private boolean
private Properties
private String
private TransformerFactory
private Class<? extends TransformerFactory>
private URIResolver

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
closeSourceIfNecessary(Source
the XSLT Source to close (may be null)
source
)

Close the underlying resource managed by the supplied Source if applicable.

protected final void
configureIndentation(Transformer
the target transformer
transformer
)

Configure the indentation settings for the supplied Transformer.

protected void
configureResponse(Map<String, Object>
merged output Map (never null)
model
,
HttpServletResponse
current HTTP response
response
,
Transformer
the target transformer
transformer
)

Configure the supplied HttpServletResponse.

protected void
configureTransformer(Map<String, Object>
merged output Map (never null)
model
,
HttpServletResponse
current HTTP response
response
,
Transformer
the target transformer
transformer
)

Configure the supplied Transformer instance.

protected Source

Returns:

the adapted XSLT Source
convertSource
(Object
the original source object
sourceObject
)

Convert the supplied Object into an XSLT Source if the Object type is supported.

protected final void
copyModelParameters(Map<String, Object>
merged output Map (never null)
model
,
Transformer
the target transformer
transformer
)

Copy all entries from the supplied Map into the parameter set of the supplied Transformer.

protected final void
copyOutputProperties(Transformer
the target transformer
transformer
)

Copy the configured output Properties, if any, into the output property set of the supplied Transformer.

protected Result

Returns:

the XSLT Result to use
createResult
(HttpServletResponse
current HTTP response
response
)

Create the XSLT Result used to render the result of the transformation.

protected Transformer

Returns:

the Transformer object
createTransformer
(Templates
the XSLT Templates instance to create a Transformer for
templates
)

Create the Transformer instance used to prefer the XSLT transformation.

protected Class<?>[]

Returns:

the supported source types
getSourceTypes
()

Return the array of Classes that are supported when converting to an XSLT Source.

protected Source

Returns:

the Source object
getStylesheetSource
()

Get the XSLT Source for the XSLT template under the configured URL.

protected final TransformerFactory

Returns:

the TransformerFactory (never null)
getTransformerFactory
()

Return the TransformerFactory that this XsltView uses.

protected void
private Templates
loadTemplates()

Load the Templates instance for the stylesheet at the configured location.

protected Source

Returns:

the XSLT Source object (or null if none found)
locateSource
(Map<String, Object>
the merged model Map
model
)

Locate the Source object in the supplied model, converting objects as required.

protected TransformerFactory

Returns:

the new TransactionFactory instance
newTransformerFactory
(Class<? extends TransformerFactory>
the specified factory class (if any)
transformerFactoryClass
)

Instantiate a new TransformerFactory for this view.

protected void
renderMergedOutputModel(Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Subclasses must implement this method to actually render the view.

public void
setCacheTemplates(boolean cacheTemplates)

Turn on/off the caching of the XSLT Templates instance.

public void
setErrorListener(ErrorListener errorListener)

Set an implementation of the javax.xml.transform.ErrorListener interface for custom handling of transformation errors and warnings.

public void
setIndent(boolean indent)

Set whether the XSLT transformer may add additional whitespace when outputting the result tree.

public void
setOutputProperties(Properties outputProperties)

Set arbitrary transformer output properties to be applied to the stylesheet.

public void
setSourceKey(String sourceKey)

Set the name of the model attribute that represents the XSLT Source.

public void
setTransformerFactoryClass(Class<? extends TransformerFactory> transformerFactoryClass)

Specify the XSLT TransformerFactory class to use.

public void
setUriResolver(URIResolver uriResolver)

Set the URIResolver used in the transform.

Inherited from org.springframework.web.servlet.view.AbstractUrlBasedView:
afterPropertiesSetcheckResourcegetUrlisUrlRequiredsetUrltoString

Field Detail

cachedTemplatesback to summary
private Templates cachedTemplates
Annotations
@Nullable
cacheTemplatesback to summary
private boolean cacheTemplates
errorListenerback to summary
private ErrorListener errorListener
indentback to summary
private boolean indent
outputPropertiesback to summary
private Properties outputProperties
Annotations
@Nullable
sourceKeyback to summary
private String sourceKey
Annotations
@Nullable
transformerFactoryback to summary
private TransformerFactory transformerFactory
Annotations
@Nullable
transformerFactoryClassback to summary
private Class<? extends TransformerFactory> transformerFactoryClass
Annotations
@Nullable
uriResolverback to summary
private URIResolver uriResolver
Annotations
@Nullable

Constructor Detail

XsltViewback to summary
public XsltView()

Method Detail

closeSourceIfNecessaryback to summary
private void closeSourceIfNecessary(Source source)

Close the underlying resource managed by the supplied Source if applicable.

Only works for StreamSources.

Parameters
source:Source

the XSLT Source to close (may be null)

configureIndentationback to summary
protected final void configureIndentation(Transformer transformer)

Configure the indentation settings for the supplied Transformer.

Parameters
transformer:Transformer

the target transformer

See Also
org.springframework.util.xml.TransformerUtils#enableIndenting(javax.xml.transform.Transformer), org.springframework.util.xml.TransformerUtils#disableIndenting(javax.xml.transform.Transformer)
configureResponseback to summary
protected void configureResponse(Map<String, Object> model, HttpServletResponse response, Transformer transformer)

Configure the supplied HttpServletResponse.

The default implementation of this method sets the content type and encoding from the "media-type" and "encoding" output properties specified in the Transformer.

Parameters
model:Map<String, Object>

merged output Map (never null)

response:HttpServletResponse

current HTTP response

transformer:Transformer

the target transformer

configureTransformerback to summary
protected void configureTransformer(Map<String, Object> model, HttpServletResponse response, Transformer transformer)

Configure the supplied Transformer instance.

The default implementation copies parameters from the model into the Transformer's parameter set. This implementation also copies the output properties into the Transformer output properties. Indentation properties are set as well.

Parameters
model:Map<String, Object>

merged output Map (never null)

response:HttpServletResponse

current HTTP response

transformer:Transformer

the target transformer

See Also
copyModelParameters(Map, Transformer), copyOutputProperties(Transformer), configureIndentation(Transformer)
convertSourceback to summary
protected Source convertSource(Object sourceObject) throws Exception

Convert the supplied Object into an XSLT Source if the Object type is supported.

Parameters
sourceObject:Object

the original source object

Returns:Source

the adapted XSLT Source

Exceptions
IllegalArgumentException:
if the given Object is not of a supported type
copyModelParametersback to summary
protected final void copyModelParameters(Map<String, Object> model, Transformer transformer)

Copy all entries from the supplied Map into the parameter set of the supplied Transformer.

Parameters
model:Map<String, Object>

merged output Map (never null)

transformer:Transformer

the target transformer

copyOutputPropertiesback to summary
protected final void copyOutputProperties(Transformer transformer)

Copy the configured output Properties, if any, into the output property set of the supplied Transformer.

Parameters
transformer:Transformer

the target transformer

createResultback to summary
protected Result createResult(HttpServletResponse response) throws Exception

Create the XSLT Result used to render the result of the transformation.

The default implementation creates a StreamResult wrapping the supplied HttpServletResponse's OutputStream.

Parameters
response:HttpServletResponse

current HTTP response

Returns:Result

the XSLT Result to use

Exceptions
Exception:
if the Result cannot be built
createTransformerback to summary
protected Transformer createTransformer(Templates templates) throws TransformerConfigurationException

Create the Transformer instance used to prefer the XSLT transformation.

The default implementation simply calls Templates#newTransformer(), and configures the Transformer with the custom URIResolver if specified.

Parameters
templates:Templates

the XSLT Templates instance to create a Transformer for

Returns:Transformer

the Transformer object

Exceptions
TransformerConfigurationException:
in case of creation failure
getSourceTypesback to summary
protected Class<?>[] getSourceTypes()

Return the array of Classes that are supported when converting to an XSLT Source.

Currently supports Source, Document, Node, Reader, InputStream and Resource.

Returns:Class<?>[]

the supported source types

getStylesheetSourceback to summary
protected Source getStylesheetSource()

Get the XSLT Source for the XSLT template under the configured URL.

Returns:Source

the Source object

getTransformerFactoryback to summary
protected final TransformerFactory getTransformerFactory()

Return the TransformerFactory that this XsltView uses.

Returns:TransformerFactory

the TransformerFactory (never null)

initApplicationContextback to summary
protected void initApplicationContext() throws BeansException

Overrides org.springframework.context.support.ApplicationObjectSupport.initApplicationContext.

Initialize this XsltView's TransformerFactory.

Annotations
@Override
Exceptions
BeansException:

Doc from org.springframework.context.support.ApplicationObjectSupport.initApplicationContext.

if thrown by ApplicationContext methods

loadTemplatesback to summary
private Templates loadTemplates() throws ApplicationContextException

Load the Templates instance for the stylesheet at the configured location.

locateSourceback to summary
protected Source locateSource(Map<String, Object> model) throws Exception

Locate the Source object in the supplied model, converting objects as required. The default implementation first attempts to look under the configured source key, if any, before attempting to locate an object of supported type.

Parameters
model:Map<String, Object>

the merged model Map

Returns:Source

the XSLT Source object (or null if none found)

Annotations
@Nullable
Exceptions
Exception:
if an error occurred during locating the source
See Also
setSourceKey, convertSource
newTransformerFactoryback to summary
protected TransformerFactory newTransformerFactory(Class<? extends TransformerFactory> transformerFactoryClass)

Instantiate a new TransformerFactory for this view.

The default implementation simply calls javax.xml.transform.TransformerFactory#newInstance(). If a "transformerFactoryClass" has been specified explicitly, the default constructor of the specified class will be called instead.

Can be overridden in subclasses.

Parameters
transformerFactoryClass:Class<? extends TransformerFactory>

the specified factory class (if any)

Returns:TransformerFactory

the new TransactionFactory instance

See Also
setTransformerFactoryClass, getTransformerFactory()
renderMergedOutputModelback to summary
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Subclasses must implement this method to actually render the view.

The first step will be preparing the request: In the JSP case, this would mean setting model objects as request attributes. The second step will be the actual rendering of the view, for example including the JSP via a RequestDispatcher.

Parameters
model:Map<String, Object>

combined output Map (never null), with dynamic values taking precedence over static attributes

request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

Annotations
@Override
Exceptions
Exception:
if rendering failed
setCacheTemplatesback to summary
public void setCacheTemplates(boolean cacheTemplates)

Turn on/off the caching of the XSLT Templates instance.

The default value is "true". Only set this to "false" in development, where caching does not seriously impact performance.

setErrorListenerback to summary
public void setErrorListener(ErrorListener errorListener)

Set an implementation of the javax.xml.transform.ErrorListener interface for custom handling of transformation errors and warnings.

If not set, a default org.springframework.util.xml.SimpleTransformErrorListener is used that simply logs warnings using the logger instance of the view class, and rethrows errors to discontinue the XML transformation.

See Also
org.springframework.util.xml.SimpleTransformErrorListener
setIndentback to summary
public void setIndent(boolean indent)

Set whether the XSLT transformer may add additional whitespace when outputting the result tree.

Default is true (on); set this to false (off) to not specify an "indent" key, leaving the choice up to the stylesheet.

See Also
javax.xml.transform.OutputKeys#INDENT
setOutputPropertiesback to summary
public void setOutputProperties(Properties outputProperties)

Set arbitrary transformer output properties to be applied to the stylesheet.

Any values specified here will override defaults that this view sets programmatically.

See Also
javax.xml.transform.Transformer#setOutputProperty
setSourceKeyback to summary
public void setSourceKey(String sourceKey)

Set the name of the model attribute that represents the XSLT Source. If not specified, the model map will be searched for a matching value type.

The following source types are supported out of the box: Source, Document, Node, Reader, InputStream and Resource.

See Also
getSourceTypes, convertSource
setTransformerFactoryClassback to summary
public void setTransformerFactoryClass(Class<? extends TransformerFactory> transformerFactoryClass)

Specify the XSLT TransformerFactory class to use.

The default constructor of the specified class will be called to build the TransformerFactory for this view.

setUriResolverback to summary
public void setUriResolver(URIResolver uriResolver)

Set the URIResolver used in the transform.

The URIResolver handles calls to the XSLT document() function.