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

java.text.MessageFormat.parse incorrectly sets errorIndex

XMLWordPrintable

    • 1.2beta4
    • sparc
    • solaris_2.5
    • Verified



      Name: dfC67450 Date: 03/18/98



      Object[] java.text.MessageFormat.parse(String text, ParsePosition pp)
      incorrectly sets error index of ParsePosition parameter. It must set
      this index to the starting offset of the sub-patterns that the string is
      comparing with. But for some incorrectly formatted input strings
      it sets this index to -1 as well as for correctly formatted ones.

      Javadoc says about MessageFormat.parse(String, ParsePosition)
         * When the parse fails, use ParsePosition.getErrorIndex() to find out
         * where in the string did the parsing failed. The returned error
         * index is the starting offset of the sub-patterns that the string
         * is comparing with. For example, if the parsing string "AAA {0} BBB"
         * is comparing against the pattern "AAD {0} BBB", the error index is
         * 0.

      about ParsePosition.getErrorIndex() :
         * Retrieve the index at which an error occurred, or -1 if the error
         * index has not been set.

      Here is the test demonstrating the bug:

      -----------------TestMF.java------------------------
      import java.text.*;

      public class TestMF {
          public static void main (String args[]){
            MessageFormat mf = new MessageFormat("pattern");
            String texts[] = {"pattern", "pat", "1234"};
            System.out.println("pattern: \"" + mf.toPattern() + "\"");
            for (int i = 0; i < texts.length; i++) {
              ParsePosition pp = new ParsePosition(0);
              Object[] objs = mf.parse(texts[i], pp);
              System.out.print(" text for parsing: \"" + texts[i] + "\"");
              if (objs == null)
                System.out.println(" (incorrectly formatted string)");
              else
                System.out.println(" (correctly formatted string)");
              System.out.println(" error index: " + pp.getErrorIndex());
              System.out.println();
            }
          }
      }
      ---------Output from the test ---------------------
      pattern: "pattern"
        text for parsing: "pattern" (correctly formatted string)
          error index: -1
       
        text for parsing: "pat" (incorrectly formatted string)
          error index: -1
       
        text for parsing: "1234" (incorrectly formatted string)
          error index: -1
      --------------------------------------------------

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

            duke J. Duke
            dfazunensunw Dmitri Fazunenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: