public abstract class

AbstractXsltView

extends AbstractView
java.lang.Object
   ↳ org.springframework.context.support.ApplicationObjectSupport
     ↳ org.springframework.web.context.support.WebApplicationObjectSupport
       ↳ org.springframework.web.servlet.view.AbstractView
         ↳ org.springframework.web.servlet.view.xslt.AbstractXsltView

This class is deprecated.
since Spring 2.5; superseded by XsltView and its more flexible locateSource(Map) mechanism

Class Overview

Convenient superclass for views rendered using an XSLT stylesheet.

Subclasses typically must provide the Source to transform by overriding createXsltSource(Map, String, HttpServletRequest, HttpServletResponse). Subclasses do not need to concern themselves with XSLT other than providing a valid stylesheet location.

Properties:

  • stylesheetLocation: a Resource pointing to the XSLT stylesheet
  • root: the name of the root element; defaults to "DocRoot"
  • uriResolver: the URIResolver to be used in the transform
  • errorListener (optional): the ErrorListener implementation instance for custom handling of warnings and errors during TransformerFactory operations
  • indent (optional): whether additional whitespace may be added when outputting the result; defaults to true
  • cache (optional): are templates to be cached; debug setting only; defaults to true

Note that setting "cache" to false will cause the template objects to be reloaded for each rendering. This is useful during development, but will seriously affect performance in production and is not thread-safe.

Summary

Constants
String DEFAULT_ROOT The default document root name
String XML_CONTENT_TYPE The default content type if no stylesheet specified
[Expand]
Inherited Constants
From class org.springframework.web.servlet.view.AbstractView
[Expand]
Inherited Fields
From class org.springframework.context.support.ApplicationObjectSupport
From interface org.springframework.web.servlet.View
Protected Constructors
AbstractXsltView()
This constructor sets the content type to "text/xml;charset=ISO-8859-1" by default.
Public Methods
final void resetCachedTemplates()
Reset the cached Templates object, if any.
void setCache(boolean cache)
Set whether to activate the template cache for this view.
void setContentType(String contentType)
Set the content type for this view.
void setErrorListener(ErrorListener errorListener)
Set an implementation of the ErrorListener interface for custom handling of transformation errors and warnings.
void setIndent(boolean indent)
Set whether the XSLT transformer may add additional whitespace when outputting the result tree.
void setOutputProperties(Properties outputProperties)
Set arbitrary transformer output properties to be applied to the stylesheet.
void setRoot(String root)
The document root element name.
void setStylesheetLocation(Resource stylesheetLocation)
Set the location of the XSLT stylesheet.
void setTransformerFactoryClass(Class transformerFactoryClass)
Specify the XSLT TransformerFactory class to use.
void setUriResolver(URIResolver uriResolver)
Set the URIResolver used in the transform.
void setUseSingleModelNameAsRoot(boolean useSingleModelNameAsRoot)
Set whether to use the name of a given single model object as the document root element name.
Protected Methods
void applyTransformerParameters(Map<StringObject> parameters, Transformer transformer)
Apply the specified parameters to the given Transformer.
Transformer buildTransformer(Map<StringObject> parameters)
Build a Transformer object for immediate use, based on the given parameters.
Source createXsltSource(Map<StringObject> model, String root, HttpServletRequest request, HttpServletResponse response)
Return the XML Source to transform.
void doTransform(Map<StringObject> model, Source source, HttpServletRequest request, HttpServletResponse response)
Perform the actual transformation, writing to the HTTP response.
void doTransform(Source source, Map<StringObject> parameters, Result result, String encoding)
Perform the actual transformation, writing to the given result.
Map getParameters(HttpServletRequest request)
Return a Map of transformer parameters to be applied to the stylesheet.
Map getParameters(Map<StringObject> model, HttpServletRequest request)
Return a Map of transformer parameters to be applied to the stylesheet.
Resource getStylesheetLocation()
Return the location of the XSLT stylesheet, if any.
Source getStylesheetSource(Resource stylesheetLocation)
Load the stylesheet from the specified location.
Templates getTemplates()
Obtain the Templates object to use, based on the configured stylesheet, either a cached one or a freshly built one.
final TransformerFactory getTransformerFactory()
Return the TransformerFactory used by this view.
final void initApplicationContext()
Here we load our template, as we need the ApplicationContext to do it.
TransformerFactory newTransformerFactory(Class transformerFactoryClass)
Instantiate a new TransformerFactory for this view.
final void renderMergedOutputModel(Map<StringObject> model, HttpServletRequest request, HttpServletResponse response)
Subclasses must implement this method to actually render the view.
boolean useWriter()
Return whether to use a java.io.Writer to write text content to the HTTP response.
[Expand]
Inherited Methods
From class org.springframework.web.servlet.view.AbstractView
From class org.springframework.web.context.support.WebApplicationObjectSupport
From class org.springframework.context.support.ApplicationObjectSupport
From class java.lang.Object
From interface org.springframework.beans.factory.BeanNameAware
From interface org.springframework.context.ApplicationContextAware
From interface org.springframework.web.context.ServletContextAware
From interface org.springframework.web.servlet.View

