001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.doc;
030:
031: import com.caucho.util.*;
032: import com.caucho.vfs.Path;
033: import com.caucho.vfs.ReadStream;
034: import com.caucho.vfs.Vfs;
035:
036: import javax.servlet.GenericServlet;
037: import javax.servlet.ServletConfig;
038: import javax.servlet.ServletContext;
039: import javax.servlet.ServletException;
040: import javax.servlet.ServletRequest;
041: import javax.servlet.ServletResponse;
042: import javax.servlet.http.HttpServletRequest;
043: import java.io.IOException;
044: import java.io.PrintWriter;
045: import java.util.logging.Logger;
046: import java.util.regex.Pattern;
047:
048: /**
049: * Servlet to view a source file, with optional emphasis based on regular
050: * expressions.
051: */
052: public class ViewFileServlet extends GenericServlet {
053: static private final Logger log = Logger
054: .getLogger(ViewFileServlet.class.getName());
055: static final L10N L = new L10N(ViewFileServlet.class);
056:
057: static private final String PARAM_CONTEXTPATH = "contextpath";
058: static private final String PARAM_SERVLETPATH = "servletpath";
059: static private final String PARAM_FILE = "file";
060: static private final String PARAM_RE_MARKER = "re-marker";
061: static private final String PARAM_RE_START = "re-start";
062: static private final String PARAM_RE_END = "re-end";
063:
064: ServletContext _context;
065:
066: public void init(ServletConfig config) throws ServletException {
067: super .init(config);
068: _context = config.getServletContext();
069: }
070:
071: public void service(ServletRequest request, ServletResponse response)
072: throws ServletException, IOException {
073: try {
074: viewFile(response.getWriter(), request);
075: } catch (Exception ex) {
076: throw new ServletException(ex);
077: }
078: }
079:
080: private void viewFile(PrintWriter out, ServletRequest request)
081: throws Exception {
082: String file = getFileName(request);
083: Path path = getFilePath(request);
084:
085: if (path != null) {
086: String re_mrk_str = request.getParameter(PARAM_RE_MARKER);
087: String re_beg_str = request.getParameter(PARAM_RE_START);
088: String re_end_str = request.getParameter(PARAM_RE_END);
089:
090: Pattern re_mrk = re_mrk_str == null
091: || re_mrk_str.length() == 0 ? null : Pattern
092: .compile(re_mrk_str);
093: Pattern re_beg = re_beg_str == null
094: || re_beg_str.length() == 0 ? null : Pattern
095: .compile(re_beg_str);
096: Pattern re_end = re_end_str == null
097: || re_end_str.length() == 0 ? null : Pattern
098: .compile(re_end_str);
099:
100: /*
101: * write the verbatim source to the browser.
102: * if re.start (and optionally re.end) are specified,
103: * highlight the corresponding code sections
104: */
105:
106: out.println("<html>");
107: out.println("<head>");
108: out.print("<title>");
109: out.print(Html.escapeHtml(file));
110: out.println("</title>");
111: out.println("<style type='text/css'>");
112: out.println(" .code-highlight { color: #1764FF; }");
113: out
114: .println(" .face-xmlelement { color: #003DB8; font-weight: bold }");
115: out.println("</style>");
116: out.println("</head>");
117: out.println("<body bgcolor=white>");
118: out.print("<code>");
119: out.print("<b>");
120: out.print(Html.escapeHtml(file));
121: out.print("</b>");
122: out.print("</code>");
123: out.println("<p>");
124:
125: ReadStream is;
126: try {
127: is = path.openRead();
128: } catch (java.io.FileNotFoundException ex) {
129: out.println("<font color='red'><b>File not found: "
130: + Html.escapeHtml(path.getPath())
131: + "</b></font>");
132: out.println("</body>");
133: out.println("</html>");
134: return;
135: }
136:
137: String line;
138: out.print("<pre>");
139:
140: boolean h = false; // true if currently highlighting
141: boolean m = false; // true if marked
142:
143: while ((line = is.readln()) != null) {
144: // check for marker
145: if (!m && re_mrk != null
146: && re_mrk.matcher(line).matches()) {
147: out.print("<a name='code-highlight'></a>");
148: m = true;
149: }
150:
151: // check for highlighting begin
152: if (!h && re_beg != null
153: && re_beg.matcher(line).matches()) {
154: h = true;
155: out.print("<b class='code-highlight'>");
156: if (!m && re_mrk == null) {
157: out.print("<a name='code-highlight'></a>");
158: m = true;
159: }
160: }
161:
162: // send string out
163: // handle '<' character and '>' character
164: int l = line.length();
165: for (int i = 0; i < l; i++) {
166: int ch = line.charAt(i);
167: if (ch == '<') {
168: if (h)
169: out.print("<span class='face-xmlelement'>");
170: out.print("<");
171: } else if (ch == '>') {
172: out.print(">");
173: if (h)
174: out.print("</span>");
175: } else
176: out.print((char) ch);
177: }
178: out.println();
179:
180: // check for highlighting end
181: if (h
182: && (re_end == null || (re_end != null && re_end
183: .matcher(line).matches()))) {
184: h = false;
185: out.print("</b>");
186: }
187: }
188:
189: is.close();
190: if (h)
191: out.print("</b>");
192: out.println("</pre>");
193: out.println("</body>");
194: out.println("</html>");
195: return;
196: }
197: }
198:
199: private String getFileName(ServletRequest request) {
200: String f = request.getParameter(PARAM_FILE);
201: if (f != null && f.length() > 0 && f.indexOf("..") < 0) {
202: return f;
203: }
204: return null;
205: }
206:
207: private Path getFilePath(ServletRequest request) {
208: String cp = request.getParameter(PARAM_CONTEXTPATH);
209: String sp = request.getParameter(PARAM_SERVLETPATH);
210: String f = getFileName(request);
211:
212: Path pwd = Vfs.lookup().createRoot();
213:
214: if (f != null) {
215: ServletContext ctx = _context;
216:
217: String requestContext = ((HttpServletRequest) request)
218: .getContextPath();
219:
220: if (cp != null && cp.startsWith(requestContext))
221: cp = cp.substring(requestContext.length());
222:
223: CharBuffer cb = new CharBuffer();
224:
225: if (cp != null)
226: cb.append(cp);
227:
228: cb.append('/');
229: cb.append(f);
230:
231: // return pwd.lookup(ctx.getRealPath(cb.toString()));
232: return pwd.lookup(cb.toString());
233: }
234:
235: return null;
236: }
237:
238: }
|