KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > doc > ViewFileServlet


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29 package com.caucho.doc;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.L10N;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.ReadStream;
35 import com.caucho.vfs.Vfs;
36
37 import javax.servlet.GenericServlet JavaDoc;
38 import javax.servlet.ServletConfig JavaDoc;
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.ServletRequest JavaDoc;
42 import javax.servlet.ServletResponse JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.PrintWriter JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47 import java.util.regex.Pattern JavaDoc;
48
49 /**
50  * Servlet to view a source file, with optional emphasis based on regular
51  * expressions.
52  */

53 public class ViewFileServlet extends GenericServlet JavaDoc {
54   static private final Logger JavaDoc log =
55     Logger.getLogger(ViewFileServlet.class.getName());
56   static final L10N L = new L10N(ViewFileServlet.class);
57
58   static private final String JavaDoc PARAM_CONTEXTPATH = "contextpath";
59   static private final String JavaDoc PARAM_SERVLETPATH = "servletpath";
60   static private final String JavaDoc PARAM_FILE = "file";
61   static private final String JavaDoc PARAM_RE_MARKER = "re-marker";
62   static private final String JavaDoc PARAM_RE_START = "re-start";
63   static private final String JavaDoc PARAM_RE_END = "re-end";
64
65   ServletContext JavaDoc _context;
66
67   public void init(ServletConfig JavaDoc config)
68     throws ServletException JavaDoc
69   {
70     super.init(config);
71     _context = config.getServletContext();
72   }
73
74   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
75     throws ServletException JavaDoc, IOException JavaDoc
76   {
77     try {
78       viewFile(response.getWriter(), request);
79     }
80     catch (Exception JavaDoc ex) {
81       throw new ServletException JavaDoc(ex);
82     }
83   }
84
85   private void viewFile(PrintWriter JavaDoc out, ServletRequest JavaDoc request)
86     throws Exception JavaDoc
87   {
88     String JavaDoc file = getFileName(request);
89     Path path = getFilePath(request);
90
91     if (path != null) {
92       String JavaDoc re_mrk_str = request.getParameter(PARAM_RE_MARKER);
93       String JavaDoc re_beg_str = request.getParameter(PARAM_RE_START);
94       String JavaDoc re_end_str = request.getParameter(PARAM_RE_END);
95
96       Pattern JavaDoc re_mrk = re_mrk_str == null || re_mrk_str.length() == 0 ? null : Pattern.compile(re_mrk_str);
97       Pattern JavaDoc re_beg = re_beg_str == null || re_beg_str.length() == 0 ? null : Pattern.compile(re_beg_str);
98       Pattern JavaDoc re_end = re_end_str == null || re_end_str.length() == 0 ? null : Pattern.compile(re_end_str);
99
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(file);
110       out.println("</title>");
111       out.println("<style type='text/css'>");
112       out.println(" .code-highlight { color: #1764FF; }");
113       out.println(" .face-xmlelement { color: #003DB8; font-weight: bold }");
114       out.println("</style>");
115       out.println("</head>");
116       out.println("<body bgcolor=white>");
117       out.print("<code>");
118       out.print("<b>");
119       out.print(file);
120       out.print("</b>");
121       out.print("</code>");
122       out.println("<p>");
123
124       ReadStream is;
125       try {
126         is = path.openRead();
127       } catch (java.io.FileNotFoundException JavaDoc ex) {
128         out.println("<font color='red'><b>File not found " + path.getPath() + "</b></font>");
129         out.println("</body>");
130         out.println("</html>");
131         return;
132       }
133
134       String JavaDoc line;
135       out.print("<pre>");
136
137       boolean h = false; // true if currently highlighting
138
boolean m = false; // true if marked
139

140       while ((line = is.readln()) != null) {
141         // check for marker
142
if (!m && re_mrk != null && re_mrk.matcher(line).matches()) {
143           out.print("<a name='code-highlight'></a>");
144           m = true;
145         }
146
147         // check for highlighting begin
148
if (!h && re_beg != null && re_beg.matcher(line).matches()) {
149           h = true;
150           out.print("<b class='code-highlight'>");
151           if (!m && re_mrk == null) {
152             out.print("<a name='code-highlight'></a>");
153             m = true;
154           }
155         }
156
157         // send string out
158
// handle '<' character and '>' character
159
int l = line.length();
160         for (int i = 0; i < l; i++) {
161           int ch = line.charAt(i);
162           if (ch == '<') {
163             if (h) out.print("<span class='face-xmlelement'>");
164             out.print("&lt;");
165           } else if (ch == '>') {
166             out.print("&gt;");
167             if (h) out.print("</span>");
168           }
169           else
170             out.print((char) ch);
171         }
172         out.println();
173
174         // check for highlighting end
175
if (h && (re_end == null || (re_end != null && re_end.matcher(line).matches()))) {
176           h = false;
177           out.print("</b>");
178         }
179       }
180
181       is.close();
182       if (h)
183         out.print("</b>");
184       out.println("</pre>");
185       out.println("</body>");
186       out.println("</html>");
187       return;
188     }
189   }
190
191   private String JavaDoc getFileName(ServletRequest JavaDoc request)
192   {
193     String JavaDoc f = request.getParameter(PARAM_FILE);
194     if (f != null && f.length() > 0 && f.indexOf("..") < 0) {
195       return f;
196     }
197     return null;
198   }
199
200   private Path getFilePath(ServletRequest JavaDoc request)
201   {
202     String JavaDoc cp = request.getParameter(PARAM_CONTEXTPATH);
203     String JavaDoc sp = request.getParameter(PARAM_SERVLETPATH);
204     String JavaDoc f = getFileName(request);
205
206     Path pwd = Vfs.lookup().createRoot();
207
208     if (f != null) {
209       ServletContext JavaDoc ctx = _context;
210
211       String JavaDoc requestContext = ((HttpServletRequest JavaDoc) request).getContextPath();
212
213       if (cp != null && cp.startsWith(requestContext))
214     cp = cp.substring(requestContext.length());
215
216       CharBuffer cb = new CharBuffer();
217
218       if (cp != null)
219     cb.append(cp);
220
221       cb.append('/');
222       cb.append(f);
223
224       // return pwd.lookup(ctx.getRealPath(cb.toString()));
225
return pwd.lookup(cb.toString());
226     }
227
228     return null;
229   }
230
231 }
232
233
Popular Tags