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

calling LSSerializer.writeToString on DOM Text node with white space causes NPE

    XMLWordPrintable

Details

    • b71
    • x86
    • windows_xp

    Description

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      Calling LSSerializer.writeToString on a DOM Text node which contains only white space causes a org.w3c.dom.ls.LSException: java.lang.NullPointerException in at com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl.writeToString(Unknown Source) which I think is a bug as the serializer should be able to deal with all types of DOM nodes.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the following test case:

      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;

      import org.w3c.dom.Document;
      import org.w3c.dom.Node;
      import org.w3c.dom.Text;

      import org.w3c.dom.ls.DOMImplementationLS;
      import org.w3c.dom.ls.LSSerializer;

      public class Test2005032102 {
        public static void main (String[] args) {
          try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            
            Document xmlDocument = documentBuilder.newDocument();
            
            String whitespace = "\r\n ";
            Text textNode = xmlDocument.createTextNode(whitespace);
            
            System.out.println("original text is:\r\n\"" + whitespace + "\"");
            
            String outerXML = getOuterXML(textNode);
            System.out.println("OuterXML is:\r\n\"" + outerXML + "\"");
            
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }
        
        public static String getOuterXML (Node node) {
          DOMImplementationLS domImplementation = (DOMImplementationLS) node.getOwnerDocument().getImplementation();
          LSSerializer lsSerializer = domImplementation.createLSSerializer();
          if (!(node instanceof Document)) {
            lsSerializer.getDomConfig().setParameter("xml-declaration", false);
          }
          return lsSerializer.writeToString(node);
        }

      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program should run without throwing an exception, it should output the serialization of the Text node where the serialization of a Text node is its text content, in this case the white space.
      ACTUAL -
      output on the console is:

      original text is:
      "
          "
      org.w3c.dom.ls.LSException: java.lang.NullPointerException
              at com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl.writeToString(Unknown Source)
              at Test2005032102.getOuterXML(Test2005032102.java:40)
              at Test2005032102.main(Test2005032102.java:25)


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      org.w3c.dom.ls.LSException: java.lang.NullPointerException
              at com.sun.org.apache.xml.internal.serialize.DOMSerializerImpl.writeToString(Unknown Source)
              at Test2005032102.getOuterXML(Test2005032102.java:40)
              at Test2005032102.main(Test2005032102.java:25)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;

      import org.w3c.dom.Document;
      import org.w3c.dom.Node;
      import org.w3c.dom.Text;

      import org.w3c.dom.ls.DOMImplementationLS;
      import org.w3c.dom.ls.LSSerializer;

      public class Test2005032102 {
        public static void main (String[] args) {
          try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            
            Document xmlDocument = documentBuilder.newDocument();
            
            String whitespace = "\r\n ";
            Text textNode = xmlDocument.createTextNode(whitespace);
            
            System.out.println("original text is:\r\n\"" + whitespace + "\"");
            
            String outerXML = getOuterXML(textNode);
            System.out.println("OuterXML is:\r\n\"" + outerXML + "\"");
            
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }
        
        public static String getOuterXML (Node node) {
          DOMImplementationLS domImplementation = (DOMImplementationLS) node.getOwnerDocument().getImplementation();
          LSSerializer lsSerializer = domImplementation.createLSSerializer();
          if (!(node instanceof Document)) {
            lsSerializer.getDomConfig().setParameter("xml-declaration", false);
          }
          return lsSerializer.writeToString(node);
        }

      }
      ---------- END SOURCE ----------

      Attachments

        Activity

          People

            sreddysunw Sunitha Reddy (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: