KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TransformApplet


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: TransformApplet.java,v 1.9 2004/02/17 19:02:49 minchau Exp $
18  */

19
20 import java.applet.Applet JavaDoc;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Button JavaDoc;
24 import java.awt.Frame JavaDoc;
25 import java.awt.Label JavaDoc;
26 import java.awt.Panel JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32
33 import javax.xml.transform.Transformer JavaDoc;
34 import javax.xml.transform.TransformerFactory JavaDoc;
35 import javax.xml.transform.TransformerException JavaDoc;
36 import javax.xml.transform.ErrorListener JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38 import javax.xml.transform.stream.StreamSource JavaDoc;
39
40 /**
41  * This applet demonstrates how XSL transformations can be made to run in
42  * browsers without native XSLT support.
43  *
44  * Note that the XSLTC transformation engine is invoked through the JAXP
45  * interface, using the XSLTC "use-classpath" attribute. The
46  * "use-classpath" attribute specifies to the XSLTC TransformerFactory
47  * that a precompiled version of the stylesheet (translet) may be available,
48  * and that that should be used in preference to recompiling the stylesheet.
49  * @author Morten Jorgensen
50  * @author Jacek Ambroziak
51  */

52 public final class TransformApplet extends Applet JavaDoc {
53     TransformerFactory JavaDoc tf;
54     TransformDelegate transformThread;
55     /**
56      * This class implements a dialog box used for XSL messages/comments
57      */

58     public class MessageFrame extends Frame JavaDoc {
59
60         public Frame JavaDoc frame;
61
62         public class ButtonHandler implements ActionListener JavaDoc {
63             public void actionPerformed(ActionEvent JavaDoc e) {
64                 frame.setVisible(false);
65             }
66         }
67
68         /**
69          * This method handles xml:message and xsl:comment by displaying
70          * the message/comment in a dialog box.
71          */

72         public MessageFrame(String JavaDoc title, String JavaDoc message) {
73             super(title);
74             frame = this; // Make visible to ButtonHandler
75
setSize(320,200);
76
77             // Create a panel for the message itself
78
Panel JavaDoc center = new Panel JavaDoc();
79             center.add(new Label JavaDoc(message));
80
81             // Create a panel for the 'OK' button
82
Panel JavaDoc bottom = new Panel JavaDoc();
83             Button JavaDoc okButton = new Button JavaDoc(" OK ");
84             okButton.addActionListener(new ButtonHandler());
85             bottom.add(okButton);
86
87             // Add the two panels to the window/frame
88
add(center, BorderLayout.CENTER);
89             add(bottom,BorderLayout.SOUTH);
90
91             // Show the fecking thing
92
setVisible(true);
93         }
94
95     }
96
97     /**
98      * The applet uses this method to display messages and comments
99      * generated by xsl:message and xsl:comment elements.
100      */

101     public class AppletErrorListener implements ErrorListener JavaDoc {
102         public void displayMessage(TransformerException JavaDoc e) {
103             MessageFrame z = new MessageFrame("XSL transformation alert",
104                                               e.getMessageAndLocation());
105         }
106
107         public void error(TransformerException JavaDoc e) {
108             displayMessage(e);
109         }
110
111         public void fatalError(TransformerException JavaDoc e) {
112             displayMessage(e);
113         }
114
115         public void warning(TransformerException JavaDoc e) {
116                     displayMessage(e);
117         }
118     }
119
120     /**
121      * This method is the main body of the applet. The method is called
122      * by some JavaScript code in an HTML document.
123      */

124     public synchronized String JavaDoc transform(Object JavaDoc arg1, Object JavaDoc arg2) {
125         final String JavaDoc stylesheetURL = (String JavaDoc)arg1;
126         final String JavaDoc documentURL = (String JavaDoc)arg2;
127
128         transformThread.setStylesheetURL(stylesheetURL);
129         transformThread.setDocumentURL(documentURL);
130         transformThread.setWaiting(false);
131         transformThread.wakeUp();
132         try{
133             wait();
134         } catch (InterruptedException JavaDoc e){}
135         return transformThread.getOutput();
136     }
137
138     public void start() {
139         transform(getParameter("stylesheet-name"),
140                   getParameter("input-document"));
141     }
142     public void destroy() {
143         transformThread.destroy();
144     }
145     public void init() {
146         tf = TransformerFactory.newInstance();
147         try {
148             tf.setAttribute("use-classpath", Boolean.TRUE);
149         } catch (IllegalArgumentException JavaDoc iae) {
150             System.err.println("Could not set XSLTC-specific TransformerFactory"
151                                + " attributes. Transformation failed.");
152         }
153         // Another thread is created to keep the context class loader
154
// information. When use JDK 1.4 plugin for browser, to get around the
155
// problem with the bundled old version of xalan and endorsed class
156
// loading mechanism
157
transformThread = new TransformDelegate(true);
158         Thread JavaDoc t = new Thread JavaDoc(transformThread);
159         t.setName("transformThread");
160         t.start();
161     }
162     public String JavaDoc getOutput(){
163         return transformThread.getOutput();
164     }
165     public synchronized void wakeUp() {
166         notifyAll();
167     }
168     class TransformDelegate implements Runnable JavaDoc {
169         private boolean isRunning, isWaiting;
170         private String JavaDoc stylesheetURL, documentURL;
171         private String JavaDoc outPut;
172         public TransformDelegate(boolean arg) {
173             isRunning = arg;
174             isWaiting = true;
175         }
176         public synchronized void run() {
177             while(isRunning){
178                 while(isWaiting){
179                     try {
180                         wait();
181                     } catch (InterruptedException JavaDoc e){}
182                 }
183                 transform();
184                 isWaiting = true;
185                 TransformApplet.this.wakeUp();
186             }
187         }
188
189         public void setStylesheetURL(String JavaDoc arg){
190             stylesheetURL = arg;
191         }
192         public void setDocumentURL(String JavaDoc arg) {
193             documentURL = arg;
194         }
195         public String JavaDoc getStylesheetURL(){
196             return stylesheetURL;
197         }
198         public String JavaDoc getDocumentURL() {
199             return documentURL;
200         }
201         public void setWaiting(boolean arg) {
202             isWaiting = arg;
203         }
204         public void destroy() {
205             isRunning = false;
206         }
207         public synchronized void wakeUp() {
208             notifyAll();
209         }
210         public String JavaDoc getOutput(){
211             return outPut;
212         }
213
214         public void transform(){
215             String JavaDoc xslURL = getStylesheetURL();
216             String JavaDoc docURL = getDocumentURL();
217             // Initialise the output stream
218
StringWriter JavaDoc sout = new StringWriter JavaDoc();
219             PrintWriter JavaDoc out = new PrintWriter JavaDoc(sout);
220             // Check that the parameters are valid
221
try {
222                 if (xslURL == null || docURL == null) {
223                     out.println("<h1>Transformation error</h1>");
224                     out.println("The parameters <b><tt>stylesheetURL</tt></b> "+
225                                 "and <b><tt>source</tt></b> must be specified");
226                 } else {
227                     Transformer JavaDoc t = tf.newTransformer(new StreamSource JavaDoc(xslURL));
228                     t.setErrorListener(new AppletErrorListener());
229
230                     final long start = System.currentTimeMillis();
231
232                     t.transform(new StreamSource JavaDoc(docURL),
233                                 new StreamResult JavaDoc(out));
234
235                     final long done = System.currentTimeMillis() - start;
236                     out.println("<!-- transformed by XSLTC in " + done
237                                 + "msecs -->");
238                 }
239                 // Now close up the sink, and return the HTML output in the
240
// StringWrite object as a string.
241
out.close();
242                 System.err.println("Transformation complete!");
243                 System.err.println(sout.toString());
244                 outPut = sout.toString();
245                 sout.close();
246             } catch (RuntimeException JavaDoc e) {
247                 out.println("<h1>RTE</h1>");
248                 out.close();
249                 outPut = sout.toString();
250             } catch (Exception JavaDoc e) {
251                 out.println("<h1>exception</h1>");
252                 out.println(e.toString());
253                 out.close();
254                 outPut = sout.toString();
255             }
256         }
257     }
258 }
259
Popular Tags