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