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