8209175: Handle 'B' character introduced in CLDR 33 JDK update for Burmese (my) locale
authornishjain
Tue, 26 Feb 2019 14:57:23 +0530
changeset 53920 7a72441858bb
parent 53919 554c3c813ad6
child 53921 a590b6107ab3
8209175: Handle 'B' character introduced in CLDR 33 JDK update for Burmese (my) locale Reviewed-by: naoto, rriggs
make/data/cldr/common/main/my.xml
make/jdk/src/classes/build/tools/cldrconverter/Bundle.java
test/jdk/java/text/Format/DateFormat/TestDayPeriodWithSDF.java
test/jdk/java/time/test/java/time/format/TestDayPeriodWithDTF.java
--- a/make/data/cldr/common/main/my.xml	Fri Feb 22 04:59:12 2019 -0800
+++ b/make/data/cldr/common/main/my.xml	Tue Feb 26 14:57:23 2019 +0530
@@ -1446,17 +1446,14 @@
 							<pattern>z HH:mm:ss</pattern>
 						</timeFormat>
 					</timeFormatLength>
-					<!--Pattern for medium and short replaced with CLDR 29's patterns 
-					    as character 'B' is currently not supported in SimpleDateFormat and java.time.DateTimeFormatter 
-					    classes. This is a restriction until JDK-8209175 is resolved.-->
 					<timeFormatLength type="medium">
 						<timeFormat>
-							<pattern>HH:mm:ss</pattern>
+							<pattern>B HH:mm:ss</pattern>
 						</timeFormat>
 					</timeFormatLength>
 					<timeFormatLength type="short">
 						<timeFormat>
-							<pattern>H:mm</pattern>
+							<pattern>B H:mm</pattern>
 						</timeFormat>
 					</timeFormatLength>
 				</timeFormats>
