001: package com.salmonllc.examples;
002:
003: //The Salmon Open Framework for Internet Applications (SOFIA)
004: //Copyright (C) 1999 - 2002, Salmon LLC
005: //
006: //This program is free software; you can redistribute it and/or
007: //modify it under the terms of the GNU General Public License version 2
008: //as published by the Free Software Foundation;
009: //
010: //This program is distributed in the hope that it will be useful,
011: //but WITHOUT ANY WARRANTY; without even the implied warranty of
012: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: //GNU General Public License for more details.
014: //
015: //You should have received a copy of the GNU General Public License
016: //along with this program; if not, write to the Free Software
017: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: //
019: //For more information please visit http://www.salmonllc.com
020:
021: import com.salmonllc.html.events.*;
022: import com.salmonllc.util.MessageLog;
023: import com.salmonllc.properties.*;
024:
025: import java.io.*;
026:
027: /**
028: * This class provides the controller for SourceView.jsp. It reads a file from the file system and displays it on the page.
029: */
030: public class SourceViewController extends BaseController {
031:
032: //Visual Components
033: public com.salmonllc.html.HtmlText _title;
034: public com.salmonllc.jsp.JspRaw _sourceCode;
035: public com.salmonllc.jsp.JspLink _retToSourceList;
036:
037: public void pageRequested(PageEvent p) throws Exception {
038: super .pageRequested(p);
039:
040: //If the parameter file isn't specified, just blank out the page and return
041: _title.setVisible(false);
042: _sourceCode.setVisible(false);
043: String file = getParameter("file");
044: if (file == null)
045: return;
046:
047: //A security check
048: if (file.indexOf("..") > -1)
049: return;
050:
051: _title.setVisible(true);
052: _sourceCode.setVisible(true);
053:
054: //If the id parm is specified, set the href on the return link so it can jump back to the
055: //source code list, otherwise make the link invisible
056: String id = getParameter("id");
057: if (id == null)
058: _retToSourceList.setVisible(false);
059: else {
060: _retToSourceList.setVisible(true);
061: _retToSourceList.setHref("%SourceCodeList?ID=" + id);
062: }
063:
064: //Figure out where on the file system, the source code file is
065: StringBuffer path = new StringBuffer();
066: Props pr = getPageProperties();
067: String javaSource = pr.getProperty("JavaSource");
068: String jspSource = pr.getProperty("JSPSource");
069: boolean loadFromResource = pr
070: .getBooleanProperty("LoadFromWebRoot");
071:
072: if (file.endsWith(".java"))
073: path.append(javaSource);
074: else if (file.endsWith(".tld") || file.endsWith("web.xml")) {
075: path.append(jspSource);
076: if (loadFromResource)
077: path.append("/..");
078: else
079: path.append(File.separatorChar + "..");
080: } else
081: path.append(jspSource);
082:
083: if (loadFromResource)
084: path.append('/');
085: else
086: path.append(File.separatorChar);
087:
088: for (int i = 0; i < file.length(); i++) {
089: char c = file.charAt(i);
090: if (c == '/' && !loadFromResource)
091: path.append(File.separatorChar);
092: else
093: path.append(c);
094: }
095:
096: file = path.toString();
097: _title.setText(file);
098:
099: //Load the file from the file system into the raw component
100: StringBuffer sourceCode = new StringBuffer();
101: BufferedReader r = null;
102: try {
103: InputStream in = null;
104: if (loadFromResource)
105: in = getPageContext().getServletContext()
106: .getResourceAsStream(file);
107: else
108: in = new FileInputStream(file);
109: r = new BufferedReader(new InputStreamReader(in));
110: } catch (Exception e) {
111: MessageLog.writeErrorMessage("Error Reading File", e, this );
112: _sourceCode.setHtml("Error reading file");
113: return;
114: }
115: String line = r.readLine();
116: while (line != null) {
117: appendLine(line, sourceCode);
118: line = r.readLine();
119: }
120: r.close();
121: _sourceCode.setHtml(sourceCode.toString());
122: }
123:
124: private void appendLine(String line, StringBuffer sb) {
125: //Add a line to the StringBuffer, and fix special html characters
126: for (int i = 0; i < line.length(); i++) {
127: char c = line.charAt(i);
128: if (c == ' ')
129: sb.append(" ");
130: else if (c == '<')
131: sb.append("<");
132: else if (c == '&')
133: sb.append("&");
134: else if (c == '>')
135: sb.append(">");
136: else if (c == '>')
137: sb.append(">");
138: else if (c == '"')
139: sb.append(""");
140: else if (c == '\t')
141: sb.append(" ");
142: else
143: sb.append(c);
144: }
145: sb.append("<BR>");
146: }
147: }
|