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

DefaultHandler sending space between elements to characters(...)

XMLWordPrintable

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

      FULL PRODUCT VERSION :
      java version "1.6.0_02-ea"
      Java(TM) SE Runtime Environment (build 1.6.0_02-ea-b02)
      Java HotSpot(TM) Client VM (build 1.6.0_02-ea-b02, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux calmer 2.6.9-42.0.10.EL #1 Fri Feb 16 17:06:10 EST 2007 i686 i686 i386 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      I have a class that extends org.xml.sax.helpers.DefaultHandler. The callback method,
      public void characters(char[] ch, int start, int length) throws SAXException,
      had been properly receiving the data inside XML elements. It now also receives the whitespace BETWEEN elements. That is, the text formatting of the XML document influences the data rcvd here. 3 examples

      1.
      <foo>abc</foo><bar>123</bar>

      2.
      <foo>abc</foo> <bar>123</bar>

      3.
      <foo>abc</foo>
          <bar>123</bar>

      In example 1, i properly see "abc" & "123".

      In example 2, i see "abc", " " (two spaces), & "123".

      In example 3 i see "abc", "\n " (newline & 4 spaces), & "123".

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - Compile DefaultHandlerTest.java
      - put alphabet.xml (see below) in the same directory as DefaultHandler.class
      - Run the test case

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Should not have seen characters between XML elements.

      <alphabet>
        <name>"Latin"</name>
        <size>"26"</size>
        <letters>
          <vowel>"a"</vowel>
          <consonant>"b"</consonant>
          <consonant>"c"</consonant>
          <consonant>"d"</consonant>
          <vowel>"e"</vowel>
          <consonant>"f"</consonant>
          <consonant>"g"</consonant>
        </letters>
      </alphabet>
      ACTUAL -
      <alphabet>"
        "
        <name>"Latin"</name>"
        "
        <size>"26"</size>"
        "
        <letters>"
          "
          <vowel>"a"</vowel>"
          "
          <consonant>"b"</consonant>
          <consonant>"c"</consonant>" "
          <consonant>"d"</consonant>"
          "
          <vowel>"e"</vowel>"
          "
          <consonant>"f"</consonant>"
          "
          <consonant>"g"</consonant>"
        "
        </letters>"
      "
      </alphabet>

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.net.URL;

      import javax.xml.parsers.SAXParser;
      import javax.xml.parsers.SAXParserFactory;

      import org.xml.sax.Attributes;
      import org.xml.sax.SAXException;
      import org.xml.sax.helpers.DefaultHandler;

      /**
       * Demonstrates problem with callback to DefaultHandler's characters
       * method by SAXParser. The problem is new with java 1.6; it was
       * not a problem with java 1.5.0_07.
       */
      public class DefaultHandlerTest extends DefaultHandler
      {
        private String xmlFileName = "alphabet.xml";
        private int depth = 0;
        private int priorDepth = -1;

        public void characters(char[] ch,
                               int start,
                               int length)
          throws SAXException
        {
          String value = new String(ch, start, length);

          //The deactivated lines below represent a work-around
          //if (value==null || value.trim().length()==0)
          // return;

          System.out.print("\"");
          System.out.print(value);
          System.out.print("\"");
        }

        public void startElement(String uri,
                                 String localName,
                                 String qName,
                                 Attributes attributes)
          throws SAXException
        {
          System.out.println();
          pad();
          System.out.print("<" + qName + ">");
          priorDepth = depth;
          depth++;
        }

        public void endElement(String uri, String localName, String qName)
          throws SAXException
        {
          boolean newLine = (priorDepth >= depth);

          priorDepth = depth;
          depth--;
         
          if (newLine)
          {
            System.out.println();
            pad();
          }
          System.out.print("</" + qName + ">");
        }
       
        public void startDocument() throws SAXException
        {
          System.out.println("Starting XML Document " + xmlFileName);
        }
       
        public void endDocument() throws SAXException
        {
          System.out.println();
          System.out.println();
          System.out.println("Done with XML Document " + xmlFileName);
        }
       
        private void pad()
        {
          for (int p=0; p < 2*depth; p++)
            System.out.print(' ');
        }
       
        public static void main(String[] args) throws Exception
        {
          DefaultHandlerTest handler = new DefaultHandlerTest();
         
          URL xmlFile = handler.getClass().getResource(handler.xmlFileName);
         
          SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
         
          parser.parse(xmlFile.openStream(), handler);
        }
      }

      ==========================================================

      apphabet.xml, WHICH IS INPUT FOR JAVA PROGRAM
      ==========================================================
      <?xml version="1.0"?>

      <alphabet>
        <name>Latin</name>
        <size>26</size>
        <letters>
          <vowel>a</vowel>
          <consonant>b</consonant><consonant>c</consonant>
      <consonant>d</consonant>
          <vowel>e</vowel>
          <consonant>f</consonant>
          <consonant>g</consonant>
        </letters>
      </alphabet>
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      In my situation i detect that value.trim().length() == 0.

            nwalshsunw Norman Walsh (Inactive)
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: