1 5 package com.opensymphony.webwork.views.xslt; 6 7 import com.opensymphony.webwork.ServletActionContext; 8 import com.opensymphony.webwork.config.Configuration; 9 import com.opensymphony.xwork.ActionContext; 10 import com.opensymphony.xwork.ActionInvocation; 11 import com.opensymphony.xwork.Result; 12 import com.opensymphony.xwork.util.OgnlValueStack; 13 import com.opensymphony.xwork.util.TextParseUtil; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 import javax.servlet.http.HttpServletResponse ; 18 import javax.xml.transform.*; 19 import javax.xml.transform.dom.DOMSource ; 20 import javax.xml.transform.stream.StreamResult ; 21 import javax.xml.transform.stream.StreamSource ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.Writer ; 25 import java.net.URL ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 30 33 public class XSLTResult implements Result { 34 36 private static final Log log = LogFactory.getLog(XSLTResult.class); 37 public static final String DEFAULT_PARAM = "location"; 38 39 41 protected boolean noCache = false; 42 private Map templatesCache; 43 private String location; 44 private boolean parse; 45 46 48 public XSLTResult() { 49 templatesCache = new HashMap (); 50 noCache = Configuration.getString("webwork.xslt.nocache").trim().equalsIgnoreCase("true"); 51 } 52 53 55 public void setLocation(String location) { 56 this.location = location; 57 } 58 59 public void setParse(boolean parse) { 60 this.parse = parse; 61 } 62 63 public void execute(ActionInvocation invocation) throws Exception { 64 long startTime = System.currentTimeMillis(); 65 66 if (parse) { 67 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 68 location = TextParseUtil.translateVariables(location, stack); 69 } 70 71 try { 72 HttpServletResponse response = ServletActionContext.getResponse(); 73 74 Writer writer = response.getWriter(); 75 76 Templates templates = getTemplates(location); 78 Transformer transformer = templates.newTransformer(); 79 80 String mimeType = templates.getOutputProperties().getProperty(OutputKeys.MEDIA_TYPE); 81 82 if (mimeType == null) { 83 mimeType = "text/html"; 85 } 86 87 response.setContentType(mimeType); 88 89 Source xmlSource = getTraxSourceForStack(invocation.getAction()); 90 91 PrintWriter out = response.getWriter(); 93 94 transformer.transform(xmlSource, new StreamResult (out)); 95 96 out.close(); 98 if (log.isDebugEnabled()) { 99 log.debug("Time:" + (System.currentTimeMillis() - startTime) + "ms"); 100 } 101 102 writer.flush(); 103 } catch (Exception e) { 104 log.error("Unable to render XSLT Template, '" + location + "'", e); 105 throw e; 106 } 107 } 108 109 private Templates getTemplates(String path) throws TransformerException, IOException { 110 String pathFromRequest = ServletActionContext.getRequest().getParameter("xslt.location"); 111 112 if (pathFromRequest != null) { 113 path = pathFromRequest; 114 } 115 116 if (path == null) { 117 throw new TransformerException("Stylesheet path is null"); 118 } 119 120 Templates templates = (Templates) templatesCache.get(path); 121 122 if (noCache || (templates == null)) { 123 synchronized (templatesCache) { 124 URL resource = ServletActionContext.getServletContext().getResource(path); 125 126 if (resource == null) { 127 throw new TransformerException("Stylesheet " + path + " not found in resources."); 128 } 129 130 if ( 131 log.isDebugEnabled()) { 134 log.debug("Preparing new XSLT stylesheet: " + path); 137 } 138 139 TransformerFactory factory = TransformerFactory.newInstance(); 140 log.trace("Uri-Resolver is: " + factory.getURIResolver()); 141 factory.setURIResolver(new ServletURIResolver(ServletActionContext.getServletContext())); 142 log.trace("Uri-Resolver is: " + factory.getURIResolver()); 143 templates = factory.newTemplates(new StreamSource (resource.openStream())); 144 templatesCache.put(path, templates); 145 } 146 } 147 148 return templates; 149 } 150 151 private Source getTraxSourceForStack(Object action) throws IllegalAccessException , InstantiationException { 152 return new DOMSource (new DOMAdapter().adapt(action)); 153 } 154 } 155 | Popular Tags |