001: /*
002: * $Id: ViewSourceAction.java 570513 2007-08-28 18:14:00Z jholmes $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.source;
022:
023: import java.io.BufferedReader;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: import javax.servlet.ServletContext;
033:
034: import org.apache.struts2.util.ServletContextAware;
035:
036: import com.opensymphony.xwork2.ActionSupport;
037: import com.opensymphony.xwork2.util.ClassLoaderUtil;
038:
039: /**
040: * Processes configuration, page, and action class paths to create snippets
041: * of the files for display.
042: */
043: public class ViewSourceAction extends ActionSupport implements
044: ServletContextAware {
045:
046: private String page;
047: private String className;
048: private String config;
049:
050: private List pageLines;
051: private List classLines;
052: private List configLines;
053:
054: private int configLine;
055: private int padding = 10;
056:
057: private ServletContext servletContext;
058:
059: public String execute() throws MalformedURLException, IOException {
060:
061: if (page != null && page.trim().length() > 0) {
062:
063: InputStream in = ClassLoaderUtil.getResourceAsStream(page
064: .substring(page.indexOf("//") + 1), getClass());
065: page = page.replace("//", "/");
066:
067: if (in == null) {
068: in = servletContext.getResourceAsStream(page);
069: while (in == null && page.indexOf('/', 1) > 0) {
070: page = page.substring(page.indexOf('/', 1));
071: in = servletContext.getResourceAsStream(page);
072: }
073: }
074: pageLines = read(in, -1);
075:
076: if (in != null) {
077: in.close();
078: }
079: }
080:
081: if (className != null && className.trim().length() > 0) {
082: className = "/" + className.replace('.', '/') + ".java";
083: InputStream in = getClass().getResourceAsStream(className);
084: if (in == null) {
085: in = servletContext.getResourceAsStream("/WEB-INF/src"
086: + className);
087: }
088: classLines = read(in, -1);
089:
090: if (in != null) {
091: in.close();
092: }
093: }
094:
095: if (config != null && config.trim().length() > 0) {
096: int pos = config.lastIndexOf(':');
097: configLine = Integer.parseInt(config.substring(pos + 1));
098: config = config.substring(0, pos).replace("//", "/");
099: configLines = read(new URL(config).openStream(), configLine);
100: }
101: return SUCCESS;
102: }
103:
104: /**
105: * @param className the className to set
106: */
107: public void setClassName(String className) {
108: this .className = className;
109: }
110:
111: /**
112: * @param config the config to set
113: */
114: public void setConfig(String config) {
115: this .config = config;
116: }
117:
118: /**
119: * @param page the page to set
120: */
121: public void setPage(String page) {
122: this .page = page;
123: }
124:
125: /**
126: * @param padding the padding to set
127: */
128: public void setPadding(int padding) {
129: this .padding = padding;
130: }
131:
132: /**
133: * @return the classLines
134: */
135: public List getClassLines() {
136: return classLines;
137: }
138:
139: /**
140: * @return the configLines
141: */
142: public List getConfigLines() {
143: return configLines;
144: }
145:
146: /**
147: * @return the pageLines
148: */
149: public List getPageLines() {
150: return pageLines;
151: }
152:
153: /**
154: * @return the className
155: */
156: public String getClassName() {
157: return className;
158: }
159:
160: /**
161: * @return the config
162: */
163: public String getConfig() {
164: return config;
165: }
166:
167: /**
168: * @return the page
169: */
170: public String getPage() {
171: return page;
172: }
173:
174: /**
175: * @return the configLine
176: */
177: public int getConfigLine() {
178: return configLine;
179: }
180:
181: /**
182: * @return the padding
183: */
184: public int getPadding() {
185: return padding;
186: }
187:
188: /**
189: * Reads in a strea, optionally only including the target line number
190: * and its padding
191: *
192: * @param in The input stream
193: * @param targetLineNumber The target line number, negative to read all
194: * @return A list of lines
195: */
196: private List read(InputStream in, int targetLineNumber) {
197: List snippet = null;
198: if (in != null) {
199: snippet = new ArrayList();
200: int startLine = 0;
201: int endLine = Integer.MAX_VALUE;
202: if (targetLineNumber > 0) {
203: startLine = targetLineNumber - padding;
204: endLine = targetLineNumber + padding;
205: }
206: try {
207: BufferedReader reader = new BufferedReader(
208: new InputStreamReader(in));
209:
210: int lineno = 0;
211: String line;
212: while ((line = reader.readLine()) != null) {
213: lineno++;
214: if (lineno >= startLine && lineno <= endLine) {
215: snippet.add(line);
216: }
217: }
218: } catch (Exception ex) {
219: // ignoring as snippet not available isn't a big deal
220: }
221: }
222: return snippet;
223: }
224:
225: public void setServletContext(ServletContext arg0) {
226: this.servletContext = arg0;
227: }
228: }
|