Author: mrdon Date: Sun Oct 1 04:02:46 2006 New Revision: 451738 URL: http://svn.apache.org/viewvc?view=rev&rev=451738 Log: Added a view source capability to the showcase WW-1461
Added: struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java struts/struts2/trunk/apps/showcase/src/main/webapp/viewSource.jsp Modified: struts/struts2/trunk/apps/showcase/pom.xml struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/web.xml Modified: struts/struts2/trunk/apps/showcase/pom.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/pom.xml?view=diff&rev=451738&r1=451737&r2=451738 ============================================================================== --- struts/struts2/trunk/apps/showcase/pom.xml (original) +++ struts/struts2/trunk/apps/showcase/pom.xml Sun Oct 1 04:02:46 2006 @@ -47,7 +47,7 @@ <artifactId>struts2-sitemesh-plugin</artifactId> <version>${pom.version}</version> </dependency> - + <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-tiles-plugin</artifactId> @@ -61,6 +61,22 @@ <scope>provided</scope> </dependency> + <!-- Velocity --> + <dependency> + <groupId>velocity</groupId> + <artifactId>velocity</artifactId> + <version>1.4</version> + <optional>true</optional> + </dependency> + + <dependency> + <groupId>velocity-tools</groupId> + <artifactId>velocity-tools</artifactId> + <version>1.1</version> + <optional>true</optional> + </dependency> + + <dependency> <groupId>opensymphony</groupId> <artifactId>sitemesh</artifactId> @@ -128,5 +144,15 @@ </dependencies> </plugin> </plugins> + <resources> + <!-- Include resources under src/main/java in WEB-INF/classes --> + <resource> + <directory>src/main/java</directory> + <includes> + <include>**/*.java</include> + </includes> + </resource> + </resources> + </build> </project> Added: struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java?view=auto&rev=451738 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java (added) +++ struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java Sun Oct 1 04:02:46 2006 @@ -0,0 +1,217 @@ +/* + * $Id: CreatePerson.java 420385 2006-07-10 00:57:05Z mrdon $ + * + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts2.showcase.source; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.ServletContext; + +import org.apache.struts2.util.ServletContextAware; + +import com.opensymphony.xwork2.ActionSupport; +import com.opensymphony.xwork2.util.ClassLoaderUtil; + +/** + * Processes configuration, page, and action class paths to create snippets + * of the files for display. + */ +public class ViewSourceAction extends ActionSupport implements ServletContextAware { + + private String page; + private String className; + private String config; + + private List pageLines; + private List classLines; + private List configLines; + + private int configLine; + private int padding = 10; + + private ServletContext servletContext; + + public String execute() throws MalformedURLException, IOException { + + if (page != null) { + + InputStream in = ClassLoaderUtil.getResourceAsStream(page.substring(page.indexOf("//")+1), getClass()); + page = page.replace("//", "/"); + + if (in == null) { + in = servletContext.getResourceAsStream(page); + while (in == null && page.indexOf('/', 1) > 0) { + page = page.substring(page.indexOf('/', 1)); + in = servletContext.getResourceAsStream(page); + } + } + pageLines = read(in, -1); + } + + if (className != null) { + className = "/"+className.replace('.', '/') + ".java"; + InputStream in = getClass().getResourceAsStream(className); + if (in == null) { + in = servletContext.getResourceAsStream("/WEB-INF/src"+className); + } + classLines = read(in, -1); + } + + if (config != null) { + int pos = config.lastIndexOf(':'); + configLine = Integer.parseInt(config.substring(pos+1)); + config = config.substring(0, pos).replace("//", "/"); + configLines = read(new URL(config).openStream(), configLine); + } + return SUCCESS; + } + + /** + * @param className the className to set + */ + public void setClassName(String className) { + this.className = className; + } + + /** + * @param config the config to set + */ + public void setConfig(String config) { + this.config = config; + } + + /** + * @param page the page to set + */ + public void setPage(String page) { + this.page = page; + } + + /** + * @param padding the padding to set + */ + public void setPadding(int padding) { + this.padding = padding; + } + + + + /** + * @return the classLines + */ + public List getClassLines() { + return classLines; + } + + /** + * @return the configLines + */ + public List getConfigLines() { + return configLines; + } + + /** + * @return the pageLines + */ + public List getPageLines() { + return pageLines; + } + + /** + * @return the className + */ + public String getClassName() { + return className; + } + + /** + * @return the config + */ + public String getConfig() { + return config; + } + + /** + * @return the page + */ + public String getPage() { + return page; + } + + /** + * @return the configLine + */ + public int getConfigLine() { + return configLine; + } + + /** + * @return the padding + */ + public int getPadding() { + return padding; + } + + /** + * Reads in a strea, optionally only including the target line number + * and its padding + * + * @param in The input stream + * @param targetLineNumber The target line number, negative to read all + * @return A list of lines + */ + private List read(InputStream in, int targetLineNumber) { + List snippet = null; + if (in != null) { + snippet = new ArrayList(); + int startLine = 0; + int endLine = Integer.MAX_VALUE; + if (targetLineNumber > 0) { + startLine = targetLineNumber - padding; + endLine = targetLineNumber + padding; + } + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + int lineno = 0; + String line; + while ((line = reader.readLine()) != null) { + lineno++; + if (lineno >= startLine && lineno <= endLine) { + snippet.add(line); + } + } + } catch (Exception ex) { + // ignoring as snippet not available isn't a big deal + } + } + return snippet; + } + + public void setServletContext(ServletContext arg0) { + this.servletContext = arg0; + } + + +} 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=451738&r1=451737&r2=451738 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml (original) +++ struts/struts2/trunk/apps/showcase/src/main/resources/struts.xml Sun Oct 1 04:02:46 2006 @@ -8,8 +8,6 @@ <struts> - <include file="struts-default.xml"/> - <include file="struts-chat.xml" /> <include file="struts-hangman.xml" /> @@ -56,6 +54,10 @@ <action name="showcase"> <result>showcase.jsp</result> + </action> + + <action name="viewSource" class="org.apache.struts2.showcase.source.ViewSourceAction"> + <result>viewSource.jsp</result> </action> <action name="date" class="org.apache.struts2.showcase.DateAction"> Modified: struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp?view=diff&rev=451738&r1=451737&r2=451738 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp (original) +++ struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp Sun Oct 1 04:02:46 2006 @@ -5,6 +5,18 @@ response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); + + // Calculate the view sources url + String sourceUrl = request.getContextPath()+"/viewSource.action"; + com.opensymphony.xwork2.ActionInvocation inv = com.opensymphony.xwork2.ActionContext.getContext().getActionInvocation(); + org.apache.struts2.dispatcher.mapper.ActionMapping mapping = org.apache.struts2.ServletActionContext.getActionMapping(); + if (inv != null) { + sourceUrl += "?config="+inv.getProxy().getConfig().getLocation().getURI()+":"+inv.getProxy().getConfig().getLocation().getLineNumber(); + sourceUrl += "&className="+inv.getProxy().getConfig().getClassName(); + sourceUrl += "&page="+mapping.getNamespace()+"/"+((org.apache.struts2.dispatcher.StrutsResultSupport)inv.getResult()).getLastFinalLocation(); + } else { + sourceUrl += "?page="+request.getServletPath(); + } %> <%@ taglib uri="sitemesh-decorator" prefix="decorator" %> @@ -89,7 +101,11 @@ </div><!-- end content --> - + <div> + <p> + <a href="<%=sourceUrl %>">View Sources</a> + </p> + </div> <div id="footer" class="clearfix"> <p>Copyright © 2003-<s:property value="#dateAction.now.year + 1900" /> The Apache Software Foundation.</p> </div><!-- end footer --> Modified: struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/web.xml?view=diff&rev=451738&r1=451737&r2=451738 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/web.xml (original) +++ struts/struts2/trunk/apps/showcase/src/main/webapp/WEB-INF/web.xml Sun Oct 1 04:02:46 2006 @@ -1,6 +1,6 @@ -<?xml version="1.0" encoding="ISO-8859-1"?> -<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> -<web-app> +<?xml version="1.0" encoding="UTF-8"?> +<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> + <display-name>Struts Showcase Application</display-name> Added: struts/struts2/trunk/apps/showcase/src/main/webapp/viewSource.jsp URL: http://svn.apache.org/viewvc/struts/struts2/trunk/apps/showcase/src/main/webapp/viewSource.jsp?view=auto&rev=451738 ============================================================================== --- struts/struts2/trunk/apps/showcase/src/main/webapp/viewSource.jsp (added) +++ struts/struts2/trunk/apps/showcase/src/main/webapp/viewSource.jsp Sun Oct 1 04:02:46 2006 @@ -0,0 +1,53 @@ +<%@ taglib prefix="s" uri="/struts-tags" %> +<html> +<head> + <title>View Sources</title> + + <jsp:include page="/ajax/commonInclude.jsp"/> + <link rel="stylesheet" type="text/css" href="<s:url value="/struts/tabs.css"/>"> + <link rel="stylesheet" type="text/css" href="<s:url value="/struts/niftycorners/niftyCorners.css"/>"> + <link rel="stylesheet" type="text/css" href="<s:url value="/struts/niftycorners/niftyPrint.css"/>" media="print"> + <script type="text/javascript" src="<s:url value="/struts/niftycorners/nifty.js"/>"></script> + <script type="text/javascript"> + window.onload = function() { + if (!NiftyCheck()) + return; + Rounded("li.tab_selected", "top", "white", "transparent", "border #ffffffS"); + Rounded("li.tab_unselected", "top", "white", "transparent", "border #ffffffS"); + // Rounded("div#tab_header_main li","top","white","transparent","border #ffffffS"); + // "white" needs to be replaced with the background color + } + </script> +</head> +<body> +<h1>View Sources</h1> + +<s:tabbedPanel id="test" theme="ajax"> + <s:panel id="one" tabName="Page" theme="ajax"> + <h3>${empty page ? "Unknown page" : page}</h3> +<pre> +<s:iterator value="pageLines" status="row"> +${row.count}: <s:property/></s:iterator> +</pre> + </s:panel> + <s:panel id="two" tabName="Configuration" theme="ajax" > + <h3>${empty config ? "Unknown configuration" : config}</h3> +<pre> + +<s:iterator value="configLines" status="row"><s:if test="%{(#row.count-1)==(configLines.size()/2)}"> +<span style="background-color:yellow">${configLine - padding + row.count - 1}: <s:property/></span></s:if><s:else> +${configLine - padding + row.count - 1}: <s:property/></s:else></s:iterator> +</pre> + </s:panel> + <s:panel id="three" tabName="Java Action" theme="ajax"> + <h3>${empty className ? "Unknown or unavailable Action class" : className}</h3> +<pre> +<s:iterator value="classLines" status="row"> +${row.count}: <s:property/></s:iterator> +</pre> + </s:panel> + +</s:tabbedPanel> + +</body> +</html>