XsltView
and its
more flexible XsltView.locateSource(java.util.Map<java.lang.String, java.lang.Object>)
mechanism@Deprecated public abstract class AbstractXsltView extends AbstractView
Subclasses typically must provide the Source
to transform
by overriding createXsltSource(java.util.Map<java.lang.String, java.lang.Object>, java.lang.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.
Modifier and Type | Field and Description |
---|---|
private boolean |
cache
Deprecated.
|
private javax.xml.transform.Templates |
cachedTemplates
Deprecated.
|
private boolean |
customContentTypeSet
Deprecated.
|
static java.lang.String |
DEFAULT_ROOT
Deprecated.
The default document root name
|
private javax.xml.transform.ErrorListener |
errorListener
Deprecated.
|
private boolean |
indent
Deprecated.
|
private java.util.Properties |
outputProperties
Deprecated.
|
private java.lang.String |
root
Deprecated.
|
private Resource |
stylesheetLocation
Deprecated.
|
private javax.xml.transform.TransformerFactory |
transformerFactory
Deprecated.
|
private java.lang.Class |
transformerFactoryClass
Deprecated.
|
private javax.xml.transform.URIResolver |
uriResolver
Deprecated.
|
private boolean |
useSingleModelNameAsRoot
Deprecated.
|
static java.lang.String |
XML_CONTENT_TYPE
Deprecated.
The default content type if no stylesheet specified
|
DEFAULT_CONTENT_TYPE
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Modifier | Constructor and Description |
---|---|
protected |
AbstractXsltView()
Deprecated.
This constructor sets the content type to "text/xml;charset=ISO-8859-1"
by default.
|
Modifier and Type | Method and Description |
---|---|
protected void |
applyTransformerParameters(java.util.Map<java.lang.String,java.lang.Object> parameters,
javax.xml.transform.Transformer transformer)
Deprecated.
Apply the specified parameters to the given Transformer.
|
protected javax.xml.transform.Transformer |
buildTransformer(java.util.Map<java.lang.String,java.lang.Object> parameters)
Deprecated.
Build a Transformer object for immediate use, based on the
given parameters.
|
protected javax.xml.transform.Source |
createXsltSource(java.util.Map<java.lang.String,java.lang.Object> model,
java.lang.String root,
HttpServletRequest request,
HttpServletResponse response)
Deprecated.
Return the XML
Source to transform. |
protected void |
doTransform(java.util.Map<java.lang.String,java.lang.Object> model,
javax.xml.transform.Source source,
HttpServletRequest request,
HttpServletResponse response)
Deprecated.
Perform the actual transformation, writing to the HTTP response.
|
protected void |
doTransform(javax.xml.transform.Source source,
java.util.Map<java.lang.String,java.lang.Object> parameters,
javax.xml.transform.Result result,
java.lang.String encoding)
Deprecated.
Perform the actual transformation, writing to the given result.
|
protected java.util.Map |
getParameters(HttpServletRequest request)
Deprecated.
Return a Map of transformer parameters to be applied to the stylesheet.
|
protected java.util.Map |
getParameters(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request)
Deprecated.
Return a Map of transformer parameters to be applied to the stylesheet.
|
protected Resource |
getStylesheetLocation()
Deprecated.
Return the location of the XSLT stylesheet, if any.
|
protected javax.xml.transform.Source |
getStylesheetSource(Resource stylesheetLocation)
Deprecated.
Load the stylesheet from the specified location.
|
protected javax.xml.transform.Templates |
getTemplates()
Deprecated.
Obtain the Templates object to use, based on the configured
stylesheet, either a cached one or a freshly built one.
|
protected javax.xml.transform.TransformerFactory |
getTransformerFactory()
Deprecated.
Return the TransformerFactory used by this view.
|
protected void |
initApplicationContext()
Deprecated.
Here we load our template, as we need the
ApplicationContext to do it. |
protected javax.xml.transform.TransformerFactory |
newTransformerFactory(java.lang.Class transformerFactoryClass)
Deprecated.
Instantiate a new TransformerFactory for this view.
|
protected void |
renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletRequest request,
HttpServletResponse response)
Deprecated.
Subclasses must implement this method to actually render the view.
|
void |
resetCachedTemplates()
Deprecated.
Reset the cached Templates object, if any.
|
void |
setCache(boolean cache)
Deprecated.
Set whether to activate the template cache for this view.
|
void |
setContentType(java.lang.String contentType)
Deprecated.
Set the content type for this view.
|
void |
setErrorListener(javax.xml.transform.ErrorListener errorListener)
Deprecated.
Set an implementation of the
ErrorListener
interface for custom handling of transformation errors and warnings. |
void |
setIndent(boolean indent)
Deprecated.
Set whether the XSLT transformer may add additional whitespace when
outputting the result tree.
|
void |
setOutputProperties(java.util.Properties outputProperties)
Deprecated.
Set arbitrary transformer output properties to be applied to the stylesheet.
|
void |
setRoot(java.lang.String root)
Deprecated.
The document root element name.
|
void |
setStylesheetLocation(Resource stylesheetLocation)
Deprecated.
Set the location of the XSLT stylesheet.
|
void |
setTransformerFactoryClass(java.lang.Class transformerFactoryClass)
Deprecated.
Specify the XSLT TransformerFactory class to use.
|
void |
setUriResolver(javax.xml.transform.URIResolver uriResolver)
Deprecated.
Set the URIResolver used in the transform.
|
void |
setUseSingleModelNameAsRoot(boolean useSingleModelNameAsRoot)
Deprecated.
Set whether to use the name of a given single model object as the
document root element name.
|
protected boolean |
useWriter()
Deprecated.
Return whether to use a
java.io.Writer to write text content
to the HTTP response. |
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setExposePathVariables, setRequestContextAttribute, setResponseContentType, toString, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
public static final java.lang.String XML_CONTENT_TYPE
public static final java.lang.String DEFAULT_ROOT
private boolean customContentTypeSet
private java.lang.Class transformerFactoryClass
private Resource stylesheetLocation
private java.lang.String root
private boolean useSingleModelNameAsRoot
private javax.xml.transform.URIResolver uriResolver
private javax.xml.transform.ErrorListener errorListener
private boolean indent
private java.util.Properties outputProperties
private boolean cache
private javax.xml.transform.TransformerFactory transformerFactory
private volatile javax.xml.transform.Templates cachedTemplates
protected AbstractXsltView()
A specific content type can be configured via the
"contentType"
bean property.
public void setContentType(java.lang.String contentType)
AbstractView
May be ignored by subclasses if the view itself is assumed to set the content type, e.g. in case of JSPs.
setContentType
in class AbstractView
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 setStylesheetLocation(Resource stylesheetLocation)
If the TransformerFactory
used by this instance has already
been initialized then invoking this setter will result in the
attendant templates
being re-cached.
stylesheetLocation
- the location of the XSLT stylesheetResourceLoader.getResource(java.lang.String)
protected Resource getStylesheetLocation()
public void setRoot(java.lang.String root)
"DocRoot"
.
Only used if we're not passed a single Node
as the model.
root
- the document root element nameDEFAULT_ROOT
public void setUseSingleModelNameAsRoot(boolean useSingleModelNameAsRoot)
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
.
useSingleModelNameAsRoot
- true
if the name of a given single
model object is to be used as the document root element namesetRoot(java.lang.String)
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)
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.
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 setCache(boolean cache)
Default is true
. Turn this off to refresh
the Templates object on every access, e.g. during development.
resetCachedTemplates()
public final void resetCachedTemplates()
The Templates object will subsequently be rebuilt on next
access
, if caching is enabled.
setCache(boolean)
protected final void initApplicationContext() throws ApplicationContextException
ApplicationContext
to do it.initApplicationContext
in class ApplicationObjectSupport
ApplicationContextException
- in case of initialization errorsApplicationObjectSupport.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)javax.xml.transform.TransformerFactoryConfigurationError
- in case of instantiation failuresetTransformerFactoryClass(java.lang.Class)
,
getTransformerFactory()
protected final javax.xml.transform.TransformerFactory getTransformerFactory()
protected final 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.Source createXsltSource(java.util.Map<java.lang.String,java.lang.Object> model, java.lang.String root, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
Source
to transform.model
- the model Maproot
- 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.java.lang.Exception
- if an error occursprotected void doTransform(java.util.Map<java.lang.String,java.lang.Object> model, javax.xml.transform.Source source, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
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()
).
model
- the model Mapsource
- the Source to transformrequest
- current HTTP requestresponse
- current HTTP responsejava.lang.Exception
- if an error occursStreamResult
,
javax.servlet.ServletResponse#getOutputStream()
,
javax.servlet.ServletResponse#getWriter()
,
useWriter()
protected java.util.Map getParameters(java.util.Map<java.lang.String,java.lang.Object> model, HttpServletRequest request)
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.
model
- the model Maprequest
- current HTTP requestTransformer.setParameter(java.lang.String, java.lang.Object)
protected java.util.Map getParameters(HttpServletRequest request)
Subclasses can override this method in order to apply one or more parameters to the transformation process.
The default implementation simply returns null
.
request
- current HTTP requestgetParameters(Map, HttpServletRequest)
,
Transformer.setParameter(java.lang.String, java.lang.Object)
protected boolean useWriter()
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
.
true
) or an OutputStream
(false
)javax.servlet.ServletResponse#getWriter()
,
javax.servlet.ServletResponse#getOutputStream()
protected void doTransform(javax.xml.transform.Source source, java.util.Map<java.lang.String,java.lang.Object> parameters, javax.xml.transform.Result result, java.lang.String encoding) throws java.lang.Exception
source
- the Source to transformparameters
- a Map of parameters to be applied to the stylesheet
(as determined by getParameters(Map, HttpServletRequest)
)result
- the result to write toencoding
- the preferred character encoding that the underlying Transformer should usejava.lang.Exception
- if an error occursprotected javax.xml.transform.Transformer buildTransformer(java.util.Map<java.lang.String,java.lang.Object> parameters) throws javax.xml.transform.TransformerConfigurationException
parameters
- a Map of parameters to be applied to the stylesheet
(as determined by getParameters(Map, HttpServletRequest)
)null
)javax.xml.transform.TransformerConfigurationException
- if the Transformer object
could not be builtprotected javax.xml.transform.Templates getTemplates() throws javax.xml.transform.TransformerConfigurationException
Subclasses may override this method e.g. in order to refresh
the Templates instance, calling resetCachedTemplates()
before delegating to this getTemplates()
implementation.
null
if there is
no stylesheet specified)javax.xml.transform.TransformerConfigurationException
- if the Templates object
could not be builtsetStylesheetLocation(org.springframework.core.io.Resource)
,
setCache(boolean)
,
resetCachedTemplates()
protected void applyTransformerParameters(java.util.Map<java.lang.String,java.lang.Object> parameters, javax.xml.transform.Transformer transformer)
parameters
- the transformer parameters
(as determined by getParameters(Map, HttpServletRequest)
)transformer
- the Transformer to aply the parametersprotected javax.xml.transform.Source getStylesheetSource(Resource stylesheetLocation) throws ApplicationContextException
stylesheetLocation
- the stylesheet resource to be loadedApplicationContextException
- if the stylesheet resource could not be loaded