KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SaxonServlet


1 import java.io.*;
2 import javax.servlet.*;
3 import javax.servlet.http.*;
4 import java.util.Hashtable JavaDoc;
5 import java.util.Enumeration JavaDoc;
6
7 import javax.xml.transform.*;
8 import javax.xml.transform.stream.*;
9 //import com.icl.saxon.ExtendedInputSource;
10
//import com.icl.saxon.output.*;
11
import com.icl.saxon.expr.StringValue;
12 //import org.xml.sax.*;
13
import java.util.Properties JavaDoc;
14
15 /**
16  * SaxonServlet. Transforms a supplied input document using a supplied stylesheet
17  */

18  
19 public class SaxonServlet extends HttpServlet {
20
21     /**
22     * service() - accept request and produce response<BR>
23     * URL parameters: <UL>
24     * <li>source - URL of source document</li>
25     * <li>style - URL of stylesheet</li>
26     * <li>clear-stylesheet-cache - if set to yes, empties the cache before running.
27     * </UL>
28     * @param req The HTTP request
29     * @param res The HTTP response
30     */

31     
32     public void service(HttpServletRequest req, HttpServletResponse res)
33     throws ServletException, IOException
34     {
35         String JavaDoc source = req.getParameter("source");
36         String JavaDoc style = req.getParameter("style");
37         String JavaDoc clear = req.getParameter("clear-stylesheet-cache");
38
39         if (clear!=null && clear.equals("yes")) {
40             clearCache();
41         }
42
43         try {
44             apply(style, source, req, res);
45         } catch (TransformerException err) {
46             res.getOutputStream().println("Error applying stylesheet: " + err.getMessage());
47         }
48
49     }
50
51     /**
52     * getServletInfo<BR>
53     * Required by Servlet interface
54     */

55
56     public String JavaDoc getServletInfo() {
57         return "Calls SAXON to apply a stylesheet to a source document";
58     }
59
60     /**
61     * Apply stylesheet to source document
62     */

63
64     private void apply(String JavaDoc style, String JavaDoc source,
65                            HttpServletRequest req, HttpServletResponse res)
66                            throws TransformerException, java.io.IOException JavaDoc {
67                             
68         ServletOutputStream out = res.getOutputStream();
69
70         if (style==null) {
71             out.println("No style parameter supplied");
72             return;
73         }
74         if (source==null) {
75             out.println("No source parameter supplied");
76             return;
77         }
78         try {
79             Templates pss = tryCache(style);
80             Transformer transformer = pss.newTransformer();
81             Properties JavaDoc details = pss.getOutputProperties();
82
83             String JavaDoc mime = pss.getOutputProperties().getProperty(OutputKeys.MEDIA_TYPE);
84             if (mime==null) {
85                // guess
86
res.setContentType("text/html");
87             } else {
88                 res.setContentType(mime);
89             }
90
91             Enumeration JavaDoc p = req.getParameterNames();
92             while (p.hasMoreElements()) {
93                 String JavaDoc name = (String JavaDoc)p.nextElement();
94                 if (!(name.equals("style") || name.equals("source"))) {
95                     String JavaDoc value = req.getParameter(name);
96                     transformer.setParameter(name, new StringValue(value));
97                 }
98             }
99             
100             String JavaDoc path = getServletContext().getRealPath(source);
101             if (path==null) {
102                 throw new TransformerException("Source file " + source + " not found");
103             }
104             File sourceFile = new File(path);
105             transformer.transform(new StreamSource(sourceFile), new StreamResult(out));
106         } catch (Exception JavaDoc err) {
107             out.println(err.getMessage());
108             err.printStackTrace();
109         }
110
111     }
112
113     /**
114     * Maintain prepared stylesheets in memory for reuse
115     */

116
117     private synchronized Templates tryCache(String JavaDoc url) throws TransformerException, java.io.IOException JavaDoc {
118         String JavaDoc path = getServletContext().getRealPath(url);
119         if (path==null) {
120             throw new TransformerException("Stylesheet " + url + " not found");
121         }
122         
123         Templates x = (Templates)cache.get(path);
124         if (x==null) {
125             TransformerFactory factory = TransformerFactory.newInstance();
126             x = factory.newTemplates(new StreamSource(new File(path)));
127             cache.put(path, x);
128         }
129         return x;
130     }
131
132     /**
133     * Clear the cache. Useful if stylesheets have been modified, or simply if space is
134     * running low. We let the garbage collector do the work.
135     */

136
137     private synchronized void clearCache() {
138         cache = new Hashtable JavaDoc();
139     }
140
141     private Hashtable JavaDoc cache = new Hashtable JavaDoc();
142 }
143
Popular Tags