📄 abstractxsltview.java
字号:
logger.warn("XSLT view is not configured: will copy XML input");
response.setContentType("text/xml; charset=ISO-8859-1");
}
else {
// normal case
response.setContentType(getContentType());
}
/*
* The preferred method has subclasses creating a Source rather than a Node for
* transformation. Support for Nodes is retained for backwards compatibility.
*/
Source source = null;
Node dom = 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
dom = createDomNode(model, (docRoot != null ? docRoot : this.root), request, response);
if (dom != null) {
source = new DOMSource(dom);
}
else {
source = createXsltSource(model, (docRoot != null ? docRoot : this.root), request, response);
}
}
doTransform(model, source, request, response);
}
/**
* Return the XML <code>Source</code> to transform. Subclasses must implement
* <b>either</b> this method <b>or</b> <code>createDomNode</code>, which is
* retained only for backward compatibility.
* @param model the model Map
* @param 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.
* @param request HTTP request. Subclasses won't normally use this, as
* request processing should have been complete. However, we might to
* create a RequestContext to expose as part of the model.
* @param response HTTP response. Subclasses won't normally use this,
* however there may sometimes be a need to set cookies.
* @return the XSLT Source to transform
* @throws Exception we let this method throw any exception; the
* AbstractXlstView superclass will catch exceptions
*/
protected Source createXsltSource(
Map model, String root, HttpServletRequest request, HttpServletResponse response)
throws Exception {
return null;
}
/**
* Return the XML <code>Node</code> to transform.
* <p>
* This method is deprecated from version 1.2 with the preferred extension point
* being <code>createXsltSource(Map, String, HttpServletRequest, HttpServletResponse)</code>
* instead. Code that previously implemented this method can now override the preferred
* method, returning <code>new DOMSource(node)</code> in place of returning <code>node</code>
* @param model the model Map
* @param 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.
* @param request HTTP request. Subclasses won't normally use this, as
* request processing should have been complete. However, we might to
* create a RequestContext to expose as part of the model.
* @param response HTTP response. Subclasses won't normally use this,
* however there may sometimes be a need to set cookies.
* @return the XML node to transform
* @throws Exception we let this method throw any exception; the
* AbstractXlstView superclass will catch exceptions
* @deprecated in favor of <code>createXsltSource(Map, String, HttpServletRequest, HttpServletResponse)</code>
*/
protected Node createDomNode(Map model, String root, HttpServletRequest request, HttpServletResponse response)
throws Exception {
return null;
}
/**
* Perform the actual transformation, writing to the HTTP response.
* <p>Default implementation delegates to the doTransform version
* that takes a Result argument, building a StreamResult for the
* ServletResponse OutputStream.
* @param model the model Map
* @param dom the XNL node to transform
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception we let this method throw any exception; the
* AbstractXlstView superclass will catch exceptions
* @see #doTransform(Node, Map, Result, String)
* @see javax.xml.transform.stream.StreamResult
* @see javax.servlet.ServletResponse#getOutputStream
* @see #doTransform(Map, Source, HttpServletRequest, HttpServletResponse)
* @deprecated the preferred method is <code>doTransform</code> with a Source argument
* @see #doTransform(java.util.Map, javax.xml.transform.Source, HttpServletRequest, HttpServletResponse)
*/
protected void doTransform(Map model, Node dom, HttpServletRequest request, HttpServletResponse response)
throws Exception {
doTransform(new DOMSource(dom), getParameters(request),
new StreamResult(new BufferedOutputStream(response.getOutputStream())),
response.getCharacterEncoding());
}
/**
* Perform the actual transformation, writing to the HTTP response.
* <p>Default implementation delegates to the doTransform version
* that takes a Result argument, building a StreamResult for the
* ServletResponse OutputStream.
* @param model the model Map
* @param source the Source to transform
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception we let this method throw any exception; the
* AbstractXlstView superclass will catch exceptions
* @see #doTransform(Node, Map, Result, String)
* @see javax.xml.transform.stream.StreamResult
* @see javax.servlet.ServletResponse#getOutputStream
*/
protected void doTransform(Map model, Source source, HttpServletRequest request, HttpServletResponse response)
throws Exception {
doTransform(source, getParameters(request),
new StreamResult(new BufferedOutputStream(response.getOutputStream())),
response.getCharacterEncoding());
}
/**
* Perform the actual transformation, writing to the given result.
* Simply delegates to the
* <code>doTransform(Source, Map, Result, String)</code> version.
* @param dom the XML node to transform
* @param parameters a Map of parameters to be applied to the stylesheet
* @param result the result to write to
* @throws Exception we let this method throw any exception; the
* AbstractXlstView superclass will catch exceptions
* @see #doTransform(Source, Map, Result, String)
* @deprecated the preferred method is
* <code>doTransform(Source source, Map parameters, Result result, String encoding)</code>
*/
protected void doTransform(Node dom, Map parameters, Result result, String encoding)
throws Exception {
doTransform(new DOMSource(dom), parameters, result, encoding);
}
/**
* Perform the actual transformation, writing to the given result.
* @param source the Source to transform
* @param parameters a Map of parameters to be applied to the stylesheet
* @param result the result to write to
* @throws Exception we let this method throw any exception; the
* AbstractXlstView superclass will catch exceptions
*/
protected void doTransform(Source source, Map parameters, Result result, String encoding)
throws Exception {
try {
Transformer trans = (this.templates != null) ?
this.templates.newTransformer() : // we have a stylesheet
this.transformerFactory.newTransformer(); // just a copy
// Explicitly apply URIResolver to every created Transformer.
if (this.uriResolver != null) {
trans.setURIResolver(this.uriResolver);
}
// Apply any subclass supplied parameters to the transformer.
if (parameters != null) {
for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
trans.setParameter(entry.getKey().toString(), entry.getValue());
}
if (logger.isDebugEnabled()) {
logger.debug("Added parameters [" + parameters + "] to transformer object");
}
}
// Specify default output properties.
trans.setOutputProperty(OutputKeys.ENCODING, encoding);
if (this.indent) {
trans.setOutputProperty(OutputKeys.INDENT, "yes");
}
// Xalan-specific, but won't do any harm in other XSLT engines.
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// 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);
if (logger.isDebugEnabled()) {
logger.debug("XSLT transformed with stylesheet [" + this.stylesheetLocation + "]");
}
}
catch (TransformerConfigurationException ex) {
throw new NestedServletException("Couldn't create XSLT transformer for stylesheet [" +
this.stylesheetLocation + "] in XSLT view with name [" + getBeanName() + "]", ex);
}
catch (TransformerException ex) {
throw new NestedServletException("Couldn't perform transform with stylesheet [" +
this.stylesheetLocation + "] in XSLT view with name [" + getBeanName() + "]", ex);
}
}
/**
* 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.
* <p>Default implementation delegates to simple
* <code>getParameters</code> version.
* @param request current HTTP request
* @return a Map of parameters to apply to the transformation process
* @see #getParameters()
* @see javax.xml.transform.Transformer#setParameter
*/
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.
* <p>Default implementation delegates simply returns <code>null</code>.
* @return a Map of parameters to apply to the transformation process
* @see #getParameters(HttpServletRequest)
* @see javax.xml.transform.Transformer#setParameter
*/
protected Map getParameters() {
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -