01: package xmlc.demo;
02:
03: import javax.servlet.http.HttpServlet;
04: import javax.servlet.http.HttpServletRequest;
05: import javax.servlet.http.HttpServletResponse;
06: import javax.servlet.ServletException;
07: import org.enhydra.xml.xmlc.servlet.XMLCContext;
08: import org.enhydra.xml.xmlc.XMLObject;
09: import org.enhydra.xml.xmlc.XMLCFactory;
10: import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory;
11:
12: public class Preview extends HttpServlet {
13:
14: protected void service(HttpServletRequest req,
15: HttpServletResponse resp) throws ServletException {
16: XMLObject xmlObj = null;
17:
18: /* set up XMLC context and factory */
19: XMLCContext context = XMLCContext.getContext(this );
20: XMLCFactory factory = context.getXMLCFactory();
21:
22: /* Cast the factory so we can use the dynamic loading API */
23: XMLCDeferredParsingFactory dpFactory = null;
24: if (factory instanceof XMLCDeferredParsingFactory) {
25: dpFactory = (XMLCDeferredParsingFactory) factory;
26: }
27:
28: /* get the path info after /Preview/. Path info is used
29: * instead of parameter because it maps nicely into the static
30: * pages */
31: String path = req.getPathInfo();
32:
33: try {
34: if (path != null) {
35: path = path.substring(1);
36: xmlObj = dpFactory.createFromFile(path);
37: if (xmlObj != null) {
38: try {
39: context.writeDOM(req, resp, xmlObj);
40: } catch (Exception e) {
41: resp.sendError(resp.SC_NOT_FOUND, "doc '"
42: + path + "' has error");
43: }
44: } else {
45: resp.sendError(resp.SC_NOT_FOUND, "doc '" + path
46: + "' not found");
47: }
48: } else {
49: resp.sendError(resp.SC_NOT_FOUND, "specify '" + path
50: + "'");
51: }
52: } catch (java.io.IOException ioe) {
53: new ServletException(ioe);
54: }
55: }
56: }
|