/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.xml.parsers.ptests; import static jaxp.library.JAXPTestUtilities.CLASS_DIR; import static jaxp.library.JAXPTestUtilities.compareWithGold; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import static javax.xml.parsers.ptests.TestUtils.GOLDEN_DIR; import static javax.xml.parsers.ptests.TestUtils.XML_DIR; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import jaxp.library.JAXPFileBaseTest; import org.testng.annotations.Test; import org.w3c.dom.Document; import org.xml.sax.SAXException; /** * This tests the setIgnoringElementWhitespace and setIgnoringComments of * DocumentBuilderFactory */ public class DocumentBuilderFactory02 extends JAXPFileBaseTest { /** * Test for the isIgnoringElementContentWhitespace and the * setIgnoringElementContentWhitespace. The xml file has all kinds of * whitespace,tab and newline characters, it uses the MyNSContentHandler * which does not invoke the characters callback when this * setIgnoringElementContentWhitespace is set to true. * @throws ParserConfigurationException if a DocumentBuilder cannot be * created which satisfies the configuration requested. * @throws SAXException If any parse errors occur. * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, or cannot * be opened for any other reason. * @throws TransformerException If an unrecoverable error occurs during * the course of the transformation. */ @Test public void testCheckElementContentWhitespace() throws ParserConfigurationException, SAXException, IOException, TransformerException { String goldFile = GOLDEN_DIR + "dbfactory02GF.out"; String outputFile = CLASS_DIR + "dbfactory02.out"; MyErrorHandler eh = MyErrorHandler.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true); assertFalse(dbf.isIgnoringElementContentWhitespace()); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(eh); Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml")); assertFalse(eh.isErrorOccured()); DOMSource domSource = new DOMSource(doc); TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); SAXResult saxResult = new SAXResult(); try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) { saxResult.setHandler(handler); transformer.transform(domSource, saxResult); } assertTrue(compareWithGold(goldFile, outputFile)); } }