Constants

public static final String DEFAULT_ROOT

The default document root name

Constant Value: "DocRoot"

public static final String XML_CONTENT_TYPE

The default content type if no stylesheet specified

Constant Value: "text/xml;charset=ISO-8859-1"

Protected Constructors

protected AbstractXsltView ()

This constructor sets the content type to "text/xml;charset=ISO-8859-1" by default. This will be switched to the standard web view default "text/html;charset=ISO-8859-1" if a stylesheet location has been specified.

A specific content type can be configured via the "contentType" bean property.

Public Methods

public final void resetCachedTemplates ()

Reset the cached Templates object, if any.

The Templates object will subsequently be rebuilt on next access, if caching is enabled.

public void setCache (boolean cache)

Set whether to activate the template cache for this view.

Default is true. Turn this off to refresh the Templates object on every access, e.g. during development.

public void setContentType (String contentType)

Set the content type for this view. Default is "text/html;charset=ISO-8859-1".

May be ignored by subclasses if the view itself is assumed to set the content type, e.g. in case of JSPs.

public void setErrorListener (ErrorListener errorListener)

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

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

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

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.

public void setRoot (String root)

The document root element name. Default is "DocRoot".

Only used if we're not passed a single Node as the model.

Parameters
root the document root element name
See Also

public void setStylesheetLocation (Resource stylesheetLocation)

Set the location of the XSLT stylesheet.

If the TransformerFactory used by this instance has already been initialized then invoking this setter will result in the attendant templates being re-cached.

Parameters
stylesheetLocation the location of the XSLT stylesheet

