1 16 19 20 import java.applet.Applet ; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Button ; 24 import java.awt.Frame ; 25 import java.awt.Label ; 26 import java.awt.Panel ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.transform.ErrorListener ; 37 import javax.xml.transform.stream.StreamResult ; 38 import javax.xml.transform.stream.StreamSource ; 39 40 52 public final class TransformApplet extends Applet { 53 TransformerFactory tf; 54 TransformDelegate transformThread; 55 58 public class MessageFrame extends Frame { 59 60 public Frame frame; 61 62 public class ButtonHandler implements ActionListener { 63 public void actionPerformed(ActionEvent e) { 64 frame.setVisible(false); 65 } 66 } 67 68 72 public MessageFrame(String title, String message) { 73 super(title); 74 frame = this; setSize(320,200); 76 77 Panel center = new Panel (); 79 center.add(new Label (message)); 80 81 Panel bottom = new Panel (); 83 Button okButton = new Button (" OK "); 84 okButton.addActionListener(new ButtonHandler()); 85 bottom.add(okButton); 86 87 add(center, BorderLayout.CENTER); 89 add(bottom,BorderLayout.SOUTH); 90 91 setVisible(true); 93 } 94 95 } 96 97 101 public class AppletErrorListener implements ErrorListener { 102 public void displayMessage(TransformerException e) { 103 MessageFrame z = new MessageFrame("XSL transformation alert", 104 e.getMessageAndLocation()); 105 } 106 107 public void error(TransformerException e) { 108 displayMessage(e); 109 } 110 111 public void fatalError(TransformerException e) { 112 displayMessage(e); 113 } 114 115 public void warning(TransformerException e) { 116 displayMessage(e); 117 } 118 } 119 120 124 public synchronized String transform(Object arg1, Object arg2) { 125 final String stylesheetURL = (String )arg1; 126 final String documentURL = (String )arg2; 127 128 transformThread.setStylesheetURL(stylesheetURL); 129 transformThread.setDocumentURL(documentURL); 130 transformThread.setWaiting(false); 131 transformThread.wakeUp(); 132 try{ 133 wait(); 134 } catch (InterruptedException 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 iae) { 150 System.err.println("Could not set XSLTC-specific TransformerFactory" 151 + " attributes. Transformation failed."); 152 } 153 transformThread = new TransformDelegate(true); 158 Thread t = new Thread (transformThread); 159 t.setName("transformThread"); 160 t.start(); 161 } 162 public String getOutput(){ 163 return transformThread.getOutput(); 164 } 165 public synchronized void wakeUp() { 166 notifyAll(); 167 } 168 class TransformDelegate implements Runnable { 169 private boolean isRunning, isWaiting; 170 private String stylesheetURL, documentURL; 171 private String 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 e){} 182 } 183 transform(); 184 isWaiting = true; 185 TransformApplet.this.wakeUp(); 186 } 187 } 188 189 public void setStylesheetURL(String arg){ 190 stylesheetURL = arg; 191 } 192 public void setDocumentURL(String arg) { 193 documentURL = arg; 194 } 195 public String getStylesheetURL(){ 196 return stylesheetURL; 197 } 198 public String 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 getOutput(){ 211 return outPut; 212 } 213 214 public void transform(){ 215 String xslURL = getStylesheetURL(); 216 String docURL = getDocumentURL(); 217 StringWriter sout = new StringWriter (); 219 PrintWriter out = new PrintWriter (sout); 220 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 t = tf.newTransformer(new StreamSource (xslURL)); 228 t.setErrorListener(new AppletErrorListener()); 229 230 final long start = System.currentTimeMillis(); 231 232 t.transform(new StreamSource (docURL), 233 new StreamResult (out)); 234 235 final long done = System.currentTimeMillis() - start; 236 out.println("<!-- transformed by XSLTC in " + done 237 + "msecs -->"); 238 } 239 out.close(); 242 System.err.println("Transformation complete!"); 243 System.err.println(sout.toString()); 244 outPut = sout.toString(); 245 sout.close(); 246 } catch (RuntimeException e) { 247 out.println("<h1>RTE</h1>"); 248 out.close(); 249 outPut = sout.toString(); 250 } catch (Exception 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 |