KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TransformHandler


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: TransformHandler.java,v 1.7 2004/02/17 19:03:08 minchau Exp $
18  */

19
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerFactory JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.ErrorListener JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32
33 import sunlabs.brazil.server.Handler;
34 import sunlabs.brazil.server.Request;
35 import sunlabs.brazil.server.Server;
36
37 /**
38  * This Brazil handler demonstrates how XSL transformations can be made
39  * available as a web service without using a full web server. This class
40  * implements the Handler interface from the Brazil project, see:
41  * http://www.sun.com/research/brazil/
42  *
43  * Note that the XSLTC transformation engine is invoked through the JAXP
44  * interface, using the XSLTC "use-classpath" attribute. The
45  * "use-from-classpath" attribute specifies to the XSLTC TransformerFactory
46  * that a precompiled version of the stylesheet (translet) may be available,
47  * and that should be used in preference to recompiling the stylesheet.
48  * @author Morten Jorgensen
49  */

50 public class TransformHandler implements Handler {
51
52     private TransformerFactory JavaDoc m_tf = null;
53
54     // These two are used while parsing the parameters in the URL
55
private final String JavaDoc PARAM_TRANSLET = "translet=";
56     private final String JavaDoc PARAM_DOCUMENT = "document=";
57     private final String JavaDoc PARAM_STATS = "stats=";
58
59     // All output goes here:
60
private PrintWriter JavaDoc m_out = null;
61
62     /**
63      * Dump an error message to output
64      */

65     public void errorMessage(String JavaDoc message, Exception JavaDoc e) {
66     if (m_out == null) {
67             return;
68         }
69     m_out.println("<h1>XSL transformation error</h1>"+message);
70     m_out.println("<br>Exception:</br>"+e.toString());
71     }
72
73     public void errorMessage(String JavaDoc message) {
74     if (m_out == null) return;
75     m_out.println("<h1>XSL transformation error</h1>"+message);
76     }
77
78     /**
79      * This method is run when the Brazil proxy is loaded
80      */

81     public boolean init(Server server, String JavaDoc prefix) {
82     return true;
83     }
84
85     /**
86      * This method is run for every HTTP request sent to the proxy
87      */

88     public boolean respond(Request request) throws IOException JavaDoc {
89
90     // Initialise the output buffer
91
final StringWriter JavaDoc sout = new StringWriter JavaDoc();
92     m_out = new PrintWriter JavaDoc(sout);
93
94     // These two hold the parameters from the URL 'translet' and 'document'
95
String JavaDoc transletName = null;
96     String JavaDoc document = null;
97     String JavaDoc stats = null;
98
99     // Get the parameters from the URL
100
final StringTokenizer JavaDoc params = new StringTokenizer JavaDoc(request.query,"&");
101     while (params.hasMoreElements()) {
102         final String JavaDoc param = params.nextToken();
103         if (param.startsWith(PARAM_TRANSLET)) {
104         transletName = param.substring(PARAM_TRANSLET.length());
105         }
106         else if (param.startsWith(PARAM_DOCUMENT)) {
107         document = param.substring(PARAM_DOCUMENT.length());
108         }
109         else if (param.startsWith(PARAM_STATS)) {
110         stats = param.substring(PARAM_STATS.length());
111         }
112     }
113
114     try {
115         // Make sure that both parameters were specified
116
if ((transletName == null) || (document == null)) {
117         errorMessage("Parameters <b><tt>translet</tt></b> and/or "+
118                  "<b><tt>document</tt></b> not specified.");
119         }
120         else {
121                 if (m_tf == null) {
122                     m_tf = TransformerFactory.newInstance();
123                     try {
124                         m_tf.setAttribute("use-classpath", Boolean.TRUE);
125                     } catch (IllegalArgumentException JavaDoc iae) {
126                         System.err.println(
127                             "Could not set XSLTC-specific TransformerFactory "
128                           + "attributes. Transformation failed.");
129                     }
130                 }
131                 Transformer JavaDoc t =
132                      m_tf.newTransformer(new StreamSource JavaDoc(transletName));
133
134         // Do the actual transformation
135
final long start = System.currentTimeMillis();
136         t.transform(new StreamSource JavaDoc(document),
137                             new StreamResult JavaDoc(m_out));
138         final long done = System.currentTimeMillis() - start;
139         m_out.println("<!-- transformed by XSLTC in "+done+"ms -->");
140         }
141     }
142     catch (Exception JavaDoc e) {
143         errorMessage("Internal error.",e);
144     }
145
146     // Pass the transformation output as the HTTP response
147
request.sendResponse(sout.toString());
148     return true;
149     }
150
151
152 }
153
Popular Tags