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

Transformer produces invalid XML

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • xml

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux ronny 2.6.12-1-686 #1 Tue Sep 27 12:52:50 JST 2005 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Short background info: I am the author of Pauker (http://pauker.sourceforge.net/). It's a flashcard program that uses XML to store the lesson files. I got a bugreport from a user who pasted text from the clipboard to the flashcards. Result was that he could no longer open the lesson. After evaluating the problem I found out that the problem is that the Transformer produces invalid XML when it gets fed some "special" characters.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and execute the attached test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect to see no Exception.
      ACTUAL -
      I saw an org.xml.sax.SAXParseException.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      [Fatal Error] :2:48: Character reference "&#1" is an invalid XML character.
      org.xml.sax.SAXParseException: Character reference "&#1" is an invalid XML character.
              at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:264)
              at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
              at XMLTest.main(XMLTest.java:43)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.ByteArrayOutputStream;
      import java.io.StringReader;
      import javax.xml.parsers.*;
      import javax.xml.transform.*;
      import javax.xml.transform.dom.DOMSource;
      import javax.xml.transform.stream.StreamResult;
      import org.w3c.dom.*;
      import org.xml.sax.InputSource;

      public class XMLTest {
          
          public static void main(String[] args) {
              
              String string = new String("Via copy & paste you can insert \"\u0001\" into a JTextArea.");
              
              try {
                  // create document
                  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                  DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
                  Document document = documentBuilder.newDocument();
                  
                  // add text element
                  Element textElement = document.createElement("Text");
                  Text text = document.createTextNode(string);
                  textElement.appendChild(text);
                  document.appendChild(textElement);

                  // transform to XML
                  TransformerFactory tf = TransformerFactory.newInstance();
                  Transformer transformer = tf.newTransformer();
                  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                  DOMSource source = new DOMSource(document);
                  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                  StreamResult result = new StreamResult(outputStream);
                  transformer.transform(source, result);
                  String xmlString = outputStream.toString();
                  System.out.println("xmlString:\n" + xmlString);
                  
                  // try parsing
                  StringReader reader = new StringReader(xmlString);
                  InputSource inputSource = new InputSource(reader);
                  // triggers org.xml.sax.SAXParseException!
                  Document document2 = documentBuilder.parse(inputSource);
                  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }

      ---------- END SOURCE ----------

            jsuttorsunw Jeff Suttor (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: