Source Code Cross Referenced for ViewFileServlet.java in  » EJB-Server-resin-3.1.5 » webutil » com » caucho » doc » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server resin 3.1.5 » webutil » com.caucho.doc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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("&lt;");
171:                            } else if (ch == '>') {
172:                                out.print("&gt;");
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.