1 import javax.xml.transform.*; 2 import javax.xml.transform.sax.*; 3 import javax.xml.transform.dom.*; 4 import javax.xml.transform.stream.*; 5 import javax.xml.parsers.*; 6 7 import java.io.InputStream ; 9 import java.io.Reader ; 10 import java.io.IOException ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileReader ; 14 import java.io.BufferedInputStream ; 15 import java.io.BufferedReader ; 16 import java.io.FileNotFoundException ; 17 18 import java.util.Properties ; 19 20 import org.xml.sax.InputSource ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.Parser ; 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.helpers.ParserAdapter ; 26 import org.xml.sax.helpers.XMLReaderFactory ; 27 import org.xml.sax.helpers.XMLFilterImpl ; 28 import org.xml.sax.XMLReader ; 29 import org.xml.sax.XMLFilter ; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.ext.LexicalHandler ; 32 import org.xml.sax.ext.DeclHandler ; 33 34 import org.w3c.dom.Node ; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 39 import javax.xml.parsers.DocumentBuilder ; 41 import javax.xml.parsers.DocumentBuilderFactory ; 42 import javax.xml.parsers.ParserConfigurationException ; 43 44 import java.io.BufferedInputStream ; 46 import java.net.URL ; 47 import java.net.MalformedURLException ; 48 49 50 56 public class TraxExamples { 57 58 61 public static void main(String argv[]) 62 throws TransformerException, TransformerConfigurationException, 63 IOException , SAXException , ParserConfigurationException , 64 FileNotFoundException { 65 66 72 String test = "all"; 73 if (argv.length > 0) { 74 test = argv[0]; 75 } 76 77 String foo_xml = "xml/foo.xml"; 78 String foo_xsl = "xsl/foo.xsl"; 79 String baz_xml = "xml/baz.xml"; 80 String baz_xsl = "xsl/baz.xsl"; 81 String foo2_xsl = "xsl/foo2.xsl"; 82 String foo3_xsl = "xsl/foo3.xsl"; 83 String text_xsl = "xsl/text.xsl"; 84 String embedded_xml = "xml/embedded.xml"; 85 86 if (test.equals("all") || test.equals("exampleParseOnly")) { 87 System.out.println("\n\n==== exampleParseOnly ===="); 88 89 try { 90 exampleParseOnly(foo_xml); 91 } catch (Exception ex) { 92 handleException(ex); 93 } 94 } 95 96 97 98 if (test.equals("all") || test.equals("exampleSimple1")) { 99 System.out.println("\n\n==== exampleSimple1 ===="); 100 101 try { 102 exampleSimple1(foo_xml, foo_xsl); 103 } catch (Exception ex) { 104 handleException(ex); 105 } 106 } 107 108 if (test.equals("all") || test.equals("exampleSimple2")) { 109 System.out.println("\n\n==== exampleSimple2 ===="); 110 111 try { 112 exampleSimple2(foo_xml, foo_xsl); 113 } catch (Exception ex) { 114 handleException(ex); 115 } 116 } 117 118 if (test.equals("all") || test.equals("exampleFromStream")) { 119 System.out.println("\n\n==== exampleFromStream ===="); 120 121 try { 122 exampleFromStream(foo_xml, baz_xsl); 123 } catch (Exception ex) { 124 handleException(ex); 125 } 126 } 127 128 if (test.equals("all") || test.equals("exampleFromReader")) { 129 System.out.println("\n\n==== exampleFromReader ===="); 130 131 try { 132 exampleFromReader(foo_xml, foo_xsl); 133 } catch (Exception ex) { 134 handleException(ex); 135 } 136 } 137 138 if (test.equals("all") || test.equals("exampleUseTemplatesObj")) { 139 System.out.println("\n\n==== exampleUseTemplatesObj ===="); 140 141 try { 142 exampleUseTemplatesObj(foo_xml, baz_xml, 143 foo_xsl); 144 } catch (Exception ex) { 145 handleException(ex); 146 } 147 } 148 149 if (test.equals("all") || test.equals("exampleUseTemplatesHandler")) { 150 System.out.println("\n\n==== exampleUseTemplatesHandler ===="); 151 152 try { 153 (new TraxExamples()).exampleUseTemplatesHandler(foo_xml, foo_xsl); 154 } catch (Exception ex) { 155 handleException(ex); 156 } 157 } 158 159 if (test.equals("all") || test.equals("exampleContentHandlerToContentHandler")) { 160 System.out 161 .println("\n\n==== exampleContentHandlerToContentHandler ===="); 162 163 try { 164 exampleContentHandlerToContentHandler(foo_xml, 165 foo_xsl); 166 } catch (Exception ex) { 167 handleException(ex); 168 } 169 } 170 171 if (test.equals("all") || test.equals("exampleXMLReader")) { 172 System.out.println("\n\n==== exampleXMLReader ===="); 173 174 try { 175 exampleXMLReader(foo_xml, foo_xsl); 176 } catch (Exception ex) { 177 handleException(ex); 178 } 179 } 180 181 if (test.equals("all") || test.equals("exampleXMLFilter")) { 182 System.out.println("\n\n==== exampleXMLFilter ===="); 183 184 try { 185 exampleXMLFilter(foo_xml, foo_xsl); 186 } catch (Exception ex) { 187 handleException(ex); 188 } 189 } 190 191 if (test.equals("all") || test.equals("exampleXMLFilterChain")) { 192 System.out.println("\n\n==== exampleXMLFilterChain ===="); 193 194 try { 195 exampleXMLFilterChain(foo_xml, foo_xsl, 196 foo2_xsl, foo3_xsl); 197 } catch (Exception ex) { 198 handleException(ex); 199 } 200 } 201 202 if (test.equals("all") || test.equals("exampleDOMtoDOM")) { 203 System.out.println("\n\n==== exampleDOMtoDOM ===="); 204 205 try { 206 exampleDOMtoDOM(foo_xml, foo_xsl); 207 } catch (Exception ex) { 208 handleException(ex); 209 } 210 } 211 212 if (test.equals("all") || test.equals("exampleDOMtoDOMNew")) { 213 System.out.println("\n\n==== exampleDOMtoDOMNew ===="); 214 215 try { 216 exampleDOMtoDOMNew(foo_xml, foo_xsl); 217 } catch (Exception ex) { 218 handleException(ex); 219 } 220 } 221 222 if (test.equals("all") || test.equals("exampleDOMtoDOMNonRoot")) { 223 System.out.println("\n\n==== exampleDOMtoDOMNonRoot ===="); 224 225 try { 226 exampleDOMtoDOMNonRoot(foo_xml, foo_xsl); 227 } catch (Exception ex) { 228 handleException(ex); 229 } 230 } 231 232 if (test.equals("all") || test.equals("exampleDOMtoDOMAlien")) { 233 System.out.println("\n\n==== exampleDOMtoDOMAlien ===="); 234 235 try { 236 exampleDOMtoDOMAlien(foo_xml, foo_xsl); 237 } catch (Exception ex) { 238 handleException(ex); 239 } 240 } 241 242 if (test.equals("all") || test.equals("exampleNodeInfo")) { 243 System.out.println("\n\n==== exampleNodeInfo ===="); 244 245 try { 246 exampleNodeInfo(foo_xml, foo_xsl); 247 } catch (Exception ex) { 248 handleException(ex); 249 } 250 } 251 252 if (test.equals("all") || test.equals("exampleSAXtoDOMNewIdentity")) { 253 System.out.println("\n\n==== exampleSAXtoDOMNewIdentity ===="); 254 255 try { 256 exampleSAXtoDOMNewIdentity(foo_xml, foo_xsl); 257 } catch (Exception ex) { 258 handleException(ex); 259 } 260 } 261 262 if (test.equals("all") || test.equals("exampleParam")) { 263 System.out.println("\n\n==== exampleParam ===="); 264 265 try { 266 exampleParam(foo_xml, foo_xsl); 267 } catch (Exception ex) { 268 handleException(ex); 269 } 270 } 271 272 if (test.equals("all") || test.equals("exampleTransformerReuse")) { 273 System.out.println("\n\n==== exampleTransformerReuse ===="); 274 275 try { 276 exampleTransformerReuse(foo_xml, foo_xsl); 277 } catch (Exception ex) { 278 handleException(ex); 279 } 280 } 281 282 if (test.equals("all") || test.equals("exampleOutputProperties")) { 283 System.out.println("\n\n==== exampleOutputProperties ===="); 284 285 try { 286 exampleOutputProperties(foo_xml, foo_xsl); 287 } catch (Exception ex) { 288 handleException(ex); 289 } 290 } 291 292 if (test.equals("all") || test.equals("exampleUseAssociated")) { 293 System.out.println("\n\n==== exampleUseAssociated ===="); 294 295 try { 296 exampleUseAssociated(foo_xml); 297 } catch (Exception ex) { 298 handleException(ex); 299 } 300 } 301 302 if (test.equals("all") || test.equals("exampleUseEmbedded")) { 303 System.out.println("\n\n==== exampleUseEmbedded ===="); 304 305 try { 306 exampleUseAssociated(embedded_xml); 307 } catch (Exception ex) { 308 handleException(ex); 309 } 310 } 311 312 if (test.equals("all") || test.equals("exampleContentHandlertoDOM")) { 313 System.out.println("\n\n==== exampleContentHandlertoDOM ===="); 314 315 try { 316 exampleContentHandlertoDOM(foo_xml, foo_xsl); 317 } catch (Exception ex) { 318 handleException(ex); 319 } 320 } 321 322 if (test.equals("all") || test.equals("exampleAsSerializer")) { 323 System.out.println("\n\n==== exampleAsSerializer ===="); 324 325 try { 326 exampleAsSerializer(foo_xml, foo_xsl); 327 } catch (Exception ex) { 328 handleException(ex); 329 } 330 } 331 332 if (test.equals("all") || test.equals("exampleUsingURIResolver")) { 333 System.out.println("\n\n==== exampleUsingURIResolver ===="); 334 335 try { 336 exampleUsingURIResolver(foo_xml, text_xsl); 337 } catch (Exception ex) { 338 handleException(ex); 339 } 340 } 341 342 System.out.println("\n==== done! ===="); 343 } 344 345 349 public static void exampleSimple1(String sourceID, String xslID) 350 throws TransformerException, TransformerConfigurationException { 351 352 TransformerFactory tfactory = TransformerFactory.newInstance(); 354 355 Transformer transformer = 357 tfactory.newTransformer(new StreamSource(xslID)); 358 359 transformer.transform(new StreamSource(sourceID), 361 new StreamResult(System.out)); 362 } 363 364 367 368 public static void exampleParseOnly(String sourceID) throws Exception { 369 System.setProperty("javax.xml.parsers.SAXParserFactory", 370 "com.icl.saxon.aelfred.SAXParserFactoryImpl"); 371 SAXParserFactory factory = SAXParserFactory.newInstance(); 372 SAXParser parser = factory.newSAXParser(); 373 parser.parse(sourceID, new org.xml.sax.helpers.DefaultHandler ()); 374 System.out.println("\nParsing complete\n"); 375 } 376 377 381 public static void exampleSimple2(String sourceID, String xslID) 382 throws TransformerException, TransformerConfigurationException { 383 384 TransformerFactory tfactory = TransformerFactory.newInstance(); 386 387 Transformer transformer = 389 tfactory.newTransformer(new StreamSource(xslID)); 390 391 transformer.transform(new StreamSource(sourceID), 393 new StreamResult(new File ("exampleSimple2.out"))); 394 395 System.out.println("\nOutput written to exampleSimple2.out\n"); 396 } 397 398 401 public static void exampleFromStream(String sourceID, String xslID) 402 throws TransformerException, TransformerConfigurationException, 403 FileNotFoundException , MalformedURLException { 404 405 TransformerFactory tfactory = TransformerFactory.newInstance(); 407 InputStream xslIS = 408 new BufferedInputStream (new FileInputStream (xslID)); 409 StreamSource xslSource = new StreamSource(xslIS); 410 411 415 Transformer transformer = tfactory.newTransformer(xslSource); 417 InputStream xmlIS = 418 new BufferedInputStream (new FileInputStream (sourceID)); 419 StreamSource xmlSource = new StreamSource(xmlIS); 420 421 425 transformer.transform(xmlSource, new StreamResult(System.out)); 427 } 428 429 434 public static void exampleFromReader(String sourceID, String xslID) 435 throws TransformerException, TransformerConfigurationException, 436 FileNotFoundException , MalformedURLException { 437 438 TransformerFactory tfactory = TransformerFactory.newInstance(); 440 441 Reader xslReader = new BufferedReader (new FileReader (xslID)); 443 StreamSource xslSource = new StreamSource(xslReader); 444 445 xslSource.setSystemId(xslID); 447 448 Transformer transformer = tfactory.newTransformer(xslSource); 450 451 Reader xmlReader = new BufferedReader (new FileReader (sourceID)); 453 StreamSource xmlSource = new StreamSource(xmlReader); 454 455 459 transformer.transform(xmlSource, new StreamResult(System.out)); 461 } 462 463 466 public static void exampleUseTemplatesObj( 467 String sourceID1, String sourceID2, String xslID) 468 throws TransformerException, 469 TransformerConfigurationException { 470 471 TransformerFactory tfactory = TransformerFactory.newInstance(); 472 473 Templates templates = tfactory.newTemplates(new StreamSource(xslID)); 476 477 Transformer transformer1 = templates.newTransformer(); 480 Transformer transformer2 = templates.newTransformer(); 481 482 System.out.println("\n\n----- transform of " + sourceID1 + " -----"); 483 transformer1.transform(new StreamSource(sourceID1), 484 new StreamResult(System.out)); 485 System.out.println("\n\n----- transform of " + sourceID2 + " -----"); 486 transformer2.transform(new StreamSource(sourceID2), 487 new StreamResult(System.out)); 488 } 489 490 493 public void exampleUseTemplatesHandler( 494 String sourceID, String xslID) 495 throws Exception { 496 497 TransformerFactory tfactory = TransformerFactory.newInstance(); 498 499 if (tfactory.getFeature(SAXSource.FEATURE)) { 501 502 SAXTransformerFactory stfactory = 504 ((SAXTransformerFactory) tfactory); 505 506 javax.xml.transform.sax.TemplatesHandler templatesHandler = 509 stfactory.newTemplatesHandler(); 510 511 XMLReader reader = makeXMLReader(); 513 reader.setFeature("http://xml.org/sax/features/namespaces", true); 514 reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 515 516 XMLFilter filter = new ModifyStylesheetFilter(); 518 filter.setParent(reader); 519 520 filter.setContentHandler(templatesHandler); 521 522 filter.parse(new InputSource (xslID)); 524 525 Templates templates = templatesHandler.getTemplates(); 528 529 templates.newTransformer().transform( 531 new StreamSource(sourceID), new StreamResult(System.out)); 532 533 } else { 534 System.out.println("Factory doesn't implement SAXTransformerFactory"); 535 } 536 537 } 538 539 private class ModifyStylesheetFilter extends XMLFilterImpl { 540 public void startDocument () throws SAXException { 541 System.err.println("ModifyStylesheetFilter#startDocument"); 542 super.startDocument(); 543 } 544 public void startElement (String namespaceURI, String localName, 545 String qName, Attributes atts) throws SAXException { 546 String lname = (qName.startsWith("xsl:") ? localName : ("XX" + localName)); 547 super.startElement(namespaceURI, lname, qName, atts); 548 } 549 public void endElement (String namespaceURI, String localName, 550 String qName) throws SAXException { 551 String lname = (qName.startsWith("xsl:") ? localName : ("XX" + localName)); 552 super.endElement(namespaceURI, lname, qName); 553 } 554 } 555 556 559 public static void exampleContentHandlerToContentHandler( 560 String sourceID, String xslID) 561 throws TransformerException, 562 TransformerConfigurationException, SAXException , 563 IOException , MalformedURLException { 564 565 TransformerFactory tfactory = TransformerFactory.newInstance(); 566 567 if (tfactory.getFeature(SAXSource.FEATURE)) { 569 570 SAXTransformerFactory stfactory = 572 ((SAXTransformerFactory) tfactory); 573 574 TransformerHandler handler = 577 stfactory.newTransformerHandler(new StreamSource(xslID)); 578 579 Result result = new SAXResult(new ExampleContentHandler()); 581 582 handler.setResult(result); 583 584 XMLReader reader = makeXMLReader(); 586 587 reader.setContentHandler(handler); 588 589 reader.setProperty( 592 "http://xml.org/sax/properties/lexical-handler", handler); 593 594 reader.parse(sourceID); 596 } else { 597 System.out.println( 598 "Can't do exampleContentHandlerToContentHandler because tfactory is not a SAXTransformerFactory"); 599 } 600 } 601 602 607 public static void exampleXMLReader(String sourceID, String xslID) 608 throws TransformerException, TransformerConfigurationException, 609 SAXException , IOException , MalformedURLException 610 { 612 613 TransformerFactory tfactory = TransformerFactory.newInstance(); 614 615 if (tfactory.getFeature(SAXSource.FEATURE)) { 616 XMLReader reader = 617 ((SAXTransformerFactory) tfactory) 618 .newXMLFilter(new StreamSource(new File (xslID))); 619 620 reader.setContentHandler(new ExampleContentHandler()); 621 reader.parse(new InputSource (new File (sourceID).toURL().toString())); 622 } else { 623 System.out.println("tfactory does not support SAX features!"); 624 } 625 } 626 627 633 public static void exampleXMLFilter(String sourceID, String xslID) 634 throws TransformerException, TransformerConfigurationException, 635 SAXException , IOException , MalformedURLException 636 { 638 639 TransformerFactory tfactory = TransformerFactory.newInstance(); 640 XMLReader reader = makeXMLReader(); 641 642 643 647 try { 648 reader.setFeature( 649 "http://xml.org/sax/features/namespace-prefixes", true); 650 reader.setFeature( 651 "http://apache.org/xml/features/validation/dynamic", true); 652 } catch (SAXException se) { 653 654 } 657 658 XMLFilter filter = 659 ((SAXTransformerFactory) tfactory) 660 .newXMLFilter(new StreamSource(new File (xslID))); 661 662 filter.setParent(reader); 663 filter.setContentHandler(new ExampleContentHandler()); 664 665 filter.parse(new InputSource (new File (sourceID).toURL().toString())); 669 } 670 671 676 public static void exampleXMLFilterChain( 677 String sourceID, String xslID_1, String xslID_2, String xslID_3) 678 throws TransformerException, 679 TransformerConfigurationException, SAXException , 680 IOException , MalformedURLException { 681 682 TransformerFactory tfactory = TransformerFactory.newInstance(); 683 Templates stylesheet1 = 684 tfactory.newTemplates(new StreamSource(new File (xslID_1))); 685 Transformer transformer1 = stylesheet1.newTransformer(); 686 687 if (tfactory.getFeature(SAXSource.FEATURE)) { 689 SAXTransformerFactory stf = (SAXTransformerFactory) tfactory; 690 XMLReader reader = makeXMLReader(); 691 692 XMLFilter filter1 = stf.newXMLFilter(new StreamSource(new File (xslID_1))); 693 XMLFilter filter2 = stf.newXMLFilter(new StreamSource(new File (xslID_2))); 694 XMLFilter filter3 = stf.newXMLFilter(new StreamSource(new File (xslID_3))); 695 696 if (null != filter1) { 698 699 filter1.setParent(reader); 701 702 filter2.setParent(filter1); 704 705 filter3.setParent(filter2); 707 filter3.setContentHandler(new ExampleContentHandler()); 708 709 filter3.parse(new InputSource (new File (sourceID).toURL().toString())); 717 } else { 718 System.out 719 .println("Can't do exampleXMLFilter because " 720 + "tfactory doesn't support newXMLFilter()"); 721 } 722 } else { 723 System.out.println("Can't do exampleXMLFilter because " 724 + "tfactory is not a SAXTransformerFactory"); 725 } 726 } 727 728 733 public static Node exampleDOMtoDOM(String sourceID, String xslID) 734 throws TransformerException, TransformerConfigurationException, 735 SAXException , IOException , ParserConfigurationException , 736 MalformedURLException { 737 738 TransformerFactory tfactory = TransformerFactory.newInstance(); 739 740 if (tfactory.getFeature(DOMSource.FEATURE)) { 741 Templates templates; 742 743 { 744 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", 745 "com.icl.saxon.om.DocumentBuilderFactoryImpl"); 746 DocumentBuilderFactory dfactory = 747 DocumentBuilderFactory.newInstance(); 748 System.err.println("Using DocumentBuilderFactory " + dfactory.getClass()); 749 750 dfactory.setNamespaceAware(true); 751 752 DocumentBuilder docBuilder = 753 dfactory.newDocumentBuilder(); 754 System.err.println("Using DocumentBuilder " + docBuilder.getClass()); 755 org.w3c.dom.Document outNode = docBuilder.newDocument(); 756 757 Node doc = 758 docBuilder.parse(new InputSource (new File (xslID).toURL().toString())); 759 System.err.println("Stylesheet document built OK"); 760 DOMSource dsource = new DOMSource(doc); 761 762 dsource.setSystemId(new File (xslID).toURL().toString()); 765 766 templates = tfactory.newTemplates(dsource); 767 } 768 769 Transformer transformer = templates.newTransformer(); 770 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 771 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 772 org.w3c.dom.Document outNode = docBuilder.newDocument(); 773 Node doc = docBuilder.parse( 774 new InputSource (new File (sourceID).toURL().toString())); 775 776 System.err.println("Source document built OK"); 777 778 DOMSource ds = new DOMSource(doc); 780 ds.setSystemId(new File (sourceID).toURL().toString()); 781 transformer.transform(ds, new DOMResult(outNode)); 783 System.err.println("Transformation done OK"); 784 785 Transformer serializer = tfactory.newTransformer(); 787 788 serializer.transform(new DOMSource(outNode), 789 new StreamResult(System.out)); 790 791 return outNode; 792 } else { 793 throw new org.xml.sax.SAXNotSupportedException ( 794 "DOM node processing not supported!"); 795 } 796 } 797 798 803 public static void exampleDOMtoDOMNew(String sourceID, String xslID) 804 throws TransformerException, TransformerConfigurationException, 805 SAXException , IOException , ParserConfigurationException , 806 MalformedURLException { 807 808 TransformerFactory tfactory = TransformerFactory.newInstance(); 809 810 if (tfactory.getFeature(DOMSource.FEATURE)) { 811 Templates templates; 812 813 { 814 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", 815 "com.icl.saxon.om.DocumentBuilderFactoryImpl"); 816 DocumentBuilderFactory dfactory = 817 DocumentBuilderFactory.newInstance(); 818 System.err.println("Using DocumentBuilderFactory " + dfactory.getClass()); 819 820 dfactory.setNamespaceAware(true); 821 822 DocumentBuilder docBuilder = 823 dfactory.newDocumentBuilder(); 824 System.err.println("Using DocumentBuilder " + docBuilder.getClass()); 825 org.w3c.dom.Document outNode = docBuilder.newDocument(); 826 827 Node doc = 828 docBuilder.parse(new InputSource (new File (xslID).toURL().toString())); 829 System.err.println("Stylesheet document built OK"); 830 DOMSource dsource = new DOMSource(doc); 831 832 dsource.setSystemId(new File (xslID).toURL().toString()); 835 836 templates = tfactory.newTemplates(dsource); 837 } 838 839 Transformer transformer = templates.newTransformer(); 840 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 841 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 842 Node doc = docBuilder.parse( 844 new InputSource (new File (sourceID).toURL().toString())); 845 846 System.err.println("Source document built OK"); 847 848 DOMSource ds = new DOMSource(doc); 850 DOMResult result = new DOMResult(); 853 transformer.transform(ds, result); 854 System.err.println("Transformation done OK"); 855 856 Transformer serializer = tfactory.newTransformer(); 858 859 serializer.transform(new DOMSource(result.getNode()), 860 new StreamResult(System.out)); 861 862 int k = result.getNode().getChildNodes().getLength(); 863 System.err.println("Result root has " + k + " children"); 864 865 } else { 866 throw new org.xml.sax.SAXNotSupportedException ( 867 "DOM node processing not supported!"); 868 } 869 } 870 871 877 public static Node exampleDOMtoDOMNonRoot(String sourceID, String xslID) 878 throws TransformerException, TransformerConfigurationException, 879 SAXException , IOException , ParserConfigurationException , 880 MalformedURLException { 881 882 TransformerFactory tfactory = TransformerFactory.newInstance(); 883 884 if (tfactory.getFeature(DOMSource.FEATURE)) { 885 Templates templates; 886 887 { 888 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", 889 "com.icl.saxon.om.DocumentBuilderFactoryImpl"); 890 891 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 892 System.err.println("Using DocumentBuilderFactory " + dfactory.getClass()); 893 894 dfactory.setNamespaceAware(true); 895 896 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 897 System.err.println("Using DocumentBuilder " + docBuilder.getClass()); 898 org.w3c.dom.Document outNode = docBuilder.newDocument(); 899 900 Node doc = 901 docBuilder.parse(new InputSource (new File (xslID).toURL().toString())); 902 System.err.println("Stylesheet document built OK"); 903 DOMSource dsource = new DOMSource(doc); 904 905 dsource.setSystemId(new File (xslID).toURL().toString()); 908 909 templates = tfactory.newTemplates(dsource); 910 } 911 912 Transformer transformer = templates.newTransformer(); 913 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 914 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 915 Document outNode = docBuilder.newDocument(); 916 Document doc = docBuilder.parse( 917 new InputSource (new File (sourceID).toURL().toString())); 918 Node bar = doc.getDocumentElement().getFirstChild(); 919 while (bar.getNodeType() != Node.ELEMENT_NODE) { 920 bar = bar.getNextSibling(); 921 } 922 923 System.err.println("Source document built OK"); 924 925 DOMSource ds = new DOMSource(bar); 927 ds.setSystemId(new File (sourceID).toURL().toString()); 928 transformer.transform(ds, new DOMResult(outNode)); 930 System.err.println("Transformation done OK"); 931 932 Transformer serializer = tfactory.newTransformer(); 934 935 serializer.transform(new DOMSource(outNode), 936 new StreamResult(System.out)); 937 938 return outNode; 939 } else { 940 throw new org.xml.sax.SAXNotSupportedException ( 941 "DOM node processing not supported!"); 942 } 943 } 944 945 946 952 public static void exampleDOMtoDOMAlien(String sourceID, String xslID) 953 throws TransformerException, TransformerConfigurationException, 954 SAXException , IOException , ParserConfigurationException , 955 MalformedURLException { 956 957 String factory = null; 958 959 try { 961 Class.forName("org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"); 962 factory = "org.apache.crimson.jaxp.DocumentBuilderFactoryImpl"; 963 } 964 catch (Exception e) { 965 factory = null; 966 } 967 968 if (factory==null) { 970 try { 971 Class.forName("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); 972 factory = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"; 973 } 974 catch (Exception e) { 975 factory = null; 976 } 977 } 978 979 if (factory==null) { 980 System.err.println("No third-party DOM Builder found"); 981 return; 982 } 983 984 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", factory); 985 986 TransformerFactory tfactory = TransformerFactory.newInstance(); 987 988 if (tfactory.getFeature(DOMSource.FEATURE)) { 989 Templates templates; 990 991 { 992 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 993 System.err.println("Using DocumentBuilderFactory " + dfactory.getClass()); 994 995 dfactory.setNamespaceAware(true); 996 997 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 998 System.err.println("Using DocumentBuilder " + docBuilder.getClass()); 999 org.w3c.dom.Document outNode = docBuilder.newDocument(); 1000 1001 Node doc = 1002 docBuilder.parse(new InputSource (new File (xslID).toURL().toString())); 1003 System.err.println("Stylesheet document built OK"); 1004 DOMSource dsource = new DOMSource(doc); 1005 1006 dsource.setSystemId(new File (xslID).toURL().toString()); 1009 1010 templates = tfactory.newTemplates(dsource); 1011 } 1012 1013 Transformer transformer = templates.newTransformer(); 1014 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 1015 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 1016 1017 Document doc = docBuilder.parse( 1018 new InputSource (new File (sourceID).toURL().toString())); 1019 Node bar = doc.getDocumentElement().getFirstChild(); 1020 while (bar.getNodeType() != Node.ELEMENT_NODE) { 1021 bar = bar.getNextSibling(); 1022 } 1023 1024 System.err.println("Source document built OK"); 1025 1026 DOMSource ds = new DOMSource(bar); 1028 ds.setSystemId(new File (sourceID).toURL().toString()); 1029 1031 1034 Document out = docBuilder.newDocument(); 1035 Element extra = out.createElement("extra"); 1036 out.appendChild(extra); 1037 1038 1039 transformer.transform(ds, new DOMResult(extra)); 1040 System.err.println("Transformation done OK"); 1041 1042 Transformer serializer = tfactory.newTransformer(); 1044 1045 serializer.transform(new DOMSource(out), 1046 new StreamResult(System.out)); 1047 1048 return; 1049 } else { 1050 throw new org.xml.sax.SAXNotSupportedException ( 1051 "DOM node processing not supported!"); 1052 } 1053 } 1054 1055 1056 1060 public static void exampleSAXtoDOMNewIdentity(String sourceID, String xslID) 1061 throws TransformerException, TransformerConfigurationException, 1062 SAXException , IOException , ParserConfigurationException , 1063 MalformedURLException { 1064 1065 TransformerFactory tfactory = TransformerFactory.newInstance(); 1066 Transformer transformer = tfactory.newTransformer(); 1067 SAXSource source = new SAXSource(new InputSource (sourceID)); 1068 DOMResult result = new DOMResult(); 1069 transformer.transform(source, result); 1070 1071 System.err.println("Transformation done OK"); 1072 1073 Transformer serializer = tfactory.newTransformer(); 1075 1076 serializer.transform(new DOMSource(result.getNode()), 1077 new StreamResult(System.out)); 1078 1079 int k = result.getNode().getChildNodes().getLength(); 1080 System.err.println("Result root has " + k + " children"); 1081 1082 } 1083 1084 1088 public static void exampleNodeInfo(String sourceID, String xslID) 1089 throws TransformerException, TransformerConfigurationException, 1090 SAXException , IOException , ParserConfigurationException , 1091 MalformedURLException { 1092 1093 TransformerFactory tfactory = TransformerFactory.newInstance(); 1094 Templates templates = tfactory.newTemplates(new StreamSource(xslID)); 1095 Transformer transformer = templates.newTransformer(); 1096 com.icl.saxon.om.Builder builder = 1097 ((com.icl.saxon.Controller)transformer).makeBuilder(); 1098 com.icl.saxon.om.DocumentInfo doc = 1099 builder.build(new SAXSource(new InputSource (sourceID))); 1100 transformer.transform(doc, new StreamResult(System.out)); 1101 } 1102 1103 1104 1109 public static void exampleParam(String sourceID, String xslID) 1110 throws TransformerException, TransformerConfigurationException { 1111 1112 TransformerFactory tfactory = TransformerFactory.newInstance(); 1113 Templates templates = 1114 tfactory.newTemplates(new StreamSource(new File (xslID))); 1115 Transformer transformer1 = templates.newTransformer(); 1116 Transformer transformer2 = templates.newTransformer(); 1117 1118 transformer1.setParameter("a-param", "hello to you!"); 1119 transformer1.transform(new StreamSource(new File (sourceID)), 1120 new StreamResult(System.out)); 1121 System.out.println("\n========= (and again with a different parameter value) ==="); 1122 transformer1.setParameter("a-param", "goodbye to you!"); 1123 transformer1.transform(new StreamSource(new File (sourceID)), 1124 new StreamResult(System.out)); 1125 System.out.println("\n========= (and again with a no parameter value) ==="); 1126 transformer2.setOutputProperty(OutputKeys.INDENT, "yes"); 1127 transformer2.transform(new StreamSource(new File (sourceID)), 1128 new StreamResult(System.out)); 1129 } 1130 1131 1135 public static void exampleTransformerReuse(String sourceID, String xslID) 1136 throws TransformerException, TransformerConfigurationException { 1137 1138 TransformerFactory tfactory = TransformerFactory.newInstance(); 1140 1141 Transformer transformer = 1143 tfactory.newTransformer(new StreamSource(new File (xslID))); 1144 1145 transformer.setParameter("a-param", "hello to you!"); 1146 1147 transformer.transform(new StreamSource(new File (sourceID)), 1149 new StreamResult(System.out)); 1150 System.out.println("\n=========\n"); 1151 transformer.setParameter("a-param", "hello to me!"); 1152 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 1153 1154 transformer.transform(new StreamSource(new File (sourceID)), 1156 new StreamResult(System.out)); 1157 } 1158 1159 1162 public static void exampleOutputProperties(String sourceID, String xslID) 1163 throws TransformerException, TransformerConfigurationException { 1164 1165 TransformerFactory tfactory = TransformerFactory.newInstance(); 1166 Templates templates = 1167 tfactory.newTemplates(new StreamSource(new File (xslID))); 1168 Properties oprops = templates.getOutputProperties(); 1169 1170 oprops.put(OutputKeys.INDENT, "yes"); 1171 1172 Transformer transformer = templates.newTransformer(); 1173 1174 transformer.setOutputProperties(oprops); 1175 transformer.transform(new StreamSource(new File (sourceID)), 1176 new StreamResult(System.out)); 1177 } 1178 1179 1183 public static void exampleUseAssociated(String sourceID) 1184 throws TransformerException, TransformerConfigurationException { 1185 1186 TransformerFactory tfactory = TransformerFactory.newInstance(); 1187 1188 if (tfactory instanceof SAXTransformerFactory) { 1191 SAXTransformerFactory stf = 1192 ((SAXTransformerFactory) tfactory); 1193 Source sources = 1194 stf.getAssociatedStylesheet(new StreamSource(sourceID), null, 1195 null, null); 1196 1197 if (null != sources) { 1198 Transformer transformer = tfactory.newTransformer(sources); 1199 1200 transformer.transform(new StreamSource(sourceID), 1201 new StreamResult(System.out)); 1202 } else { 1203 System.out.println("Can't find the associated stylesheet!"); 1204 } 1205 } 1206 } 1207 1208 1211 public static void exampleContentHandlertoDOM( 1212 String sourceID, String xslID) 1213 throws TransformerException, 1214 TransformerConfigurationException, SAXException , 1215 IOException , ParserConfigurationException { 1216 1217 TransformerFactory tfactory = TransformerFactory.newInstance(); 1218 1219 if (tfactory.getFeature(SAXSource.FEATURE) 1222 && tfactory.getFeature(DOMSource.FEATURE)) { 1223 1224 SAXTransformerFactory sfactory = (SAXTransformerFactory) tfactory; 1226 1227 DocumentBuilderFactory dfactory = 1229 DocumentBuilderFactory.newInstance(); 1230 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 1231 org.w3c.dom.Document outNode = docBuilder.newDocument(); 1232 1233 TransformerHandler handler = 1236 sfactory.newTransformerHandler(new StreamSource(xslID)); 1237 1238 handler.setResult(new DOMResult(outNode)); 1239 1240 XMLReader reader = makeXMLReader(); 1243 1244 reader.setContentHandler(handler); 1245 reader.setProperty( 1246 "http://xml.org/sax/properties/lexical-handler", handler); 1247 1248 reader.parse(sourceID); 1251 1252 exampleSerializeNode(outNode); 1254 } else { 1255 System.out.println( 1256 "Can't do exampleContentHandlerToContentHandler because tfactory is not a SAXTransformerFactory"); 1257 } 1258 } 1259 1260 1263 1264 public static void exampleUsingURIResolver(String sourceID, String xslID) 1265 throws TransformerException, TransformerConfigurationException { 1266 1267 TransformerFactory tfactory = TransformerFactory.newInstance(); 1269 1270 Transformer transformer = 1272 tfactory.newTransformer(new StreamSource(xslID)); 1273 1274 transformer.setURIResolver(new UserURIResolver(transformer)); 1276 1277 transformer.transform(new StreamSource(sourceID), 1279 new StreamResult(System.out)); 1280 1281 } 1282 1283 1291 1292 public static class UserURIResolver implements URIResolver { 1293 1294 Transformer transformer; 1295 public UserURIResolver(Transformer t) { 1296 transformer = t; 1297 } 1298 1299 1307 1308 public Source resolve(String href, String base) 1309 throws TransformerException { 1310 if (href.endsWith(".txt")) { 1311 try { 1312 URL url = new URL (new URL (base), href); 1313 java.io.InputStream in = url.openConnection().getInputStream(); 1314 java.io.InputStreamReader reader = 1315 new java.io.InputStreamReader (in, "iso-8859-1"); 1316 1317 1319 StringBuffer sb = new StringBuffer (); 1320 while (true) { 1321 int c = reader.read(); 1322 if (c<0) break; 1323 sb.append((char)c); 1324 } 1325 com.icl.saxon.expr.TextFragmentValue tree = 1326 new com.icl.saxon.expr.TextFragmentValue( 1327 sb.toString(), url.toString(), 1328 (com.icl.saxon.Controller)transformer); 1329 return tree.getFirst(); 1330 } catch (Exception err) { 1331 throw new TransformerException(err); 1332 } 1333 } else { 1334 return null; 1335 } 1336 } 1337 1338 } 1340 1341 1344 public static void exampleSerializeNode(Node node) 1345 throws TransformerException, TransformerConfigurationException, 1346 SAXException , IOException , ParserConfigurationException { 1347 1348 TransformerFactory tfactory = TransformerFactory.newInstance(); 1349 1350 Transformer serializer = tfactory.newTransformer(); 1353 1354 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 1355 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 1356 serializer.transform(new DOMSource(node), 1357 new StreamResult(System.out)); 1358 } 1359 1360 1364 public static void exampleAsSerializer(String sourceID, String xslID) 1365 throws TransformerException, TransformerConfigurationException, 1366 SAXException , IOException , ParserConfigurationException { 1367 1368 DocumentBuilderFactory dfactory = 1369 DocumentBuilderFactory.newInstance(); 1370 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 1371 org.w3c.dom.Document outNode = docBuilder.newDocument(); 1372 Node doc = 1373 docBuilder.parse(new InputSource (sourceID)); 1374 TransformerFactory tfactory = TransformerFactory.newInstance(); 1375 1376 Transformer serializer = tfactory.newTransformer(); 1379 Properties oprops = new Properties (); 1380 1381 oprops.put("method", "html"); 1382 oprops.put("{http://icl.com/saxon}indent-spaces", "2"); 1383 serializer.setOutputProperties(oprops); 1384 serializer.transform(new DOMSource(doc), 1385 new StreamResult(System.out)); 1386 } 1387 1388 private static void handleException(Exception ex) { 1389 1390 System.out.println("EXCEPTION: " + ex); 1391 ex.printStackTrace(); 1392 1393 if (ex instanceof TransformerConfigurationException) { 1394 System.out.println(); 1395 System.out.println("Test failed"); 1396 1397 Throwable ex1 = 1398 ((TransformerConfigurationException) ex).getException(); 1399 1400 if (ex1!=null) { ex1.printStackTrace(); 1402 1403 if (ex1 instanceof SAXException ) { 1404 Exception ex2 = ((SAXException ) ex1).getException(); 1405 1406 System.out.println("Internal sub-exception: "); 1407 ex2.printStackTrace(); 1408 } 1409 } 1410 } 1411 } 1412 1413 1416 1417 private static XMLReader makeXMLReader() { 1418 return new com.icl.saxon.aelfred.SAXDriver(); 1419 } 1420} 1421 | Popular Tags |