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