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 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(); 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 } | 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 static org.w3c.dom.ls.DOMImplementationLS.MODE_SYNCHRONOUS; 27 28 import java.io.IOException; 29 import java.io.OutputStream; 30 import java.io.StringReader; 31 import java.io.Writer; 32 33 import javax.xml.parsers.DocumentBuilder; 34 import javax.xml.parsers.DocumentBuilderFactory; 35 import javax.xml.parsers.ParserConfigurationException; 36 37 import org.testng.Assert; 38 import org.testng.annotations.Listeners; 39 import org.testng.annotations.Test; 40 import org.w3c.dom.DOMError; 41 import org.w3c.dom.DOMErrorHandler; 42 import org.w3c.dom.DOMImplementation; 43 import org.w3c.dom.Document; 44 import org.w3c.dom.ls.DOMImplementationLS; 45 import org.w3c.dom.ls.LSException; 46 import org.w3c.dom.ls.LSInput; 47 import org.w3c.dom.ls.LSOutput; 48 import org.w3c.dom.ls.LSParser; 49 import org.w3c.dom.ls.LSSerializer; 50 import org.xml.sax.InputSource; 51 import org.xml.sax.SAXException; 52 53 /* 54 * @test 55 * @bug 8080906 8114834 56 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 57 * @run testng/othervm -DrunSecMngr=true dom.ls.LSSerializerTest 58 * @run testng/othervm dom.ls.LSSerializerTest 59 * @summary Test LSSerializer. 60 */ 61 @Listeners({jaxp.library.BasePolicy.class}) 62 public class LSSerializerTest { 63 64 class DOMErrorHandlerImpl implements DOMErrorHandler { 65 66 boolean NoOutputSpecifiedErrorReceived = false; 67 68 public boolean handleError(final DOMError error) { 69 // consume "no-output-specified" errors 70 if ("no-output-specified".equalsIgnoreCase(error.getType())) { 71 NoOutputSpecifiedErrorReceived = true; 72 return true; 73 } 74 75 // unexpected error 76 Assert.fail("Unexpected Error Type: " + error.getType() + " @ (" + error.getLocation().getLineNumber() + ", " 77 + error.getLocation().getColumnNumber() + ")" + ", " + error.getMessage()); 78 79 return false; 80 } 81 } 82 151 writer.getDomConfig().setParameter("error-handler", eh); 152 153 boolean serialized = false; 154 try { 155 serialized = writer.write(doc, new Output()); 156 157 // unexpected success 158 Assert.fail("Serialized without raising an LSException due to " + "'no-output-specified'."); 159 } catch (LSException lsException) { 160 // expected exception 161 System.out.println("Expected LSException: " + lsException.toString()); 162 // continue processing 163 } 164 165 Assert.assertFalse(serialized, "Expected writer.write(doc, new Output()) == false"); 166 167 Assert.assertTrue(eh.NoOutputSpecifiedErrorReceived, "'no-output-specified' error was expected"); 168 } 169 170 @Test 171 public void testXML11() { 172 173 /** 174 * XML 1.1 document to parse. 175 */ 176 final String XML11_DOCUMENT = "<?xml version=\"1.1\" encoding=\"UTF-16\"?>\n" + "<hello>" + "world" + "<child><children/><children/></child>" 177 + "</hello>"; 178 179 /**JDK-8035467 180 * no newline in default output 181 */ 182 final String XML11_DOCUMENT_OUTPUT = 183 "<?xml version=\"1.1\" encoding=\"UTF-16\"?>" 184 + "<hello>" 185 + "world" 186 + "<child><children/><children/></child>" 187 + "</hello>"; 188 189 // it all begins with a Document 190 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 207 } catch (IOException ioException) { 208 ioException.printStackTrace(); 209 Assert.fail(ioException.toString()); 210 } 211 212 // query DOM Interfaces to get to a LSSerializer 213 DOMImplementation domImplementation = documentBuilder.getDOMImplementation(); 214 DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; 215 LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); 216 217 System.out.println("Serializer is: " + lsSerializer.getClass().getName() + " " + lsSerializer); 218 219 // get default serialization 220 String defaultSerialization = lsSerializer.writeToString(document); 221 222 System.out.println("XML 1.1 serialization = \"" + defaultSerialization + "\""); 223 224 // output should == input 225 Assert.assertEquals(XML11_DOCUMENT_OUTPUT, defaultSerialization, "Invalid serialization of XML 1.1 document: "); 226 } 227 228 /* 229 * @bug 8114834 test entity reference, nested entity reference when entities 230 * is true and false 231 */ 232 @Test 233 public void testEntityReference() throws Exception { 234 final String XML_DOCUMENT = "<?xml version=\"1.1\" encoding=\"UTF-16\"?>\n" + 235 "<!DOCTYPE author [\n" + 236 " <!ENTITY name \"Jo Smith\">" + 237 " <!ENTITY name1 \"&name;\">" + 238 " <!ENTITY name2 \"&name1;\">" + 239 "<!ENTITY ele \"<aa><bb>text</bb></aa>\">" + 240 " <!ENTITY ele1 \"&ele;\">" + 241 " <!ENTITY ele2 \"&ele1;\">" + 242 " ]>" + 243 " <author><a>&name1;</a>" + 244 "<b>b &name2; &name1; b</b>" + 245 "<c> &name; </c>" + 246 "<d>&ele1;d</d>" + 247 "<e> &ele2;eee </e>" + 248 "<f><att></f>" + 249 "<g> &ele; g</g>" + 250 "<h>&ele2;</h></author>" ; 251 252 253 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 254 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 255 256 DOMImplementation domImplementation = documentBuilder.getDOMImplementation(); 257 DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; 258 259 LSParser domParser = domImplementationLS.createLSParser(MODE_SYNCHRONOUS, null); 260 domParser.getDomConfig().setParameter("entities", Boolean.TRUE); 261 262 LSInput src = domImplementationLS.createLSInput(); 263 src.setStringData(XML_DOCUMENT); 264 Document document = domParser.parse(src); 265 266 LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); 267 268 lsSerializer.getDomConfig().setParameter("format-pretty-print", true); 269 System.out.println("test with default entities is " + lsSerializer.getDomConfig().getParameter("entities")); 270 Assert.assertEquals(lsSerializer.writeToString(document), 271 "<?xml version=\"1.1\" encoding=\"UTF-16\"?><!DOCTYPE author [ \n" + 272 "<!ENTITY name 'Jo Smith'>\n" + 273 "<!ENTITY name1 '&name;'>\n" + 274 "<!ENTITY name2 '&name1;'>\n" + 275 "<!ENTITY ele '<aa><bb>text</bb></aa>'>\n" + 276 "<!ENTITY ele1 '&ele;'>\n" + 277 "<!ENTITY ele2 '&ele1;'>\n" + 278 "]>\n" + 279 "<author>\n" + 280 " <a>&name1;Jo Smith</a>\n" + 281 " <b>b &name2;Jo Smith &name1;Jo Smith b</b>\n" + 282 " <c> &name;Jo Smith </c>\n" + 283 " <d>&ele1;d</d>\n" + 284 " <e> &ele2;eee </e>\n" + 285 " <f><att></f>\n" + 286 " <g> &ele; g</g>\n" + 287 " <h>&ele2;</h>\n" + 288 "</author>\n"); 289 290 lsSerializer.getDomConfig().setParameter("entities", Boolean.FALSE); 291 System.out.println("test with entities is false"); 292 Assert.assertEquals(lsSerializer.writeToString(document), 293 "<?xml version=\"1.1\" encoding=\"UTF-16\"?><!DOCTYPE author [ \n" + 294 "<!ENTITY name 'Jo Smith'>\n" + 295 "<!ENTITY name1 '&name;'>\n" + 296 "<!ENTITY name2 '&name1;'>\n" + 297 "<!ENTITY ele '<aa><bb>text</bb></aa>'>\n" + 298 "<!ENTITY ele1 '&ele;'>\n" + 299 "<!ENTITY ele2 '&ele1;'>\n" + 300 "]>\n" + 301 "<author>\n" + 302 " <a>&name;Jo Smith</a>\n" + 303 " <b>b &name;Jo Smith &name;Jo Smith b</b>\n" + 304 " <c> &name;Jo Smith </c>\n" + 305 " <d>\n" + 306 " <aa>\n" + 307 " <bb>text</bb>\n" + 308 " </aa>\n" + 309 " d\n" + 310 " </d>\n" + 311 " <e>\n" + 312 " <aa>\n" + 313 " <bb>text</bb>\n" + 314 " </aa>\n" + 315 " eee \n" + 316 " </e>\n" + 317 " <f><att></f>\n" + 318 " <g>\n" + 319 " <aa>\n" + 320 " <bb>text</bb>\n" + 321 " </aa>\n" + 322 " g\n" + 323 " </g>\n" + 324 " <h>\n" + 325 " <aa>\n" + 326 " <bb>text</bb>\n" + 327 " </aa>\n" + 328 " </h>\n" + 329 "</author>\n"); 330 331 } 332 } |