8261209: isStandalone property: remove dependency on pretty-print

Reviewed-by: lancea, naoto
This commit is contained in:
Joe Wang 2021-02-09 17:50:25 +00:00
parent 01d928080a
commit 7c565f8b37
3 changed files with 34 additions and 18 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
*/ */
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
@ -40,7 +40,7 @@ import org.xml.sax.SAXException;
* be viewed as internal or package private, this is not an API. * be viewed as internal or package private, this is not an API.
* *
* @xsl.usage internal * @xsl.usage internal
* @LastModified: Aug 2019 * @LastModified: Feb 2021
*/ */
public final class ToXMLStream extends ToStream public final class ToXMLStream extends ToStream
{ {
@ -171,7 +171,7 @@ public final class ToXMLStream extends ToStream
writer.write('\"'); writer.write('\"');
writer.write(standalone); writer.write(standalone);
writer.write("?>"); writer.write("?>");
if (m_doIndent) { if (m_doIndent || m_isStandalone) {
if (m_standaloneWasSpecified if (m_standaloneWasSpecified
|| getDoctypePublic() != null || getDoctypePublic() != null
|| getDoctypeSystem() != null || getDoctypeSystem() != null

View file

@ -217,9 +217,7 @@
* <th scope="row" style="font-weight:normal" id="ISSTANDALONE">isStandalone</th> * <th scope="row" style="font-weight:normal" id="ISSTANDALONE">isStandalone</th>
* <td>indicates that the serializer should treat the output as a * <td>indicates that the serializer should treat the output as a
* standalone document. The property can be used to ensure a newline is written * standalone document. The property can be used to ensure a newline is written
* after the XML declaration when the property * after the XML declaration. Unlike the property
* {@link org.w3c.dom.ls.LSSerializer#getDomConfig() format-pretty-print} is set
* to true. Unlike the property
* {@link org.w3c.dom.ls.LSSerializer#getDomConfig() xml-declaration}, this property * {@link org.w3c.dom.ls.LSSerializer#getDomConfig() xml-declaration}, this property
* does not have an effect on whether an XML declaration should be written out. * does not have an effect on whether an XML declaration should be written out.
* </td> * </td>

View file

@ -66,7 +66,7 @@ import org.xml.sax.SAXException;
/* /*
* @test * @test
* @bug 6439439 8087303 8174025 8223291 8249867 * @bug 6439439 8087303 8174025 8223291 8249867 8261209
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true common.prettyprint.PrettyPrintTest * @run testng/othervm -DrunSecMngr=true common.prettyprint.PrettyPrintTest
* @run testng/othervm common.prettyprint.PrettyPrintTest * @run testng/othervm common.prettyprint.PrettyPrintTest
@ -78,11 +78,15 @@ public class PrettyPrintTest {
private static final String JDK_IS_STANDALONE = private static final String JDK_IS_STANDALONE =
"http://www.oracle.com/xml/jaxp/properties/isStandalone"; "http://www.oracle.com/xml/jaxp/properties/isStandalone";
private static final String SP_JDK_IS_STANDALONE = "jdk.xml.isStandalone"; private static final String SP_JDK_IS_STANDALONE = "jdk.xml.isStandalone";
// pretty-print=true, isStandalone=true, linebreak added after header
private static final String XML_LB private static final String XML_LB
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<sometag/>\n"; = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<sometag/>\n";
private static final String XML_NOLB // pretty-print=true, isStandalone=false, no linebreak after header
private static final String XML_PPTRUE_NOLB
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sometag/>\n"; = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sometag/>\n";
// pretty-print=false, isStandalone=true, linebreak added after header
private static final String XML_PPFALSE_LB
= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<sometag/>";
/* /*
* test CDATA, elements only, text and element, xml:space property, mixed * test CDATA, elements only, text and element, xml:space property, mixed
* node types. * node types.
@ -101,16 +105,22 @@ public class PrettyPrintTest {
/* /*
* Bug: 8249867 * Bug: 8249867
* DataProvider: for testing the isStandalone property * DataProvider: for testing the isStandalone property
* Data columns: property, system property, value, expected result * Data columns: pretty-print, property, system property, value, expected result
*/ */
@DataProvider(name = "setting") @DataProvider(name = "setting")
Object[][] getData() throws Exception { Object[][] getData() throws Exception {
return new Object[][]{ return new Object[][]{
{false, true, true, XML_LB}, //set System property // pretty-print = true
{false, true, false, XML_NOLB},//set System property {true, false, true, true, XML_LB}, //set System property = true
{true, false, true, XML_LB}, //set property {true, false, true, false, XML_PPTRUE_NOLB}, //set System property = false
{true, false, false, XML_NOLB},//set property {true, true, false, true, XML_LB}, //set property = true
{false, false, false, XML_NOLB} //default {true, true, false, false, XML_PPTRUE_NOLB}, //set property = false
{true, false, false, false, XML_PPTRUE_NOLB},//default
// pretty-print = false
{false, false, true, true, XML_PPFALSE_LB}, //System property = true
{false, true, false, true, XML_PPFALSE_LB}, //set property = true
}; };
} }
@ -134,9 +144,14 @@ public class PrettyPrintTest {
* Bug: 8249867 * Bug: 8249867
* Verifies the use of the new property "isStandalone" and the * Verifies the use of the new property "isStandalone" and the
* corresponding System property "jdk.xml.isStandalone". * corresponding System property "jdk.xml.isStandalone".
*
* Bug: 8261209
* Verifies that the property takes effect regardless of the settings of
* property "pretty-print".
*/ */
@Test(dataProvider = "setting") @Test(dataProvider = "setting")
public void test(boolean p, boolean sp, boolean val, String expected) public void testIsStandalone_DOMLS(boolean pretty, boolean p, boolean sp,
boolean val, String expected)
throws Exception { throws Exception {
if (sp) { if (sp) {
setSystemProperty(SP_JDK_IS_STANDALONE, Boolean.toString(val)); setSystemProperty(SP_JDK_IS_STANDALONE, Boolean.toString(val));
@ -144,9 +159,12 @@ public class PrettyPrintTest {
Document document = getDocument(); Document document = getDocument();
DOMImplementationLS impl = (DOMImplementationLS)document.getImplementation(); DOMImplementationLS impl = (DOMImplementationLS)document.getImplementation();
LSSerializer ser = impl.createLSSerializer(); LSSerializer ser = impl.createLSSerializer();
ser.getDomConfig().setParameter("format-pretty-print", true); DOMConfiguration config = ser.getDomConfig();
if (pretty) {
config.setParameter("format-pretty-print", true);
}
if (p && !sp) { if (p && !sp) {
ser.getDomConfig().setParameter(JDK_IS_STANDALONE, val); config.setParameter(JDK_IS_STANDALONE, val);
} }
if (sp) { if (sp) {
clearSystemProperty(SP_JDK_IS_STANDALONE); clearSystemProperty(SP_JDK_IS_STANDALONE);