1 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 ; 39 import javax.servlet.ServletException ; 40 import javax.servlet.http.HttpServlet ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import javax.xml.stream.FactoryConfigurationError; 44 import javax.xml.stream.XMLOutputFactory; 45 import javax.xml.stream.XMLStreamWriter; 46 import java.io.IOException ; 47 import java.io.OutputStream ; 48 import java.io.PrintWriter ; 49 import java.util.logging.Logger ; 50 51 public class ResinDocServlet extends HttpServlet { 52 private static Logger log = Logger.getLogger(ResinDocServlet.class.getName()); 53 54 private String _contextPath; 55 private Config _config; 56 private Path _pwd; 57 private XMLOutputFactory _outputFactory; 58 private String _encoding = "utf-8"; 59 60 public void setDocumentEncoding(String encoding) 61 { 62 _encoding = encoding; 63 } 64 65 public void setDocContextPath(String contextPath) 66 { 67 _contextPath = contextPath; 68 } 69 70 public void init(ServletConfig config) 71 throws ServletException  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 ("Error configuring factory", e); 85 } 86 87 if (_outputFactory == null) 88 throw new ServletException ("Error configuring factory"); 89 } 90 91 public void service(HttpServletRequest request, 92 HttpServletResponse response) 93 throws ServletException , IOException  94 { 95 OutputStream os = response.getOutputStream(); 96 String 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 e) { 118 throw e; 119 } catch (LineConfigException e) { 120 if (e.getLineNumber() < 0) 121 throw new ServletException ("Error configuring document", e); 122 123 try { 124 PrintWriter 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 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("<", "<"); 142 line = line.replace(">", ">"); 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 iOException) { 156 throw new ServletException ("Error configuring document", e); 157 } 158 } catch (Exception e) { 159 throw new ServletException ("Error configuring document", e); 160 } 161 } 162 } 163 | Popular Tags |