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 */
43
44 import org.testng.Assert;
45 import org.testng.annotations.DataProvider;
46 import org.testng.annotations.Listeners;
47 import org.testng.annotations.Test;
48 import org.w3c.dom.DOMConfiguration;
49 import org.w3c.dom.DOMImplementation;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Node;
52 import org.w3c.dom.Text;
53 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
54 import org.w3c.dom.ls.DOMImplementationLS;
55 import org.w3c.dom.ls.LSOutput;
56 import org.w3c.dom.ls.LSSerializer;
57 import org.xml.sax.InputSource;
58 import org.xml.sax.SAXException;
59
60
61 /*
62 * @test
63 * @bug 6439439 8087303 8174025
64 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
65 * @run testng/othervm -DrunSecMngr=true common.prettyprint.PrettyPrintTest
66 * @run testng/othervm common.prettyprint.PrettyPrintTest
67 * @summary Test serializing xml and html with indentation.
68 */
69 @Listeners({jaxp.library.FilePolicy.class})
70 public class PrettyPrintTest {
71 /*
72 * test CDATA, elements only, text and element, xml:space property, mixed
73 * node types.
74 */
75 @DataProvider(name = "xml-data")
76 public Object[][] xmlData() throws Exception {
77 return new Object[][] {
78 { "xmltest1.xml", "xmltest1.out" },
79 { "xmltest2.xml", "xmltest2.out" },
80 { "xmltest3.xml", "xmltest3.out" },
81 { "xmltest4.xml", "xmltest4.out" },
82 { "xmltest6.xml", "xmltest6.out" },
83 { "xmltest8.xml", "xmltest8.out" } };
365 StringWriter writer = new StringWriter();
366 LSOutput formattedOutput = domImplementation.createLSOutput();
367 formattedOutput.setCharacterStream(writer);
368 LSSerializer domSerializer = domImplementation.createLSSerializer();
369 domSerializer.getDomConfig().setParameter(DOM_FORMAT_PRETTY_PRINT, pretty);
370 domSerializer.getDomConfig().setParameter("xml-declaration", false);
371 domSerializer.write(xml, formattedOutput);
372 return writer.toString();
373 }
374
375 private String transform(Node xml, boolean pretty) throws Exception {
376 Transformer transformer = getTransformer(false, pretty);
377 StringWriter writer = new StringWriter();
378 transformer.transform(new DOMSource(xml), new StreamResult(writer));
379 return writer.toString();
380 }
381
382 private Document toXmlDocument(String xmlString) throws Exception {
383 InputSource xmlInputSource = new InputSource(new StringReader(xmlString));
384 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
385 dbf.setValidating(true);
386 DocumentBuilder xmlDocumentBuilder = dbf.newDocumentBuilder();
387 Document node = xmlDocumentBuilder.parse(xmlInputSource);
388 return node;
389 }
390
391 private Text newTextNode(String text) throws Exception {
392 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
393 return db.newDocument().createTextNode(text);
394 }
395
396 private Document createDocWithSequentTextNodes() throws Exception {
397 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
398 Document doc = db.newDocument();
399 Node root = doc.createElement("root");
400 doc.appendChild(root);
401 root.appendChild(doc.createTextNode("\n"));
402 root.appendChild(doc.createTextNode("\n"));
403 root.appendChild(doc.createTextNode("\n"));
404 root.appendChild(doc.createTextNode(" "));
405 root.appendChild(doc.createTextNode("t"));
|
1 /*
2 * Copyright (c) 2014, 2019, 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 */
43
44 import org.testng.Assert;
45 import org.testng.annotations.DataProvider;
46 import org.testng.annotations.Listeners;
47 import org.testng.annotations.Test;
48 import org.w3c.dom.DOMConfiguration;
49 import org.w3c.dom.DOMImplementation;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Node;
52 import org.w3c.dom.Text;
53 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
54 import org.w3c.dom.ls.DOMImplementationLS;
55 import org.w3c.dom.ls.LSOutput;
56 import org.w3c.dom.ls.LSSerializer;
57 import org.xml.sax.InputSource;
58 import org.xml.sax.SAXException;
59
60
61 /*
62 * @test
63 * @bug 6439439 8087303 8174025 8223291
64 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
65 * @run testng/othervm -DrunSecMngr=true common.prettyprint.PrettyPrintTest
66 * @run testng/othervm common.prettyprint.PrettyPrintTest
67 * @summary Test serializing xml and html with indentation.
68 */
69 @Listeners({jaxp.library.FilePolicy.class})
70 public class PrettyPrintTest {
71 /*
72 * test CDATA, elements only, text and element, xml:space property, mixed
73 * node types.
74 */
75 @DataProvider(name = "xml-data")
76 public Object[][] xmlData() throws Exception {
77 return new Object[][] {
78 { "xmltest1.xml", "xmltest1.out" },
79 { "xmltest2.xml", "xmltest2.out" },
80 { "xmltest3.xml", "xmltest3.out" },
81 { "xmltest4.xml", "xmltest4.out" },
82 { "xmltest6.xml", "xmltest6.out" },
83 { "xmltest8.xml", "xmltest8.out" } };
365 StringWriter writer = new StringWriter();
366 LSOutput formattedOutput = domImplementation.createLSOutput();
367 formattedOutput.setCharacterStream(writer);
368 LSSerializer domSerializer = domImplementation.createLSSerializer();
369 domSerializer.getDomConfig().setParameter(DOM_FORMAT_PRETTY_PRINT, pretty);
370 domSerializer.getDomConfig().setParameter("xml-declaration", false);
371 domSerializer.write(xml, formattedOutput);
372 return writer.toString();
373 }
374
375 private String transform(Node xml, boolean pretty) throws Exception {
376 Transformer transformer = getTransformer(false, pretty);
377 StringWriter writer = new StringWriter();
378 transformer.transform(new DOMSource(xml), new StreamResult(writer));
379 return writer.toString();
380 }
381
382 private Document toXmlDocument(String xmlString) throws Exception {
383 InputSource xmlInputSource = new InputSource(new StringReader(xmlString));
384 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
385 DocumentBuilder xmlDocumentBuilder = dbf.newDocumentBuilder();
386 Document node = xmlDocumentBuilder.parse(xmlInputSource);
387 return node;
388 }
389
390 private Text newTextNode(String text) throws Exception {
391 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
392 return db.newDocument().createTextNode(text);
393 }
394
395 private Document createDocWithSequentTextNodes() throws Exception {
396 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
397 Document doc = db.newDocument();
398 Node root = doc.createElement("root");
399 doc.appendChild(root);
400 root.appendChild(doc.createTextNode("\n"));
401 root.appendChild(doc.createTextNode("\n"));
402 root.appendChild(doc.createTextNode("\n"));
403 root.appendChild(doc.createTextNode(" "));
404 root.appendChild(doc.createTextNode("t"));
|