1 16 19 package com.sun.org.apache.xalan.internal.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 com.sun.org.apache.xalan.internal.res.XSLMessages; 41 import com.sun.org.apache.xalan.internal.res.XSLTErrorResources; 42 43 59 public class XSLTProcessorApplet extends Applet 60 { 61 62 68 transient TransformerFactory m_tfactory = null; 69 70 73 private String m_styleURL; 74 75 78 private String m_documentURL; 79 80 84 87 private final String PARAM_styleURL = "styleURL"; 88 89 92 private final String PARAM_documentURL = "documentURL"; 93 94 95 98 101 private String m_styleURLOfCached = null; 102 103 106 private String m_documentURLOfCached = null; 107 108 112 private URL m_codeBase = null; 113 114 117 private String m_treeURL = null; 118 119 123 private URL m_documentBase = null; 124 125 128 transient private Thread m_callThread = null; 129 130 132 transient private TrustedAgent m_trustedAgent = null; 133 134 137 transient private Thread m_trustedWorker = null; 138 139 142 transient private String m_htmlText = null; 143 144 147 transient private String m_sourceText = null; 148 149 152 transient private String m_nameOfIDAttrOfElemToModify = null; 153 154 156 transient private String m_elemIdToModify = null; 157 158 160 transient private String m_attrNameToSet = null; 161 162 164 transient private String m_attrValueToSet = null; 165 166 169 public XSLTProcessorApplet(){} 170 171 175 public String getAppletInfo() 176 { 177 return "Name: XSLTProcessorApplet\r\n" + "Author: Scott Boag"; 178 } 179 180 185 public String [][] getParameterInfo() 186 { 187 188 String [][] info = 189 { 190 { PARAM_styleURL, "String", "URL to an XSL stylesheet" }, 191 { PARAM_documentURL, "String", "URL to an XML document" }, 192 }; 193 194 return info; 195 } 196 197 200 public void init() 201 { 202 203 String param; 209 210 param = getParameter(PARAM_styleURL); 213 214 m_parameters = new Hashtable (); 216 217 if (param != null) 218 setStyleURL(param); 219 220 param = getParameter(PARAM_documentURL); 223 224 if (param != null) 225 setDocumentURL(param); 226 227 m_codeBase = this.getCodeBase(); 228 m_documentBase = this.getDocumentBase(); 229 230 resize(320, 240); 237 } 238 239 243 public void start() 244 { 245 boolean passed = false; 247 try { 248 java.security.AccessController.checkPermission(new java.security.AllPermission ()); 249 } catch (SecurityException se) { 250 passed = true; 252 } 253 if (!passed) { 254 throw new SecurityException ("The XSLTProcessorApplet class must be extended and its method start() overridden."); 255 } 256 257 m_trustedAgent = new TrustedAgent(); 258 Thread currentThread = Thread.currentThread(); 259 m_trustedWorker = new Thread (currentThread.getThreadGroup(), 260 m_trustedAgent); 261 m_trustedWorker.start(); 262 try 263 { 264 m_tfactory = TransformerFactory.newInstance(); 265 this.showStatus("Causing Transformer and Parser to Load and JIT..."); 266 267 StringReader xmlbuf = new StringReader ("<?xml version='1.0'?><foo/>"); 269 StringReader xslbuf = new StringReader ( 270 "<?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>"); 271 PrintWriter pw = new PrintWriter (new StringWriter ()); 272 273 synchronized (m_tfactory) 274 { 275 Templates templates = m_tfactory.newTemplates(new StreamSource (xslbuf)); 276 Transformer transformer = templates.newTransformer(); 277 transformer.transform(new StreamSource (xmlbuf), new StreamResult (pw)); 278 } 279 System.out.println("Primed the pump!"); 280 this.showStatus("Ready to go!"); 281 } 282 catch (Exception e) 283 { 284 this.showStatus("Could not prime the pump!"); 285 System.out.println("Could not prime the pump!"); 286 e.printStackTrace(); 287 } 288 } 289 290 294 public void paint(Graphics g){} 295 296 300 public void stop() 301 { 302 if (null != m_trustedWorker) 303 { 304 m_trustedWorker.stop(); 305 306 m_trustedWorker = null; 308 } 309 310 m_styleURLOfCached = null; 311 m_documentURLOfCached = null; 312 } 313 314 317 public void destroy() 318 { 319 if (null != m_trustedWorker) 320 { 321 m_trustedWorker.stop(); 322 323 m_trustedWorker = null; 325 } 326 m_styleURLOfCached = null; 327 m_documentURLOfCached = null; 328 } 329 330 335 public void setStyleURL(String urlString) 336 { 337 m_styleURL = urlString; 338 } 339 340 345 public void setDocumentURL(String urlString) 346 { 347 m_documentURL = urlString; 348 } 349 350 355 public void freeCache() 356 { 357 m_styleURLOfCached = null; 358 m_documentURLOfCached = null; 359 } 360 361 369 public void setStyleSheetAttribute(String nameOfIDAttrOfElemToModify, 370 String elemId, String attrName, 371 String value) 372 { 373 m_nameOfIDAttrOfElemToModify = nameOfIDAttrOfElemToModify; 374 m_elemIdToModify = elemId; 375 m_attrNameToSet = attrName; 376 m_attrValueToSet = value; 377 } 378 379 380 383 transient Hashtable m_parameters; 384 385 392 public void setStylesheetParam(String key, String expr) 393 { 394 m_parameters.put(key, expr); 395 } 396 397 405 public String escapeString(String s) 406 { 407 StringBuffer sb = new StringBuffer (); 408 int length = s.length(); 409 410 for (int i = 0; i < length; i++) 411 { 412 char ch = s.charAt(i); 413 414 if ('<' == ch) 415 { 416 sb.append("<"); 417 } 418 else if ('>' == ch) 419 { 420 sb.append(">"); 421 } 422 else if ('&' == ch) 423 { 424 sb.append("&"); 425 } 426 else if (0xd800 <= ch && ch < 0xdc00) 427 { 428 int next; 430 431 if (i + 1 >= length) 432 { 433 throw new RuntimeException ( 434 XSLMessages.createMessage( 435 XSLTErrorResources.ER_INVALID_UTF16_SURROGATE, 436 new Object []{ Integer.toHexString(ch) })); 438 } 440 else 441 { 442 next = s.charAt(++i); 443 444 if (!(0xdc00 <= next && next < 0xe000)) 445 throw new RuntimeException ( 446 XSLMessages.createMessage( 447 XSLTErrorResources.ER_INVALID_UTF16_SURROGATE, 448 new Object []{ 449 Integer.toHexString(ch) + " " 450 + Integer.toHexString(next) })); 452 next = ((ch - 0xd800) << 10) + next - 0xdc00 + 0x00010000; 454 } 455 sb.append("&#x"); 456 sb.append(Integer.toHexString(next)); 457 sb.append(";"); 458 } 459 else 460 { 461 sb.append(ch); 462 } 463 } 464 return sb.toString(); 465 } 466 467 474 public String getHtmlText() 475 { 476 m_trustedAgent.m_getData = true; 477 m_callThread = Thread.currentThread(); 478 try 479 { 480 synchronized (m_callThread) 481 { 482 m_callThread.wait(); 483 } 484 } 485 catch (InterruptedException ie) 486 { 487 System.out.println(ie.getMessage()); 488 } 489 return m_htmlText; 490 } 491 492 501 public String getTreeAsText(String treeURL) throws IOException 502 { 503 m_treeURL = treeURL; 504 m_trustedAgent.m_getData = true; 505 m_trustedAgent.m_getSource = true; 506 m_callThread = Thread.currentThread(); 507 try 508 { 509 synchronized (m_callThread) 510 { 511 m_callThread.wait(); 512 } 513 } 514 catch (InterruptedException ie) 515 { 516 System.out.println(ie.getMessage()); 517 } 518 return m_sourceText; 519 } 520 521 527 private String getSource() throws TransformerException 528 { 529 StringWriter osw = new StringWriter (); 530 PrintWriter pw = new PrintWriter (osw, false); 531 String text = ""; 532 try 533 { 534 URL docURL = new URL (m_documentBase, m_treeURL); 535 synchronized (m_tfactory) 536 { 537 Transformer transformer = m_tfactory.newTransformer(); 538 StreamSource source = new StreamSource (docURL.toString()); 539 StreamResult result = new StreamResult (pw); 540 transformer.transform(source, result); 541 text = osw.toString(); 542 } 543 } 544 catch (MalformedURLException e) 545 { 546 e.printStackTrace(); 547 throw new RuntimeException (e.getMessage()); 548 } 549 catch (Exception any_error) 550 { 551 any_error.printStackTrace(); 552 } 553 return text; 554 } 555 556 564 public String getSourceTreeAsText() throws Exception 565 { 566 return getTreeAsText(m_documentURL); 567 } 568 569 577 public String getStyleTreeAsText() throws Exception 578 { 579 return getTreeAsText(m_styleURL); 580 } 581 582 590 public String getResultTreeAsText() throws Exception 591 { 592 return escapeString(getHtmlText()); 593 } 594 595 605 public String transformToHtml(String doc, String style) 606 { 607 608 if (null != doc) 609 { 610 m_documentURL = doc; 611 } 612 613 if (null != style) 614 { 615 m_styleURL = style; 616 } 617 618 return getHtmlText(); 619 } 620 621 630 public String transformToHtml(String doc) 631 { 632 633 if (null != doc) 634 { 635 m_documentURL = doc; 636 } 637 638 m_styleURL = null; 639 640 return getHtmlText(); 641 } 642 643 644 651 private String processTransformation() throws TransformerException 652 { 653 String htmlData = null; 654 this.showStatus("Waiting for Transformer and Parser to finish loading and JITing..."); 655 656 synchronized (m_tfactory) 657 { 658 URL documentURL = null; 659 URL styleURL = null; 660 StringWriter osw = new StringWriter (); 661 PrintWriter pw = new PrintWriter (osw, false); 662 StreamResult result = new StreamResult (pw); 663 664 this.showStatus("Begin Transformation..."); 665 try 666 { 667 documentURL = new URL (m_codeBase, m_documentURL); 668 StreamSource xmlSource = new StreamSource (documentURL.toString()); 669 670 styleURL = new URL (m_codeBase, m_styleURL); 671 StreamSource xslSource = new StreamSource (styleURL.toString()); 672 673 Transformer transformer = m_tfactory.newTransformer(xslSource); 674 675 676 Enumeration m_keys = m_parameters.keys(); 677 while (m_keys.hasMoreElements()){ 678 Object key = m_keys.nextElement(); 679 Object expression = m_parameters.get(key); 680 transformer.setParameter((String ) key, expression); 681 } 682 transformer.transform(xmlSource, result); 683 } 684 catch (TransformerConfigurationException tfe) 685 { 686 tfe.printStackTrace(); 687 throw new RuntimeException (tfe.getMessage()); 688 } 689 catch (MalformedURLException e) 690 { 691 e.printStackTrace(); 692 throw new RuntimeException (e.getMessage()); 693 } 694 695 this.showStatus("Transformation Done!"); 696 htmlData = osw.toString(); 697 } 698 return htmlData; 699 } 700 701 707 class TrustedAgent implements Runnable 708 { 709 710 713 public boolean m_getData = false; 714 715 718 public boolean m_getSource = false; 719 720 724 public void run() 725 { 726 while (true) 727 { 728 m_trustedWorker.yield(); 729 730 if (m_getData) { 732 try 733 { 734 m_getData = false; 735 m_htmlText = null; 736 m_sourceText = null; 737 if (m_getSource) { 739 m_getSource = false; 740 m_sourceText = getSource(); 741 } 742 else m_htmlText = processTransformation(); 744 } 745 catch (Exception e) 746 { 747 e.printStackTrace(); 748 } 749 finally 750 { 751 synchronized (m_callThread) 752 { 753 m_callThread.notify(); 754 } 755 } 756 } 757 else 758 { 759 try 760 { 761 m_trustedWorker.sleep(50); 762 } 763 catch (InterruptedException ie) 764 { 765 ie.printStackTrace(); 766 } 767 } 768 } 769 } 770 } 771 772 private static final long serialVersionUID=4618876841979251422L; 776 777 private void readObject(java.io.ObjectInputStream inStream) throws IOException , ClassNotFoundException 779 { 780 inStream.defaultReadObject(); 781 782 784 m_tfactory = TransformerFactory.newInstance(); 787 } 788 } 789 | Popular Tags |