KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xmlc > demo > Preview


1 package xmlc.demo;
2
3 import javax.servlet.http.HttpServlet JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import javax.servlet.http.HttpServletResponse JavaDoc;
6 import javax.servlet.ServletException JavaDoc;
7 import org.enhydra.xml.xmlc.servlet.XMLCContext;
8 import org.enhydra.xml.xmlc.XMLObject;
9 import org.enhydra.xml.xmlc.XMLCFactory;
10 import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory;
11
12 public class Preview extends HttpServlet JavaDoc {
13
14     protected void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc {
15     XMLObject xmlObj = null;
16
17     /* set up XMLC context and factory */
18     XMLCContext context = XMLCContext.getContext(this);
19     XMLCFactory factory = context.getXMLCFactory();
20
21     /* Cast the factory so we can use the dynamic loading API */
22     XMLCDeferredParsingFactory dpFactory = null;
23     if (factory instanceof XMLCDeferredParsingFactory) {
24         dpFactory = (XMLCDeferredParsingFactory)factory;
25     }
26
27     /* get the path info after /Preview/. Path info is used
28      * instead of parameter because it maps nicely into the static
29      * pages */

30     String JavaDoc path = req.getPathInfo();
31
32     try {
33         if (path != null) {
34         path = path.substring(1);
35         xmlObj = dpFactory.createFromFile(path);
36         if (xmlObj != null) {
37             try {
38             context.writeDOM(req, resp, xmlObj);
39             } catch (Exception JavaDoc e) {
40             resp.sendError(resp.SC_NOT_FOUND, "doc '" + path + "' has error");
41             }
42         } else {
43             resp.sendError(resp.SC_NOT_FOUND, "doc '" + path + "' not found");
44         }
45         } else {
46         resp.sendError(resp.SC_NOT_FOUND, "specify '" + path + "'");
47         }
48     } catch (java.io.IOException JavaDoc ioe) {
49         new ServletException JavaDoc (ioe);
50     }
51     }
52 }
53
54
Popular Tags