1 /* 2 * Copyright (c) 2014, 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 parsers; 25 26 import java.io.File; 27 import java.io.IOException; 28 import java.io.InputStream; 29 30 import javax.xml.XMLConstants; 31 import javax.xml.parsers.DocumentBuilder; 32 import javax.xml.parsers.DocumentBuilderFactory; 33 import javax.xml.parsers.ParserConfigurationException; 34 import javax.xml.parsers.SAXParser; 35 import javax.xml.parsers.SAXParserFactory; 36 import javax.xml.transform.stream.StreamSource; 37 import javax.xml.validation.Schema; 38 import javax.xml.validation.SchemaFactory; 39 40 import org.testng.Assert; 41 import org.testng.annotations.Listeners; 42 import org.testng.annotations.Test; 43 import org.w3c.dom.Document; 44 import org.w3c.dom.Node; 45 import org.w3c.dom.Text; 46 import org.xml.sax.SAXException; 47 import org.xml.sax.helpers.DefaultHandler; 48 49 /* 50 * @test 51 * @bug 6564400 52 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 53 * @run testng/othervm -DrunSecMngr=true parsers.Bug6564400 54 * @run testng/othervm parsers.Bug6564400 55 * @summary Test ignorable whitespace handling with schema validation. 56 */ 57 @Listeners({jaxp.library.FilePolicy.class}) 58 public class Bug6564400 { 59 private boolean sawIgnorable = false; 60 Schema schema = null; 61 62 public Bug6564400(String name) { 63 String xsdFile = "Bug6564400.xsd"; 64 File schemaFile = new File(xsdFile); 65 66 // Now attempt to load up the schema 67 try { 68 SchemaFactory schFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 69 schema = schFactory.newSchema(new StreamSource(getClass().getResourceAsStream(xsdFile))); 70 } catch (Exception e) { 71 // Nevermind, bad things will happen later 72 } 73 } 74 75 @Test 76 public void testDOM() throws ParserConfigurationException, SAXException, IOException { 77 InputStream xmlFile = getClass().getResourceAsStream("Bug6564400.xml"); 78 79 // Set the options on the DocumentFactory to remove comments, remove 80 // whitespace 81 // and validate against the schema. 82 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 83 docFactory.setIgnoringComments(true); 84 docFactory.setIgnoringElementContentWhitespace(true); 85 docFactory.setSchema(schema); 86 87 DocumentBuilder parser = docFactory.newDocumentBuilder(); 88 Document xmlDoc = parser.parse(xmlFile); 89 90 boolean ok = dump(xmlDoc, true); 91 Assert.assertEquals(true, ok); 92 } 93 94 @Test 95 public void testSAX() throws ParserConfigurationException, SAXException, IOException { 96 InputStream xmlFile = getClass().getResourceAsStream("Bug6564400.xml"); 97 98 // Parse with SAX 99 SAXParserFactory saxFactory = SAXParserFactory.newInstance(); 100 saxFactory.setSchema(schema); 101 102 SAXParser saxparser = saxFactory.newSAXParser(); 103 104 sawIgnorable = false; 105 saxparser.parse(xmlFile, new MyHandler()); 106 Assert.assertEquals(true, sawIgnorable); 107 } 108 109 @Test 110 public void testConformantDOM() throws ParserConfigurationException, SAXException, IOException { 111 InputStream xmlFile = getClass().getResourceAsStream("Bug6564400.xml"); 112 113 // Set the options on the DocumentFactory to remove comments, remove 114 // whitespace 115 // and validate against the schema. 116 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 117 docFactory.setIgnoringComments(true); 118 docFactory.setIgnoringElementContentWhitespace(true); 119 docFactory.setSchema(schema); 120 docFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace", true); 121 122 DocumentBuilder parser = docFactory.newDocumentBuilder(); 123 Document xmlDoc = parser.parse(xmlFile); 124 125 boolean ok = dump(xmlDoc, true); 126 Assert.assertEquals(false, ok); 127 } 128 129 @Test 130 public void testConformantSAX() throws ParserConfigurationException, SAXException, IOException { 131 InputStream xmlFile = getClass().getResourceAsStream("Bug6564400.xml"); 132 133 // Parse with SAX 134 SAXParserFactory saxFactory = SAXParserFactory.newInstance(); 135 saxFactory.setSchema(schema); 136 saxFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace", true); 137 138 SAXParser saxparser = saxFactory.newSAXParser(); 139 140 sawIgnorable = false; 141 saxparser.parse(xmlFile, new MyHandler()); 142 Assert.assertEquals(false, sawIgnorable); 143 } 144 145 private boolean dump(Node node) { 146 return dump(node, false); 147 } 148 149 private boolean dump(Node node, boolean silent) { 150 return dump(node, silent, 0); 151 } 152 153 private boolean dump(Node node, boolean silent, int depth) { 154 boolean ok = true; 155 if (!silent) { 156 for (int i = 0; i < depth; i++) { 157 System.out.print(" "); 158 } 159 System.out.println(node); 160 } 161 162 if (node.getNodeType() == Node.TEXT_NODE) { 163 String text = ((Text) node).getData(); 164 ok = ok && text.trim().length() > 0; 165 } 166 167 if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE) { 168 Node child = node.getFirstChild(); 169 while (child != null) { 170 ok = ok && dump(child, silent, depth + 1); 171 child = child.getNextSibling(); 172 } 173 } 174 return ok; 175 } 176 177 public class MyHandler extends DefaultHandler { 178 public void ignorableWhitespace(char[] ch, int start, int length) { 179 sawIgnorable = true; 180 } 181 } 182 } 183