JDK-8054115 : LSSerializer remove a '\n' following the xml declaration
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxp
  • Affected Version: 9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-08-01
  • Updated: 2020-07-22
  • Resolved: 2015-06-17
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
9Fixed
Related Reports
Relates :  
Description
SQE xml test for LSSerializer failed since build b25_2014-07-30-1719_1029. Seems there is some defect after recent xml parser updated, that causes LSSerializer remove "return carrige" character from the following xml:
<?xml version="1.0" encoding="UTF-16"?>
<test>tt</test>
The xml will be converted to <?xml version="1.0" encoding="UTF-16"?><test>tt</test>

Run the following java program:

import java.io.*;
import javax.xml.parsers.*;
import com.sun.org.apache.xerces.internal.impl.Constants;
import org.xml.sax.*;
import org.w3c.dom.*;
import org.w3c.dom.ls.*;

public class Test
{
	public static void main(String[] args) throws Exception
	{
		test("1.0");
		test("1.1");
	}
	
	private static void test(String version) throws Exception
	{
		final String XML_DOCUMENT =
                "<?xml version=\"" + version + "\" encoding=\"UTF-16\"?>\n"
                + "<test>tt</test>";

        final String XML_DOCUMENT_PRETTY_PRINT =
                "<?xml version=\"" + version + "\" encoding=\"UTF-16\"?>\n"
                + "<test>tt</test>\n";
                

        // it all begins with a Document
        DocumentBuilderFactory documentBuilderFactory =
                DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();


        StringReader stringReader = new StringReader(XML_DOCUMENT);
        InputSource inputSource = new InputSource(stringReader);
        Document document = documentBuilder.parse(inputSource);


        // query DOM Interfaces to get to a LSSerializer
        DOMImplementation domImplementation =
                documentBuilder.getDOMImplementation();
        DOMImplementationLS domImplementationLS =
                (DOMImplementationLS) domImplementation;
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();

        // get configuration
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();

        
        // get default serialization
        System.out.println("default " + Constants.DOM_FORMAT_PRETTY_PRINT
                + " should be false. Actual value is " + domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT));

        String result = lsSerializer.writeToString(document);
        System.out.println(
                "serialization result is: " + result
                + "\n it equals the original xml: " + XML_DOCUMENT.equals(result));
                
        // configure LSSerializer to format-pretty-print
        domConfiguration.setParameter(
                Constants.DOM_FORMAT_PRETTY_PRINT,
                Boolean.TRUE);
        System.out.println("try to set " + Constants.DOM_FORMAT_PRETTY_PRINT
                + " to true. Actual result is " + domConfiguration.getParameter(Constants.DOM_FORMAT_PRETTY_PRINT));
        
        result = lsSerializer.writeToString(document);
        System.out.println(
                "serialization result is: " + result
                + "\n it equals the pretty format xml: " + XML_DOCUMENT_PRETTY_PRINT.equals(result));
     
	}
}


output:
default format-pretty-print should be false. Actual value is false
serialization result is: <?xml version="1.0" encoding="UTF-16"?><test>tt</test>
 it equals the original xml: false
try to set format-pretty-print to true. Actual result is true
serialization result is: <?xml version="1.0" encoding="UTF-16"?><test>tt</test>

 it equals the pretty format xml: false
default format-pretty-print should be false. Actual value is false
serialization result is: <?xml version="1.1" encoding="UTF-16"?><test>tt</test>
 it equals the original xml: false
try to set format-pretty-print to true. Actual result is true
serialization result is: <?xml version="1.1" encoding="UTF-16"?><test>tt</test>

 it equals the pretty format xml: false


It's not expected.

Comments
LSSerializerTest.java had been fixed in bug JDK-8043084.
17-06-2015

I assume this is unit-test/org/w3c/dom/ls/LSSerializerTest.java. Please update the test to reflect the change from Xerces' DOM L3 Serializer impl to that of Xalan. Look for JDK-8035467 in the changed test attached. See also JDK-7150637 for why it was changed.
16-06-2015