1 16 19 package org.apache.xalan.client; 20 21 import java.applet.Applet ; 22 import java.awt.Graphics ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.io.StringReader ; 26 import java.io.StringWriter ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.Hashtable ; 30 import java.util.Enumeration ; 31 32 import javax.xml.transform.Templates ; 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.TransformerConfigurationException ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.transform.TransformerFactory ; 37 import javax.xml.transform.stream.StreamResult ; 38 import javax.xml.transform.stream.StreamSource ; 39 40 import org.apache.xalan.res.XSLMessages; 41 import org.apache.xalan.res.XSLTErrorResources; 42 43 54 public class XSLTProcessorApplet extends Applet 55 { 56 57 61 TransformerFactory m_tfactory = null; 62 63 66 private String m_styleURL; 67 68 71 private String m_documentURL; 72 73 77 80 private final String PARAM_styleURL = "styleURL"; 81 82 85 private final String PARAM_documentURL = "documentURL"; 86 87 88 91 94 private String m_styleURLOfCached = null; 95 96 99 private String m_documentURLOfCached = null; 100 101 105 private URL m_codeBase = null; 106 107 110 private String m_treeURL = null; 111 112 116 private URL m_documentBase = null; 117 118 121 transient private Thread m_callThread = null; 122 123 125 transient private TrustedAgent m_trustedAgent = null; 126 127 130 transient private Thread m_trustedWorker = null; 131 132 135 transient private String m_htmlText = null; 136 137 140 transient private String m_sourceText = null; 141 142 145 transient private String m_nameOfIDAttrOfElemToModify = null; 146 147 149 transient private String m_elemIdToModify = null; 150 151 153 transient private String m_attrNameToSet = null; 154 155 157 transient private String m_attrValueToSet = null; 158 159 162 public XSLTProcessorApplet(){} 163 164 168 public String getAppletInfo() 169 { 170 return "Name: XSLTProcessorApplet\r\n" + "Author: Scott Boag"; 171 } 172 173 178 public String [][] getParameterInfo() 179 { 180 181 String [][] info = 182 { 183 { PARAM_styleURL, "String", "URL to an XSL stylesheet" }, 184 { PARAM_documentURL, "String", "URL to an XML document" }, 185 }; 186 187 return info; 188 } 189 190 193 public void init() 194 { 195 196 String param; 202 203 param = getParameter(PARAM_styleURL); 206 207 m_parameters = new Hashtable (); 209 210 if (param != null) 211 setStyleURL(param); 212 213 param = getParameter(PARAM_documentURL); 216 217 if (param != null) 218 setDocumentURL(param); 219 220 m_codeBase = this.getCodeBase(); 221 m_documentBase = this.getDocumentBase(); 222 223 resize(320, 240); 230 } 231 232 236 public void start() 237 { 238 239 m_trustedAgent = new TrustedAgent(); 240 Thread currentThread = Thread.currentThread(); 241 m_trustedWorker = new Thread (currentThread.getThreadGroup(), 242 m_trustedAgent); 243 m_trustedWorker.start(); 244 try 245 { 246 m_tfactory = TransformerFactory.newInstance(); 247 this.showStatus("Causing Transformer and Parser to Load and JIT..."); 248 249 StringReader xmlbuf = new StringReader ("<?xml version='1.0'?><foo/>"); 251 StringReader xslbuf = new StringReader ( 252 "<?xml version='1.0'?><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'><xsl:template match='foo'><out/></xsl:template></xsl:stylesheet>"); 253 PrintWriter pw = new PrintWriter (new StringWriter ()); 254 255 synchronized (m_tfactory) 256 { 257 Templates templates = m_tfactory.newTemplates(new StreamSource (xslbuf)); 258 Transformer transformer = templates.newTransformer(); 259 transformer.transform(new StreamSource (xmlbuf), new StreamResult (pw)); 260 } 261 System.out.println("Primed the pump!"); 262 this.showStatus("Ready to go!"); 263 } 264 catch (Exception e) 265 { 266 this.showStatus("Could not prime the pump!"); 267 System.out.println("Could not prime the pump!"); 268 e.printStackTrace(); 269 } 270 } 271 272 276 public void paint(Graphics g){} 277 278 282 public void stop() 283 { 284 if (null != m_trustedWorker) 285 { 286 m_trustedWorker.stop(); 287 288 m_trustedWorker = null; 290 } 291 292 m_styleURLOfCached = null; 293 m_documentURLOfCached = null; 294 } 295 296 299 public void destroy() 300 { 301 if (null != m_trustedWorker) 302 { 303 m_trustedWorker.stop(); 304 305 m_trustedWorker = null; 307 } 308 m_styleURLOfCached = null; 309 m_documentURLOfCached = null; 310 } 311 312 317 public void setStyleURL(String urlString) 318 { 319 m_styleURL = urlString; 320 } 321 322 327 public void setDocumentURL(String urlString) 328 { 329 m_documentURL = urlString; 330 } 331 332 337 public void freeCache() 338 { 339 m_styleURLOfCached = null; 340 m_documentURLOfCached = null; 341 } 342 343 351 public void setStyleSheetAttribute(String nameOfIDAttrOfElemToModify, 352 String elemId, String attrName, 353 String value) 354 { 355 m_nameOfIDAttrOfElemToModify = nameOfIDAttrOfElemToModify; 356 m_elemIdToModify = elemId; 357 m_attrNameToSet = attrName; 358 m_attrValueToSet = value; 359 } 360 361 364 private Enumeration m_keys; 365 366 369 transient Hashtable m_parameters; 370 371 378 public void setStylesheetParam(String key, String expr) 379 { 380 m_parameters.put(key, expr); 381 } 382 383 391 public String escapeString(String s) 392 { 393 StringBuffer sb = new StringBuffer (); 394 int length = s.length(); 395 396 for (int i = 0; i < length; i++) 397 { 398 char ch = s.charAt(i); 399 400 if ('<' == ch) 401 { 402 sb.append("<"); 403 } 404 else if ('>' == ch) 405 { 406 sb.append(">"); 407 } 408 else if ('&' == ch) 409 { 410 sb.append("&"); 411 } 412 else if (0xd800 <= ch && ch < 0xdc00) 413 { 414 int next; 416 417 if (i + 1 >= length) 418 { 419 throw new RuntimeException ( 420 XSLMessages.createMessage( 421 XSLTErrorResources.ER_INVALID_UTF16_SURROGATE, 422 new Object []{ Integer.toHexString(ch) })); 424 } 426 else 427 { 428 next = s.charAt(++i); 429 430 if (!(0xdc00 <= next && next < 0xe000)) 431 throw new RuntimeException ( 432 XSLMessages.createMessage( 433 XSLTErrorResources.ER_INVALID_UTF16_SURROGATE, 434 new Object []{ 435 Integer.toHexString(ch) + " " 436 + Integer.toHexString(next) })); 438 next = ((ch - 0xd800) << 10) + next - 0xdc00 + 0x00010000; 440 } 441 sb.append("&#x"); 442 sb.append(Integer.toHexString(next)); 443 sb.append(";"); 444 } 445 else 446 { 447 sb.append(ch); 448 } 449 } 450 return sb.toString(); 451 } 452 453 460 public String getHtmlText() 461 { 462 m_trustedAgent.m_getData = true; 463 m_callThread = Thread.currentThread(); 464 try 465 { 466 synchronized (m_callThread) 467 { 468 m_callThread.wait(); 469 } 470 } 471 catch (InterruptedException ie) 472 { 473 System.out.println(ie.getMessage()); 474 } 475 return m_htmlText; 476 } 477 478 487 public String getTreeAsText(String treeURL) throws IOException 488 { 489 m_treeURL = treeURL; 490 m_trustedAgent.m_getData = true; 491 m_trustedAgent.m_getSource = true; 492 m_callThread = Thread.currentThread(); 493 try 494 { 495 synchronized (m_callThread) 496 { 497 m_callThread.wait(); 498 } 499 } 500 catch (InterruptedException ie) 501 { 502 System.out.println(ie.getMessage()); 503 } 504 return m_sourceText; 505 } 506 507 513 private String getSource() throws TransformerException 514 { 515 StringWriter osw = new StringWriter (); 516 PrintWriter pw = new PrintWriter (osw, false); 517 String text = ""; 518 try 519 { 520 URL docURL = new URL (m_documentBase, m_treeURL); 521 synchronized (m_tfactory) 522 { 523 Transformer transformer = m_tfactory.newTransformer(); 524 StreamSource source = new StreamSource (docURL.toString()); 525 StreamResult result = new StreamResult (pw); 526 transformer.transform(source, result); 527 text = osw.toString(); 528 } 529 } 530 catch (MalformedURLException e) 531 { 532 e.printStackTrace(); 533 System.exit(-1); 534 } 535 catch (Exception any_error) 536 { 537 any_error.printStackTrace(); 538 } 539 return text; 540 } 541 542 550 public String getSourceTreeAsText() throws Exception 551 { 552 return getTreeAsText(m_documentURL); 553 } 554 555 563 public String getStyleTreeAsText() throws Exception 564 { 565 return getTreeAsText(m_styleURL); 566 } 567 568 576 public String getResultTreeAsText() throws Exception 577 { 578 return escapeString(getHtmlText()); 579 } 580 581 591 public String transformToHtml(String doc, String style) 592 { 593 594 if (null != doc) 595 { 596 m_documentURL = doc; 597 } 598 599 if (null != style) 600 { 601 m_styleURL = style; 602 } 603 604 return getHtmlText(); 605 } 606 607 616 public String transformToHtml(String doc) 617 { 618 619 if (null != doc) 620 { 621 m_documentURL = doc; 622 } 623 624 m_styleURL = null; 625 626 return getHtmlText(); 627 } 628 629 630 637 private String processTransformation() throws TransformerException 638 { 639 String htmlData = null; 640 this.showStatus("Waiting for Transformer and Parser to finish loading and JITing..."); 641 642 synchronized (m_tfactory) 643 { 644 URL documentURL = null; 645 URL styleURL = null; 646 StringWriter osw = new StringWriter (); 647 PrintWriter pw = new PrintWriter (osw, false); 648 StreamResult result = new StreamResult (pw); 649 650 this.showStatus("Begin Transformation..."); 651 try 652 { 653 documentURL = new URL (m_codeBase, m_documentURL); 654 StreamSource xmlSource = new StreamSource (documentURL.toString()); 655 656 styleURL = new URL (m_codeBase, m_styleURL); 657 StreamSource xslSource = new StreamSource (styleURL.toString()); 658 659 Transformer transformer = m_tfactory.newTransformer(xslSource); 660 661 m_keys = m_parameters.keys(); 662 while (m_keys.hasMoreElements()){ 663 Object key = m_keys.nextElement(); 664 Object expression = m_parameters.get(key); 665 transformer.setParameter((String ) key, expression); 666 } 667 transformer.transform(xmlSource, result); 668 } 669 catch (TransformerConfigurationException tfe) 670 { 671 tfe.printStackTrace(); 672 System.exit(-1); 673 } 674 catch (MalformedURLException e) 675 { 676 e.printStackTrace(); 677 System.exit(-1); 678 } 679 680 this.showStatus("Transformation Done!"); 681 htmlData = osw.toString(); 682 } 683 return htmlData; 684 } 685 686 692 class TrustedAgent implements Runnable 693 { 694 695 698 public boolean m_getData = false; 699 700 703 public boolean m_getSource = false; 704 705 709 public void run() 710 { 711 while (true) 712 { 713 m_trustedWorker.yield(); 714 715 if (m_getData) { 717 try 718 { 719 m_getData = false; 720 m_htmlText = null; 721 m_sourceText = null; 722 if (m_getSource) { 724 m_getSource = false; 725 m_sourceText = getSource(); 726 } 727 else m_htmlText = processTransformation(); 729 } 730 catch (Exception e) 731 { 732 e.printStackTrace(); 733 } 734 finally 735 { 736 synchronized (m_callThread) 737 { 738 m_callThread.notify(); 739 } 740 } 741 } 742 else 743 { 744 try 745 { 746 m_trustedWorker.sleep(50); 747 } 748 catch (InterruptedException ie) 749 { 750 ie.printStackTrace(); 751 } 752 } 753 } 754 } 755 } 756 } 757 | Popular Tags |