--- a/make/jdk/src/classes/build/tools/cldrconverter/Bundle.java	Fri Feb 22 04:59:12 2019 -0800
+++ b/make/jdk/src/classes/build/tools/cldrconverter/Bundle.java	Tue Feb 26 14:57:23 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -523,36 +523,46 @@
         for (String k : patternKeys) {
             if (myMap.containsKey(calendarPrefix + k)) {
                 int len = patternKeys.length;
-                List<String> rawPatterns = new ArrayList<>(len);
-                List<String> patterns = new ArrayList<>(len);
+                List<String> dateTimePatterns = new ArrayList<>(len);
+                List<String> sdfPatterns = new ArrayList<>(len);
                 for (int i = 0; i < len; i++) {
                     String key = calendarPrefix + patternKeys[i];
                     String pattern = (String) myMap.remove(key);
                     if (pattern == null) {
                         pattern = (String) parentsMap.remove(key);
                     }
-                    rawPatterns.add(i, pattern);
                     if (pattern != null) {
-                        patterns.add(i, translateDateFormatLetters(calendarType, pattern));
+                        // Perform date-time format pattern conversion which is
+                        // applicable to both SimpleDateFormat and j.t.f.DateTimeFormatter.
+                        // For example, character 'B' is mapped with 'a', as 'B' is not
+                        // supported in either SimpleDateFormat or j.t.f.DateTimeFormatter
+                        String transPattern = translateDateFormatLetters(calendarType, pattern, this::convertDateTimePatternLetter);
+                        dateTimePatterns.add(i, transPattern);
+                        // Additionally, perform SDF specific date-time format pattern conversion
+                        sdfPatterns.add(i, translateDateFormatLetters(calendarType, transPattern, this::convertSDFLetter));
                     } else {
-                        patterns.add(i, null);
+                        dateTimePatterns.add(i, null);
+                        sdfPatterns.add(i, null);
                     }
                 }
-                // If patterns is empty or has any nulls, discard patterns.
-                if (patterns.isEmpty()) {
+                // If empty, discard patterns
+                if (sdfPatterns.isEmpty()) {
                     return;
                 }
                 String key = calendarPrefix + name;
-                if (!rawPatterns.equals(patterns)) {
-                    myMap.put("java.time." + key, rawPatterns.toArray(new String[len]));
+
+                // If additional changes are made in the SDF specific conversion,
+                // keep the commonly converted patterns as java.time patterns
+                if (!dateTimePatterns.equals(sdfPatterns)) {
+                    myMap.put("java.time." + key, dateTimePatterns.toArray(String[]::new));
                 }
-                myMap.put(key, patterns.toArray(new String[len]));
+                myMap.put(key, sdfPatterns.toArray(new String[len]));
                 break;
             }
         }
     }
 
-    private String translateDateFormatLetters(CalendarType calendarType, String cldrFormat) {
+    private String translateDateFormatLetters(CalendarType calendarType, String cldrFormat, ConvertDateTimeLetters converter) {
         String pattern = cldrFormat;
         int length = pattern.length();
         boolean inQuote = false;
@@ -571,7 +581,7 @@
                     if (nextc == '\'') {
                         i++;
                         if (count != 0) {
-                            convert(calendarType, lastLetter, count, jrePattern);
+                            converter.convert(calendarType, lastLetter, count, jrePattern);
                             lastLetter = 0;
                             count = 0;
                         }
@@ -581,7 +591,7 @@
                 }
                 if (!inQuote) {
                     if (count != 0) {
-                        convert(calendarType, lastLetter, count, jrePattern);
+                        converter.convert(calendarType, lastLetter, count, jrePattern);
                         lastLetter = 0;
                         count = 0;
                     }
@@ -598,7 +608,7 @@
             }
             if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
                 if (count != 0) {
-                    convert(calendarType, lastLetter, count, jrePattern);
+                    converter.convert(calendarType, lastLetter, count, jrePattern);
                     lastLetter = 0;
                     count = 0;
                 }
@@ -611,7 +621,7 @@
                 count++;
                 continue;
             }
-            convert(calendarType, lastLetter, count, jrePattern);
+            converter.convert(calendarType, lastLetter, count, jrePattern);
             lastLetter = c;
             count = 1;
         }
@@ -621,7 +631,7 @@
         }
 
         if (count != 0) {
-            convert(calendarType, lastLetter, count, jrePattern);
+            converter.convert(calendarType, lastLetter, count, jrePattern);
         }
         if (cldrFormat.contentEquals(jrePattern)) {
             return cldrFormat;
@@ -676,71 +686,91 @@
         }
     }
 
