The Spring Framework

org.springframework.web.servlet.view.xslt
Class XsltView

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.view.AbstractView
              extended by org.springframework.web.servlet.view.AbstractUrlBasedView
                  extended by org.springframework.web.servlet.view.xslt.XsltView
All Implemented Interfaces:
BeanNameAware, InitializingBean, ApplicationContextAware, ServletContextAware, View

public class XsltView
extends AbstractUrlBasedView

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.

Since:
2.0
Author:
Rob Harrop

Field Summary
 
Fields inherited from class org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
XsltView()
           
 
Method Summary
protected  void configureIndentation(Transformer transformer)
          Configures the indentation settings for the supplied Transformer.
protected  void configureResponse(Map model, HttpServletResponse response, Transformer transformer)
          Configures the supplied HttpServletResponse.
protected  void configureTransformer(Map model, HttpServletResponse response, Transformer transformer)
          Configures the supplied Transformer instance.
protected  Source convertSource(Object source)
          Converts the supplied Object into an XSLT Source if the Object type is supported.
protected  void copyModelParameters(Map model, Transformer transformer)
          Copies all entries from the supplied Map into the parameter set of the supplied Transformer.
protected  void copyOutputProperties(Transformer transformer)
          Copies the configured output Properties, if any, into the output property set of the supplied Transformer.
protected  Result createResult(HttpServletResponse response)
          Creates the XSLT Result used to render the result of the transformation.
protected  Transformer createTransformer(Templates templates)
          Creates the Transformer instance used to prefer the XSLT transformation.
protected  Class[] getSourceTypes()
          Returns the array of Classes that are supported when converting to an XSLT Source.
protected  Source getStylesheetSource()
          Gets the XSLT Source for the XSLT template under the configured URL.
protected  TransformerFactory getTransformerFactory()
           
protected  void initApplicationContext()
          Subclasses can override this for custom initialization behavior.
protected  Source locateSource(Map model)
          Locates the Source object in the supplied model, converting objects as required.
protected  void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
          Subclasses must implement this method to actually render the view.
 void setCacheTemplates(boolean cacheTemplates)
          Turns on/off the caching of the XSLT Templates instance.
 void setErrorListener(ErrorListener errorListener)
           
 void setIndent(boolean indent)
           
 void setOutputProperties(Properties outputProperties)
           
 void setSourceKey(String sourceKey)
           
 void setUriResolver(URIResolver uriResolver)
          Sets a custom URIResolver to use for processing the transformation and loading the
 
Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView
afterPropertiesSet, getUrl, setUrl, toString
 
Methods inherited from class org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createRequestContext, exposeModelAsRequestAttributes, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XsltView

public XsltView()
Method Detail

setCacheTemplates

public void setCacheTemplates(boolean cacheTemplates)
Turns on/off the caching of the XSLT Templates instance. The default value is true. Only set this to false in development as not caching seriously impacts performance.


setUriResolver

public void setUriResolver(URIResolver uriResolver)
Sets a custom URIResolver to use for processing the transformation and loading the

Parameters:
uriResolver -

setErrorListener

public void setErrorListener(ErrorListener errorListener)

setSourceKey

public void setSourceKey(String sourceKey)

setOutputProperties

public void setOutputProperties(Properties outputProperties)

setIndent

public void setIndent(boolean indent)

getTransformerFactory

protected final TransformerFactory getTransformerFactory()

initApplicationContext

protected void initApplicationContext()
                               throws BeansException
Description copied from class: ApplicationObjectSupport
Subclasses can override this for custom initialization behavior. Gets called by setApplicationContext after setting the context instance.

Note: Does not get called on reinitialization of the context but rather just on first initialization of this object's context reference.

Overrides:
initApplicationContext in class ApplicationObjectSupport
Throws:
ApplicationContextException - in case of initialization errors
BeansException - if thrown by ApplicationContext methods
See Also:
ApplicationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext)

renderMergedOutputModel

protected void renderMergedOutputModel(Map model,
                                       HttpServletRequest request,
                                       HttpServletResponse response)
                                throws Exception
Description copied from class: AbstractView
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.

Specified by:
renderMergedOutputModel in class AbstractView
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 - if rendering failed

createResult

protected Result createResult(HttpServletResponse response)
                       throws Exception
Creates the XSLT Result used to render the result of the transformation. Default implementation creates a StreamResult wrapping the supplied HttpServletResponse.

Throws:
Exception

locateSource

protected Source locateSource(Map model)
                       throws Exception
Locates the Source object in the supplied model, converting objects as required. Default implementation first attempts to look under the configured source key, if any, before attempting to locate an object of supported type.

Throws:
Exception

getSourceTypes

protected Class[] getSourceTypes()
Returns the array of Classes that are supported when converting to an XSLT Source. Current supports Source, Document, Node, InputStream, Reader and Resource.


convertSource

protected Source convertSource(Object source)
                        throws Exception
Converts the supplied Object into an XSLT Source if the Object type is supported.

Throws:
IllegalArgumentException - if the Object cannot if not of a supported type.
Exception

configureTransformer

protected void configureTransformer(Map model,
                                    HttpServletResponse response,
                                    Transformer transformer)
Configures the supplied Transformer instance. Default implementation copies parameters from the model into the Transformer parameter set. This implementation also copies the output properties into the Transformer output properties. Indentation properties are also set by this implementation.

See Also:
copyModelParameters(Map, Transformer), copyOutputProperties(Transformer), configureIndentation(Transformer)

configureIndentation

protected final void configureIndentation(Transformer transformer)
Configures the indentation settings for the supplied Transformer.

Parameters:
transformer - the target transformer
Throws:
IllegalArgumentException - if the supplied Transformer is null
See Also:
TransformerUtils.enableIndenting(javax.xml.transform.Transformer), TransformerUtils.disableIndenting(javax.xml.transform.Transformer)

copyOutputProperties

protected final void copyOutputProperties(Transformer transformer)
Copies the configured output Properties, if any, into the output property set of the supplied Transformer.


copyModelParameters

protected final void copyModelParameters(Map model,
                                         Transformer transformer)
Copies all entries from the supplied Map into the parameter set of the supplied Transformer.


configureResponse

protected void configureResponse(Map model,
                                 HttpServletResponse response,
                                 Transformer transformer)
Configures the supplied HttpServletResponse. The default implementation of this method sets the content type and encoding from properties specified in the Transformer.


createTransformer

protected Transformer createTransformer(Templates templates)
                                 throws TransformerConfigurationException
Creates the Transformer instance used to prefer the XSLT transformation. Default implementation simply calls Templates.newTransformer(). Configures the Transformer with the custom URIResolver if specified.

Throws:
TransformerConfigurationException

getStylesheetSource

protected Source getStylesheetSource()
Gets the XSLT Source for the XSLT template under the configured URL.


The Spring Framework

Copyright © 2002-2007 The Spring Framework.