public class XsltView extends AbstractUrlBasedView
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.
Modifier and Type | Field and Description |
---|---|
private javax.xml.transform.Templates |
cachedTemplates |
private boolean |
cacheTemplates |
private javax.xml.transform.ErrorListener |
errorListener |
private boolean |
indent |
private java.util.Properties |
outputProperties |
private java.lang.String |
sourceKey |
private javax.xml.transform.TransformerFactory |
transformerFactory |
private java.lang.Class |
transformerFactoryClass |
private javax.xml.transform.URIResolver |
uriResolver |
DEFAULT_CONTENT_TYPE
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
XsltView() |
Modifier and Type | Method and Description |
---|---|
private void |
closeSourceIfNecessary(javax.xml.transform.Source source)
Close the underlying resource managed by the supplied <code>Source</code> if applicable.
|
protected void |
configureIndentation(javax.xml.transform.Transformer transformer)
Configure the indentation settings for the supplied <code>Transformer</code>.
|
protected void |
configureResponse(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletResponse response,
javax.xml.transform.Transformer transformer)
Configure the supplied
HttpServletResponse . |
protected void |
configureTransformer(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletResponse response,
javax.xml.transform.Transformer transformer)
Configure the supplied <code>Transformer</code> instance.
|
protected javax.xml.transform.Source |
convertSource(java.lang.Object source)
Convert the supplied <code>Object</code> into an XSLT <code>Source</code> if the
<code>Object</code> type is
supported . |
protected void |
copyModelParameters(java.util.Map<java.lang.String,java.lang.Object> model,
javax.xml.transform.Transformer transformer)
Copy all entries from the supplied Map into the
parameter set
of the supplied <code>Transformer</code>. |
protected void |
copyOutputProperties(javax.xml.transform.Transformer transformer)
Copy the configured output <code>Properties</code>, if any, into the
output property set of the supplied
<code>Transformer</code>. |
protected javax.xml.transform.Result |
createResult(HttpServletResponse response)
Create the XSLT <code>Result</code> used to render the result of the transformation.
|
protected javax.xml.transform.Transformer |
createTransformer(javax.xml.transform.Templates templates)
Create the <code>Transformer</code> instance used to prefer the XSLT transformation.
|
protected java.lang.Class[] |
getSourceTypes()
Return the array of <code>Classes</code> that are supported when converting to an
XSLT <code>Source</code>.
|
protected javax.xml.transform.Source |
getStylesheetSource()
Get the XSLT <code>Source</code> for the XSLT template under the
configured URL . |
protected javax.xml.transform.TransformerFactory |
getTransformerFactory()
Return the TransformerFactory that this XsltView uses.
|
protected void |
initApplicationContext()
Initialize this XsltView's TransformerFactory.
|
private javax.xml.transform.Templates |
loadTemplates()
Load the <code>Templates</code> instance for the stylesheet at the configured location.
|
protected javax.xml.transform.Source |
locateSource(java.util.Map<java.lang.String,java.lang.Object> model)
Locate the <code>Source</code> object in the supplied model,
converting objects as required.
|
protected javax.xml.transform.TransformerFactory |
newTransformerFactory(java.lang.Class transformerFactoryClass)
Instantiate a new TransformerFactory for this view.
|
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Subclasses must implement this method to actually render the view.
|
void |
setCacheTemplates(boolean cacheTemplates)
Turn on/off the caching of the XSLT <code>Templates</code> instance.
|
void |
setErrorListener(javax.xml.transform.ErrorListener errorListener)
Set an implementation of the <code>ErrorListener</code>
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(java.util.Properties outputProperties)
Set arbitrary transformer output properties to be applied to the stylesheet.
|
void |
setSourceKey(java.lang.String sourceKey)
Set the name of the model attribute that represents the XSLT Source.
|
void |
setTransformerFactoryClass(java.lang.Class transformerFactoryClass)
Specify the XSLT TransformerFactory class to use.
|
void |
setUriResolver(javax.xml.transform.URIResolver uriResolver)
Set the URIResolver used in the transform.
|
afterPropertiesSet, checkResource, getUrl, isUrlRequired, setUrl, toString
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
private java.lang.Class transformerFactoryClass
private java.lang.String sourceKey
private javax.xml.transform.URIResolver uriResolver
private javax.xml.transform.ErrorListener errorListener
private boolean indent
private java.util.Properties outputProperties
private boolean cacheTemplates
private javax.xml.transform.TransformerFactory transformerFactory
private javax.xml.transform.Templates cachedTemplates
public void setTransformerFactoryClass(java.lang.Class transformerFactoryClass)
The default constructor of the specified class will be called to build the TransformerFactory for this view.
public void setSourceKey(java.lang.String sourceKey)
The following source types are supported out of the box:
<code>Source</code>, <code>Document</code>, <code>Node</code>, <code>Reader</code>,
<code>InputStream</code> and Resource
.
getSourceTypes()
,
convertSource(java.lang.Object)
public void setUriResolver(javax.xml.transform.URIResolver uriResolver)
The URIResolver handles calls to the XSLT document()
function.
public void setErrorListener(javax.xml.transform.ErrorListener errorListener)
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.
SimpleTransformErrorListener
public void setIndent(boolean indent)
Default is true
(on); set this to false
(off)
to not specify an "indent" key, leaving the choice up to the stylesheet.
OutputKeys.INDENT
public void setOutputProperties(java.util.Properties outputProperties)
Any values specified here will override defaults that this view sets programmatically.
Transformer.setOutputProperty(java.lang.String, java.lang.String)
public void setCacheTemplates(boolean cacheTemplates)
The default value is "true". Only set this to "false" in development, where caching does not seriously impact performance.
protected void initApplicationContext() throws BeansException
initApplicationContext
in class ApplicationObjectSupport
ApplicationContextException
- in case of initialization errorsBeansException
- if thrown by ApplicationContext methodsApplicationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext)
protected javax.xml.transform.TransformerFactory newTransformerFactory(java.lang.Class transformerFactoryClass)
The default implementation simply calls
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.
transformerFactoryClass
- the specified factory class (if any)setTransformerFactoryClass(java.lang.Class)
,
getTransformerFactory()
protected final javax.xml.transform.TransformerFactory getTransformerFactory()
null
)protected void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
AbstractView
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.
renderMergedOutputModel
in class AbstractView
model
- combined output Map (never null
),
with dynamic values taking precedence over static attributesrequest
- current HTTP requestresponse
- current HTTP responsejava.lang.Exception
- if rendering failedprotected javax.xml.transform.Result createResult(HttpServletResponse response) throws java.lang.Exception
The default implementation creates a <code>StreamResult</code> wrapping the supplied
HttpServletResponse's OutputStream
.
response
- current HTTP responsejava.lang.Exception
- if the Result cannot be builtprotected javax.xml.transform.Source locateSource(java.util.Map<java.lang.String,java.lang.Object> model) throws java.lang.Exception
Locate the <code>Source</code> 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
.
model
- the merged model Mapnull
if none found)java.lang.Exception
- if an error occured during locating the sourcesetSourceKey(java.lang.String)
,
convertSource(java.lang.Object)
protected java.lang.Class[] getSourceTypes()
Currently supports <code>Source</code>, <code>Document</code>, <code>Node</code>,
<code>Reader</code>, <code>InputStream</code> and Resource
.
protected javax.xml.transform.Source convertSource(java.lang.Object source) throws java.lang.Exception
supported
.source
- the original source objectjava.lang.IllegalArgumentException
- if the given Object is not of a supported typejava.lang.Exception
protected void configureTransformer(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletResponse response, javax.xml.transform.Transformer transformer)
The default implementation copies parameters from the model into the
Transformer's parameter set
.
This implementation also copies the output properties
into the <code>Transformer</code> output properties
.
Indentation properties are set as well.
model
- merged output Map (never null
)response
- current HTTP responsetransformer
- the target transformercopyModelParameters(Map, Transformer)
,
copyOutputProperties(Transformer)
,
configureIndentation(Transformer)
protected final void configureIndentation(javax.xml.transform.Transformer transformer)
transformer
- the target transformerTransformerUtils.enableIndenting(javax.xml.transform.Transformer)
,
TransformerUtils.disableIndenting(javax.xml.transform.Transformer)
protected final void copyOutputProperties(javax.xml.transform.Transformer transformer)
output property set
of the supplied
<code>Transformer</code>.transformer
- the target transformerprotected final void copyModelParameters(java.util.Map<java.lang.String,java.lang.Object> model, javax.xml.transform.Transformer transformer)
parameter set
of the supplied <code>Transformer</code>.model
- merged output Map (never null
)transformer
- the target transformerprotected void configureResponse(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletResponse response, javax.xml.transform.Transformer transformer)
HttpServletResponse
.
The default implementation of this method sets the
content type
and
encoding
from the "media-type" and "encoding" output properties
specified in the <code>Transformer</code>.
model
- merged output Map (never null
)response
- current HTTP responsetransformer
- the target transformerprivate javax.xml.transform.Templates loadTemplates() throws ApplicationContextException
ApplicationContextException
protected javax.xml.transform.Transformer createTransformer(javax.xml.transform.Templates templates) throws javax.xml.transform.TransformerConfigurationException
The default implementation simply calls Templates.newTransformer()
, and
configures the <code>Transformer</code> with the custom <code>URIResolver</code> if specified.
templates
- the XSLT Templates instance to create a Transformer forjavax.xml.transform.TransformerConfigurationException
- in case of creation failureprotected javax.xml.transform.Source getStylesheetSource()
configured URL
.private void closeSourceIfNecessary(javax.xml.transform.Source source)
Only works for <code>StreamSources</code>.
source
- the XSLT Source to close (may be null
)