1 /*
   2  * Copyright (c) 2014, 2015, 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.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 6376823
  47  * @summary Test LSSerializer works.
  48  */
  49 public class Bug6376823 {
  50 
  51     private static String XML_STRING = "<?xml version=\"1.0\"?><ROOT><ELEMENT1><CHILD1/><CHILD1><COC1/></CHILD1></ELEMENT1><ELEMENT2>test1<CHILD2/></ELEMENT2></ROOT>";
  52     private static DOMImplementationLS implLS;
  53 
  54     @Test
  55     public void testStringSourceWithXmlDecl() {
  56         String result = prepare(XML_STRING, true);
  57         System.out.println("testStringSource: output: " + result);
  58         Assert.assertTrue(result.indexOf("<?xml", 5) < 0, "XML Declaration expected in output");
  59     }
  60 
  61     private String prepare(String source, boolean xmlDeclFlag) {
  62         Document startDoc = null;
  63         DocumentBuilder domParser = null;
  64         try {
  65             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  66             domParser = factory.newDocumentBuilder();
  67         } catch (ParserConfigurationException e) {
  68             e.printStackTrace();
  69             Assert.fail("Exception occured: " + e.getMessage());
  70         }
  71 
  72         final StringBufferInputStream is = new StringBufferInputStream(XML_STRING);
  73         try {
  74             startDoc = domParser.parse(is);
  75         } catch (Exception e) {
  76             e.printStackTrace();
  77             Assert.fail("Exception occured: " + e.getMessage());
  78         }
  79 
  80         DOMImplementation impl = startDoc.getImplementation();
  81         implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
  82         LSParser parser = implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
  83 
  84         LSInput src = getXmlSource(source);
  85 
  86         LSSerializer writer = implLS.createLSSerializer();
  87 
  88         DOMConfiguration conf = writer.getDomConfig();
  89         conf.setParameter("xml-declaration", Boolean.valueOf(xmlDeclFlag));
  90 
  91         // set filter
  92         writer.setFilter(new LSSerializerFilter() {
  93             public short acceptNode(Node enode) {
  94                 return FILTER_ACCEPT;
  95 
  96             }
  97 
  98             public int getWhatToShow() {
  99                 return NodeFilter.SHOW_ALL;
 100             }
 101         });
 102 
 103         Document doc = parser.parse(src);
 104         return writer.writeToString(doc);
 105     }
 106 
 107     private LSInput getXmlSource(String xml1) {
 108         LSInput src = implLS.createLSInput();
 109         try {
 110             src.setStringData(xml1);
 111         } catch (Exception e) {
 112             e.printStackTrace();
 113             Assert.fail("Exception occured: " + e.getMessage());
 114         }
 115         return src;
 116     }
 117 }