Author: ddewolf Date: Fri Dec 8 07:02:24 2006 New Revision: 484626 URL: http://svn.apache.org/viewvc?view=rev&rev=484626 Log: Providing a mechanism which allows only portions of the action to be exposed/converted to XML by the XSLT result
Added: struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/ struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java (with props) struts/struts2/trunk/apps/showcase/src/main/resources/struts-xslt.xml (with props) struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/ struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/environment.xsl struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/index.jsp (with props) Modified: struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java Added: struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java?view=auto&rev=484626 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java (added) +++ struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java Fri Dec 8 07:02:24 2006 @@ -0,0 +1,83 @@ +package org.apache.struts2.showcase.xslt; + +import java.util.Map; +import java.util.Properties; + +import com.opensymphony.xwork2.ActionSupport; + +import org.apache.struts2.interceptor.ServletRequestAware; + +import javax.servlet.http.HttpServletRequest; + +public class JVMAction implements ServletRequestAware { + + private ImportantInfo info; + private Map<String, String> environment; + + /** Captured only to show that undesired data can creep into the result. */ + private HttpServletRequest servletRequest; + + + public String execute() { + environment = System.getenv(); + Properties props = System.getProperties(); + + String classpath = environment.get("CLASSPATH"); + info = new ImportantInfo(classpath, props); + + return ActionSupport.SUCCESS; + } + + + public HttpServletRequest getServletRequest() { + return servletRequest; + } + + public void setServletRequest(HttpServletRequest servletRequest) { + this.servletRequest = servletRequest; + } + + public Map<String, String> getEnvironment() { + return environment; + } + + public void setEnvironment(Map<String, String> environment) { + this.environment = environment; + } + + + public ImportantInfo getInfo() { + return info; + } + + public void setInfo(ImportantInfo info) { + this.info = info; + } + + public class ImportantInfo { + private String classpath; + private Properties systemProperties; + + + public ImportantInfo(String classpath, Properties properties) { + this.classpath = classpath; + this.systemProperties = properties; + } + + public String getClasspath() { + return classpath; + } + + public void setClasspath(String classpath) { + this.classpath = classpath; + } + + public Properties getSystemProperties() { + return systemProperties; + } + + public void setSystemProperties(Properties systemProperties) { + this.systemProperties = systemProperties; + } + } +} Propchange: struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/xslt/JVMAction.java ------------------------------------------------------------------------------ svn:keywords = Id Author Date Rev Added: struts/struts2/trunk/apps/showcase/src/main/resources/struts-xslt.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/resources/struts-xslt.xml?view=auto&rev=484626 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/resources/struts-xslt.xml (added) +++ struts/struts2/trunk/apps/showcase/src/main/resources/struts-xslt.xml Fri Dec 8 07:02:24 2006 @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!DOCTYPE struts PUBLIC + "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" + "http://struts.apache.org/dtds/struts-2.0.dtd"> + +<struts> + <package name="xslt" extends="struts-default" namespace="/xslt"> + <default-action-ref name="index"/> + + <action name="index"> + <result>index.jsp</result> + </action> + + <action name="classpath" class="org.apache.struts2.showcase.xslt.JVMAction"> + <result type="xslt"> + <param name="exposedValue">info.classpath</param> + </result> + </action> + + <action name="jvmInfoRaw" class="org.apache.struts2.showcase.xslt.JVMAction"> + <result type="xslt"> + <param name="exposedValue">info</param> + </result> + </action> + + <action name="jvmInfoAll" class="org.apache.struts2.showcase.xslt.JVMAction"> + <result type="xslt"/> + </action> + + <action name="jvmInfo" class="org.apache.struts2.showcase.xslt.JVMAction"> + <result type="xslt"> + <param name="stylesheetLocation">/xslt/environment.xsl</param> + </result> + </action> + + </package> +</struts> + + Propchange: struts/struts2/trunk/apps/showcase/src/main/resources/struts-xslt.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/struts2/trunk/apps/showcase/src/main/resources/struts-xslt.xml ------------------------------------------------------------------------------ svn:keywords = Id Author Date Rev Modified: struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml?view=diff&rev=484626&r1=484625&r2=484626 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml (original) +++ struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml Fri Dec 8 07:02:24 2006 @@ -43,6 +43,8 @@ <include file="struts-tiles.xml" /> + <include file="struts-xslt.xml" /> + <package name="default" extends="struts-default"> <interceptors> <interceptor-stack name="crudStack"> Added: struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/environment.xsl URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/environment.xsl?view=auto&rev=484626 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/environment.xsl (added) +++ struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/environment.xsl Fri Dec 8 07:02:24 2006 @@ -0,0 +1,25 @@ +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" + xmlns="http://www.w3.org/1999/xhtml"> + + <xsl:template match="/result"> + <html> + <head> + <title>JVM Info</title> + </head> + <body> + <h1>JVM Info</h1> + <xsl:value-of select="info/classpath"/> + <table> + <tr><th>System Property</th><th>Value</th></tr> + <xsl:for-each select="info/systemProperties/entry"> + <tr> + <td><xsl:value-of select="key"/></td> + <td><xsl:value-of select="value"/></td> + </tr> + </xsl:for-each> + </table> + </body> + </html> + </xsl:template> + +</xsl:stylesheet> Added: struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/index.jsp URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/index.jsp?view=auto&rev=484626 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/index.jsp (added) +++ struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/index.jsp Fri Dec 8 07:02:24 2006 @@ -0,0 +1,26 @@ +<[EMAIL PROTECTED] prefix="s" uri="/struts-tags" %> + +<html> +<head> +<title>Showcase - XSLT </title> +</head> +<body> +<h1> XSLT Result Showcase</h1> + +<p> + The XSLT result can be used to generate xml from the action. By default, each of the action's + properties will be converted into a DOM and rendered. These results can be transformed via + xslt. Additionally, the result's exposedValue parameter can be used to define an ognl expression + which can be used to manipulate the object which will be converted to xml. + + <ul> + <li><s:url id="url" namespace="/xslt" action="jvmInfo"/><s:a href="%{url}">Render the exposed portion of the action as html</s:a></li> + <li><s:url id="url" namespace="/xslt" action="jvmInfoRaw"/><s:a href="%{url}">Render the exposed portion of the action as xml</s:a></li> + <li><s:url id="url" namespace="/xslt" action="jvmInfoAll"/><s:a href="%{url}">Render the action as xml</s:a></li> + <li><s:url id="url" namespace="/xslt" action="classpath"/><s:a href="%{url}">Render an ognl property</s:a></li> + </ul> +</p> + + +</body> +</html> \ No newline at end of file Propchange: struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/index.jsp ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/struts2/trunk/apps/showcase/src/main/webapp/xslt/index.jsp ------------------------------------------------------------------------------ svn:keywords = Id Author Date Rev Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java?view=diff&rev=484626&r1=484625&r2=484626 ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java Fri Dec 8 07:02:24 2006 @@ -151,6 +151,19 @@ * </result> * <!-- END SNIPPET: description.example --></pre> * + * <p> + * In the following example the XSLT result would use the action's user property + * instead of the action as it's base document and walk through it's properties. + * The exposedValue uses an ognl expression to derive it's value. + * </p> + * + * <pre> + * <result name="success" type="xslt"> + * <param name="location">foo.xslt</param> + * <param name="exposedValue">user$</param> + * </result> + * </pre> + * * * <b>This result type takes the following parameters:</b> * * <!-- START SNIPPET: params --> @@ -192,17 +205,41 @@ public class XSLTResult implements Result { private static final long serialVersionUID = 6424691441777176763L; - private static final Log log = LogFactory.getLog(XSLTResult.class); + + /** Log instance for this result. */ + private static final Log LOG = LogFactory.getLog(XSLTResult.class); + + /** 'stylesheetLocation' parameter. Points to the xsl. */ public static final String DEFAULT_PARAM = "stylesheetLocation"; + /** Cache of all tempaltes. */ + private static final Map<String, Templates> templatesCache; + + static { + templatesCache = new HashMap<String, Templates>(); + } + + // Configurable Parameters + + /** Determines whether or not the result should allow caching. */ protected boolean noCache; - private final Map<String, Templates> templatesCache; + + /** Indicates the location of the xsl template. */ private String stylesheetLocation; + + /** Indicates the property name patterns which should be exposed to the xml. */ + private String matchingPattern; + + /** Indicates the property name patterns which should be excluded from the xml. */ + private String exludingPattern; + + /** Indicates the ognl expression respresenting the bean which is to be exposed as xml. */ + private String exposedValue; + private boolean parse; private AdapterFactory adapterFactory; public XSLTResult() { - templatesCache = new HashMap<String, Templates>(); } public XSLTResult(String stylesheetLocation) { @@ -232,6 +269,30 @@ return stylesheetLocation; } + public String getExposedValue() { + return exposedValue; + } + + public void setExposedValue(String exposedValue) { + this.exposedValue = exposedValue; + } + + public String getMatchingPattern() { + return matchingPattern; + } + + public void setMatchingPattern(String matchingPattern) { + this.matchingPattern = matchingPattern; + } + + public String getExludingPattern() { + return exludingPattern; + } + + public void setExludingPattern(String exludingPattern) { + this.exludingPattern = exludingPattern; + } + /** * If true, parse the stylesheet location for OGNL expressions. * @@ -250,6 +311,7 @@ location = TextParseUtil.translateVariables(location, stack); } + try { HttpServletResponse response = ServletActionContext.getResponse(); @@ -278,23 +340,29 @@ response.setContentType(mimeType); - Source xmlSource = getDOMSourceForStack(invocation.getAction()); + Object result = invocation.getAction(); + if (exposedValue != null) { + ValueStack stack = invocation.getStack(); + result = stack.findValue(exposedValue); + } + + Source xmlSource = getDOMSourceForStack(result); // Transform the source XML to System.out. PrintWriter out = response.getWriter(); - log.debug("xmlSource = " + xmlSource); + LOG.debug("xmlSource = " + xmlSource); transformer.transform(xmlSource, new StreamResult(out)); out.close(); // ...and flush... - if (log.isDebugEnabled()) { - log.debug("Time:" + (System.currentTimeMillis() - startTime) + "ms"); + if (LOG.isDebugEnabled()) { + LOG.debug("Time:" + (System.currentTimeMillis() - startTime) + "ms"); } writer.flush(); } catch (Exception e) { - log.error("Unable to render XSLT Template, '" + location + "'", e); + LOG.error("Unable to render XSLT Template, '" + location + "'", e); throw e; } } @@ -337,7 +405,7 @@ throw new TransformerException("Stylesheet " + path + " not found in resources."); } - log.debug("Preparing XSLT stylesheet templates: " + path); + LOG.debug("Preparing XSLT stylesheet templates: " + path); TransformerFactory factory = TransformerFactory.newInstance(); templates = factory.newTemplates(new StreamSource(resource.openStream())); @@ -348,8 +416,8 @@ return templates; } - protected Source getDOMSourceForStack(Object action) + protected Source getDOMSourceForStack(Object value) throws IllegalAccessException, InstantiationException { - return new DOMSource(getAdapterFactory().adaptDocument("result", action) ); + return new DOMSource(getAdapterFactory().adaptDocument("result", value) ); } }