1 /* 2 * Copyright (c) 2014, 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 org.w3c.dom.ls; 25 26 import java.io.IOException; 27 import java.io.OutputStream; 28 import java.io.StringReader; 29 import java.io.Writer; 30 31 import javax.xml.parsers.DocumentBuilder; 32 import javax.xml.parsers.DocumentBuilderFactory; 33 import javax.xml.parsers.ParserConfigurationException; 34 35 import org.testng.Assert; 36 import org.testng.annotations.Test; 37 import org.w3c.dom.DOMConfiguration; 38 import org.w3c.dom.DOMError; 39 import org.w3c.dom.DOMErrorHandler; 40 import org.w3c.dom.DOMImplementation; 41 import org.w3c.dom.Document; 42 import org.xml.sax.InputSource; 43 import org.xml.sax.SAXException; 44 45 import com.sun.org.apache.xerces.internal.impl.Constants; 46 47 /* 48 * @summary Test LSSerializer. 49 */ 50 public class LSSerializerTest { 51 52 class DOMErrorHandlerImpl implements DOMErrorHandler { 53 54 boolean NoOutputSpecifiedErrorReceived = false; 55 56 public boolean handleError(final DOMError error) { 57 // consume "no-output-specified" errors 58 if ("no-output-specified".equalsIgnoreCase(error.getType())) { 59 NoOutputSpecifiedErrorReceived = true; 60 return true; 61 } 62 63 // unexpected error 64 Assert.fail("Unexpected Error Type: " + error.getType() + " @ (" + error.getLocation().getLineNumber() + ", " 65 + error.getLocation().getColumnNumber() + ")" + ", " + error.getMessage()); 66 67 return false; 68 } 69 } 70 71 class Output implements LSOutput { 72 public OutputStream getByteStream() { 73 return null; 74 } 75 76 public void setByteStream(final OutputStream byteStream) { 77 } 78 79 public Writer getCharacterStream() { 80 return null; 81 } 82 83 public void setCharacterStream(final Writer characterStream) { 84 } 85 86 public String getSystemId() { 87 return null; 88 } 89 90 public void setSystemId(final String systemId) { 91 } 92 93 public String getEncoding() { 94 return "UTF8"; 95 } 96 97 public void setEncoding(final String encoding) { 98 } 99 } 100 101 @Test 102 public void testDOMErrorHandler() { 103 104 final String XML_DOCUMENT = "<?xml version=\"1.0\"?>" + "<hello>" + "world" + "</hello>"; 105 106 StringReader stringReader = new StringReader(XML_DOCUMENT); 107 InputSource inputSource = new InputSource(stringReader); 108 Document doc = null; 109 try { 110 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 111 // LSSerializer defaults to Namespace processing 112 // so parsing must also 113 documentBuilderFactory.setNamespaceAware(true); 114 DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); 115 doc = parser.parse(inputSource); 116 117 } catch (Throwable e) { 118 e.printStackTrace(); 119 Assert.fail(e.toString()); 120 } 121 122 DOMImplementation impl = doc.getImplementation(); 123 DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); 124 LSSerializer writer = implLS.createLSSerializer(); 125 DOMErrorHandlerImpl eh = new DOMErrorHandlerImpl(); 126 writer.getDomConfig().setParameter("error-handler", eh); 127 128 boolean serialized = false; 129 try { 130 serialized = writer.write(doc, new Output()); 131 132 // unexpected success 133 Assert.fail("Serialized without raising an LSException due to " + "'no-output-specified'."); 134 } catch (LSException lsException) { 135 // expected exception 136 System.out.println("Expected LSException: " + lsException.toString()); 137 // continue processing 138 } 139 140 Assert.assertFalse(serialized, "Expected writer.write(doc, new Output()) == false"); 141 142 Assert.assertTrue(eh.NoOutputSpecifiedErrorReceived, "'no-output-specified' error was expected"); 143 } 144 145 @Test 146 public void testFormatPrettyPrint() { 147 148 final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n" + "<hello>" + "world" + "<child><children/><children/></child>" 149 + "</hello>"; 150 /**JDK-8035467 151 * no newline in default output 152 */ 153 final String XML_DOCUMENT_DEFAULT_PRINT = 154 "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" 155 + "<hello>" 156 + "world" 157 + "<child><children/><children/></child>" 158 + "</hello>"; 159 160 final String XML_DOCUMENT_PRETTY_PRINT = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + "<hello>" + "world" + "<child>" + "\n" + " " 161 + "<children/>" + "\n" + " " + "<children/>" + "\n" + " " + "</child>" + "\n" + "</hello>" + "\n"; 162 163 // it all begins with a Document 164 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 165 DocumentBuilder documentBuilder = null; 166 try { 167 documentBuilder = documentBuilderFactory.newDocumentBuilder(); 168 } catch (ParserConfigurationException parserConfigurationException) { 169 parserConfigurationException.printStackTrace(); 170 Assert.fail(parserConfigurationException.toString()); 171 } 172 Document document = null; 173 174 StringReader stringReader = new StringReader(XML_DOCUMENT); 175 InputSource inputSource = new InputSource(stringReader); 176 try { 177 document = documentBuilder.parse(inputSource); 178 } catch (SAXException saxException) { 179 saxException.printStackTrace(); 180 Assert.fail(saxException.toString()); 181 } catch (IOException ioException) { 182 ioException.printStackTrace(); 183 Assert.fail(ioException.toString()); 184 } 185 186 // query DOM Interfaces to get to a LSSerializer 187 DOMImplementation domImplementation = documentBuilder.getDOMImplementation(); 188 DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; 189 LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); 190 191 // get configuration 192 DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); 193 194 // query current configuration 195 Boolean defaultFormatPrettyPrint = (Boolean) domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT); 196 Boolean canSetFormatPrettyPrintFalse = (Boolean) domConfiguration.canSetParameter(Constants.DOM_FORMAT_PRETTY_PRINT, Boolean.FALSE); 197 Boolean canSetFormatPrettyPrintTrue = (Boolean) domConfiguration.canSetParameter(Constants.DOM_FORMAT_PRETTY_PRINT, Boolean.TRUE); 198 199 System.out.println(Constants.DOM_FORMAT_PRETTY_PRINT + " default/can set false/can set true = " + defaultFormatPrettyPrint + "/" 200 + canSetFormatPrettyPrintFalse + "/" + canSetFormatPrettyPrintTrue); 201 202 // test values 203 Assert.assertEquals(defaultFormatPrettyPrint, Boolean.FALSE, "Default value of " + Constants.DOM_FORMAT_PRETTY_PRINT + " should be " + Boolean.FALSE); 204 205 Assert.assertEquals(canSetFormatPrettyPrintFalse, Boolean.TRUE, "Can set " + Constants.DOM_FORMAT_PRETTY_PRINT + " to " + Boolean.FALSE + " should be " 206 + Boolean.TRUE); 207 208 Assert.assertEquals(canSetFormatPrettyPrintTrue, Boolean.TRUE, "Can set " + Constants.DOM_FORMAT_PRETTY_PRINT + " to " + Boolean.TRUE + " should be " 209 + Boolean.TRUE); 210 211 // get default serialization 212 String prettyPrintDefault = lsSerializer.writeToString(document); 213 System.out.println("(default) " + Constants.DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT) 214 + ": \n\"" + prettyPrintDefault + "\""); 215 216 Assert.assertEquals(XML_DOCUMENT_DEFAULT_PRINT, prettyPrintDefault, "Invalid serialization with default value, " + Constants.DOM_FORMAT_PRETTY_PRINT + "==" 217 + (Boolean) domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT)); 218 219 // configure LSSerializer to not format-pretty-print 220 domConfiguration.setParameter(Constants.DOM_FORMAT_PRETTY_PRINT, Boolean.FALSE); 221 String prettyPrintFalse = lsSerializer.writeToString(document); 222 System.out.println("(FALSE) " + Constants.DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT) 223 + ": \n\"" + prettyPrintFalse + "\""); 224 225 Assert.assertEquals(XML_DOCUMENT_DEFAULT_PRINT, prettyPrintFalse, "Invalid serialization with FALSE value, " + Constants.DOM_FORMAT_PRETTY_PRINT + "==" 226 + (Boolean) domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT)); 227 228 // configure LSSerializer to format-pretty-print 229 domConfiguration.setParameter(Constants.DOM_FORMAT_PRETTY_PRINT, Boolean.TRUE); 230 String prettyPrintTrue = lsSerializer.writeToString(document); 231 System.out.println("(TRUE) " + Constants.DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT) 232 + ": \n\"" + prettyPrintTrue + "\""); 233 234 Assert.assertEquals(XML_DOCUMENT_PRETTY_PRINT, prettyPrintTrue, "Invalid serialization with TRUE value, " + Constants.DOM_FORMAT_PRETTY_PRINT + "==" 235 + (Boolean) domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT)); 236 } 237 238 @Test 239 public void testXML11() { 240 241 /** 242 * XML 1.1 document to parse. 243 */ 244 final String XML11_DOCUMENT = "<?xml version=\"1.1\" encoding=\"UTF-16\"?>\n" + "<hello>" + "world" + "<child><children/><children/></child>" 245 + "</hello>"; 246 247 /**JDK-8035467 248 * no newline in default output 249 */ 250 final String XML11_DOCUMENT_OUTPUT = 251 "<?xml version=\"1.1\" encoding=\"UTF-16\"?>" 252 + "<hello>" 253 + "world" 254 + "<child><children/><children/></child>" 255 + "</hello>"; 256 257 // it all begins with a Document 258 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 259 DocumentBuilder documentBuilder = null; 260 try { 261 documentBuilder = documentBuilderFactory.newDocumentBuilder(); 262 } catch (ParserConfigurationException parserConfigurationException) { 263 parserConfigurationException.printStackTrace(); 264 Assert.fail(parserConfigurationException.toString()); 265 } 266 Document document = null; 267 268 StringReader stringReader = new StringReader(XML11_DOCUMENT); 269 InputSource inputSource = new InputSource(stringReader); 270 try { 271 document = documentBuilder.parse(inputSource); 272 } catch (SAXException saxException) { 273 saxException.printStackTrace(); 274 Assert.fail(saxException.toString()); 275 } catch (IOException ioException) { 276 ioException.printStackTrace(); 277 Assert.fail(ioException.toString()); 278 } 279 280 // query DOM Interfaces to get to a LSSerializer 281 DOMImplementation domImplementation = documentBuilder.getDOMImplementation(); 282 DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; 283 LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); 284 285 // get default serialization 286 String defaultSerialization = lsSerializer.writeToString(document); 287 288 System.out.println("XML 1.1 serialization = \"" + defaultSerialization + "\""); 289 290 // output should == input 291 Assert.assertEquals(XML11_DOCUMENT_OUTPUT, defaultSerialization, "Invalid serialization of XML 1.1 document: "); 292 } 293 }