1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.view.xslt;
  17. import java.io.BufferedOutputStream;
  18. import java.io.IOException;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import javax.servlet.ServletException;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import javax.xml.transform.OutputKeys;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Templates;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerConfigurationException;
  29. import javax.xml.transform.TransformerException;
  30. import javax.xml.transform.TransformerFactory;
  31. import javax.xml.transform.URIResolver;
  32. import javax.xml.transform.dom.DOMSource;
  33. import javax.xml.transform.stream.StreamResult;
  34. import javax.xml.transform.stream.StreamSource;
  35. import org.w3c.dom.Node;
  36. import org.springframework.context.ApplicationContextException;
  37. import org.springframework.core.io.Resource;
  38. import org.springframework.web.servlet.view.AbstractView;
  39. /**
  40. * Convenient superclass for views rendered using an XSLT stylesheet.
  41. * Subclasses must provide the XML W3C document to transform.
  42. * They do not need to concern themselves with XSLT.
  43. *
  44. * <p>Properties:
  45. * <ul>
  46. * <li>stylesheet: no transform is null
  47. * <li>root: name of the root element
  48. * <li>uriResolver: URIResolver used in the transform
  49. * <li>cache (optional, default=true): debug setting only
  50. * </ul>
  51. *
  52. * <p>Setting cache to false will cause the templates object to be reloaded
  53. * for each rendering. This is useful during development, but will seriously
  54. * affect performance in production and isn't threadsafe.
  55. *
  56. * @author Rod Johnson
  57. * @author Darren Davison
  58. * @version $Id: AbstractXsltView.java,v 1.11 2004/05/26 10:48:56 jhoeller Exp $
  59. */
  60. public abstract class AbstractXsltView extends AbstractView {
  61. public static final String DEFAULT_ROOT = "DocRoot";
  62. /** URL of stylesheet */
  63. private Resource stylesheetLocation;
  64. /** Document root element name, normally overridden in the view definition config */
  65. private String root = DEFAULT_ROOT;
  66. /** Custom URIResolver, set by subclass or as bean property */
  67. private URIResolver uriResolver;
  68. private boolean cache = true;
  69. private TransformerFactory transformerFactory;
  70. /** XSLT Template */
  71. private Templates templates;
  72. /**
  73. * Set the location of the XSLT stylesheet.
  74. * @param stylesheetLocation the location of the XSLT stylesheet
  75. * @see org.springframework.context.ApplicationContext#getResource
  76. */
  77. public void setStylesheetLocation(Resource stylesheetLocation) {
  78. this.stylesheetLocation = stylesheetLocation;
  79. }
  80. /**
  81. * Document root element name. Default is "DocRoot".
  82. * Only used if we're not passed a single Node as model.
  83. * @param root document root element name
  84. * @see #DEFAULT_ROOT
  85. */
  86. public void setRoot(String root) {
  87. this.root = root;
  88. }
  89. /**
  90. * Set the URIResolver used in the transform. The URIResolver
  91. * handles calls to the XSLT document() function.
  92. * This method can be used by subclasses or as a bean property.
  93. * @param uriResolver URIResolver to set. No URIResolver
  94. * will be set if this is null (this is the default).
  95. */
  96. public void setUriResolver(URIResolver uriResolver) {
  97. this.uriResolver = uriResolver;
  98. }
  99. /**
  100. * Set whether to activate the cache. Default is true.
  101. */
  102. public void setCache(boolean cache) {
  103. this.cache = cache;
  104. }
  105. /**
  106. * Here we load our template, as we need the ApplicationContext to do it.
  107. */
  108. protected final void initApplicationContext() throws ApplicationContextException {
  109. this.transformerFactory = TransformerFactory.newInstance();
  110. if (this.uriResolver != null) {
  111. logger.info("Using custom URIResolver [" + this.uriResolver + "] in XSLT view with name '" + getBeanName() + "'");
  112. this.transformerFactory.setURIResolver(this.uriResolver);
  113. }
  114. logger.debug("URL in view is " + this.stylesheetLocation);
  115. cacheTemplates();
  116. }
  117. private void cacheTemplates() throws ApplicationContextException {
  118. if (this.stylesheetLocation != null && !"".equals(this.stylesheetLocation)) {
  119. try {
  120. this.templates = this.transformerFactory.newTemplates(getStylesheetSource(this.stylesheetLocation));
  121. logger.debug("Loaded templates [" + this.templates + "] in XSLT view '" + getBeanName() + "'");
  122. }
  123. catch (TransformerConfigurationException ex) {
  124. throw new ApplicationContextException(
  125. "Can't load stylesheet from " + this.stylesheetLocation + " in XSLT view '" + getBeanName() + "'", ex);
  126. }
  127. }
  128. }
  129. /**
  130. * Load the stylesheet. Subclasses can override this.
  131. */
  132. protected Source getStylesheetSource(Resource stylesheetLocation) throws ApplicationContextException {
  133. logger.debug("Loading XSLT stylesheet from " + stylesheetLocation);
  134. try {
  135. return new StreamSource(stylesheetLocation.getInputStream());
  136. }
  137. catch (IOException ex) {
  138. throw new ApplicationContextException("Can't load XSLT stylesheet from " + stylesheetLocation, ex);
  139. }
  140. }
  141. protected final void renderMergedOutputModel(Map model, HttpServletRequest request,
  142. HttpServletResponse response) throws Exception {
  143. if (!this.cache) {
  144. logger.warn("DEBUG SETTING: NOT THREADSAFE AND WILL IMPAIR PERFORMANCE: template will be refreshed");
  145. cacheTemplates();
  146. }
  147. if (this.templates == null) {
  148. if (this.transformerFactory == null) {
  149. throw new ServletException("XLST view is incorrectly configured. Templates AND TransformerFactory are null");
  150. }
  151. logger.warn("XSLT view is not configured: will copy XML input");
  152. response.setContentType("text/xml; charset=ISO-8859-1");
  153. }
  154. else {
  155. // normal case
  156. response.setContentType(getContentType());
  157. }
  158. Node dom = null;
  159. String docRoot = null;
  160. // value of a single element in the map, if there is one
  161. Object singleModel = null;
  162. if (model.size() == 1) {
  163. docRoot = (String) model.keySet().iterator().next();
  164. logger.info("Single model object received, keyname [" + docRoot + "] will be used as root tag name");
  165. singleModel = model.get(docRoot);
  166. }
  167. // handle special case when we have a single node
  168. if (singleModel != null && (singleModel instanceof Node)) {
  169. // Don't domify if the model is already an XML node.
  170. // We don't need to worry about model name, either:
  171. // we leave the Node alone.
  172. logger.debug("No need to domify: was passed an XML node");
  173. dom = (Node) singleModel;
  174. }
  175. else
  176. // docRoot local variable takes precedence
  177. dom = createDomNode(model, (docRoot == null) ? this.root : docRoot, request, response);
  178. doTransform(response, dom);
  179. }
  180. /**
  181. * Return the XML node to transform.
  182. * Subclasses must implement this method.
  183. * @param model the model Map
  184. * @param root name for root element. This can be supplied as a bean property
  185. * to concrete subclasses within the view definition file, but will be overridden
  186. * in the case of a single object in the model map to be the key for that object.
  187. * If no root property is specified and multiple model objects exist, a default
  188. * root tag name will be supplied.
  189. * @param request HTTP request. Subclasses won't normally use this, as
  190. * request processing should have been complete. However, we might to
  191. * create a RequestContext to expose as part of the model.
  192. * @param response HTTP response. Subclasses won't normally use this,
  193. * however there may sometimes be a need to set cookies.
  194. * @throws Exception we let this method throw any exception; the
  195. * AbstractXlstView superclass will catch exceptions
  196. */
  197. protected abstract Node createDomNode(Map model, String root, HttpServletRequest request,
  198. HttpServletResponse response) throws Exception;
  199. /**
  200. * Return a <code>Map</code> of parameters to be applied to the stylesheet. Subclasses
  201. * can override the default implementation (which simply returns null) in order to
  202. * apply one or more parameters to the transformation process.
  203. * @return a Map of parameters to apply to the transformation process
  204. * @see javax.xml.transform.Transformer#setParameter
  205. */
  206. protected Map getParameters() {
  207. return null;
  208. }
  209. /**
  210. * Use TrAX to perform the transform.
  211. */
  212. protected void doTransform(HttpServletResponse response, Node dom) throws ServletException, IOException {
  213. try {
  214. Transformer trans = (this.templates != null) ?
  215. this.templates.newTransformer() : // we have a stylesheet
  216. this.transformerFactory.newTransformer(); // just a copy
  217. // apply any subclass supplied parameters to the transformer
  218. Map parameters = getParameters();
  219. if (parameters != null) {
  220. for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) {
  221. Map.Entry entry = (Map.Entry) iter.next();
  222. trans.setParameter(entry.getKey().toString(), entry.getValue());
  223. }
  224. logger.debug("Added parameters [" + parameters + "] to transformer object");
  225. }
  226. trans.setOutputProperty(OutputKeys.INDENT, "yes");
  227. // Xalan-specific, but won't do any harm in other XSLT engines
  228. trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
  229. trans.transform(new DOMSource(dom), new StreamResult(new BufferedOutputStream(response.getOutputStream())));
  230. logger.debug("XSLT transformed OK with stylesheet [" + this.stylesheetLocation + "]");
  231. }
  232. catch (TransformerConfigurationException ex) {
  233. throw new ServletException(
  234. "Couldn't create XSLT transformer for stylesheet [" + this.stylesheetLocation +
  235. "] in XSLT view with name [" + getBeanName() + "]", ex);
  236. }
  237. catch (TransformerException ex) {
  238. throw new ServletException(
  239. "Couldn't perform transform with stylesheet [" + this.stylesheetLocation +
  240. "] in XSLT view with name [" + getBeanName() + "]", ex);
  241. }
  242. }
  243. }