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

LSSerializer pretty print does not work anymore. regression?

XMLWordPrintable

    • b150
    • x86
    • os_x

      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 ----------

            fyuan Frank Yuan (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: