1 import java.io.*; 2 import javax.servlet.*; 3 import javax.servlet.http.*; 4 import java.util.Hashtable ; 5 import java.util.Enumeration ; 6 7 import javax.xml.transform.*; 8 import javax.xml.transform.stream.*; 9 import com.icl.saxon.expr.StringValue; 12 import java.util.Properties ; 14 15 18 19 public class SaxonServlet extends HttpServlet { 20 21 31 32 public void service(HttpServletRequest req, HttpServletResponse res) 33 throws ServletException, IOException 34 { 35 String source = req.getParameter("source"); 36 String style = req.getParameter("style"); 37 String 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 55 56 public String getServletInfo() { 57 return "Calls SAXON to apply a stylesheet to a source document"; 58 } 59 60 63 64 private void apply(String style, String source, 65 HttpServletRequest req, HttpServletResponse res) 66 throws TransformerException, java.io.IOException { 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 details = pss.getOutputProperties(); 82 83 String mime = pss.getOutputProperties().getProperty(OutputKeys.MEDIA_TYPE); 84 if (mime==null) { 85 res.setContentType("text/html"); 87 } else { 88 res.setContentType(mime); 89 } 90 91 Enumeration p = req.getParameterNames(); 92 while (p.hasMoreElements()) { 93 String name = (String )p.nextElement(); 94 if (!(name.equals("style") || name.equals("source"))) { 95 String value = req.getParameter(name); 96 transformer.setParameter(name, new StringValue(value)); 97 } 98 } 99 100 String 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 err) { 107 out.println(err.getMessage()); 108 err.printStackTrace(); 109 } 110 111 } 112 113 116 117 private synchronized Templates tryCache(String url) throws TransformerException, java.io.IOException { 118 String 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 136 137 private synchronized void clearCache() { 138 cache = new Hashtable (); 139 } 140 141 private Hashtable cache = new Hashtable (); 142 } 143 | Popular Tags |