-    private void convert(CalendarType calendarType, char cldrLetter, int count, StringBuilder sb) {
+    /**
+     * Perform a generic conversion of CLDR date-time format pattern letter based
+     * on the support given by the SimpleDateFormat and the j.t.f.DateTimeFormatter
+     * for date-time formatting.
+     */
+    private void convertDateTimePatternLetter(CalendarType calendarType, char cldrLetter, int count, StringBuilder sb) {
         switch (cldrLetter) {
-        case 'G':
-            if (calendarType != CalendarType.GREGORIAN) {
-                // Adjust the number of 'G's for JRE SimpleDateFormat
-                if (count == 5) {
-                    // CLDR narrow -> JRE short
-                    count = 1;
-                } else if (count == 1) {
-                    // CLDR abbr -> JRE long
-                    count = 4;
-                }
-            }
-            appendN(cldrLetter, count, sb);
-            break;
-
-        // TODO: support 'c' and 'e' in JRE SimpleDateFormat
-        // Use 'u' and 'E' for now.
-        case 'c':
-        case 'e':
-            switch (count) {
-            case 1:
-                sb.append('u');
+            case 'u':
+                // Change cldr letter 'u' to 'y', as 'u' is interpreted as
+                // "Extended year (numeric)" in CLDR/LDML,
+                // which is not supported in SimpleDateFormat and
+                // j.t.f.DateTimeFormatter, so it is replaced with 'y'
+                // as the best approximation
+                appendN('y', count, sb);
                 break;
-            case 3:
-            case 4:
-                appendN('E', count, sb);
+            case 'B':
+                // 'B' character (day period) is not supported by
+                // SimpleDateFormat and j.t.f.DateTimeFormatter,
+                // this is a workaround in which 'B' character
+                // appearing in CLDR date-time pattern is replaced
+                // with 'a' character and hence resolved with am/pm strings.
+                // This workaround is based on the the fallback mechanism
+                // specified in LDML spec for 'B' character, when a locale
+                // does not have data for day period ('B')
+                appendN('a', count, sb);
                 break;
-            case 5:
-                appendN('E', 3, sb);
+            default:
+                appendN(cldrLetter, count, sb);
                 break;
-            }
-            break;
 
-        case 'l':
-            // 'l' is deprecated as a pattern character. Should be ignored.
-            break;
+        }
+    }
 
-        case 'u':
-            // Use 'y' for now.
-            appendN('y', count, sb);
-            break;
-
-        case 'v':
-        case 'V':
-            appendN('z', count, sb);
-            break;
+    /**
+     * Perform a conversion of CLDR date-time format pattern letter which is
+     * specific to the SimpleDateFormat.
+     */
+    private void convertSDFLetter(CalendarType calendarType, char cldrLetter, int count, StringBuilder sb) {
+        switch (cldrLetter) {
+            case 'G':
+                if (calendarType != CalendarType.GREGORIAN) {
+                    // Adjust the number of 'G's for JRE SimpleDateFormat
+                    if (count == 5) {
+                        // CLDR narrow -> JRE short
+                        count = 1;
+                    } else if (count == 1) {
+                        // CLDR abbr -> JRE long
+                        count = 4;
+                    }
+                }
+                appendN(cldrLetter, count, sb);
+                break;
 
-        case 'Z':
-            if (count == 4 || count == 5) {
-                sb.append("XXX");
-            }
-            break;
+            // TODO: support 'c' and 'e' in JRE SimpleDateFormat
+            // Use 'u' and 'E' for now.
+            case 'c':
+            case 'e':
+                switch (count) {
+                    case 1:
+                        sb.append('u');
+                        break;
+                    case 3:
+                    case 4:
+                        appendN('E', count, sb);
+                        break;
+                    case 5:
+                        appendN('E', 3, sb);
+                        break;
+                }
+                break;
 
-        case 'U':
-        case 'q':
-        case 'Q':
-        case 'g':
-        case 'j':
-        case 'A':
-            throw new InternalError(String.format("Unsupported letter: '%c', count=%d, id=%s%n",
-                                                  cldrLetter, count, id));
-        default:
-            appendN(cldrLetter, count, sb);
-            break;
+            case 'v':
+            case 'V':
+                appendN('z', count, sb);
+                break;
+
+            case 'Z':
+                if (count == 4 || count == 5) {
+                    sb.append("XXX");
+                }
+                break;
+
+            default:
+                appendN(cldrLetter, count, sb);
+                break;
         }
     }
 
@@ -758,4 +788,9 @@
         }
         return false;
     }
