1 package net.sf.navigator.example;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PrintWriter;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.apache.commons.lang.StringUtils;
13
14
15 /**
16 * <p>Servlet used to display source code for example pages</p>
17 * @author fgiust (from displaytag project)
18 */
19 public class DisplaySourceServlet extends HttpServlet {
20
21
22
23
24 /**
25 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
26 */
27 protected final void doGet(HttpServletRequest request,
28 HttpServletResponse response)
29 throws ServletException, IOException {
30
31 String file = request.getRequestURI();
32 file = file.substring(0, file.lastIndexOf("."));
33 file = StringUtils.replace(file, request.getContextPath(), "");
34
35
36
37
38 InputStream is =
39 getServletContext().getResourceAsStream(file);
40
41 if (is == null) {
42 throw new ServletException("Unable to find file: " + file);
43 }
44
45 response.setContentType("text/html");
46
47 PrintWriter lOut = response.getWriter();
48
49 lOut.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " +
50 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
51 lOut.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">");
52 lOut.println("<head>");
53 lOut.println("<title>");
54 lOut.println("source for " + file);
55 lOut.println("</title>");
56 lOut.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\" />");
57 lOut.println("</head>");
58 lOut.println("<body>");
59 lOut.println("<pre>");
60
61 for (int lChar = is.read(); lChar != -1;
62 lChar = is.read()) {
63 if (lChar == '<') {
64 lOut.print("<");
65 } else {
66 lOut.print((char) lChar);
67 }
68 }
69
70 lOut.println("</pre>");
71 lOut.println("</body>");
72 lOut.println("</html>");
73 }
74 }