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 javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 
29 import org.testng.Assert;
30 import org.testng.annotations.Listeners;
31 import org.testng.annotations.Test;
32 import org.w3c.dom.CDATASection;
33 import org.w3c.dom.Comment;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.EntityReference;
36 import org.w3c.dom.Node;
37 import org.w3c.dom.ProcessingInstruction;
38 import org.w3c.dom.Text;
39 import org.w3c.dom.ls.DOMImplementationLS;
40 import org.w3c.dom.ls.LSSerializer;
41 
42 
43 /*
44  * @test
45  * @bug 6354955
46  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
47  * @run testng/othervm -DrunSecMngr=true dom.ls.Bug6354955
48  * @run testng/othervm dom.ls.Bug6354955
49  * @summary Test LSSerializer can writeToString on DOM Text node with white space.
50  */
51 @Listeners({jaxp.library.BasePolicy.class})
52 public class Bug6354955 {
53 
54     @Test
testTextNode()55     public void testTextNode() {
56         try {
57             Document xmlDocument = createNewDocument();
58 
59             String whitespace = "\r\n    ";
60             Text textNode = xmlDocument.createTextNode(whitespace);
61 
62             System.out.println("original text is:\r\n\"" + whitespace + "\"");
63             String outerXML = getOuterXML(textNode);
64             System.out.println("OuterXML Text Node is:\r\n\"" + outerXML + "\"");
65 
66         } catch (Exception e) {
67             e.printStackTrace();
68             Assert.fail("Exception occured: " + e.getMessage());
69         }
70     }
71 
72     @Test
testCommentNode()73     public void testCommentNode() {
74         try {
75             Document xmlDocument = createNewDocument();
76             String commentStr = "This is a comment node";
77             Comment cmtNode = xmlDocument.createComment(commentStr);
78             String outerXML = getOuterXML(cmtNode);
79             System.out.println("OuterXML of Comment Node is:" + outerXML);
80 
81         } catch (Exception e) {
82             e.printStackTrace();
83             Assert.fail("Exception occured: " + e.getMessage());
84         }
85     }
86 
87     @Test
testPINode()88     public void testPINode() {
89         try {
90             Document xmlDocument = createNewDocument();
91             ProcessingInstruction piNode = xmlDocument.createProcessingInstruction("execute", "test");
92             String outerXML = getOuterXML(piNode);
93             System.out.println("OuterXML of Comment Node is:" + outerXML);
94 
95         } catch (Exception e) {
96             e.printStackTrace();
97             Assert.fail("Exception occured: " + e.getMessage());
98         }
99     }
100 
101     @Test
testCDATA()102     public void testCDATA() {
103         try {
104             Document xmlDocument = createNewDocument();
105             CDATASection cdataNode = xmlDocument.createCDATASection("See Data!!");
106             String outerXML = getOuterXML(cdataNode);
107             System.out.println("OuterXML of Comment Node is:" + outerXML);
108 
109         } catch (Exception e) {
110             e.printStackTrace();
111             Assert.fail("Exception occured: " + e.getMessage());
112         }
113     }
114 
115     @Test
testEntityReference()116     public void testEntityReference() {
117         try {
118             Document xmlDocument = createNewDocument();
119             EntityReference erefNode = xmlDocument.createEntityReference("entityref");
120             String outerXML = getOuterXML(erefNode);
121             System.out.println("OuterXML of Comment Node is:" + outerXML);
122 
123         } catch (Exception e) {
124             e.printStackTrace();
125             Assert.fail("Exception occured: " + e.getMessage());
126         }
127     }
128 
getOuterXML(Node node)129     private String getOuterXML(Node node) {
130         DOMImplementationLS domImplementation = (DOMImplementationLS) node.getOwnerDocument().getImplementation();
131         LSSerializer lsSerializer = domImplementation.createLSSerializer();
132         if (!(node instanceof Document)) {
133             lsSerializer.getDomConfig().setParameter("xml-declaration", false);
134         }
135         return lsSerializer.writeToString(node);
136     }
137 
createNewDocument()138     private Document createNewDocument() throws Exception {
139         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
140         documentBuilderFactory.setNamespaceAware(true);
141         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
142         return documentBuilder.newDocument();
143     }
144 }
145