From 7e03cf2916a69f947c46ac85b222ee7a99f68ad8 Mon Sep 17 00:00:00 2001 From: Joe Wang Date: Fri, 18 Jun 2021 16:03:30 +0000 Subject: [PATCH] 8265073: XML transformation and indentation when using xml:space Reviewed-by: naoto, lancea, iris --- .../xml/internal/serializer/ToStream.java | 10 ++-- .../common/prettyprint/PrettyPrintTest.java | 52 +++++++++++++++++-- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java index 135b8b5759a..4fab3016bea 100644 --- a/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java +++ b/src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java @@ -53,7 +53,7 @@ import org.xml.sax.SAXException; * serializers (xml, html, text ...) that write output to a stream. * * @xsl.usage internal - * @LastModified: May 2021 + * @LastModified: June 2021 */ abstract public class ToStream extends SerializerBase { @@ -1893,10 +1893,6 @@ abstract public class ToStream extends SerializerBase { throw new SAXException(e); } - // process the attributes now, because after this SAX call they might be gone - if (atts != null) - addAttributes(atts); - if (m_doIndent) { m_ispreserveSpace = m_preserveSpaces.peekOrFalse(); m_preserveSpaces.push(m_ispreserveSpace); @@ -1905,6 +1901,10 @@ abstract public class ToStream extends SerializerBase { m_childNodeNum = 0; } + // process the attributes now, because after this SAX call they might be gone + if (atts != null) + addAttributes(atts); + m_elemContext = m_elemContext.push(namespaceURI,localName,name); m_isprevtext = false; diff --git a/test/jaxp/javax/xml/jaxp/unittest/common/prettyprint/PrettyPrintTest.java b/test/jaxp/javax/xml/jaxp/unittest/common/prettyprint/PrettyPrintTest.java index 2ff1b87dbf7..6d10d783779 100644 --- a/test/jaxp/javax/xml/jaxp/unittest/common/prettyprint/PrettyPrintTest.java +++ b/test/jaxp/javax/xml/jaxp/unittest/common/prettyprint/PrettyPrintTest.java @@ -33,8 +33,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; -import java.nio.file.Files; -import java.nio.file.Paths; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -66,7 +64,7 @@ import org.xml.sax.SAXException; /* * @test - * @bug 6439439 8087303 8174025 8223291 8249867 8261209 8260858 + * @bug 6439439 8087303 8174025 8223291 8249867 8261209 8260858 8265073 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest * @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow common.prettyprint.PrettyPrintTest * @run testng/othervm common.prettyprint.PrettyPrintTest @@ -180,6 +178,54 @@ public class PrettyPrintTest { }; } + private final String xml1 = "" + + "element belement cTRUEtest"; + private final String expected1 = "\n" + + " element b\n" + + " element c\n" + + " TRUE\n" + + " test\n" + + "\n"; + + private final String xml2 = "" + + "level 2 level 1 level 2" + + "level 2 level 1 level 2"; + private final String expected2 = "\n" + + " \n" + + " level 2\n" + + " level 1 \n" + + " level 2\n" + + " \n" + + " level 2 level 1 level 2\n" + + "\n"; + + /* + * Bug: 8265073 + * source and expected output + */ + @DataProvider + public Object[][] preserveSpace() { + return new Object[][]{ + {xml1, expected1}, + {xml2, expected2}, + }; + } + + /** + * Bug: 8265073 + * Verifies that the scope of the preserve attribute is applied properly + * within the relevant elements. + * @param xml the source + * @param expected the expected result + * @throws Exception if the assertion fails or an error occurs in the + * transform process + */ + @Test(dataProvider = "preserveSpace") + public void test(String xml, String expected) throws Exception { + String result = transform(null, xml, true, true, false, false, false); + Assert.assertEquals(result.replaceAll("\r\n", "\n"), expected); + } + /* * Bug: 8260858 * Verifies the use of the new property "xsltcIsStandalone" and the