Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8054115

LSSerializer remove a '\n' following the xml declaration

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 9
    • 9
    • xml
    • None
    • b25
    • 9

    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.

      Attachments

        Issue Links

          Activity

            People

              fyuan Frank Yuan (Inactive)
              fyuan Frank Yuan (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: