JDK-8087303 : LSSerializer pretty print does not work anymore. regression?
  • Type: Bug
  • Component: xml
  • Sub-Component: org.w3c.dom
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2015-06-10
  • Updated: 2020-06-09
  • Resolved: 2016-12-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b150Fixed
Related Reports
Relates :  
Relates :  
Sub Tasks
JDK-8175793 :  
Description
FULL PRODUCT VERSION :
java version "1.9.0-ea"
Java(TM) SE Runtime Environment (build 1.9.0-ea-b66)
Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b66, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Mac OS X 10.9.5

A DESCRIPTION OF THE PROBLEM :
The following code tries to convert a xml-string to a pretty-print-xml-string which works with jdk7 and jdk8 but not with jdk9 anymore.

REGRESSION.  Last worked in version 8u45

ADDITIONAL REGRESSION INFORMATION: 
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
just javac and java. test fails if runtimeexception is thrown

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
<rss version="2.0">
    <channel>
        <title>Java Tutorials and Examples 1</title>
        <language>en-us</language>
    </channel>
</rss>
ACTUAL -
<rss version="2.0">
    <channel>  <title>Java Tutorials and Examples 1</title>  <language>en-us</language>
    </channel>
</rss>

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

public class PrettyPrint {
    public static void main(String[] args) throws Exception {
        String xml = "<rss version=\"2.0\"><channel>  <title>Java Tutorials and Examples 1</title>  <language>en-us</language></channel></rss>";
        Writer stringWriter = createPrettyPrint(xml);
        String actual = stringWriter.toString();
        String expected = "<rss version=\"2.0\">\n"
                          + "    <channel>\n"
                          + "        <title>Java Tutorials and Examples 1</title>\n"
                          + "        <language>en-us</language>\n"
                          + "    </channel>\n"
                          + "</rss>\n";

        if (!actual.equals(expected)) throw new RuntimeException("not equal");
    }

    private static Writer createPrettyPrint(String xml) throws Exception {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImplementation = (DOMImplementationLS) registry.getDOMImplementation("LS");
        Writer stringWriter = new StringWriter();
        LSOutput formattedOutput = domImplementation.createLSOutput();
        formattedOutput.setCharacterStream(stringWriter);
        LSSerializer domSerializer = domImplementation.createLSSerializer();
        domSerializer.getDomConfig().setParameter("format-pretty-print", true);
        // Set this to true if the declaration is needed to be in the output.
        domSerializer.getDomConfig().setParameter("xml-declaration", false);
        domSerializer.write(toXmlDocument(xml), formattedOutput);
        return stringWriter;
    }

    private static Document toXmlDocument(String xmlString) throws Exception {
        InputSource xmlInputSource = new InputSource(new StringReader(xmlString));
        DocumentBuilder xmlDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return xmlDocumentBuilder.parse(xmlInputSource);
    }
}
---------- END SOURCE ----------


Comments
We are going beyond fixing the reported bug and intend to improve PrettyPrint so that the output matches those of major browsers. We are also supporting xml:space attribute. We'll need to document it in the release notes.
23-09-2016

This issue was introduced with moving to Xalan based DOM L3 serializer. Please note there are whitespace between the tags in test xml(refer to https://bugs.openjdk.java.net/secure/attachment/28816/PrettyPrint.java in the bug): <channel> <title>Java Tutorials and Examples 1</title> <language>en-us</language></channel> DOMSerializerImpl ignores the whitespace text node, look at the following code in BaseMarkupSerializer.java else if ( !_indenting || getElementState().preserveSpace || (text.replace('\n',' ').trim().length() != 0)) characters( text ); But LSSerializerImpl doesn���t, look at the following code in DOM3TreeWalker.serializeText(Text) // keep track of dispatch or not to avoid duplicaiton of filter code boolean bDispatch = false; // well-formed=true if ((fFeatures & WELLFORMED) != 0) { isTextWellFormed(node); } // if the node is whitespace // Determine the Attr's type. boolean isElementContentWhitespace = false; if (fIsLevel3DOM) { isElementContentWhitespace = node.isElementContentWhitespace(); } if (isElementContentWhitespace) { // element-content-whitespace=true if ((fFeatures & ELEM_CONTENT_WHITESPACE) != 0) { bDispatch = true; } } else { bDispatch = true; } // apply the LSSerializer filter if (!applyFilter(node, NodeFilter.SHOW_TEXT)) { return; } if (bDispatch) { dispatachChars(node); }
12-09-2016

This issue is started from JDK9 b26 on wards. I think this is caused because of the implementation of bug : JDK-8035467
10-05-2016

I think the expected result needs to be modified, as this will fail in every version However, current JDK9 formatting is a little off, as shown below sample JDK8 -------------------- $ java -version java version "1.8.0_31" Java(TM) SE Runtime Environment (build 1.8.0_31-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode) $ java -cp . XmlPrettyPrinter ACTUAL: <rss version="2.0"> <channel> <title>Java Tutorials and Examples 1</title> <language>en-us</language> </channel> </rss> EXPECTED: <rss version="2.0"> <channel> <title>Java Tutorials and Examples 1</title> <language>en-us</language> </channel> </rss> Exception in thread "main" java.lang.RuntimeException: not equal at XmlPrettyPrinter.main(XmlPrettyPrinter.java:29) java version "1.8.0_60-ea" Java(TM) SE Runtime Environment (build 1.8.0_60-ea-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b02, mixed mode) $ ../../JAVA/jdk1.8.0_60/bin/java -cp . XmlPrettyPrinter ACTUAL: <rss version="2.0"> <channel> <title>Java Tutorials and Examples 1</title> <language>en-us</language> </channel> </rss> EXPECTED: <rss version="2.0"> <channel> <title>Java Tutorials and Examples 1</title> <language>en-us</language> </channel> </rss> Exception in thread "main" java.lang.RuntimeException: not equal at XmlPrettyPrinter.main(XmlPrettyPrinter.java:29) sample jdk9 ---------------------- ../../JAVA/latest/jdk1.9.0/bin/java -version java version "1.9.0-ea" Java(TM) SE Runtime Environment (build 1.9.0-ea-b67) Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b67, mixed mode) $ ../../JAVA/latest/jdk1.9.0/bin/java -cp . XmlPrettyPrinter ACTUAL: <rss version="2.0"> <channel> <title>Java Tutorials and Examples 1</title> <language>en-us</language> </channel> </rss> EXPECTED: <rss version="2.0"> <channel> <title>Java Tutorials and Examples 1</title> <language>en-us</language> </channel> </rss> Exception in thread "main" java.lang.RuntimeException: not equal at XmlPrettyPrinter.main(XmlPrettyPrinter.java:29)
12-06-2015

1. Run the attached test case (PrettyPrint.java) in Windows 7 and Mac OS X. 2. Result: 7u67: FAIL 7u80: FAIL 8: FAIL 8u45: FAIL 8u60: FAIL JDK 9 ea b68: FAIL 3. Output with JDK 9 ea b68: > java -version java version "1.9.0-ea" Java(TM) SE Runtime Environment (build 1.9.0-ea-langtools-nightly-h2829-20150610-b68) Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-langtools-nightly-h2829-20150610-b68, mixed mode) > java PrettyPrint Picked up _JAVA_OPTIONS: -Dcom.oracle.usagetracker.config.file="C:\ProgramData\Oracle\Java_AMC_2\jutConfig.txt" Exception in thread "main" java.lang.RuntimeException: not equal at PrettyPrint.main(PrettyPrint.java:26) 4. Conclusion: The test fails with run time exception with all checked JDK versions. (tested on MAC OS X and Windows 7). Written back to the submitter for the reconfirmation about the issue.
12-06-2015