1 package com.icl.saxon; 2 3 8 64 65 import java.applet.Applet ; 66 67 import java.awt.Graphics ; 68 69 import java.net.URL ; 70 import java.net.MalformedURLException ; 71 72 import java.io.PrintWriter ; 73 import java.io.StringWriter ; 74 import java.io.StringReader ; 75 import java.io.IOException ; 76 import java.io.InputStream ; 77 78 import java.util.Properties ; 79 80 84 import javax.xml.transform.Result ; 86 import javax.xml.transform.TransformerFactory ; 87 import javax.xml.transform.TransformerConfigurationException ; 88 import javax.xml.transform.Transformer ; 89 import javax.xml.transform.TransformerException ; 90 import javax.xml.transform.Templates ; 91 import javax.xml.transform.Source ; 92 import javax.xml.transform.Result ; 93 import javax.xml.transform.stream.StreamResult ; 94 import javax.xml.transform.stream.StreamSource ; 95 96 107 public class XSLTProcessorApplet extends Applet 108 { 109 110 114 TransformerFactory m_tfactory = null; 115 116 119 private String m_styleURL; 120 121 124 private String m_documentURL; 125 126 130 133 private final String PARAM_styleURL = "styleURL"; 134 135 138 private final String PARAM_documentURL = "documentURL"; 139 140 141 144 147 private String m_styleURLOfCached = null; 148 149 152 private String m_documentURLOfCached = null; 153 154 158 private URL m_codeBase = null; 159 160 163 private String m_treeURL = null; 164 165 169 private URL m_documentBase = null; 170 171 174 transient private Thread m_callThread = null; 175 176 178 transient private TrustedAgent m_trustedAgent = null; 179 180 183 transient private Thread m_trustedWorker = null; 184 185 188 transient private String m_htmlText = null; 189 190 193 transient private String m_sourceText = null; 194 195 198 transient private String m_nameOfIDAttrOfElemToModify = null; 199 200 202 transient private String m_elemIdToModify = null; 203 204 206 transient private String m_attrNameToSet = null; 207 208 210 transient private String m_attrValueToSet = null; 211 212 215 public XSLTProcessorApplet(){} 216 217 221 public String getAppletInfo() 222 { 223 return "Name: XSLTProcessorApplet\r\n" + "Author: Scott Boag"; 224 } 225 226 231 public String [][] getParameterInfo() 232 { 233 234 String [][] info = 235 { 236 { PARAM_styleURL, "String", "URL to an XSL stylesheet" }, 237 { PARAM_documentURL, "String", "URL to an XML document" }, 238 }; 239 240 return info; 241 } 242 243 246 public void init() 247 { 248 249 String param; 255 256 param = getParameter(PARAM_styleURL); 259 260 if (param != null) 261 setStyleURL(param); 262 263 param = getParameter(PARAM_documentURL); 266 267 if (param != null) 268 setDocumentURL(param); 269 270 m_codeBase = this.getCodeBase(); 271 m_documentBase = this.getDocumentBase(); 272 273 resize(320, 240); 280 } 281 282 286 public void start() 287 { 288 289 m_trustedAgent = new TrustedAgent(); 290 Thread currentThread = Thread.currentThread(); 291 m_trustedWorker = new Thread (currentThread.getThreadGroup(), 292 m_trustedAgent); 293 m_trustedWorker.start(); 294 try 295 { 296 m_tfactory = TransformerFactory.newInstance(); 297 this.showStatus("Causing Transformer and Parser to Load and JIT..."); 298 299 StringReader xmlbuf = new StringReader ("<?xml version='1.0'?><foo/>"); 301 StringReader xslbuf = new StringReader ( 302 "<?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>"); 303 PrintWriter pw = new PrintWriter (new StringWriter ()); 304 305 synchronized (m_tfactory) 306 { 307 Templates templates = m_tfactory.newTemplates(new StreamSource (xslbuf)); 308 Transformer transformer = templates.newTransformer(); 309 transformer.transform(new StreamSource (xmlbuf), new StreamResult (pw)); 310 } 311 System.out.println("Primed the pump!"); 312 this.showStatus("Ready to go!"); 313 } 314 catch (Exception e) 315 { 316 this.showStatus("Could not prime the pump!"); 317 System.out.println("Could not prime the pump!"); 318 e.printStackTrace(); 319 } 320 } 321 322 326 public void paint(Graphics g){} 327 328 332 public void stop() 333 { 334 if (null != m_trustedWorker) 335 { 336 m_trustedWorker.stop(); 337 338 m_trustedWorker = null; 340 } 341 342 m_styleURLOfCached = null; 343 m_documentURLOfCached = null; 344 } 345 346 349 public void destroy() 350 { 351 if (null != m_trustedWorker) 352 { 353 m_trustedWorker.stop(); 354 355 m_trustedWorker = null; 357 } 358 m_styleURLOfCached = null; 359 m_documentURLOfCached = null; 360 } 361 362 367 public void setStyleURL(String urlString) 368 { 369 m_styleURL = urlString; 370 } 371 372 377 public void setDocumentURL(String urlString) 378 { 379 m_documentURL = urlString; 380 } 381 382 387 public void freeCache() 388 { 389 m_styleURLOfCached = null; 390 m_documentURLOfCached = null; 391 } 392 393 401 public void setStyleSheetAttribute(String nameOfIDAttrOfElemToModify, 402 String elemId, String attrName, 403 String value) 404 { 405 m_nameOfIDAttrOfElemToModify = nameOfIDAttrOfElemToModify; 406 m_elemIdToModify = elemId; 407 m_attrNameToSet = attrName; 408 m_attrValueToSet = value; 409 } 410 411 414 transient String m_key; 415 416 419 transient String m_expression; 420 421 428 public void setStylesheetParam(String key, String expr) 429 { 430 m_key = key; 431 m_expression = expr; 432 } 433 434 442 public String escapeString(String s) 443 { 444 StringBuffer sb = new StringBuffer (); 445 int length = s.length(); 446 447 for (int i = 0; i < length; i++) 448 { 449 char ch = s.charAt(i); 450 451 if ('<' == ch) 452 { 453 sb.append("<"); 454 } 455 else if ('>' == ch) 456 { 457 sb.append(">"); 458 } 459 else if ('&' == ch) 460 { 461 sb.append("&"); 462 } 463 else if (0xd800 <= ch && ch < 0xdc00) 464 { 465 int next; 467 468 if (i + 1 >= length) 469 { 470 next = '?'; } 472 else 473 { 474 next = s.charAt(++i); 475 476 if (!(0xdc00 <= next && next < 0xe000)) { 477 next = '?'; } else { 479 next = ((ch - 0xd800) << 10) + next - 0xdc00 + 0x00010000; 481 } 482 } 483 sb.append("&#x"); 484 sb.append(Integer.toHexString(next)); 485 sb.append(";"); 486 } 487 else 488 { 489 sb.append(ch); 490 } 491 } 492 return sb.toString(); 493 } 494 495 502 public String getHtmlText() 503 { 504 m_trustedAgent.m_getData = true; 505 m_callThread = Thread.currentThread(); 506 try 507 { 508 synchronized (m_callThread) 509 { 510 m_callThread.wait(); 511 } 512 } 513 catch (InterruptedException ie) 514 { 515 System.out.println(ie.getMessage()); 516 } 517 return m_htmlText; 518 } 519 520 529 public String getTreeAsText(String treeURL) throws IOException 530 { 531 m_treeURL = treeURL; 532 m_trustedAgent.m_getData = true; 533 m_trustedAgent.m_getSource = true; 534 m_callThread = Thread.currentThread(); 535 try 536 { 537 synchronized (m_callThread) 538 { 539 m_callThread.wait(); 540 } 541 } 542 catch (InterruptedException ie) 543 { 544 System.out.println(ie.getMessage()); 545 } 546 return m_sourceText; 547 } 548 549 555 private String getSource() throws TransformerException 556 { 557 StringWriter osw = new StringWriter (); 558 PrintWriter pw = new PrintWriter (osw, false); 559 String text = ""; 560 try 561 { 562 URL docURL = new URL (m_documentBase, m_treeURL); 563 synchronized (m_tfactory) 564 { 565 Transformer transformer = m_tfactory.newTransformer(); 566 StreamSource source = new StreamSource (docURL.toString()); 567 StreamResult result = new StreamResult (pw); 568 transformer.transform(source, result); 569 text = osw.toString(); 570 } 571 } 572 catch (MalformedURLException e) 573 { 574 e.printStackTrace(); 575 System.exit(-1); 576 } 577 catch (Exception any_error) 578 { 579 any_error.printStackTrace(); 580 } 581 return text; 582 } 583 584 592 public String getSourceTreeAsText() throws Exception 593 { 594 return getTreeAsText(m_documentURL); 595 } 596 597 605 public String getStyleTreeAsText() throws Exception 606 { 607 return getTreeAsText(m_styleURL); 608 } 609 610 618 public String getResultTreeAsText() throws Exception 619 { 620 return escapeString(getHtmlText()); 621 } 622 623 633 public String transformToHtml(String doc, String style) 634 { 635 636 if (null != doc) 637 { 638 m_documentURL = doc; 639 } 640 641 if (null != style) 642 { 643 m_styleURL = style; 644 } 645 646 return getHtmlText(); 647 } 648 649 658 public String transformToHtml(String doc) 659 { 660 661 if (null != doc) 662 { 663 m_documentURL = doc; 664 } 665 666 m_styleURL = null; 667 668 return getHtmlText(); 669 } 670 671 672 679 private String processTransformation() throws TransformerException 680 { 681 String htmlData = null; 682 this.showStatus("Waiting for Transformer and Parser to finish loading and JITing..."); 683 684 synchronized (m_tfactory) 685 { 686 URL documentURL = null; 687 URL styleURL = null; 688 StringWriter osw = new StringWriter (); 689 PrintWriter pw = new PrintWriter (osw, false); 690 StreamResult result = new StreamResult (pw); 691 692 this.showStatus("Begin Transformation..."); 693 try 694 { 695 documentURL = new URL (m_codeBase, m_documentURL); 696 StreamSource xmlSource = new StreamSource (documentURL.toString()); 697 698 styleURL = new URL (m_codeBase, m_styleURL); 699 StreamSource xslSource = new StreamSource (styleURL.toString()); 700 701 Transformer transformer = m_tfactory.newTransformer(xslSource); 702 703 if (null != m_key) 704 transformer.setParameter(m_key, m_expression); 705 706 transformer.transform(xmlSource, result); 707 } 708 catch (TransformerConfigurationException tfe) 709 { 710 tfe.printStackTrace(); 711 System.exit(-1); 712 } 713 catch (MalformedURLException e) 714 { 715 e.printStackTrace(); 716 System.exit(-1); 717 } 718 719 this.showStatus("Transformation Done!"); 720 htmlData = osw.toString(); 721 } 722 return htmlData; 723 } 724 725 731 class TrustedAgent implements Runnable 732 { 733 734 737 public boolean m_getData = false; 738 739 742 public boolean m_getSource = false; 743 744 748 public void run() 749 { 750 while (true) 751 { 752 m_trustedWorker.yield(); 753 754 if (m_getData) { 756 try 757 { 758 m_getData = false; 759 m_htmlText = null; 760 m_sourceText = null; 761 if (m_getSource) { 763 m_getSource = false; 764 m_sourceText = getSource(); 765 } 766 else m_htmlText = processTransformation(); 768 } 769 catch (Exception e) 770 { 771 e.printStackTrace(); 772 } 773 finally 774 { 775 synchronized (m_callThread) 776 { 777 m_callThread.notify(); 778 } 779 } 780 } 781 else 782 { 783 try 784 { 785 m_trustedWorker.sleep(50); 786 } 787 catch (InterruptedException ie) 788 { 789 ie.printStackTrace(); 790 } 791 } 792 } 793 } 794 } 795 } 796 | Popular Tags |