FileDocCategorySizeDatePackage
ViewSourceAction.javaAPI DocApache Struts 2.0.9 Apps6195Mon Jul 23 13:43:26 BST 2007org.apache.struts2.showcase.source

ViewSourceAction

public class ViewSourceAction extends com.opensymphony.xwork2.ActionSupport implements org.apache.struts2.util.ServletContextAware
Processes configuration, page, and action class paths to create snippets of the files for display.

Fields Summary
private String
page
private String
className
private String
config
private List
pageLines
private List
classLines
private List
configLines
private int
configLine
private int
padding
private ServletContext
servletContext
Constructors Summary
Methods Summary
public java.lang.Stringexecute()


          

        if (page != null && page.trim().length() > 0) {

            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.trim().length() > 0) {
            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 && config.trim().length() > 0) {
            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;
    
public java.util.ListgetClassLines()

return
the classLines

        return classLines;
    
public java.lang.StringgetClassName()

return
the className

        return className;
    
public java.lang.StringgetConfig()

return
the config

        return config;
    
public intgetConfigLine()

return
the configLine

        return configLine;
    
public java.util.ListgetConfigLines()

return
the configLines

        return configLines;
    
public intgetPadding()

return
the padding

        return padding;
    
public java.lang.StringgetPage()

return
the page

        return page;
    
public java.util.ListgetPageLines()

return
the pageLines

        return pageLines;
    
private java.util.Listread(java.io.InputStream in, int targetLineNumber)
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

        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 voidsetClassName(java.lang.String className)

param
className the className to set

        this.className = className;
    
public voidsetConfig(java.lang.String config)

param
config the config to set

        this.config = config;
    
public voidsetPadding(int padding)

param
padding the padding to set

        this.padding = padding;
    
public voidsetPage(java.lang.String page)

param
page the page to set

        this.page = page;
    
public voidsetServletContext(javax.servlet.ServletContext arg0)

        this.servletContext = arg0;