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