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

javax.xml.XMLEventWriter does not respect StartDocument.encodingSet

XMLWordPrintable

    • b13
    • 18
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      XMLEventWriterImpl does not use encodingSet, which means if the input document does not have an encoding, UTF-8 is always written.

      This is particularly problematic if the writer was created with createXMLEventWriter(OutputStream) and the system encoding is not UTF-8. In that case, XMLStreamWriterImpl (via XMLStreamWriterBase.writeStartDocument) verifies that the specified encoding matches the output encoding, and if not, it throws an exception to XMLEventWriterImpl, which prevents the standalone declaration from being written.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Set the platform encoding to something other than UTF-8
      2. Use XMLEventReader and XMLEventWriter to read a simple document without an encoding but with a standalone document declaration:
      <?xml version='1.0' standalone='yes'?>

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The standalone document declaration is round-tripped.
      ACTUAL -
      The standalone document declaration is not round-tripped.

      ---------- BEGIN SOURCE ----------
      $ cat a.java
      import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;;
      import javax.xml.stream.XMLInputFactory;
      import javax.xml.stream.XMLOutputFactory;

      public class a {
        public static void main(String[] a) throws Exception {
          String xml = "<?xml version='1.0' standalone='yes'?><x/>";
          var r = XMLInputFactory.newInstance().createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
          var out = new ByteArrayOutputStream();
          var w = XMLOutputFactory.newInstance().createXMLEventWriter(out);
          w.add(r);
          w.close();
          System.out.println(out);
        }
      }
      $ java a.java
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?><x></x>
      $ java -Dfile.encoding=ASCII a.java
      <?xml version="1.0"?><x></x>
      ---------- END SOURCE ----------

            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: