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 org.w3c.dom.ls;
  25 
  26 import java.io.StringBufferInputStream;
  27 
  28 import javax.xml.parsers.DocumentBuilder;
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.parsers.ParserConfigurationException;
  31 
  32 import org.testng.Assert;
  33 import org.testng.annotations.Test;
  34 import org.w3c.dom.DOMConfiguration;
  35 import org.w3c.dom.DOMImplementation;
  36 import org.w3c.dom.Document;
  37 import org.w3c.dom.Node;
  38 import org.w3c.dom.ls.DOMImplementationLS;
  39 import org.w3c.dom.ls.LSInput;
  40 import org.w3c.dom.ls.LSParser;
  41 import org.w3c.dom.ls.LSSerializer;
  42 import org.w3c.dom.ls.LSSerializerFilter;
  43 import org.w3c.dom.traversal.NodeFilter;
  44 
  45 /*
  46  * @bug 6290947
  47  * @summary Test LSSerializer writes the XML declaration when LSSerializerFilter is set that rejects all nodes and
  48  * LSSerializer's configuration set parameter "xml-declaration" to "true".
  49  */
  50 public class Bug6290947 {
  51 
  52     private static String XML_STRING = "<?xml version=\"1.0\"?><ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
  53     private static DOMImplementationLS implLS;
  54     private final String XML_FILE_INTERNAL_DTD = "note_in_dtd.xml";
  55 
  56     @Test
  57     public void testStringSourceWithXmlDecl() {
  58         String result = prepare(XML_STRING, true);
  59         System.out.println("testStringSource: output: " + result);
  60         Assert.assertTrue(result.indexOf("<?xml")>-1, "XML Declaration expected in output");
  61     }
  62 
  63     @Test
  64     public void testStringSourceWithOutXmlDecl() {
  65         String result = prepare(XML_STRING, false);
  66         System.out.println("testStringSource: output: " + result);
  67         Assert.assertTrue(result.indexOf("<?xml")==-1, "XML Declaration is not expected in output");
  68     }
  69 
  70     @Test
  71     public void testXmlWithInternalDTD1() {
  72         String result = prepare(XML_FILE_INTERNAL_DTD, true);
  73         System.out.println("testStringSource: output: " + result);
  74         Assert.assertTrue(result.indexOf("<!DOCTYPE")>0, "XML Declaration and DTD are expected in output");
  75     }
  76 
  77     @Test
  78     public void testXmlWithInternalDTD2() {
  79         String result = prepare(XML_FILE_INTERNAL_DTD, false);
  80         System.out.println("testStringSource: output: " + result);
  81         Assert.assertTrue(result.indexOf("<!DOCTYPE")>-1, "DTD is expected in output");
  82     }
  83 
  84     private String prepare(String source, boolean xmlDeclFlag) {
  85         Document startDoc = null;
  86         DocumentBuilder domParser = null;
  87         try {
  88             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  89             domParser = factory.newDocumentBuilder();
  90         } catch (ParserConfigurationException e) {
  91             e.printStackTrace();
  92             Assert.fail("Exception occured: " + e.getMessage());
  93         }
  94 
  95         final StringBufferInputStream is = new StringBufferInputStream(XML_STRING);
  96         try {
  97             startDoc = domParser.parse(is);
  98         } catch (Exception e) {
  99             e.printStackTrace();
 100             Assert.fail("Exception occured: " + e.getMessage());
 101         }
 102 
 103         DOMImplementation impl = startDoc.getImplementation();
 104         implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
 105         LSParser parser = implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
 106 
 107         LSInput src = getXmlSource(source);
 108 
 109         LSSerializer writer = implLS.createLSSerializer();
 110 
 111         DOMConfiguration conf = writer.getDomConfig();
 112         conf.setParameter("xml-declaration", Boolean.valueOf(xmlDeclFlag));
 113 
 114         // set filter
 115         writer.setFilter(new LSSerializerFilter() {
 116             public short acceptNode(Node enode) {
 117                 return FILTER_REJECT;
 118 
 119             }
 120 
 121             public int getWhatToShow() {
 122                 return NodeFilter.SHOW_ELEMENT;
 123             }
 124         });
 125 
 126         Document doc = parser.parse(src);
 127         return writer.writeToString(doc);
 128     }
 129 
 130     private LSInput getXmlSource(String xml1) {
 131         LSInput src = implLS.createLSInput();
 132         try {
 133             if (xml1.endsWith(".xml"))
 134                 src.setByteStream(this.getClass().getResourceAsStream(XML_FILE_INTERNAL_DTD));
 135             else
 136                 src.setStringData(xml1);
 137         } catch (Exception e) {
 138             e.printStackTrace();
 139             Assert.fail("Exception occured: " + e.getMessage());
 140         }
 141         return src;
 142     }
 143 }