public void setTransformerFactoryClass (Class 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.

public void setUriResolver (URIResolver uriResolver)

Set the URIResolver used in the transform.

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

public void setUseSingleModelNameAsRoot (boolean useSingleModelNameAsRoot)

Set whether to use the name of a given single model object as the document root element name.

Default is true : If you pass in a model with a single object named "myElement", then the document root will be named "myElement" as well. Set this flag to false if you want to pass in a single model object while still using the root element name configured through the "root" property.

Parameters
useSingleModelNameAsRoot true if the name of a given single model object is to be used as the document root element name
See Also

Protected Methods

protected void applyTransformerParameters (Map<StringObject> parameters, Transformer transformer)

Apply the specified parameters to the given Transformer.

Parameters
parameters the transformer parameters (as determined by getParameters(Map, HttpServletRequest))
transformer the Transformer to aply the parameters

protected Transformer buildTransformer (Map<StringObject> parameters)

Build a Transformer object for immediate use, based on the given parameters.

Parameters
parameters a Map of parameters to be applied to the stylesheet (as determined by getParameters(Map, HttpServletRequest))
Returns
  • the Transformer object (never null)
Throws
TransformerConfigurationException if the Transformer object could not be built

protected Source createXsltSource (Map<StringObject> model, String root, HttpServletRequest request, HttpServletResponse response)

Return the XML Source to transform.

Parameters
model the model Map
root name for root element. This can be supplied as a bean property to concrete subclasses within the view definition file, but will be overridden in the case of a single object in the model map to be the key for that object. If no root property is specified and multiple model objects exist, a default root tag name will be supplied.
request HTTP request. Subclasses won't normally use this, as request processing should have been complete. However, we might want to create a RequestContext to expose as part of the model.
response HTTP response. Subclasses won't normally use this, however there may sometimes be a need to set cookies.
Returns
  • the XSLT Source to transform
Throws
Exception if an error occurs

protected void doTransform (Map<StringObject> model, Source source, HttpServletRequest request, HttpServletResponse response)

Perform the actual transformation, writing to the HTTP response.

The default implementation delegates to the doTransform(javax.xml.transform.Source, java.util.Map, javax.xml.transform.Result, String) method, building a StreamResult for the ServletResponse OutputStream or for the ServletResponse Writer (according to useWriter()).

Parameters
model the model Map
source the Source to transform
request current HTTP request
response current HTTP response
Throws
Exception if an error occurs

protected void doTransform (Source source, Map<StringObject> parameters, Result result, String encoding)

Perform the actual transformation, writing to the given result.

Parameters
source the Source to transform
parameters a Map of parameters to be applied to the stylesheet (as determined by getParameters(Map, HttpServletRequest))
result the result to write to
encoding the preferred character encoding that the underlying Transformer should use
Throws
Exception if an error occurs

protected Map getParameters (HttpServletRequest request)

Return a Map of transformer parameters to be applied to the stylesheet.

Subclasses can override this method in order to apply one or more parameters to the transformation process.

The default implementation simply returns null.

Parameters
request current HTTP request
Returns
  • a Map of parameters to apply to the transformation process

protected Map getParameters (Map<StringObject> model, HttpServletRequest request)

Return a Map of transformer parameters to be applied to the stylesheet.

Subclasses can override this method in order to apply one or more parameters to the transformation process.

The default implementation delegates to the getParameters(HttpServletRequest) variant.

Parameters
model the model Map
request current HTTP request
Returns
  • a Map of parameters to apply to the transformation process

protected Resource getStylesheetLocation ()

Return the location of the XSLT stylesheet, if any.

protected Source getStylesheetSource (Resource stylesheetLocation)

Load the stylesheet from the specified location.

Parameters
stylesheetLocation the stylesheet resource to be loaded
Returns
  • the stylesheet source
Throws
ApplicationContextException if the stylesheet resource could not be loaded

protected Templates getTemplates ()

Obtain the Templates object to use, based on the configured stylesheet, either a cached one or a freshly built one.

Subclasses may override this method e.g. in order to refresh the Templates instance, calling resetCachedTemplates() before delegating to this getTemplates() implementation.

Returns
  • the Templates object (or null if there is no stylesheet specified)
Throws
TransformerConfigurationException if the Templates object could not be built

protected final TransformerFactory getTransformerFactory ()

Return the TransformerFactory used by this view. Available once the View object has been fully initialized.

protected final void initApplicationContext ()

Here we load our template, as we need the ApplicationContext to do it.

protected TransformerFactory newTransformerFactory (Class transformerFactoryClass)

Instantiate a new TransformerFactory for this view.

The default implementation simply calls 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 the specified factory class (if any)
Returns
  • the new TransactionFactory instance
Throws
TransformerFactoryConfigurationError in case of instantiation failure

protected final void renderMergedOutputModel (Map<StringObject> model, HttpServletRequest request, HttpServletResponse response)

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 combined output Map (never null), with dynamic values taking precedence over static attributes
request current HTTP request
response current HTTP response
Throws
Exception

protected boolean useWriter ()

Return whether to use a java.io.Writer to write text content to the HTTP response. Else, a java.io.OutputStream will be used, to write binary content to the response.

The default implementation returns false, indicating a a java.io.OutputStream.

Returns
  • whether to use a Writer (true) or an OutputStream (false)