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

XML indentation regression after JDK-8265073

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • tbd
    • 17, 18, 19, 20, 21
    • xml

      In the following example, the XML output is correctly indented on JDK 16 and earlier, and incorrectly indented on JDK 17 and newer.

      The regression bisects to the fix for JDK-8265073 (XML transformation and indentation when using xml:space)

      ```
      import java.io.ByteArrayOutputStream;
      import javax.xml.transform.OutputKeys;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.sax.SAXTransformerFactory;
      import javax.xml.transform.sax.TransformerHandler;
      import javax.xml.transform.stream.StreamResult;
      import org.xml.sax.helpers.AttributesImpl;

      public final class Test {

        public static void main(String[] args) throws Exception {
          SAXTransformerFactory transformerFactory =
              (SAXTransformerFactory) TransformerFactory.newInstance();
          TransformerHandler transformerHandler = transformerFactory.newTransformerHandler();
          Transformer transformer = transformerHandler.getTransformer();
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
          transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          transformerHandler.setResult(new StreamResult(baos));

          AttributesImpl attributes = new AttributesImpl();
          transformerHandler.startDocument();

          attributes.addAttribute("", "xml:space", "xml:space", "BAR", "preserve");
          transformerHandler.startElement("", "Foo", "Foo", attributes);
          attributes.clear();

          attributes.addAttribute("", "bar", "bar", "BAR", "test-val");

          transformerHandler.startElement("", "foo", "foo", attributes);
          transformerHandler.endElement("", "foo", "foo");

          transformerHandler.endElement("", "Foo", "Foo");
          transformerHandler.endDocument();

          String output = baos.toString("UTF-8");
          System.err.println(output);
          System.exit(output.split("\\R").length == 1 ? 1 : 0);
        }
      }
      ```

      Expected:

      <?xml version="1.0" encoding="UTF-8"?><Foo xml:space="preserve">
        <foo bar="test-val"/>
      </Foo>

      Actual:

      <?xml version="1.0" encoding="UTF-8"?><Foo xml:space="preserve"><foo bar="test-val"/></Foo>

            joehw Joe Wang
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: