JDK-8249867 : XML declaration is not followed by a newline
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxp
  • Affected Version: 7,11,15
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2020-07-15
  • Updated: 2021-04-15
  • Resolved: 2021-01-30
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 11 JDK 16 JDK 17 JDK 7 JDK 8
11.0.11Fixed 16.0.1Fixed 17 b08Fixed 7u301Fixed 8u281Fixed
Related Reports
CSR :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Sub Tasks
JDK-8259502 :  
Description
A DESCRIPTION OF THE PROBLEM :
Using LSSerializer to pretty-print a DOM results in an xml-declaration being not followed by a newline.

Related to bugs #7150637 and #8054115.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the following code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
<?xml version="1.0" encoding="UTF-8"?>
<sometag/>
ACTUAL -
<?xml version="1.0" encoding="UTF-8"?><sometag/>

---------- BEGIN SOURCE ----------
package io.github.oliviercailloux.jaris.xml;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class Snippet {
	public static void main(String[] args) throws Exception {
		final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		final DocumentBuilder builder = factory.newDocumentBuilder();
		final Document document = builder.newDocument();

		final Element child = document.createElement("sometag");
		document.appendChild(child);

		final DOMImplementationLS impl = (DOMImplementationLS) document.getImplementation().getFeature("LS", "3.0");
		final LSSerializer ser = impl.createLSSerializer();
		/** But see https://bugs.openjdk.java.net/browse/JDK-7150637 */
		/**
		 * But see
		 * <a href="https://bugs.openjdk.java.net/browse/JDK-8054115">8054115</a>:
		 * LSSerializer remove a '\n' following the xml declaration
		 */
		ser.getDomConfig().setParameter("format-pretty-print", true);
		final StringWriter writer = new StringWriter();
		final LSOutput output = impl.createLSOutput();
		output.setCharacterStream(writer);
		ser.write(document, output);

		System.out.println(writer.toString());
	}
}

---------- END SOURCE ----------

FREQUENCY : always



Comments
No response from the submitter.
06-04-2021

issue resolved in Apr CPU JDK 11 - removing the 11 BPR label
29-03-2021

Requested the submitter to verify the fix with the latest version of JDK at https://jdk.java.net/17/
10-03-2021

I'm not able to reproduce this with OpenJDK 8u282. So my guess on what has been done for Oracle JDK 8u is to introduce the new properties controlling this behaviour as specified by the CSR in JDK-8259704: $ java -showversion Snippet openjdk version "11.0.10" 2021-01-19 OpenJDK Runtime Environment 18.9 (build 11.0.10+9) OpenJDK 64-Bit Server VM 18.9 (build 11.0.10+9, mixed mode, sharing) <?xml version="1.0" encoding="UTF-8"?><sometag/> $ java -showversion Snippet openjdk version "1.8.0_282" OpenJDK Runtime Environment (build 1.8.0_282-b08) OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode) <?xml version="1.0" encoding="UTF-8"?> <sometag/>
03-03-2021

Fix Request (11u): Should get backported for parity with 11.0.11-oracle. Applies almost cleanly. Review thread: http://mail.openjdk.java.net/pipermail/jdk-updates-dev/2021-March/005204.html
03-03-2021

Fix Request(16u): Please approve backporting this change to jdk16u. Fix applies clean. The change introduces a new property to revert XML behaviour caused by regression. Risk is minimal. The change also involves updating the module-info summary for jdk16u. For more information visit CSR - https://bugs.openjdk.java.net/browse/JDK-8259704
09-02-2021

Changeset: 69ee314b Author: Joe Wang <joehw@openjdk.org> Date: 2021-01-30 02:34:52 +0000 URL: https://git.openjdk.java.net/jdk/commit/69ee314b
30-01-2021

The observations on Windows 10: JDK 11: Fail JDK 15: Fail ILW=MML=P4
22-07-2020