+
+    @FunctionalInterface
+    private interface ConvertDateTimeLetters {
+        void convert(CalendarType calendarType, char cldrLetter, int count, StringBuilder sb);
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/text/Format/DateFormat/TestDayPeriodWithSDF.java	Tue Feb 26 14:57:23 2019 +0530
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ * @test
+ * @bug 8209175
+ * @summary Checks the 'B' character added in the CLDR date-time patterns is
+ *          getting resolved with 'a' character (am/pm strings) for burmese locale.
+ *          This test case assumes that the 'B' character is added in CLDRv33 update
+ *          for burmese locale in the time patterns. Since it is not supported by
+ *          SimpleDateFormat it is replaced with the 'a' while CLDR resource
+ *          conversion.
+ * @modules jdk.localedata
+ * @run testng/othervm TestDayPeriodWithSDF
+ */
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+import java.text.DateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+
+public class TestDayPeriodWithSDF {
+
+    private static final Locale BURMESE = new Locale("my");
+    private static final DateFormat FORMAT_SHORT_BURMESE = DateFormat.getTimeInstance(DateFormat.SHORT, BURMESE);
+    private static final DateFormat FORMAT_MEDIUM_BURMESE = DateFormat.getTimeInstance(DateFormat.MEDIUM, BURMESE);
+
+    private static final Date DATE_AM = new GregorianCalendar(2019, Calendar.FEBRUARY, 14, 10, 10, 10).getTime();
+    private static final Date DATE_PM = new GregorianCalendar(2019, Calendar.FEBRUARY, 14, 12, 12, 12).getTime();
+
+    @DataProvider(name = "timePatternData")
+    Object[][] timePatternData() {
+        return new Object[][] {
+                {FORMAT_SHORT_BURMESE, DATE_AM, "\u1014\u1036\u1014\u1000\u103A \u1041\u1040:\u1041\u1040"},
+                {FORMAT_SHORT_BURMESE, DATE_PM, "\u100A\u1014\u1031 \u1041\u1042:\u1041\u1042"},
+                {FORMAT_MEDIUM_BURMESE, DATE_AM, "\u1014\u1036\u1014\u1000\u103A \u1041\u1040:\u1041\u1040:\u1041\u1040"},
+                {FORMAT_MEDIUM_BURMESE, DATE_PM, "\u100A\u1014\u1031 \u1041\u1042:\u1041\u1042:\u1041\u1042"},
+        };
+    }
+
+    @Test(dataProvider = "timePatternData")
+    public void testTimePattern(DateFormat format, Date date, String expected) {
+        String actual = format.format(date);
+        assertEquals(actual, expected);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/time/test/java/time/format/TestDayPeriodWithDTF.java	Tue Feb 26 14:57:23 2019 +0530
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ * @test
+ * @bug 8209175
+ * @summary Checks the 'B' character added in the CLDR date-time patterns is
+ *          getting resolved with 'a' character (am/pm strings) for burmese locale.
+ *          This test case assumes that the 'B' character is added in CLDRv33 update
+ *          for burmese locale in the time patterns. Since it is not supported by
+ *          DateTimeFormatter it is replaced with the 'a' while CLDR resource
+ *          conversion.
+ * @modules jdk.localedata
+ */
+package test.java.time.format;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.FormatStyle;
+import java.util.Locale;
+
+@Test
+public class TestDayPeriodWithDTF {
+
+    private static final Locale BURMESE = new Locale("my");
+
+    private static final DateTimeFormatter FORMAT_SHORT_BURMESE = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(BURMESE);
+    private static final DateTimeFormatter FORMAT_MEDIUM_BURMESE = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM).withLocale(BURMESE);
+
+    private static final LocalTime LOCAL_TIME_AM = LocalTime.of(10, 10, 10);
+    private static final LocalTime LOCAL_TIME_PM = LocalTime.of(12, 12, 12);
+
+    @DataProvider(name = "timePatternData")
+    Object[][] timePatternData() {
+        return new Object[][] {
+                {FORMAT_SHORT_BURMESE, LOCAL_TIME_AM, "\u1014\u1036\u1014\u1000\u103A 10:10"},
+                {FORMAT_SHORT_BURMESE, LOCAL_TIME_PM, "\u100A\u1014\u1031 12:12"},
+                {FORMAT_MEDIUM_BURMESE, LOCAL_TIME_AM, "\u1014\u1036\u1014\u1000\u103A 10:10:10"},
+                {FORMAT_MEDIUM_BURMESE, LOCAL_TIME_PM, "\u100A\u1014\u1031 12:12:12"},
+        };
+    }
+
+    @Test(dataProvider = "timePatternData")
+    public void testTimePattern(DateTimeFormatter formatter, LocalTime time, String expected) {
+        String actual = formatter.format(time);
+        assertEquals(actual, expected);
+    }
+
+}