KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xtpdoc > ResinDocServlet


1 /*
2  * Copyright (c) 1998-2006 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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.xtpdoc;
31
32 import com.caucho.config.Config;
33 import com.caucho.config.LineConfigException;
34 import com.caucho.vfs.Path;
35 import com.caucho.vfs.ReadStream;
36 import com.caucho.vfs.Vfs;
37
38 import javax.servlet.ServletConfig JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.http.HttpServlet JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import javax.xml.stream.FactoryConfigurationError;
44 import javax.xml.stream.XMLOutputFactory;
45 import javax.xml.stream.XMLStreamWriter;
46 import java.io.IOException JavaDoc;
47 import java.io.OutputStream JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50
51 public class ResinDocServlet extends HttpServlet JavaDoc {
52   private static Logger JavaDoc log = Logger.getLogger(ResinDocServlet.class.getName());
53
54   private String JavaDoc _contextPath;
55   private Config _config;
56   private Path _pwd;
57   private XMLOutputFactory _outputFactory;
58   private String JavaDoc _encoding = "utf-8";
59
60   public void setDocumentEncoding(String JavaDoc encoding)
61   {
62     _encoding = encoding;
63   }
64
65   public void setDocContextPath(String JavaDoc contextPath)
66   {
67     _contextPath = contextPath;
68   }
69
70   public void init(ServletConfig JavaDoc config)
71     throws ServletException JavaDoc
72   {
73     super.init(config);
74     
75     _config = new Config();
76     _pwd = Vfs.lookup().createRoot();
77
78     if (_contextPath == null)
79       _contextPath = config.getServletContext().getServletContextName();
80
81     try {
82       _outputFactory = XMLOutputFactory.newInstance();
83     } catch (FactoryConfigurationError e) {
84       throw new ServletException JavaDoc("Error configuring factory", e);
85     }
86
87     if (_outputFactory == null)
88       throw new ServletException JavaDoc("Error configuring factory");
89   }
90
91   public void service(HttpServletRequest JavaDoc request,
92                       HttpServletResponse JavaDoc response)
93     throws ServletException JavaDoc, IOException JavaDoc
94   {
95     OutputStream JavaDoc os = response.getOutputStream();
96     String JavaDoc servletPath = request.getServletPath();
97
98     Path path = Vfs.lookup(request.getRealPath(servletPath));
99
100     Document document = new Document(getServletContext(),
101                                      path, _contextPath,
102                                      request.getContextPath() + servletPath,
103                                      _encoding);
104
105     try {
106       response.setContentType("text/html");
107       response.addHeader("Cache-Control", "max-age=3600");
108       
109       XMLStreamWriter xmlOut
110         = _outputFactory.createXMLStreamWriter(os, _encoding);
111       
112       _config.configure(document, path);
113
114       document.writeHtml(xmlOut);
115
116       xmlOut.flush();
117     } catch (IOException JavaDoc e) {
118       throw e;
119     } catch (LineConfigException e) {
120       if (e.getLineNumber() < 0)
121         throw new ServletException JavaDoc("Error configuring document", e);
122
123       try {
124         PrintWriter JavaDoc out = response.getWriter();
125
126         out.println("<html>");
127         out.println("<body>");
128
129         out.println("Error configuring document: " + e);
130
131         ReadStream readStream = path.openRead();
132
133         String JavaDoc line = readStream.readLine();
134
135         out.println("<pre>");
136
137         for (int i = 1; line != null; i++, line = readStream.readLine()) {
138           if (i == e.getLineNumber())
139             out.print("<div style='background-color: fa8072'>");
140
141           line = line.replace("<", "&lt;");
142           line = line.replace(">", "&gt;");
143           out.print(line);
144
145           if (i == e.getLineNumber())
146             out.print("</div>");
147           else
148             out.println();
149         }
150
151         out.println("</pre>");
152
153         out.println("</body>");
154         out.println("</html>");
155       } catch (IOException JavaDoc iOException) {
156         throw new ServletException JavaDoc("Error configuring document", e);
157       }
158     } catch (Exception JavaDoc e) {
159       throw new ServletException JavaDoc("Error configuring document", e);
160     }
161   }
162 }
163
Popular Tags