| Method from org.springframework.web.servlet.view.xslt.AbstractXsltView Detail: |
protected void applyTransformerParameters(Map parameters,
Transformer transformer) {
if (parameters != null) {
for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
transformer.setParameter(entry.getKey().toString(), entry.getValue());
}
}
}
Apply the specified parameters to the given Transformer. |
protected Transformer buildTransformer(Map parameters) throws TransformerConfigurationException {
Templates templates = getTemplates();
Transformer transformer =
(templates != null ? templates.newTransformer() : getTransformerFactory().newTransformer());
applyTransformerParameters(parameters, transformer);
return transformer;
}
Build a Transformer object for immediate use, based on the
given parameters. |
protected Source createXsltSource(Map model,
String root,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
return null;
}
Return the XML Source to transform. |
protected void doTransform(Map model,
Source source,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map parameters = getParameters(model, request);
Result result = (useWriter() ?
new StreamResult(response.getWriter()) :
new StreamResult(new BufferedOutputStream(response.getOutputStream())));
String encoding = response.getCharacterEncoding();
doTransform(source, parameters, result, encoding);
}
|
protected void doTransform(Source source,
Map parameters,
Result result,
String encoding) throws Exception {
try {
Transformer trans = buildTransformer(parameters);
// Explicitly apply URIResolver to every created Transformer.
if (this.uriResolver != null) {
trans.setURIResolver(this.uriResolver);
}
// Specify default output properties.
trans.setOutputProperty(OutputKeys.ENCODING, encoding);
if (this.indent) {
TransformerUtils.enableIndenting(trans);
}
// Apply any arbitrary output properties, if specified.
if (this.outputProperties != null) {
Enumeration propsEnum = this.outputProperties.propertyNames();
while (propsEnum.hasMoreElements()) {
String propName = (String) propsEnum.nextElement();
trans.setOutputProperty(propName, this.outputProperties.getProperty(propName));
}
}
// Perform the actual XSLT transformation.
trans.transform(source, result);
}
catch (TransformerConfigurationException ex) {
throw new NestedServletException(
"Couldn't create XSLT transformer in XSLT view with name [" + getBeanName() + "]", ex);
}
catch (TransformerException ex) {
throw new NestedServletException(
"Couldn't perform transform in XSLT view with name [" + getBeanName() + "]", ex);
}
}
Perform the actual transformation, writing to the given result. |
protected Map getParameters() {
return null;
} Deprecated! as - of Spring 2.0.4, in favor of the
#getParameters(HttpServletRequest) variant
Return a Map of transformer parameters to be applied to the stylesheet. |
protected Map getParameters(HttpServletRequest request) {
return getParameters();
}
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 simple
#getParameters() variant. |
protected Map getParameters(Map model,
HttpServletRequest request) {
return getParameters(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. |
protected Resource getStylesheetLocation() {
return this.stylesheetLocation;
}
Return the location of the XSLT stylesheet, if any. |
protected Source getStylesheetSource(Resource stylesheetLocation) throws ApplicationContextException {
if (logger.isDebugEnabled()) {
logger.debug("Loading XSLT stylesheet from " + stylesheetLocation);
}
try {
URL url = stylesheetLocation.getURL();
String urlPath = url.toString();
String systemId = urlPath.substring(0, urlPath.lastIndexOf('/") + 1);
return new StreamSource(url.openStream(), systemId);
}
catch (IOException ex) {
throw new ApplicationContextException("Can't load XSLT stylesheet from " + stylesheetLocation, ex);
}
}
Load the stylesheet from the specified location. |
protected Templates getTemplates() throws TransformerConfigurationException {
if (this.cachedTemplates != null) {
return this.cachedTemplates;
}
Resource location = getStylesheetLocation();
if (location != null) {
Templates templates = getTransformerFactory().newTemplates(getStylesheetSource(location));
if (this.cache) {
this.cachedTemplates = templates;
}
return templates;
}
return null;
}
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. |
protected final TransformerFactory getTransformerFactory() {
return this.transformerFactory;
}
Return the TransformerFactory used by this view.
Available once the View object has been fully initialized. |
protected final void initApplicationContext() throws ApplicationContextException {
this.transformerFactory = newTransformerFactory(this.transformerFactoryClass);
this.transformerFactory.setErrorListener(this.errorListener);
if (this.uriResolver != null) {
this.transformerFactory.setURIResolver(this.uriResolver);
}
if (getStylesheetLocation() != null && !this.customContentTypeSet) {
// Use "text/html" as default (instead of "text/xml") if a stylesheet
// has been configured but no custom content type has been set.
super.setContentType(DEFAULT_CONTENT_TYPE);
}
try {
getTemplates();
}
catch (TransformerConfigurationException ex) {
throw new ApplicationContextException("Cannot load stylesheet for XSLT view '" + getBeanName() + "'", ex);
}
}
|
protected TransformerFactory newTransformerFactory(Class transformerFactoryClass) {
if (transformerFactoryClass != null) {
try {
return (TransformerFactory) transformerFactoryClass.newInstance();
}
catch (Exception ex) {
throw new TransformerFactoryConfigurationError(ex, "Could not instantiate TransformerFactory");
}
}
else {
return TransformerFactory.newInstance();
}
}
|
protected final void renderMergedOutputModel(Map model,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setContentType(getContentType());
Source source = null;
String docRoot = null;
// Value of a single element in the map, if there is one.
Object singleModel = null;
if (this.useSingleModelNameAsRoot && model.size() == 1) {
docRoot = (String) model.keySet().iterator().next();
if (logger.isDebugEnabled()) {
logger.debug("Single model object received, key [" + docRoot + "] will be used as root tag");
}
singleModel = model.get(docRoot);
}
// Handle special case when we have a single node.
if (singleModel instanceof Node || singleModel instanceof Source) {
// Don't domify if the model is already an XML node/source.
// We don't need to worry about model name, either:
// we leave the Node alone.
logger.debug("No need to domify: was passed an XML Node or Source");
source = (singleModel instanceof Node ? new DOMSource((Node) singleModel) : (Source) singleModel);
}
else {
// docRoot local variable takes precedence
source = createXsltSource(model, (docRoot != null ? docRoot : this.root), request, response);
}
doTransform(model, source, request, response);
}
|
public final void resetCachedTemplates() {
this.cachedTemplates = null;
}
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) {
this.cache = 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) {
super.setContentType(contentType);
this.customContentTypeSet = true;
}
|
public void setErrorListener(ErrorListener errorListener) {
this.errorListener = errorListener;
}
|
public void setIndent(boolean indent) {
this.indent = 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. |
public void setOutputProperties(Properties outputProperties) {
this.outputProperties = outputProperties;
}
|
public void setRoot(String root) {
this.root = root;
}
|
public void setStylesheetLocation(Resource stylesheetLocation) {
this.stylesheetLocation = stylesheetLocation;
// Re-cache templates if transformer factory already initialized.
resetCachedTemplates();
}
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. |
public void setTransformerFactoryClass(Class transformerFactoryClass) {
Assert.isAssignable(TransformerFactory.class, transformerFactoryClass);
this.transformerFactoryClass = 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) {
this.uriResolver = uriResolver;
}
|
public void setUseSingleModelNameAsRoot(boolean useSingleModelNameAsRoot) {
this.useSingleModelNameAsRoot = 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 . |
protected boolean useWriter() {
return false;
}
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. |