| 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.Documen
|