1 /* 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package javax.xml.parsers.ptests; 25 26 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 27 import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR; 28 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR; 29 import static jaxp.library.JAXPTestUtilities.USER_DIR; 30 import static jaxp.library.JAXPTestUtilities.compareWithGold; 31 import static jaxp.library.JAXPTestUtilities.filenameToURL; 32 import static org.testng.Assert.assertEquals; 33 import static org.testng.Assert.assertFalse; 34 import static org.testng.Assert.assertNotNull; 35 import static org.testng.Assert.assertNull; 36 import static org.testng.Assert.assertTrue; 37 import static org.testng.Assert.assertSame; 38 import static org.testng.Assert.assertNotSame; 39 40 import java.io.BufferedReader; 41 import java.io.Closeable; 42 import java.io.File; 43 import java.io.FileInputStream; 44 import java.io.FileNotFoundException; 45 import java.io.FileReader; 46 47 import javax.xml.parsers.DocumentBuilder; 48 import javax.xml.parsers.DocumentBuilderFactory; 49 import javax.xml.parsers.FactoryConfigurationError; 50 import javax.xml.parsers.ParserConfigurationException; 51 import javax.xml.parsers.SAXParser; 52 import javax.xml.parsers.SAXParserFactory; 53 import javax.xml.transform.Transformer; 54 import javax.xml.transform.TransformerFactory; 55 import javax.xml.transform.dom.DOMSource; 56 import javax.xml.transform.sax.SAXResult; 57 58 import jaxp.library.JAXPDataProvider; 59 60 import org.testng.annotations.DataProvider; 61 import org.testng.annotations.Listeners; 62 import org.testng.annotations.Test; 63 import org.w3c.dom.Document; 64 import org.w3c.dom.Element; 65 import org.w3c.dom.NodeList; 66 import org.xml.sax.InputSource; 67 import org.xml.sax.SAXException; 68 import org.xml.sax.helpers.DefaultHandler; 69 70 /** 71 * @bug 8080907 8169778 72 * This checks the methods of DocumentBuilderFactoryImpl. 73 */ 74 /* 75 * @test 76 * @library /javax/xml/jaxp/libs 77 * @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow javax.xml.parsers.ptests.DocumentBuilderFactoryTest 78 * @run testng/othervm javax.xml.parsers.ptests.DocumentBuilderFactoryTest 79 */ 80 @Listeners({jaxp.library.FilePolicy.class}) 81 public class DocumentBuilderFactoryTest { 82 83 /** 84 * DocumentBuilderFactory builtin system-default implementation class name. 85 */ 86 private static final String DEFAULT_IMPL_CLASS = 87 "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"; 88 89 /** 90 * DocumentBuilderFactory implementation class name. 91 */ 92 private static final String DOCUMENT_BUILDER_FACTORY_CLASSNAME = DEFAULT_IMPL_CLASS; 93 94 /** 95 * Provide valid DocumentBuilderFactory instantiation parameters. 96 * 97 * @return a data provider contains DocumentBuilderFactory instantiation parameters. 98 */ 99 @DataProvider(name = "parameters") getValidateParameters()100 public Object[][] getValidateParameters() { 101 return new Object[][] { { DOCUMENT_BUILDER_FACTORY_CLASSNAME, null }, { DOCUMENT_BUILDER_FACTORY_CLASSNAME, this.getClass().getClassLoader() } }; 102 } 103 104 /** 105 * Test if newDefaultInstance() method returns an instance 106 * of the expected factory. 107 * @throws Exception If any errors occur. 108 */ 109 @Test testDefaultInstance()110 public void testDefaultInstance() throws Exception { 111 DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newDefaultInstance(); 112 DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance(); 113 assertNotSame(dbf1, dbf2, "same instance returned:"); 114 assertSame(dbf1.getClass(), dbf2.getClass(), 115 "unexpected class mismatch for newDefaultInstance():"); 116 assertEquals(dbf1.getClass().getName(), DEFAULT_IMPL_CLASS); 117 } 118 119 /** 120 * Test for DocumentBuilderFactory.newInstance(java.lang.String 121 * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName 122 * points to correct implementation of 123 * javax.xml.parsers.DocumentBuilderFactory , should return newInstance of 124 * DocumentBuilderFactory 125 * 126 * @param factoryClassName 127 * @param classLoader 128 * @throws ParserConfigurationException 129 */ 130 @Test(dataProvider = "parameters") testNewInstance(String factoryClassName, ClassLoader classLoader)131 public void testNewInstance(String factoryClassName, ClassLoader classLoader) throws ParserConfigurationException { 132 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(factoryClassName, classLoader); 133 DocumentBuilder builder = dbf.newDocumentBuilder(); 134 assertNotNull(builder); 135 } 136 137 /** 138 * test for DocumentBuilderFactory.newInstance(java.lang.String 139 * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName is 140 * null , should throw FactoryConfigurationError 141 * 142 * @param factoryClassName 143 * @param classLoader 144 */ 145 @Test(expectedExceptions = FactoryConfigurationError.class, dataProvider = "new-instance-neg", dataProviderClass = JAXPDataProvider.class) testNewInstanceNeg(String factoryClassName, ClassLoader classLoader)146 public void testNewInstanceNeg(String factoryClassName, ClassLoader classLoader) { 147 DocumentBuilderFactory.newInstance(factoryClassName, classLoader); 148 } 149 150 /** 151 * Test the default functionality of schema support method. 152 * @throws Exception If any errors occur. 153 */ 154 @Test testCheckSchemaSupport1()155 public void testCheckSchemaSupport1() throws Exception { 156 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 157 dbf.setValidating(true); 158 dbf.setNamespaceAware(true); 159 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 160 W3C_XML_SCHEMA_NS_URI); 161 MyErrorHandler eh = MyErrorHandler.newInstance(); 162 DocumentBuilder db = dbf.newDocumentBuilder(); 163 db.setErrorHandler(eh); 164 db.parse(new File(XML_DIR, "test.xml")); 165 assertFalse(eh.isErrorOccured()); 166 } 167 168 @DataProvider(name = "schema-source") getSchemaSource()169 public Object[][] getSchemaSource() throws FileNotFoundException { 170 return new Object[][] { 171 { new FileInputStream(new File(XML_DIR, "test.xsd")) }, 172 { new InputSource(filenameToURL(XML_DIR + "test.xsd")) } }; 173 } 174 175 /** 176 * Test the default functionality of schema support method. In 177 * this case the schema source property is set. 178 * @throws Exception If any errors occur. 179 */ 180 @Test(dataProvider = "schema-source") testCheckSchemaSupport2(Object schemaSource)181 public void testCheckSchemaSupport2(Object schemaSource) throws Exception { 182 try { 183 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 184 dbf.setValidating(true); 185 dbf.setNamespaceAware(true); 186 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 187 W3C_XML_SCHEMA_NS_URI); 188 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource); 189 MyErrorHandler eh = MyErrorHandler.newInstance(); 190 DocumentBuilder db = dbf.newDocumentBuilder(); 191 db.setErrorHandler(eh); 192 db.parse(new File(XML_DIR, "test1.xml")); 193 assertFalse(eh.isErrorOccured()); 194 } finally { 195 if (schemaSource instanceof Closeable) { 196 ((Closeable) schemaSource).close(); 197 } 198 } 199 200 } 201 202 /** 203 * Test the default functionality of schema support method. In 204 * this case the schema source property is set. 205 * @throws Exception If any errors occur. 206 */ 207 @Test(dataProvider = "schema-source") testCheckSchemaSupport3(Object schemaSource)208 public void testCheckSchemaSupport3(Object schemaSource) throws Exception { 209 try { 210 SAXParserFactory spf = SAXParserFactory.newInstance(); 211 spf.setValidating(true); 212 spf.setNamespaceAware(true); 213 SAXParser sp = spf.newSAXParser(); 214 sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 215 W3C_XML_SCHEMA_NS_URI); 216 sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource); 217 DefaultHandler dh = new DefaultHandler(); 218 // Not expect any unrecoverable error here. 219 sp.parse(new File(XML_DIR, "test1.xml"), dh); 220 } finally { 221 if (schemaSource instanceof Closeable) { 222 ((Closeable) schemaSource).close(); 223 } 224 } 225 } 226 227 /** 228 * Test the default functionality of newInstance method. To test 229 * the isCoalescing method and setCoalescing This checks to see if the CDATA 230 * and text nodes got combined In that case it will print "<xml>This 231 * is not parsed</xml> yet". 232 * @throws Exception If any errors occur. 233 */ 234 @Test testCheckDocumentBuilderFactory02()235 public void testCheckDocumentBuilderFactory02() throws Exception { 236 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 237 dbf.setCoalescing(true); 238 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 239 Document doc = docBuilder.parse(new File(XML_DIR, "DocumentBuilderFactory01.xml")); 240 Element e = (Element) doc.getElementsByTagName("html").item(0); 241 NodeList nl = e.getChildNodes(); 242 assertEquals(nl.getLength(), 1); 243 } 244 245 /** 246 * Test the isIgnoringComments. By default it is false. 247 */ 248 @Test testCheckDocumentBuilderFactory03()249 public void testCheckDocumentBuilderFactory03() { 250 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 251 assertFalse(dbf.isIgnoringComments()); 252 } 253 254 /** 255 * Test the isValidating. By default it is false, set it to true and then 256 * use a document which is not valid. It should throw a warning or 257 * an error at least. The test passes in case retval 0 is set in the error 258 * method . 259 * @throws Exception If any errors occur. 260 */ 261 @Test testCheckDocumentBuilderFactory04()262 public void testCheckDocumentBuilderFactory04() throws Exception { 263 MyErrorHandler eh = MyErrorHandler.newInstance(); 264 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 265 dbf.setValidating(true); 266 DocumentBuilder db = dbf.newDocumentBuilder(); 267 db.setErrorHandler(eh); 268 db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml")); 269 assertTrue(eh.isErrorOccured()); 270 } 271 272 /** 273 * Test the setValidating. By default it is false, use a 274 * document which is not valid. It should not throw a warning or an error. 275 * The test passes in case the return value equals 1. 276 * @throws Exception If any errors occur. 277 */ 278 @Test testCheckDocumentBuilderFactory16()279 public void testCheckDocumentBuilderFactory16() throws Exception { 280 MyErrorHandler eh = MyErrorHandler.newInstance(); 281 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 282 DocumentBuilder db = dbf.newDocumentBuilder(); 283 db.setErrorHandler(eh); 284 db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml")); 285 assertFalse(eh.isErrorOccured()); 286 } 287 288 /** 289 * Test the setValidating. By default it is false, use a 290 * document which is valid. It should not throw a warning or an error. The 291 * test passes in case the return value equals 1. 292 * @throws Exception If any errors occur. 293 */ 294 @Test testCheckDocumentBuilderFactory17()295 public void testCheckDocumentBuilderFactory17() throws Exception { 296 MyErrorHandler eh = MyErrorHandler.newInstance(); 297 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 298 DocumentBuilder db = dbf.newDocumentBuilder(); 299 db.setErrorHandler(eh); 300 db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml")); 301 assertFalse(eh.isErrorOccured()); 302 } 303 304 /** 305 * Test the isExpandEntityReferences. By default it is true. 306 * @throws Exception If any errors occur. 307 */ 308 @Test testCheckDocumentBuilderFactory05()309 public void testCheckDocumentBuilderFactory05() throws Exception { 310 try(FileInputStream fis = new FileInputStream(new File( 311 XML_DIR, "DocumentBuilderFactory02.xml"))) { 312 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 313 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 314 Document doc = docBuilder.parse(fis); 315 Element e = (Element) doc.getElementsByTagName("title").item(0); 316 NodeList nl = e.getChildNodes(); 317 assertTrue(dbf.isExpandEntityReferences()); 318 assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W'); 319 } 320 } 321 322 /** 323 * Test the default functionality of setValidating method. The 324 * XML file has a DTD which has namespaces defined. The parser takes care to 325 * check if the namespaces using elements and defined attributes are there 326 * or not. 327 * @throws Exception If any errors occur. 328 */ 329 @Test testCheckDocumentBuilderFactory06()330 public void testCheckDocumentBuilderFactory06() throws Exception { 331 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 332 dbf.setValidating(true); 333 DocumentBuilder db = dbf.newDocumentBuilder(); 334 MyErrorHandler eh = MyErrorHandler.newInstance(); 335 db.setErrorHandler(eh); 336 Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml")); 337 assertTrue(doc instanceof Document); 338 assertFalse(eh.isErrorOccured()); 339 } 340 341 /** 342 * Test the setExpandEntityReferences. 343 * @throws Exception If any errors occur. 344 */ 345 @Test testCheckDocumentBuilderFactory07()346 public void testCheckDocumentBuilderFactory07() throws Exception { 347 try (FileInputStream fis = new FileInputStream(new File( 348 XML_DIR, "DocumentBuilderFactory02.xml"))) { 349 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 350 dbf.setExpandEntityReferences(true); 351 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 352 Document doc = docBuilder.parse(fis); 353 Element e = (Element) doc.getElementsByTagName("title").item(0); 354 NodeList nl = e.getChildNodes(); 355 assertTrue(dbf.isExpandEntityReferences()); 356 assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W'); 357 } 358 } 359 360 /** 361 * Test the setExpandEntityReferences. 362 * @throws Exception If any errors occur. 363 */ 364 @Test testCheckDocumentBuilderFactory08()365 public void testCheckDocumentBuilderFactory08() throws Exception { 366 try (FileInputStream fis = new FileInputStream(new File( 367 XML_DIR, "DocumentBuilderFactory02.xml"))) { 368 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 369 dbf.setExpandEntityReferences(false); 370 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 371 Document doc = docBuilder.parse(fis); 372 Element e = (Element) doc.getElementsByTagName("title").item(0); 373 NodeList nl = e.getChildNodes(); 374 assertNull(nl.item(0).getNodeValue()); 375 } 376 } 377 378 /** 379 * Test the setIgnoringComments. By default it is set to false. 380 * explicitly setting it to false, it recognizes the comment which is in 381 * Element Node Hence the Element's child node is not null. 382 * @throws Exception If any errors occur. 383 */ 384 @Test testCheckDocumentBuilderFactory09()385 public void testCheckDocumentBuilderFactory09() throws Exception { 386 try (FileInputStream fis = new FileInputStream(new File( 387 XML_DIR, "DocumentBuilderFactory07.xml"))) { 388 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 389 dbf.setIgnoringComments(false); 390 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 391 Document doc = docBuilder.parse(fis); 392 Element e = (Element) doc.getElementsByTagName("body").item(0); 393 NodeList nl = e.getChildNodes(); 394 assertNotNull(nl.item(0).getNodeValue()); 395 } 396 } 397 398 /** 399 * This tests for the parse(InputSource). 400 * @throws Exception If any errors occur. 401 */ 402 @Test testCheckDocumentBuilderFactory10()403 public void testCheckDocumentBuilderFactory10() throws Exception { 404 try (BufferedReader br = new BufferedReader(new FileReader(new File( 405 XML_DIR, "DocumentBuilderFactory07.xml")))) { 406 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 407 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 408 Document doc = docBuilder.parse(new InputSource(br)); 409 assertNotNull(doc); 410 } 411 } 412 413 /** 414 * This tests for the parse InputStream with SystemID as a second parameter. 415 * @throws Exception If any errors occur. 416 */ 417 @Test testCheckDocumentBuilderFactory11()418 public void testCheckDocumentBuilderFactory11() throws Exception { 419 try (FileInputStream fis = new FileInputStream(new File( 420 XML_DIR, "dbf10import.xsl"))) { 421 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 422 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 423 Document doc = docBuilder.parse(fis, new File(XML_DIR).toURI() 424 .toASCIIString()); 425 assertNotNull(doc); 426 } 427 } 428 429 /** 430 * This tests for the parse InputStream with empty SystemID as a second 431 * parameter. 432 * @throws Exception If any errors occur. 433 */ 434 @Test testCheckDocumentBuilderFactory12()435 public void testCheckDocumentBuilderFactory12() throws Exception { 436 try (FileInputStream fis = new FileInputStream(new File( 437 XML_DIR, "dbf10import.xsl"))) { 438 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 439 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 440 Document doc = docBuilder.parse(fis, " "); 441 assertNotNull(doc); 442 } 443 } 444 445 /** 446 * This tests for the parse(uri). 447 * @throws Exception If any errors occur. 448 */ 449 @Test testCheckDocumentBuilderFactory13()450 public void testCheckDocumentBuilderFactory13() throws Exception { 451 // Accesing default working directory. 452 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 453 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 454 Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl") 455 .toURI().toASCIIString()); 456 assertNotNull(doc); 457 } 458 459 /** 460 * This tests for the parse(uri) with empty string as parameter should 461 * throw Sax Exception. 462 * @throws Exception If any errors occur. 463 */ 464 @Test(expectedExceptions = SAXException.class) testCheckDocumentBuilderFactory14()465 public void testCheckDocumentBuilderFactory14() throws Exception { 466 // Accesing default working directory. 467 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 468 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 469 docBuilder.parse(""); 470 } 471 472 /** 473 * This tests for the parse (uri) with null uri as parameter should throw 474 * IllegalArgumentException. 475 * @throws Exception If any errors occur. 476 * 477 */ 478 @Test(expectedExceptions = IllegalArgumentException.class) testCheckDocumentBuilderFactory15()479 public void testCheckDocumentBuilderFactory15() throws Exception { 480 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 481 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 482 String uri = null; 483 docBuilder.parse(uri); 484 } 485 486 /** 487 * Test the setIgnoringComments. By default it is set to false, 488 * setting this to true, It does not recognize the comment, Here the 489 * nodelist has a length 0 because the ignoring comments is true. 490 * @throws Exception If any errors occur. 491 */ 492 @Test testCheckIgnoringComments()493 public void testCheckIgnoringComments() throws Exception { 494 try (FileInputStream fis = new FileInputStream(new File( 495 XML_DIR, "DocumentBuilderFactory08.xml"))) { 496 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 497 dbf.setIgnoringComments(true); 498 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 499 Document doc = docBuilder.parse(fis); 500 Element e = (Element) doc.getElementsByTagName("body").item(0); 501 NodeList nl = e.getChildNodes(); 502 assertEquals(nl.getLength(), 0); 503 } 504 } 505 506 /** 507 * Test the default behaviour of setIgnoringComments. By default 508 * it is set to false, this is similar to case 9 but not setIgnoringComments 509 * explicitly, it does not recognize the comment. 510 * @throws Exception If any errors occur. 511 */ 512 @Test testCheckIgnoringComments1()513 public void testCheckIgnoringComments1() throws Exception { 514 try (FileInputStream fis = new FileInputStream(new File( 515 XML_DIR, "DocumentBuilderFactory07.xml"))) { 516 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 517 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 518 Document doc = docBuilder.parse(fis); 519 Element e = (Element) doc.getElementsByTagName("body").item(0); 520 NodeList nl = e.getChildNodes(); 521 assertFalse(dbf.isIgnoringComments()); 522 assertNotNull(nl.item(0).getNodeValue()); 523 } 524 } 525 526 /** 527 * Test for the isIgnoringElementContentWhitespace and the 528 * setIgnoringElementContentWhitespace. The xml file has all kinds of 529 * whitespace,tab and newline characters, it uses the MyNSContentHandler 530 * which does not invoke the characters callback when this 531 * setIgnoringElementContentWhitespace is set to true. 532 * @throws Exception If any errors occur. 533 */ 534 @Test testCheckElementContentWhitespace()535 public void testCheckElementContentWhitespace() throws Exception { 536 String goldFile = GOLDEN_DIR + "dbfactory02GF.out"; 537 String outputFile = USER_DIR + "dbfactory02.out"; 538 MyErrorHandler eh = MyErrorHandler.newInstance(); 539 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 540 dbf.setValidating(true); 541 assertFalse(dbf.isIgnoringElementContentWhitespace()); 542 dbf.setIgnoringElementContentWhitespace(true); 543 DocumentBuilder db = dbf.newDocumentBuilder(); 544 db.setErrorHandler(eh); 545 Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml")); 546 assertFalse(eh.isErrorOccured()); 547 DOMSource domSource = new DOMSource(doc); 548 TransformerFactory tfactory = TransformerFactory.newInstance(); 549 Transformer transformer = tfactory.newTransformer(); 550 SAXResult saxResult = new SAXResult(); 551 try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) { 552 saxResult.setHandler(handler); 553 transformer.transform(domSource, saxResult); 554 } 555 assertTrue(compareWithGold(goldFile, outputFile)); 556 } 557 } 558