KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TransformServlet


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TransformServlet.java,v 1.7 2004/02/17 19:06:04 minchau Exp $
18  */

19
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServlet JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.stream.StreamResult JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * This servlet demonstrates how XSL transformations can be made available as
38  * a web service. See the CompileServlet for an example on how stylesheets
39  * can be pre-compiled before this servlet is invoked.
40  *
41  * Note that the XSLTC transformation engine is invoked through the JAXP
42  * interface, using the XSLTC "use-classpath" attribute. The
43  * "use-classpath" attribute specifies to the XSLTC TransformerFactory
44  * that a precompiled version of the stylesheet (translet) may be available,
45  * and that that should be used in preference to recompiling the stylesheet.
46  * @author Morten Jorgensen
47  * @author Jacek Ambroziak
48  */

49 public final class TransformServlet extends HttpServlet JavaDoc {
50
51     /**
52      * Main servlet entry point
53      */

54     public void doGet(HttpServletRequest JavaDoc request,
55               HttpServletResponse JavaDoc response)
56     throws IOException JavaDoc, ServletException JavaDoc {
57
58     // Initialise the output writer
59
response.setContentType("text/html");
60     PrintWriter JavaDoc out = response.getWriter();
61
62     // Get the two paramters "class" and "source".
63
String JavaDoc transletName = request.getParameter("class");
64     String JavaDoc documentURI = request.getParameter("source");
65
66     try {
67         if ((transletName == null) || (documentURI == null)) {
68             out.println("<h1>XSL transformation error</h1>");
69         out.println("The parameters <b><tt>class</tt></b> and " +
70                 "<b><tt>source</tt></b> must be specified");
71         }
72         else {
73                 TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
74                 try {
75                     tf.setAttribute("use-classpath", Boolean.TRUE);
76                 } catch (IllegalArgumentException JavaDoc iae) {
77                     System.err.println(
78                            "Could not set XSLTC-specific TransformerFactory "
79                          + "attributes. Transformation failed.");
80                 }
81                 Transformer JavaDoc t =
82                          tf.newTransformer(new StreamSource JavaDoc(transletName));
83
84         // Start the transformation
85
final long start = System.currentTimeMillis();
86         t.transform(new StreamSource JavaDoc(documentURI),
87                             new StreamResult JavaDoc(out));
88         final long done = System.currentTimeMillis() - start;
89         out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
90         }
91     }
92     catch (Exception JavaDoc e) {
93         out.println("<h1>Error</h1>");
94         out.println(e.toString());
95     }
96     }
97 }
98
Popular Tags