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