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

WLS/Tuxedo error in encoding post JDK upgrade

XMLWordPrintable

      Customer reported an issue where setting writer.getDomConfig().setParameter("xml-declaration", true); produces no line break after the xml headers.

      Expected:
      <?xml version="1.0" encoding="UTF-8"?>
      <root>
          <item/>
      </root>

      Actual:
      <?xml version="1.0" encoding="UTF-8"?><root> <<<<--- missing line break after header
          </item>
      </root>

      import java.io.ByteArrayOutputStream;
      import java.io.StringWriter;
      import java.io.Writer;

      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.transform.OutputKeys;
      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamResult;

      import org.w3c.dom.*;
      import org.w3c.dom.Document;
      import org.w3c.dom.Element;
      import org.w3c.dom.bootstrap.DOMImplementationRegistry;
      import org.w3c.dom.ls.DOMImplementationLS;
      import org.w3c.dom.ls.LSOutput;
      import org.w3c.dom.ls.LSSerializer;

      public class TestToStringLS {

          public static void main(final String[] args) {
              try {
                  final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                  final DocumentBuilder builder = dbf.newDocumentBuilder();
                  final Document doc = builder.newDocument();

                  final Element element = doc.createElement("root");
                  doc.appendChild(element);

                  final Element itemElement = doc.createElement("item");
                  element.appendChild(itemElement);
      itemElement.appendChild(itemElement.getOwnerDocument().createTextNode(""));

                  System.out.println("------------W3C----------");
                  writeDocument(doc);
              } catch (final Exception ex) {
                  ex.printStackTrace();
              }
          }

          public static final void writeDocument(final Document doc) {
              final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

              DOMImplementationRegistry domRegistry = null;
              try {
                  domRegistry = DOMImplementationRegistry.newInstance();
                  final DOMImplementationLS impl =
                          (DOMImplementationLS) domRegistry.getDOMImplementation("LS");
                  final LSSerializer writer = impl.createLSSerializer();
      writer.getDomConfig().setParameter("xml-declaration", true);
                  writer.getDomConfig().setParameter("format-pretty-print", true);
                  final LSOutput output = impl.createLSOutput();
                  output.setEncoding("UTF-8");
                  output.setByteStream(outStream);
                  writer.write(doc, output);
                  final byte[] result = outStream.toByteArray();
                  System.out.println(new String(result));
              } catch (final Exception e) {
                  System.err.println("ERROR" + e.getMessage());
                  e.printStackTrace();
              }
          }

      }

            kravikumar Kiran Sidhartha Ravikumar (Inactive)
            shadowbug Shadow Bug
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: