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