1 /* 2 * Copyright (c) 2014, 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 jaxp.library.JAXPTestUtilities.CLASS_DIR; 27 import static jaxp.library.JAXPTestUtilities.compareWithGold; 28 import static org.testng.Assert.assertFalse; 29 import static org.testng.Assert.assertTrue; 30 import java.io.File; 31 import java.io.IOException; 32 import javax.xml.parsers.DocumentBuilder; 33 import javax.xml.parsers.DocumentBuilderFactory; 34 import javax.xml.parsers.ParserConfigurationException; 35 import static javax.xml.parsers.ptests.TestUtils.GOLDEN_DIR; 36 import static javax.xml.parsers.ptests.TestUtils.XML_DIR; 37 import javax.xml.transform.Transformer; 38 import javax.xml.transform.TransformerException; 39 import javax.xml.transform.TransformerFactory; 40 import javax.xml.transform.dom.DOMSource; 41 import javax.xml.transform.sax.SAXResult; 42 import jaxp.library.JAXPFileBaseTest; 43 import org.testng.annotations.Test; 44 import org.w3c.dom.Document; 45 import org.xml.sax.SAXException; 46 47 /** 48 * This tests the setIgnoringElementWhitespace and setIgnoringComments of 49 * DocumentBuilderFactory 50 */ 51 public class DocumentBuilderFactory02 extends JAXPFileBaseTest { 52 /** 53 * Test for the isIgnoringElementContentWhitespace and the 54 * setIgnoringElementContentWhitespace. The xml file has all kinds of 55 * whitespace,tab and newline characters, it uses the MyNSContentHandler 56 * which does not invoke the characters callback when this 57 * setIgnoringElementContentWhitespace is set to true. 58 * @throws ParserConfigurationException if a DocumentBuilder cannot be 59 * created which satisfies the configuration requested. 60 * @throws SAXException If any parse errors occur. 61 * @throws IOException if the file exists but is a directory rather than 62 * a regular file, does not exist but cannot be created, or cannot 63 * be opened for any other reason. 64 * @throws TransformerException If an unrecoverable error occurs during 65 * the course of the transformation. 66 */ 67 @Test 68 public void testCheckElementContentWhitespace() throws 69 ParserConfigurationException, SAXException, IOException, TransformerException { 70 String goldFile = GOLDEN_DIR + "dbfactory02GF.out"; 71 String outputFile = CLASS_DIR + "dbfactory02.out"; 72 MyErrorHandler eh = MyErrorHandler.newInstance(); 73 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 74 dbf.setValidating(true); 75 assertFalse(dbf.isIgnoringElementContentWhitespace()); 76 dbf.setIgnoringElementContentWhitespace(true); 77 DocumentBuilder db = dbf.newDocumentBuilder(); 78 db.setErrorHandler(eh); 79 Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml")); 80 assertFalse(eh.isErrorOccured()); 81 DOMSource domSource = new DOMSource(doc); 82 TransformerFactory tfactory = TransformerFactory.newInstance(); 83 Transformer transformer = tfactory.newTransformer(); 84 SAXResult saxResult = new SAXResult(); 85 try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) { 86 saxResult.setHandler(handler); 87 transformer.transform(domSource, saxResult); 88 } 89 assertTrue(compareWithGold(goldFile, outputFile)); 90 } 91 }