1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 /* 19 * $Id$ 20 */ 21 22 /* 23 * 24 * ExamplesTest.java 25 * 26 */ 27 package org.apache.qetest.trax; 28 29 // Support for test reporting and harness classes 30 import java.io.BufferedInputStream; 31 import java.io.BufferedReader; 32 import java.io.File; 33 import java.io.FileInputStream; 34 import java.io.FileOutputStream; 35 import java.io.IOException; 36 import java.io.InputStream; 37 import java.io.InputStreamReader; 38 import java.io.Reader; 39 import java.util.Properties; 40 41 import javax.xml.parsers.DocumentBuilder; 42 import javax.xml.parsers.DocumentBuilderFactory; 43 import javax.xml.parsers.ParserConfigurationException; 44 import javax.xml.parsers.SAXParser; 45 import javax.xml.parsers.SAXParserFactory; 46 import javax.xml.transform.OutputKeys; 47 import javax.xml.transform.Result; 48 import javax.xml.transform.Source; 49 import javax.xml.transform.Templates; 50 import javax.xml.transform.Transformer; 51 import javax.xml.transform.TransformerConfigurationException; 52 import javax.xml.transform.TransformerException; 53 import javax.xml.transform.TransformerFactory; 54 import javax.xml.transform.dom.DOMResult; 55 import javax.xml.transform.dom.DOMSource; 56 import javax.xml.transform.sax.SAXResult; 57 import javax.xml.transform.sax.SAXSource; 58 import javax.xml.transform.sax.SAXTransformerFactory; 59 import javax.xml.transform.sax.TransformerHandler; 60 import javax.xml.transform.stream.StreamResult; 61 import javax.xml.transform.stream.StreamSource; 62 63 import org.apache.qetest.FileBasedTest; 64 import org.apache.qetest.OutputNameManager; 65 import org.apache.qetest.QetestUtils; 66 import org.apache.qetest.xsl.XSLTestfileInfo; 67 import org.apache.xml.serializer.OutputPropertiesFactory; 68 import org.apache.xml.serializer.Serializer; 69 import org.apache.xml.serializer.SerializerFactory; 70 import org.w3c.dom.Node; 71 import org.xml.sax.InputSource; 72 import org.xml.sax.SAXException; 73 import org.xml.sax.XMLFilter; 74 import org.xml.sax.XMLReader; 75 76 //------------------------------------------------------------------------- 77 78 /** 79 * Test version of xml-xalan/java/samples/trax/Examples.java. 80 * <p>This file is essentially copied from the Examples for TRAX, or 81 * javax.xml.transform; however this file actually validates most 82 * output and behavior for correctness. Hopefully, we can get this 83 * file updated at the same time as Examples.java in the future.</p> 84 * <p>In general, I merely copied each method from Examples.java 85 * and made minor updates (try...catch within methods, call to 86 * reporter.logBlah to output messages, etc.) then added validation 87 * of actual output files. Note that each method validates it's 88 * output by calling fileChecker.check(...) explicitly, so we can't 89 * change the input files without carefully changing the gold files 90 * for each area.</p> 91 * <p>Note that some tests may use NOT_DEFINED for their gold file 92 * if we haven't yet validated what the 'correct' output should be 93 * for each case - these should be updated as time permits.</p> 94 * @author shane_curcuru@lotus.com 95 * @author scott_boag@lotus.com 96 * @version $Id$ 97 */ 98 public class ExamplesTest extends FileBasedTest 99 { 100 101 /** 102 * Provides nextName(), currentName() functionality for tests 103 * that may produce any number of output files. 104 */ 105 protected OutputNameManager outNames; 106 107 /** 108 * Provides nextName(), currentName() functionality for tests 109 * that may produce any number of output files, for the gold files 110 * NOTE: Gold names must match the test cases in order, and 111 * must match the checked-in gold files ExamplesTest*.out! 112 */ 113 protected OutputNameManager goldNames; 114 115 /** Sample test stylesheet to use for transformations, includes gold file. */ 116 protected XSLTestfileInfo fooFile = new XSLTestfileInfo(); 117 118 /** Sample test stylesheet to use for transformations, includes gold file. */ 119 protected XSLTestfileInfo bazFile = new XSLTestfileInfo(); 120 121 /** Sample test stylesheet name to use for multi-transformations. */ 122 protected String foo2File; 123 124 /** Sample test stylesheet name to use for multi-transformations. */ 125 protected String foo3File; 126 127 /** Sample gold files used for specific transforms - with params. */ 128 // protected String param1GoldName; 129 130 /** Sample gold files used for specific transforms - with params and output format. */ 131 // protected String param2GoldName; 132 133 /** Sample gold files used for specific transforms - with output format. */ 134 // protected String outputGoldName; 135 136 /** Sample gold files used for specific transforms - ContentHandler. */ 137 // protected String sax2GoldName; 138 139 /** Sample gold files used for specific transforms - XMLFilter/Reader. */ 140 // protected String saxGoldName; 141 142 /** Subdirectory under test\tests\api for our xsl/xml files. */ 143 public static final String TRAX_SUBDIR = "trax"; 144 145 /** Just initialize test name, comment, numTestCases. */ ExamplesTest()146 public ExamplesTest() 147 { 148 numTestCases = 1; // REPLACE_num 149 testName = "ExamplesTest"; 150 testComment = "Test various combinations of Source and Result types"; 151 } 152 153 154 /** 155 * Initialize this test - Set names of xml/xsl test files, 156 * REPLACE_other_test_file_init. 157 * 158 * @param p Properties to initialize from (if needed) 159 * @return false if we should abort the test; true otherwise 160 */ doTestFileInit(Properties p)161 public boolean doTestFileInit(Properties p) 162 { 163 // NOTE: 'reporter' variable is already initialized at this point 164 165 // Used for all tests; just dump files in trax subdir 166 File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR); 167 if (!outSubDir.mkdirs()) 168 reporter.logWarningMsg("Could not create output dir: " + outSubDir); 169 // Initialize an output name manager to that dir with .out extension 170 outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR 171 + File.separator + testName, ".out"); 172 173 String testBasePath = inputDir 174 + File.separator 175 + TRAX_SUBDIR 176 + File.separator; 177 String goldBasePath = goldDir 178 + File.separator 179 + TRAX_SUBDIR 180 + File.separator; 181 182 goldNames = new OutputNameManager(goldBasePath 183 + File.separator + testName, ".out"); 184 185 reporter.logTraceMsg("NOTE! This file is very sensitive to pathing issues!"); 186 fooFile.inputName = swapSlash(testBasePath + "xsl/foo.xsl"); 187 fooFile.xmlName = swapSlash(testBasePath + "xml/foo.xml"); 188 189 bazFile.xmlName = swapSlash(testBasePath + "xml/baz.xml"); 190 191 foo2File = swapSlash(testBasePath + "xsl/foo2.xsl"); 192 193 foo3File = swapSlash(testBasePath + "xsl/foo3.xsl"); 194 195 // param1GoldName = goldBasePath + "param1.out"; 196 // param2GoldName = goldBasePath + "param2.out"; 197 // outputGoldName = goldBasePath + "output.out"; 198 // saxGoldName = goldBasePath + "fooSAX.out"; 199 // sax2GoldName = goldBasePath + "fooSAX2.out"; 200 return true; 201 } 202 203 204 /** 205 * Worker method to swap / for \ in Strings. 206 */ swapSlash(String s)207 public String swapSlash(String s) 208 { 209 return (new String(s)).replace('\\', '/'); 210 } 211 212 213 /** 214 * Call each of the methods found in Examples. 215 * 216 * @return false if we should abort the test; true otherwise 217 */ testCase1()218 public boolean testCase1() 219 { 220 reporter.testCaseInit("Call each of the methods found in Examples"); 221 222 // Note: the tests must be used with the same input files, 223 // since they hard-code the gold files within the methods 224 String tmpFooNames = fooFile.xmlName + ", " + fooFile.inputName; 225 reporter.logStatusMsg("exampleSimple1(" + tmpFooNames + ")"); 226 exampleSimple1(fooFile.xmlName, fooFile.inputName); 227 228 reporter.logStatusMsg("exampleSimple2(" + tmpFooNames + ")"); 229 exampleSimple2(fooFile.xmlName, fooFile.inputName); 230 231 reporter.logStatusMsg("exampleFromStream(" + tmpFooNames + ")"); 232 exampleFromStream(fooFile.xmlName, fooFile.inputName); 233 234 reporter.logStatusMsg("exampleFromReader(" + tmpFooNames + ")"); 235 exampleFromReader(fooFile.xmlName, fooFile.inputName); 236 237 reporter.logStatusMsg("exampleUseTemplatesObj(" + fooFile.xmlName + ", " + bazFile.xmlName + ", " + fooFile.inputName + ")"); 238 exampleUseTemplatesObj(fooFile.xmlName, bazFile.xmlName, fooFile.inputName); 239 240 reporter.logStatusMsg("exampleContentHandlerToContentHandler(" + tmpFooNames + ")"); 241 reporter.logErrorMsg("exampleContentHandlerToContentHandler(" + tmpFooNames + ") NOTE: See SmoketestOuttakes instead!"); 242 String unused = goldNames.nextName(); // must increment out, gold names to simulate actual test 243 unused = outNames.nextName(); // must increment out, gold names to simulate actual test 244 // exampleContentHandlerToContentHandler(fooFile.xmlName, fooFile.inputName); 245 246 reporter.logStatusMsg("exampleXMLReader(" + tmpFooNames + ")"); 247 exampleXMLReader(fooFile.xmlName, fooFile.inputName); 248 249 reporter.logStatusMsg("exampleXMLFilter(" + tmpFooNames + ")"); 250 exampleXMLFilter(fooFile.xmlName, fooFile.inputName); 251 252 reporter.logStatusMsg("exampleXMLFilterChain(" + tmpFooNames 253 + ", " + foo2File + ", " + foo3File + ")"); 254 exampleXMLFilterChain(fooFile.xmlName, fooFile.inputName, foo2File, foo3File); 255 256 reporter.logStatusMsg("exampleDOM2DOM(" + tmpFooNames + ")"); 257 exampleDOM2DOM(fooFile.xmlName, fooFile.inputName); 258 259 reporter.logStatusMsg("exampleParam(" + tmpFooNames + ")"); 260 exampleParam(fooFile.xmlName, fooFile.inputName); 261 262 reporter.logStatusMsg("exampleTransformerReuse(" + tmpFooNames + ")"); 263 exampleTransformerReuse(fooFile.xmlName, fooFile.inputName); 264 265 reporter.logStatusMsg("exampleOutputProperties(" + tmpFooNames + ")"); 266 exampleOutputProperties(fooFile.xmlName, fooFile.inputName); 267 268 reporter.logStatusMsg("exampleUseAssociated(" + fooFile.xmlName +")"); 269 exampleUseAssociated(fooFile.xmlName); 270 271 reporter.logStatusMsg("exampleContentHandler2DOM(" + tmpFooNames + ")"); 272 reporter.logErrorMsg("exampleContentHandler2DOM(" + tmpFooNames + ") NOTE: See SmoketestOuttakes instead!"); 273 unused = goldNames.nextName(); // must increment out, gold names to simulate actual test 274 unused = outNames.nextName(); // must increment out, gold names to simulate actual test 275 //exampleContentHandler2DOM(fooFile.xmlName, fooFile.inputName); 276 277 reporter.logStatusMsg("exampleAsSerializer(" + tmpFooNames + ")"); 278 exampleAsSerializer(fooFile.xmlName, fooFile.inputName); 279 280 reporter.testCaseClose(); 281 return true; 282 } 283 284 /** 285 * Show the simplest possible transformation from system id 286 * to output stream. 287 */ exampleSimple1(String sourceID, String xslID)288 public void exampleSimple1(String sourceID, String xslID) 289 { 290 try 291 { 292 // Create a transform factory instance. 293 TransformerFactory tfactory = TransformerFactory.newInstance(); 294 295 // Create a transformer for the stylesheet. 296 reporter.logTraceMsg("newTransformer(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 297 Transformer transformer 298 = tfactory.newTransformer(new StreamSource(QetestUtils.filenameToURL(xslID))); 299 // No need to setSystemId, the transformer can get it from the URL 300 301 // Transform the source XML to System.out. 302 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 303 transformer.transform( new StreamSource(QetestUtils.filenameToURL(sourceID)), 304 new StreamResult(outNames.nextName())); 305 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 306 String goldname = goldNames.nextName(); 307 System.out.println("fooFile.goldName: "+goldname); 308 fileChecker.check(reporter, new File(outNames.currentName()), 309 new File(goldname), 310 "exampleSimple1 fileChecker of:" + outNames.currentName()); 311 } 312 catch (Throwable t) 313 { 314 reporter.checkFail("exampleSimple1 threw: " + t.toString()); 315 reporter.logThrowable(reporter.ERRORMSG, t, "exampleSimple1 threw"); 316 } 317 } 318 319 /** 320 * Show the simplest possible transformation from File 321 * to a File. 322 */ exampleSimple2(String sourceID, String xslID)323 public void exampleSimple2(String sourceID, String xslID) 324 { 325 try 326 { 327 // Create a transform factory instance. 328 TransformerFactory tfactory = TransformerFactory.newInstance(); 329 330 // Create a transformer for the stylesheet. 331 reporter.logTraceMsg("newTransformer(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 332 Transformer transformer 333 = tfactory.newTransformer(new StreamSource(new File(xslID))); 334 // No need to setSystemId, the transformer can get it from the File 335 336 // Transform the source XML to System.out. 337 reporter.logTraceMsg("new StreamSource(new File(" + sourceID); 338 transformer.transform( new StreamSource(new File(sourceID)), 339 new StreamResult(new File(outNames.nextName()))); 340 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 341 fileChecker.check(reporter, new File(outNames.currentName()), 342 new File(goldNames.nextName()), 343 "exampleSimple2 fileChecker of:" + outNames.currentName()); 344 } 345 catch (Throwable t) 346 { 347 reporter.checkFail("exampleSimple2 threw: " + t.toString()); 348 reporter.logThrowable(reporter.ERRORMSG, t, "exampleSimple2 threw"); 349 } 350 } 351 352 353 /** 354 * Show simple transformation from input stream to output stream. 355 */ exampleFromStream(String sourceID, String xslID)356 public void exampleFromStream(String sourceID, String xslID) 357 { 358 try 359 { 360 // Create a transform factory instance. 361 TransformerFactory tfactory = TransformerFactory.newInstance(); 362 363 reporter.logTraceMsg("new BufferedInputStream(new FileInputStream(" + xslID); 364 InputStream xslIS = new BufferedInputStream(new FileInputStream(xslID)); 365 StreamSource xslSource = new StreamSource(xslIS); 366 // Note that if we don't do this, relative URLs can not be resolved correctly! 367 xslSource.setSystemId(QetestUtils.filenameToURL(xslID)); 368 369 // Create a transformer for the stylesheet. 370 Transformer transformer = tfactory.newTransformer(xslSource); 371 372 reporter.logTraceMsg("new BufferedInputStream(new FileInputStream(" + sourceID); 373 InputStream xmlIS = new BufferedInputStream(new FileInputStream(sourceID)); 374 StreamSource xmlSource = new StreamSource(xmlIS); 375 // Note that if we don't do this, relative URLs can not be resolved correctly! 376 xmlSource.setSystemId(QetestUtils.filenameToURL(sourceID)); 377 378 // Transform the source XML to System.out. 379 transformer.transform( xmlSource, new StreamResult(outNames.nextName())); 380 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 381 fileChecker.check(reporter, new File(outNames.currentName()), 382 new File(goldNames.nextName()), 383 "exampleFromStream fileChecker of:" + outNames.currentName()); 384 } 385 catch (Throwable t) 386 { 387 reporter.checkFail("exampleFromStream threw: " + t.toString()); 388 reporter.logThrowable(reporter.ERRORMSG, t, "exampleFromStream threw"); 389 } 390 } 391 392 /** 393 * Show simple transformation from reader to output stream. In general 394 * this use case is discouraged, since the XML encoding can not be 395 * processed. 396 */ exampleFromReader(String sourceID, String xslID)397 public void exampleFromReader(String sourceID, String xslID) 398 { 399 try 400 { 401 // Create a transform factory instance. 402 TransformerFactory tfactory = TransformerFactory.newInstance(); 403 404 // Note that in this case the XML encoding can not be processed! 405 reporter.logTraceMsg("new BufferedReader(new InputStreamReader(new FileInputStream(" + xslID); 406 Reader xslReader = new BufferedReader(new InputStreamReader(new FileInputStream(xslID), "UTF-8")); //@DEM 407 // Reader xslReader = new BufferedReader(new FileReader(xslID)); @DEM 408 StreamSource xslSource = new StreamSource(xslReader); 409 // Note that if we don't do this, relative URLs can not be resolved correctly! 410 xslSource.setSystemId(QetestUtils.filenameToURL(xslID)); 411 412 // Create a transformer for the stylesheet. 413 Transformer transformer = tfactory.newTransformer(xslSource); 414 415 // Note that in this case the XML encoding can not be processed! 416 reporter.logTraceMsg("new BufferedReader(new FileReader(" + sourceID); 417 Reader xmlReader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceID), "UTF-8")); //@DEM 418 // Reader xmlReader = new BufferedReader(new FileReader(sourceID)); @DEM 419 StreamSource xmlSource = new StreamSource(xmlReader); 420 // Note that if we don't do this, relative URLs can not be resolved correctly! 421 xmlSource.setSystemId(QetestUtils.filenameToURL(sourceID)); 422 423 // Transform the source XML to System.out. 424 transformer.transform( xmlSource, new StreamResult(outNames.nextName())); 425 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 426 fileChecker.check(reporter, new File(outNames.currentName()), 427 new File(goldNames.nextName()), 428 "exampleFromReader fileChecker of:" + outNames.currentName()); 429 } 430 catch (Throwable t) 431 { 432 reporter.checkFail("exampleFromReader threw: " + t.toString()); 433 reporter.logThrowable(reporter.ERRORMSG, t, "exampleFromReader threw"); 434 } 435 } 436 437 438 439 /** 440 * Show the simplest possible transformation from system id to output stream. 441 */ exampleUseTemplatesObj(String sourceID1, String sourceID2, String xslID)442 public void exampleUseTemplatesObj(String sourceID1, 443 String sourceID2, 444 String xslID) 445 { 446 try 447 { 448 TransformerFactory tfactory = TransformerFactory.newInstance(); 449 450 // Create a templates object, which is the processed, 451 // thread-safe representation of the stylesheet. 452 reporter.logTraceMsg("newTemplates(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 453 Templates templates = tfactory.newTemplates(new StreamSource(QetestUtils.filenameToURL(xslID))); 454 455 // Illustrate the fact that you can make multiple transformers 456 // from the same template. 457 Transformer transformer1 = templates.newTransformer(); 458 Transformer transformer2 = templates.newTransformer(); 459 460 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID1)); 461 transformer1.transform(new StreamSource(QetestUtils.filenameToURL(sourceID1)), 462 new StreamResult(outNames.nextName())); 463 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 464 fileChecker.check(reporter, new File(outNames.currentName()), 465 new File(goldNames.nextName()), 466 "exampleUseTemplatesObj(1) fileChecker of:" + outNames.currentName()); 467 468 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID2)); 469 transformer2.transform(new StreamSource(QetestUtils.filenameToURL(sourceID2)), 470 new StreamResult(outNames.nextName())); 471 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 472 fileChecker.check(reporter, new File(outNames.currentName()), 473 new File(goldNames.nextName()), 474 "exampleUseTemplatesObj(2) fileChecker of:" + outNames.currentName()); 475 } 476 catch (Throwable t) 477 { 478 reporter.checkFail("exampleUseTemplatesObj threw: " + t.toString()); 479 reporter.logThrowable(reporter.ERRORMSG, t, "exampleUseTemplatesObj threw"); 480 } 481 } 482 483 484 /** 485 * Show the Transformer using SAX events in and SAX events out. 486 */ exampleContentHandlerToContentHandler(String sourceID, String xslID)487 public void exampleContentHandlerToContentHandler(String sourceID, 488 String xslID) 489 { 490 try 491 { 492 TransformerFactory tfactory = TransformerFactory.newInstance(); 493 494 // Does this factory support SAX features? 495 if (!tfactory.getFeature(SAXSource.FEATURE)) 496 { 497 reporter.logErrorMsg("exampleContentHandlerToContentHandler:Processor does not support SAX"); 498 return; 499 } 500 // If so, we can safely cast. 501 SAXTransformerFactory stfactory = ((SAXTransformerFactory) tfactory); 502 503 // A TransformerHandler is a ContentHandler that will listen for 504 // SAX events, and transform them to the result. 505 reporter.logTraceMsg("newTransformerHandler(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 506 TransformerHandler handler 507 = stfactory.newTransformerHandler(new StreamSource(QetestUtils.filenameToURL(xslID))); 508 509 // Set the result handling to be a serialization to the file output stream. 510 Serializer serializer = SerializerFactory.getSerializer 511 (OutputPropertiesFactory.getDefaultMethodProperties("xml")); 512 FileOutputStream fos = new FileOutputStream(outNames.nextName()); 513 serializer.setOutputStream(fos); 514 reporter.logStatusMsg("Test-output-to: new FileOutputStream(" + outNames.currentName()); 515 516 Result result = new SAXResult(serializer.asContentHandler()); 517 518 handler.setResult(result); 519 520 // Create a reader, and set it's content handler to be the TransformerHandler. 521 XMLReader reader=null; 522 523 // Use JAXP1.1 ( if possible ) 524 try { 525 javax.xml.parsers.SAXParserFactory factory= 526 javax.xml.parsers.SAXParserFactory.newInstance(); 527 factory.setNamespaceAware( true ); 528 javax.xml.parsers.SAXParser jaxpParser= 529 factory.newSAXParser(); 530 reader=jaxpParser.getXMLReader(); 531 532 } catch( javax.xml.parsers.ParserConfigurationException ex ) { 533 throw new org.xml.sax.SAXException( ex ); 534 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { 535 throw new org.xml.sax.SAXException( ex1.toString() ); 536 } catch( NoSuchMethodError ex2 ) { 537 } 538 if( reader==null ) reader = getJAXPXMLReader(); 539 reader.setContentHandler(handler); 540 541 // It's a good idea for the parser to send lexical events. 542 // The TransformerHandler is also a LexicalHandler. 543 reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 544 545 // Parse the source XML, and send the parse events to the TransformerHandler. 546 reporter.logTraceMsg("reader.parse(" + QetestUtils.filenameToURL(sourceID)); 547 reader.parse(QetestUtils.filenameToURL(sourceID)); 548 fos.close(); 549 550 reporter.logTraceMsg("Note: See SPR SCUU4RZT78 for discussion as to why this output is different than XMLReader/XMLFilter"); 551 fileChecker.check(reporter, new File(outNames.currentName()), 552 new File(goldNames.nextName()), 553 "exampleContentHandlerToContentHandler fileChecker of:" + outNames.currentName()); 554 } 555 catch (Throwable t) 556 { 557 reporter.checkFail("exampleContentHandlerToContentHandler threw: " + t.toString()); 558 reporter.logThrowable(reporter.ERRORMSG, t, "exampleContentHandlerToContentHandler threw"); 559 } 560 561 } 562 563 /** 564 * Show the Transformer as a SAX2 XMLReader. An XMLFilter obtained 565 * from newXMLFilter should act as a transforming XMLReader if setParent is not 566 * called. Internally, an XMLReader is created as the parent for the XMLFilter. 567 */ exampleXMLReader(String sourceID, String xslID)568 public void exampleXMLReader(String sourceID, String xslID) 569 { 570 try 571 { 572 TransformerFactory tfactory = TransformerFactory.newInstance(); 573 if(!tfactory.getFeature(SAXSource.FEATURE)) 574 { 575 reporter.logErrorMsg("exampleXMLReader:Processor does not support SAX"); 576 return; 577 } 578 reporter.logTraceMsg("newXMLFilter(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 579 XMLReader reader 580 = ((SAXTransformerFactory) tfactory).newXMLFilter(new StreamSource(QetestUtils.filenameToURL(xslID))); 581 582 // Set the result handling to be a serialization to the file output stream. 583 Serializer serializer = SerializerFactory.getSerializer 584 (OutputPropertiesFactory.getDefaultMethodProperties("xml")); 585 FileOutputStream fos = new FileOutputStream(outNames.nextName()); 586 serializer.setOutputStream(fos); 587 reporter.logStatusMsg("Test-output-to: new FileOutputStream(" + outNames.currentName()); 588 589 reader.setContentHandler(serializer.asContentHandler()); 590 591 reporter.logTraceMsg("reader.parse(new InputSource(" + QetestUtils.filenameToURL(sourceID)); 592 reader.parse(new InputSource(QetestUtils.filenameToURL(sourceID))); 593 fos.close(); 594 595 fileChecker.check(reporter, new File(outNames.currentName()), 596 new File(goldNames.nextName()), 597 "exampleXMLReader fileChecker of:" + outNames.currentName()); 598 } 599 catch (Throwable t) 600 { 601 reporter.checkFail("exampleXMLReader threw: " + t.toString()); 602 reporter.logThrowable(reporter.ERRORMSG, t, "exampleXMLReader threw"); 603 } 604 605 } 606 607 /** 608 * Show the Transformer as a simple XMLFilter. This is pretty similar 609 * to exampleXMLReader, except that here the parent XMLReader is created 610 * by the caller, instead of automatically within the XMLFilter. This 611 * gives the caller more direct control over the parent reader. 612 */ exampleXMLFilter(String sourceID, String xslID)613 public void exampleXMLFilter(String sourceID, String xslID) 614 { 615 try 616 { 617 TransformerFactory tfactory = TransformerFactory.newInstance(); 618 619 XMLReader reader=null; 620 621 // Use JAXP1.1 ( if possible ) 622 try { 623 javax.xml.parsers.SAXParserFactory factory= 624 javax.xml.parsers.SAXParserFactory.newInstance(); 625 factory.setNamespaceAware( true ); 626 javax.xml.parsers.SAXParser jaxpParser= 627 factory.newSAXParser(); 628 reader=jaxpParser.getXMLReader(); 629 630 } catch( javax.xml.parsers.ParserConfigurationException ex ) { 631 throw new org.xml.sax.SAXException( ex ); 632 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { 633 throw new org.xml.sax.SAXException( ex1.toString() ); 634 } catch( NoSuchMethodError ex2 ) { 635 } 636 if( reader==null ) reader = getJAXPXMLReader(); 637 638 // Set the result handling to be a serialization to the file output stream. 639 Serializer serializer = SerializerFactory.getSerializer 640 (OutputPropertiesFactory.getDefaultMethodProperties("xml")); 641 FileOutputStream fos = new FileOutputStream(outNames.nextName()); 642 serializer.setOutputStream(fos); 643 reporter.logStatusMsg("Test-output-to: new FileOutputStream(" + outNames.currentName()); 644 reader.setContentHandler(serializer.asContentHandler()); 645 646 try 647 { 648 reader.setFeature("http://xml.org/sax/features/namespace-prefixes", 649 true); 650 reader.setFeature("http://apache.org/xml/features/validation/dynamic", 651 true); 652 } 653 catch (SAXException se) 654 { 655 reporter.logErrorMsg("exampleXMLFilter: reader threw :" + se.toString()); 656 // What can we do? 657 // TODO: User diagnostics. 658 } 659 660 reporter.logTraceMsg("newXMLFilter(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 661 XMLFilter filter 662 = ((SAXTransformerFactory) tfactory).newXMLFilter(new StreamSource(QetestUtils.filenameToURL(xslID))); 663 664 filter.setParent(reader); 665 666 // Now, when you call transformer.parse, it will set itself as 667 // the content handler for the parser object (it's "parent"), and 668 // will then call the parse method on the parser. 669 reporter.logTraceMsg("filter.parse(new InputSource(" + QetestUtils.filenameToURL(sourceID)); 670 filter.parse(new InputSource(QetestUtils.filenameToURL(sourceID))); 671 672 fos.close(); 673 674 fileChecker.check(reporter, new File(outNames.currentName()), 675 new File(goldNames.nextName()), 676 "exampleXMLFilter fileChecker of:" + outNames.currentName()); 677 } 678 catch (Throwable t) 679 { 680 reporter.checkFail("exampleXMLFilter threw: " + t.toString()); 681 reporter.logThrowable(reporter.ERRORMSG, t, "exampleXMLFilter threw"); 682 } 683 } 684 685 /** 686 * This example shows how to chain events from one Transformer 687 * to another transformer, using the Transformer as a 688 * SAX2 XMLFilter/XMLReader. 689 */ exampleXMLFilterChain(String sourceID, String xslID_1, String xslID_2, String xslID_3)690 public void exampleXMLFilterChain(String sourceID, String xslID_1, 691 String xslID_2, String xslID_3) 692 { 693 try 694 { 695 TransformerFactory tfactory = TransformerFactory.newInstance(); 696 697 Templates stylesheet1 = tfactory.newTemplates(new StreamSource(QetestUtils.filenameToURL(xslID_1))); 698 Transformer transformer1 = stylesheet1.newTransformer(); 699 700 // If one success, assume all will succeed. 701 if (!tfactory.getFeature(SAXSource.FEATURE)) 702 { 703 reporter.logErrorMsg("exampleXMLFilterChain:Processor does not support SAX"); 704 return; 705 } 706 SAXTransformerFactory stf = (SAXTransformerFactory)tfactory; 707 XMLReader reader=null; 708 709 // Use JAXP1.1 ( if possible ) 710 try { 711 javax.xml.parsers.SAXParserFactory factory= 712 javax.xml.parsers.SAXParserFactory.newInstance(); 713 factory.setNamespaceAware( true ); 714 javax.xml.parsers.SAXParser jaxpParser= 715 factory.newSAXParser(); 716 reader=jaxpParser.getXMLReader(); 717 718 } catch( javax.xml.parsers.ParserConfigurationException ex ) { 719 throw new org.xml.sax.SAXException( ex ); 720 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { 721 throw new org.xml.sax.SAXException( ex1.toString() ); 722 } catch( NoSuchMethodError ex2 ) { 723 } 724 if( reader==null ) reader = getJAXPXMLReader(); 725 726 reporter.logTraceMsg("newXMLFilter(new StreamSource(" + QetestUtils.filenameToURL(xslID_1)); 727 XMLFilter filter1 = stf.newXMLFilter(new StreamSource(QetestUtils.filenameToURL(xslID_1))); 728 729 reporter.logTraceMsg("newXMLFilter(new StreamSource(" + QetestUtils.filenameToURL(xslID_2)); 730 XMLFilter filter2 = stf.newXMLFilter(new StreamSource(QetestUtils.filenameToURL(xslID_2))); 731 732 reporter.logTraceMsg("newXMLFilter(new StreamSource(" + QetestUtils.filenameToURL(xslID_3)); 733 XMLFilter filter3 = stf.newXMLFilter(new StreamSource(QetestUtils.filenameToURL(xslID_3))); 734 735 if (null == filter1) // If one success, assume all were success. 736 { 737 reporter.checkFail("exampleXMLFilterChain: filter is null"); 738 return; 739 } 740 741 // transformer1 will use a SAX parser as it's reader. 742 filter1.setParent(reader); 743 744 // transformer2 will use transformer1 as it's reader. 745 filter2.setParent(filter1); 746 747 // transform3 will use transform2 as it's reader. 748 filter3.setParent(filter2); 749 750 // Set the result handling to be a serialization to the file output stream. 751 Serializer serializer = SerializerFactory.getSerializer 752 (OutputPropertiesFactory.getDefaultMethodProperties("xml")); 753 FileOutputStream fos = new FileOutputStream(outNames.nextName()); 754 serializer.setOutputStream(fos); 755 reporter.logStatusMsg("Test-output-to: new FileOutputStream(" + outNames.currentName()); 756 filter3.setContentHandler(serializer.asContentHandler()); 757 758 // Now, when you call transformer3 to parse, it will set 759 // itself as the ContentHandler for transform2, and 760 // call transform2.parse, which will set itself as the 761 // content handler for transform1, and call transform1.parse, 762 // which will set itself as the content listener for the 763 // SAX parser, and call parser.parse(new InputSource(fooFile.xmlName)). 764 reporter.logTraceMsg("filter3.parse(new InputSource(" + QetestUtils.filenameToURL(sourceID)); 765 filter3.parse(new InputSource(QetestUtils.filenameToURL(sourceID))); 766 fos.close(); 767 fileChecker.check(reporter, new File(outNames.currentName()), 768 new File(goldNames.nextName()), 769 "exampleXMLFilterChain fileChecker of:" + outNames.currentName()); 770 } 771 catch (Throwable t) 772 { 773 reporter.checkFail("exampleXMLFilterChain threw: " + t.toString()); 774 reporter.logThrowable(reporter.ERRORMSG, t, "exampleXMLFilterChain threw"); 775 } 776 } 777 778 /** 779 * Show how to transform a DOM tree into another DOM tree. 780 * This uses the javax.xml.parsers to parse an XML file into a 781 * DOM, and create an output DOM. 782 */ exampleDOM2DOM(String sourceID, String xslID)783 public Node exampleDOM2DOM(String sourceID, String xslID) 784 { 785 try 786 { 787 TransformerFactory tfactory = TransformerFactory.newInstance(); 788 789 if (!tfactory.getFeature(DOMSource.FEATURE)) 790 { 791 reporter.logErrorMsg("exampleDOM2DOM:Processor does not support SAX"); 792 return null; 793 } 794 795 Templates templates; 796 797 { 798 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 799 dfactory.setNamespaceAware(true); 800 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 801 org.w3c.dom.Document outNode = docBuilder.newDocument(); 802 reporter.logTraceMsg("docBuilder.parse(new InputSource(" + QetestUtils.filenameToURL(xslID)); 803 Node doc = docBuilder.parse(new InputSource(QetestUtils.filenameToURL(xslID))); 804 805 DOMSource dsource = new DOMSource(doc); 806 // If we don't do this, the transformer won't know how to 807 // resolve relative URLs in the stylesheet. 808 dsource.setSystemId(QetestUtils.filenameToURL(xslID)); 809 810 templates = tfactory.newTemplates(dsource); 811 } 812 813 Transformer transformer = templates.newTransformer(); 814 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 815 dfactory.setNamespaceAware(true); // must have namespaces for xsl files! 816 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 817 org.w3c.dom.Document outNode = docBuilder.newDocument(); 818 reporter.logTraceMsg("docBuilder.parse(new InputSource(" + QetestUtils.filenameToURL(sourceID)); 819 Node doc = docBuilder.parse(new InputSource(QetestUtils.filenameToURL(sourceID))); 820 821 transformer.transform(new DOMSource(doc), new DOMResult(outNode)); 822 823 Transformer serializer = tfactory.newTransformer(); 824 serializer.transform(new DOMSource(outNode), new StreamResult(outNames.nextName())); 825 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 826 827 fileChecker.check(reporter, new File(outNames.currentName()), 828 new File(goldNames.nextName()), 829 "exampleDOM2DOM fileChecker of:" + outNames.currentName()); 830 return outNode; 831 } 832 catch (Throwable t) 833 { 834 reporter.checkFail("exampleDOM2DOM threw: " + t.toString()); 835 reporter.logThrowable(reporter.ERRORMSG, t, "exampleDOM2DOM threw"); 836 return null; 837 838 } 839 } 840 841 /** 842 * This shows how to set a parameter for use by the templates. Use 843 * two transformers to show that different parameters may be set 844 * on different transformers. 845 */ exampleParam(String sourceID, String xslID)846 public void exampleParam(String sourceID, String xslID) 847 { 848 try 849 { 850 TransformerFactory tfactory = TransformerFactory.newInstance(); 851 reporter.logTraceMsg("newTemplates(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 852 Templates templates = tfactory.newTemplates(new StreamSource(QetestUtils.filenameToURL(xslID))); 853 Transformer transformer1 = templates.newTransformer(); 854 Transformer transformer2 = templates.newTransformer(); 855 856 transformer1.setParameter("a-param", 857 "hello to you!"); 858 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 859 transformer1.transform(new StreamSource(QetestUtils.filenameToURL(sourceID)), 860 new StreamResult(outNames.nextName())); 861 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 862 fileChecker.check(reporter, new File(outNames.currentName()), 863 new File(goldNames.nextName()), 864 "exampleParam(1) fileChecker of:" + outNames.currentName()); 865 866 867 transformer2.setOutputProperty(OutputKeys.INDENT, "yes"); 868 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 869 transformer2.transform(new StreamSource(QetestUtils.filenameToURL(sourceID)), 870 new StreamResult(outNames.nextName())); 871 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 872 fileChecker.check(reporter, new File(outNames.currentName()), 873 new File(goldNames.nextName()), 874 "exampleParam(2) fileChecker of:" + outNames.currentName()); 875 } 876 catch (Throwable t) 877 { 878 reporter.checkFail("exampleParam threw: " + t.toString()); 879 reporter.logThrowable(reporter.ERRORMSG, t, "exampleParam threw"); 880 } 881 } 882 883 /** 884 * Show the that a transformer can be reused, and show resetting 885 * a parameter on the transformer. 886 */ exampleTransformerReuse(String sourceID, String xslID)887 public void exampleTransformerReuse(String sourceID, String xslID) 888 { 889 try 890 { 891 // Create a transform factory instance. 892 TransformerFactory tfactory = TransformerFactory.newInstance(); 893 894 // Create a transformer for the stylesheet. 895 reporter.logTraceMsg("newTemplates(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 896 Transformer transformer 897 = tfactory.newTransformer(new StreamSource(QetestUtils.filenameToURL(xslID))); 898 899 transformer.setParameter("a-param", 900 "hello to you!"); 901 902 // Transform the source XML to System.out. 903 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 904 transformer.transform( new StreamSource(QetestUtils.filenameToURL(sourceID)), 905 new StreamResult(outNames.nextName())); 906 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 907 fileChecker.check(reporter, new File(outNames.currentName()), 908 new File(goldNames.nextName()), 909 "exampleTransformerReuse(1) fileChecker of:" + outNames.currentName()); 910 911 transformer.setParameter("a-param", 912 "hello to me!"); 913 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 914 915 // Transform the source XML to System.out. 916 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 917 transformer.transform( new StreamSource(QetestUtils.filenameToURL(sourceID)), 918 new StreamResult(outNames.nextName())); 919 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 920 fileChecker.check(reporter, new File(outNames.currentName()), 921 new File(goldNames.nextName()), 922 "exampleTransformerReuse(2) fileChecker of:" + outNames.currentName()); 923 } 924 catch (Throwable t) 925 { 926 reporter.checkFail("exampleTransformerReuse threw: " + t.toString()); 927 reporter.logThrowable(reporter.ERRORMSG, t, "exampleTransformerReuse threw"); 928 } 929 } 930 931 /** 932 * Show how to override output properties. 933 */ exampleOutputProperties(String sourceID, String xslID)934 public void exampleOutputProperties(String sourceID, String xslID) 935 { 936 try 937 { 938 TransformerFactory tfactory = TransformerFactory.newInstance(); 939 reporter.logTraceMsg("newTemplates(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 940 Templates templates = tfactory.newTemplates(new StreamSource(QetestUtils.filenameToURL(xslID))); 941 Properties oprops = templates.getOutputProperties(); 942 943 oprops.put(OutputKeys.INDENT, "yes"); 944 945 Transformer transformer = templates.newTransformer(); 946 947 transformer.setOutputProperties(oprops); 948 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 949 transformer.transform(new StreamSource(QetestUtils.filenameToURL(sourceID)), 950 new StreamResult(outNames.nextName())); 951 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 952 fileChecker.check(reporter, new File(outNames.currentName()), 953 new File(goldNames.nextName()), 954 "exampleOutputProperties fileChecker of:" + outNames.currentName()); 955 } 956 catch (Throwable t) 957 { 958 reporter.checkFail("exampleOutputProperties threw: " + t.toString()); 959 reporter.logThrowable(reporter.ERRORMSG, t, "exampleOutputProperties threw"); 960 } 961 } 962 963 /** 964 * Show how to get stylesheets that are associated with a given 965 * xml document via the xml-stylesheet PI (see http://www.w3.org/TR/xml-stylesheet/). 966 */ exampleUseAssociated(String sourceID)967 public void exampleUseAssociated(String sourceID) 968 { 969 try 970 { 971 TransformerFactory tfactory = TransformerFactory.newInstance(); 972 973 // The DOM tfactory will have it's own way, based on DOM2, 974 // of getting associated stylesheets. 975 if (!(tfactory instanceof SAXTransformerFactory)) 976 { 977 reporter.logErrorMsg("exampleUseAssociated:Processor does not support SAX"); 978 return; 979 } 980 SAXTransformerFactory stf = ((SAXTransformerFactory) tfactory); 981 reporter.logTraceMsg("getAssociatedStylesheet(new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 982 Source sources = 983 stf.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(sourceID)), 984 null, null, null); 985 986 if(null == sources) 987 { 988 reporter.checkFail("exampleUseAssociated:problem with source objects"); 989 return; 990 } 991 Transformer transformer = tfactory.newTransformer(sources); 992 993 reporter.logTraceMsg("new StreamSource(" + QetestUtils.filenameToURL(sourceID)); 994 transformer.transform(new StreamSource(QetestUtils.filenameToURL(sourceID)), 995 new StreamResult(outNames.nextName())); 996 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 997 fileChecker.check(reporter, new File(outNames.currentName()), 998 new File(goldNames.nextName()), 999 "exampleUseAssociated fileChecker of:" + outNames.currentName()); 1000 } 1001 catch (Throwable t) 1002 { 1003 reporter.checkFail("exampleUseAssociated threw: " + t.toString()); 1004 reporter.logThrowable(reporter.ERRORMSG, t, "exampleUseAssociated threw"); 1005 } 1006 } 1007 1008 /** 1009 * Show the Transformer using SAX events in and DOM nodes out. 1010 */ exampleContentHandler2DOM(String sourceID, String xslID)1011 public void exampleContentHandler2DOM(String sourceID, String xslID) 1012 { 1013 try 1014 { 1015 TransformerFactory tfactory = TransformerFactory.newInstance(); 1016 1017 // Make sure the transformer factory we obtained supports both 1018 // DOM and SAX. 1019 if (!(tfactory.getFeature(SAXSource.FEATURE) 1020 && tfactory.getFeature(DOMSource.FEATURE))) 1021 { 1022 reporter.logErrorMsg("exampleContentHandler2DOM:Processor does not support SAX/DOM"); 1023 return; 1024 } 1025 // We can now safely cast to a SAXTransformerFactory. 1026 SAXTransformerFactory sfactory = (SAXTransformerFactory) tfactory; 1027 1028 // Create an Document node as the root for the output. 1029 DocumentBuilderFactory dfactory 1030 = DocumentBuilderFactory.newInstance(); 1031 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 1032 org.w3c.dom.Document outNode = docBuilder.newDocument(); 1033 1034 // Create a ContentHandler that can liston to SAX events 1035 // and transform the output to DOM nodes. 1036 reporter.logTraceMsg("newTransformerHandler(new StreamSource(" + QetestUtils.filenameToURL(xslID)); 1037 TransformerHandler handler 1038 = sfactory.newTransformerHandler(new StreamSource(QetestUtils.filenameToURL(xslID))); 1039 handler.setResult(new DOMResult(outNode)); 1040 1041 // Create a reader and set it's ContentHandler to be the 1042 // transformer. 1043 XMLReader reader=null; 1044 1045 // Use JAXP1.1 ( if possible ) 1046 try { 1047 javax.xml.parsers.SAXParserFactory factory= 1048 javax.xml.parsers.SAXParserFactory.newInstance(); 1049 factory.setNamespaceAware( true ); 1050 javax.xml.parsers.SAXParser jaxpParser= 1051 factory.newSAXParser(); 1052 reader=jaxpParser.getXMLReader(); 1053 1054 } catch( javax.xml.parsers.ParserConfigurationException ex ) { 1055 throw new org.xml.sax.SAXException( ex ); 1056 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { 1057 throw new org.xml.sax.SAXException( ex1.toString() ); 1058 } catch( NoSuchMethodError ex2 ) { 1059 } 1060 if( reader==null ) reader= getJAXPXMLReader(); 1061 reader.setContentHandler(handler); 1062 reader.setProperty("http://xml.org/sax/properties/lexical-handler", 1063 handler); 1064 1065 // Send the SAX events from the parser to the transformer, 1066 // and thus to the DOM tree. 1067 reporter.logTraceMsg("reader.parse(" + QetestUtils.filenameToURL(sourceID)); 1068 reader.parse(QetestUtils.filenameToURL(sourceID)); 1069 1070 // Serialize the node for diagnosis. 1071 // This serializes to outNames.nextName() 1072 exampleSerializeNode(outNode); 1073 1074 fileChecker.check(reporter, new File(outNames.currentName()), 1075 new File(goldNames.nextName()), 1076 "exampleContentHandler2DOM fileChecker of:" + outNames.currentName()); 1077 } 1078 catch (Throwable t) 1079 { 1080 reporter.checkFail("exampleContentHandler2DOM threw: " + t.toString()); 1081 reporter.logThrowable(reporter.ERRORMSG, t, "exampleContentHandler2DOM threw"); 1082 } 1083 } 1084 1085 /** 1086 * Serialize a node to System.out. 1087 */ exampleSerializeNode(Node node)1088 public void exampleSerializeNode(Node node) 1089 throws TransformerException, TransformerConfigurationException, SAXException, IOException, 1090 ParserConfigurationException 1091 { 1092 TransformerFactory tfactory = TransformerFactory.newInstance(); 1093 1094 // This creates a transformer that does a simple identity transform, 1095 // and thus can be used for all intents and purposes as a serializer. 1096 Transformer serializer = tfactory.newTransformer(); 1097 1098 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 1099 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 1100 serializer.transform(new DOMSource(node), 1101 new StreamResult(outNames.nextName())); 1102 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 1103 // TEST UPDATE - Caller must validate outNames.currentName() 1104 } 1105 1106 /** 1107 * A fuller example showing how the TrAX interface can be used 1108 * to serialize a DOM tree. 1109 */ exampleAsSerializer(String sourceID, String xslID)1110 public void exampleAsSerializer(String sourceID, String xslID) 1111 { 1112 try 1113 { 1114 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 1115 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 1116 org.w3c.dom.Document outNode = docBuilder.newDocument(); 1117 reporter.logTraceMsg("docBuilder.parse(new InputSource(" + QetestUtils.filenameToURL(sourceID)); 1118 Node doc = docBuilder.parse(new InputSource(QetestUtils.filenameToURL(sourceID))); 1119 1120 TransformerFactory tfactory = TransformerFactory.newInstance(); 1121 1122 // This creates a transformer that does a simple identity transform, 1123 // and thus can be used for all intents and purposes as a serializer. 1124 Transformer serializer = tfactory.newTransformer(); 1125 1126 Properties oprops = new Properties(); 1127 oprops.put("method", "html"); 1128 oprops.put("{http://xml.apache.org/xslt}indent-amount", "2"); 1129 serializer.setOutputProperties(oprops); 1130 serializer.transform(new DOMSource(doc), 1131 new StreamResult(outNames.nextName())); 1132 reporter.logStatusMsg("Test-output-to: new StreamResult(" + outNames.currentName()); 1133 fileChecker.check(reporter, new File(outNames.currentName()), 1134 new File(goldNames.nextName()), 1135 "exampleAsSerializer fileChecker of:" + outNames.currentName()); 1136 } 1137 catch (Throwable t) 1138 { 1139 reporter.checkFail("exampleAsSerializer threw: " + t.toString()); 1140 reporter.logThrowable(reporter.ERRORMSG, t, "exampleAsSerializer threw"); 1141 } 1142 } 1143 1144 1145 /** 1146 * Worker method to get an XMLReader. 1147 * 1148 * Not the most efficient of methods, but makes the code simpler. 1149 * 1150 * @return a new XMLReader for use, with setNamespaceAware(true) 1151 */ getJAXPXMLReader()1152 protected XMLReader getJAXPXMLReader() 1153 throws Exception 1154 { 1155 // Be sure to use the JAXP methods only! 1156 SAXParserFactory factory = SAXParserFactory.newInstance(); 1157 factory.setNamespaceAware(true); 1158 SAXParser saxParser = factory.newSAXParser(); 1159 return saxParser.getXMLReader(); 1160 } 1161 1162 1163 /** 1164 * Convenience method to print out usage information - update if needed. 1165 * @return String denoting usage of this test class 1166 */ usage()1167 public String usage() 1168 { 1169 return ("Common [optional] options supported by ExamplesTest:\n" 1170 + "(Note: assumes inputDir=.\\tests\\api)\n" 1171 + super.usage()); // Grab our parent classes usage as well 1172 } 1173 1174 1175 /** 1176 * Main method to run test from the command line - can be left alone. 1177 * @param args command line argument array 1178 */ main(String[] args)1179 public static void main(String[] args) 1180 { 1181 1182 ExamplesTest app = new ExamplesTest(); 1183 1184 app.doMain(args); 1185 } 1186 } 1187