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

[Fmt-Ch] Recursive MessageFormats in ChoiceFormats ignore indicated subformats

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 23
    • 5.0, 22
    • core-libs
    • b02
    • x86
    • linux

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


      ADDITIONAL OS VERSION INFORMATION :
      Linux sub 2.6.8-2-k7 #1 Mon Jan 24 03:29:52 EST 2005 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Both the documentation for ChoiceFormat and the java tutorial shows
      examples similar to the following one:

        double[] filelimits = {0,1,2};
        String[] filepart = {"are no files","is one file","are {2} files"};
        ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
        Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
        MessageFormat pattform = new MessageFormat("There {0} on {1}");
        pattform.setFormats(testFormats);
        Object[] testArgs = {null, "ADisk", null};
        for (int i = 0; i < 4; ++i) {
             testArgs[0] = new Integer(i);
             testArgs[2] = testArgs[0];
             System.out.println(pattform.format(testArgs));
         }

      The intention is that NumberFormat.getInstance() (in testFormats[2]) act
      as a format for "are {2} files". But it is ignored. In fact, from the documentation
      of MessageFormat, if subformat is a ChoiceFormat, the action is:

        subformat.format(argument).indexOf('{') >= 0 ?
        (new MessageFormat(subformat.format(argument),
         getLocale())).format(argument) : subformat.format(argument)

      And this is what is really done in the implementation. So NumberFormat.getInstance() in the example above is ignored, even when passed through the setFormat(s)ByArgumentIndex methods.

      This situation is very confusing. One of documentation or implementation shoud be fixed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example-1dot1/ChoiceFormatDemo.java (from the java tutorial). Then change the last format in Format[] formats = {...} (line 46) to a DateFormat, for expample. Or just run the test case provided in "Source code for an executable test case".

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Oberve that nothing new happens. Even when the format is invalid for the argument you will get no errors.
      ACTUAL -
      For the unmodified demo:

      There are no files on XDisk.
      There is one file on XDisk.
      There are 2 files on XDisk.
      There are 3 files on XDisk.
      ....

      Using a DateFormat instead of the original one:

      the same




      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.text.*;


      class test
      {
         public static void main(String args[]) {
            System.out.println("First with NumberFormat:");
            doItWithFormat(NumberFormat.getInstance());
            System.out.println("Then with DateFormat:");
            doItWithFormat(DateFormat.getInstance());
            System.out.println("Oops, they seem the same");
         }

         private static void doItWithFormat(Format format) {
            double[] filelimits = {0,1,2};
            String[] filepart = { "are no files","is one file","are {2} files" };
            ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
            Format[] testFormats = { fileform, null, format };
            MessageFormat pattform = new MessageFormat("There {0} on {1}");
            pattform.setFormatsByArgumentIndex(testFormats);
            Object[] testArgs ={ null, "ADisk", null };
            for (int i = 0; i < 4; ++i) {
               testArgs[0] = new Integer(i);
               testArgs[2] = testArgs[0];
               System.out.println(pattform.format(testArgs));
            }
         }
      }

      ---------- END SOURCE ----------
      ###@###.### 2005-2-18 05:54:24 GMT

            jlu Justin Lu
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: