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

Transform with indent moves tabs to new lines

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      In Java 11 (and probably all 9+) calling transform with a xml with \t in it and OutputKeys.INDENT enabled moves the \t to new lines making the result broken.

      REGRESSION : Last worked in version 8u221

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use java 11
      Create transformer with property OutputKeys.INDENT set to "yes"
      Set transformer property "{http://xml.apache.org/xslt}indent-amount" to "2"
      Transform an xml that is already indented with \t characters

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      XML is prettified, readable without empty new lines.
      ACTUAL -
      New lines are added for \t characters creating a bloated XML.

      ---------- BEGIN SOURCE ----------
          @Test
          public void pretty_format_with_tabs() throws TransformerException {
              String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                      "<document>\n" +
                      "\t<test>\n" +
                      "\t\t12345\n" +
                      "\t</test>\n" +
                      "</document>";

              String expectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><document>\n" +
                      " <test>12345</test>\n" +
                      "</document>\n";

              javax.xml.transform.TransformerFactory transformerFactory = TransformerFactory.newInstance();
              javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
              transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
              transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2));

              javax.xml.transform.Source in = new StreamSource(new StringReader(xml));
              javax.xml.transform.stream.StreamResult out = new StreamResult(new StringWriter());
              transformer.transform(in, out);
              String result = out.getWriter().toString();

              assertEquals(expectedResult, result);
          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Trimming each string row before transformation removes the tabs.

      FREQUENCY : always


            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: