1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.util.locale.provider;
  26 
  27 import static java.util.Calendar.*;
  28 import java.util.Comparator;
  29 import java.util.Locale;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import java.util.TreeMap;
  33 import java.util.spi.CalendarNameProvider;
  34 import sun.util.calendar.CalendarSystem;
  35 import sun.util.calendar.Era;
  36 
  37 /**
  38  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
  39  * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
  40  *
  41  * @author Masayoshi Okutsu
  42  * @author Naoto Sato
  43  */
  44 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
  45     private final LocaleProviderAdapter.Type type;
  46     private final Set<String> langtags;
  47 
  48     public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  49         this.type = type;
  50         this.langtags = langtags;
  51     }
  52 
  53     @Override
  54     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
  55         return getDisplayNameImpl(calendarType, field, value, style, locale, false);
  56     }
  57 
  58     public String getJavaTimeDisplayName(String calendarType, int field, int value, int style, Locale locale) {
  59         return getDisplayNameImpl(calendarType, field, value, style, locale, true);
  60     }
  61 
  62     public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean javatime) {
  63         String name = null;
  64         String key = getResourceKey(calendarType, field, style, javatime);
  65         if (key != null) {
  66             LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);
  67             String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
  68             if (strings != null && strings.length > 0) {
  69                 if (field == DAY_OF_WEEK || field == YEAR) {
  70                     --value;
  71                 }
  72                 if (value < 0 || value > strings.length) {
  73                     return null;
  74                 } else if (value == strings.length) {
  75                     if (field == ERA && "japanese".equals(calendarType)) {
  76                         // get the supplemental era, if any, specified through
  77                         // the property "jdk.calendar.japanese.supplemental.era"
  78                         // which is always the last element.
  79                         Era[] jeras = CalendarSystem.forName("japanese").getEras();
  80                         if (jeras.length == value) {
  81                             Era supEra = jeras[value - 1]; // 0-based index
  82                             return style == LONG ?
  83                                 supEra.getName() :
  84                                 supEra.getAbbreviation();
  85                         }
  86                     }
  87                     return null;
  88                 }
  89                 name = strings[value];
  90                 // If name is empty in standalone, try its `format' style.
  91                 if (name.length() == 0
  92                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  93                             || style == NARROW_STANDALONE)) {
  94                     name = getDisplayName(calendarType, field, value,
  95                                           getBaseStyle(style),
  96                                           locale);
  97                 }
  98             }
  99         }
 100         return name;
 101     }
 102 
 103     private static int[] REST_OF_STYLES = {
 104         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
 105         NARROW_FORMAT, NARROW_STANDALONE
 106     };
 107 
 108     @Override
 109     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
 110         Map<String, Integer> names;
 111         if (style == ALL_STYLES) {
 112             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false);
 113             for (int st : REST_OF_STYLES) {
 114                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false));
 115             }
 116         } else {
 117             // specific style
 118             names = getDisplayNamesImpl(calendarType, field, style, locale, false);
 119         }
 120         return names.isEmpty() ? null : names;
 121     }
 122 
 123     // NOTE: This method should be used ONLY BY JSR 310 classes.
 124     public Map<String, Integer> getJavaTimeDisplayNames(String calendarType, int field, int style, Locale locale) {
 125         Map<String, Integer> names;
 126         names = getDisplayNamesImpl(calendarType, field, style, locale, true);
 127         return names.isEmpty() ? null : names;
 128     }
 129 
 130     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
 131                                                      int style, Locale locale, boolean javatime) {
 132         String key = getResourceKey(calendarType, field, style, javatime);
 133         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
 134         if (key != null) {
 135             LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale);
 136             String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key);
 137             if (strings != null) {
 138                 if (!hasDuplicates(strings)) {
 139                     if (field == YEAR) {
 140                         if (strings.length > 0) {
 141                             map.put(strings[0], 1);
 142                         }
 143                     } else {
 144                         int base = (field == DAY_OF_WEEK) ? 1 : 0;
 145                         for (int i = 0; i < strings.length; i++) {
 146                             String name = strings[i];
 147                             // Ignore any empty string (some standalone month names
 148                             // are not defined)
 149                             if (name.length() == 0) {
 150                                 continue;
 151                             }
 152                             map.put(name, base + i);
 153                         }
 154                     }
 155                 }
 156             }
 157         }
 158         return map;
 159     }
 160 
 161     private int getBaseStyle(int style) {
 162         return style & ~(SHORT_STANDALONE - SHORT_FORMAT);
 163     }
 164 
 165     /**
 166      * Comparator implementation for TreeMap which iterates keys from longest
 167      * to shortest.
 168      */
 169     private static class LengthBasedComparator implements Comparator<String> {
 170         private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();
 171 
 172         private LengthBasedComparator() {
 173         }
 174 
 175         @Override
 176         public int compare(String o1, String o2) {
 177             int n = o2.length() - o1.length();
 178             return (n == 0) ? o1.compareTo(o2) : n;
 179         }
 180     }
 181 
 182     @Override
 183     public Locale[] getAvailableLocales() {
 184         return LocaleProviderAdapter.toLocaleArray(langtags);
 185     }
 186 
 187     @Override
 188     public boolean isSupportedLocale(Locale locale) {
 189         if (Locale.ROOT.equals(locale)) {
 190             return true;
 191         }
 192         String calendarType = null;
 193         if (locale.hasExtensions()) {
 194             calendarType = locale.getUnicodeLocaleType("ca");
 195             locale = locale.stripExtensions();
 196         }
 197 
 198         if (calendarType != null) {
 199             switch (calendarType) {
 200             case "buddhist":
 201             case "japanese":
 202             case "gregory":
 203             case "islamic":
 204             case "roc":
 205                 break;
 206             default:
 207                 // Unknown calendar type
 208                 return false;
 209             }
 210         }
 211         if (langtags.contains(locale.toLanguageTag())) {
 212             return true;
 213         }
 214         if (type == LocaleProviderAdapter.Type.JRE) {
 215             String oldname = locale.toString().replace('_', '-');
 216             return langtags.contains(oldname);
 217         }
 218         return false;
 219     }
 220 
 221     @Override
 222     public Set<String> getAvailableLanguageTags() {
 223         return langtags;
 224     }
 225 
 226     private boolean hasDuplicates(String[] strings) {
 227         int len = strings.length;
 228         for (int i = 0; i < len - 1; i++) {
 229             String a = strings[i];
 230             if (a != null) {
 231                 for (int j = i + 1; j < len; j++) {
 232                     if (a.equals(strings[j]))  {
 233                         return true;
 234                     }
 235                 }
 236             }
 237         }
 238         return false;
 239     }
 240 
 241     private String getResourceKey(String type, int field, int style, boolean javatime) {
 242         int baseStyle = getBaseStyle(style);
 243         boolean isStandalone = (style != baseStyle);
 244 
 245         if ("gregory".equals(type)) {
 246             type = null;
 247         }
 248         boolean isNarrow = (baseStyle == NARROW_FORMAT);
 249         StringBuilder key = new StringBuilder();
 250         // If javatime is true, use prefix "java.time.".
 251         if (javatime) {
 252             key.append("java.time.");
 253         }
 254         switch (field) {
 255         case ERA:
 256             if (type != null) {
 257                 key.append(type).append('.');
 258             }
 259             if (isNarrow) {
 260                 key.append("narrow.");
 261             } else {
 262                 // JRE and CLDR use different resource key conventions
 263                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
 264                 // abbreviations while other getShort*() return abbreviations.)
 265                 if (this.type == LocaleProviderAdapter.Type.JRE) {
 266                     if (javatime) {
 267                         if (baseStyle == LONG) {
 268                             key.append("long.");
 269                         }
 270                     }
 271                     if (baseStyle == SHORT) {
 272                         key.append("short.");
 273                     }
 274                 } else { // this.type == LocaleProviderAdapter.Type.CLDR
 275                     if (baseStyle == LONG) {
 276                         key.append("long.");
 277                     }
 278                 }
 279             }
 280             key.append("Eras");
 281             break;
 282 
 283         case YEAR:
 284             if (!isNarrow) {
 285                 key.append(type).append(".FirstYear");
 286             }
 287             break;
 288 
 289         case MONTH:
 290             if ("islamic".equals(type)) {
 291                 key.append(type).append('.');
 292             }
 293             if (isStandalone) {
 294                 key.append("standalone.");
 295             }
 296             key.append("Month").append(toStyleName(baseStyle));
 297             break;
 298 
 299         case DAY_OF_WEEK:
 300             // support standalone narrow day names
 301             if (isStandalone && isNarrow) {
 302                 key.append("standalone.");
 303             }
 304             key.append("Day").append(toStyleName(baseStyle));
 305             break;
 306 
 307         case AM_PM:
 308             if (isNarrow) {
 309                 key.append("narrow.");
 310             }
 311             key.append("AmPmMarkers");
 312             break;
 313         }
 314         return key.length() > 0 ? key.toString() : null;
 315     }
 316 
 317     private String toStyleName(int baseStyle) {
 318         switch (baseStyle) {
 319         case SHORT:
 320             return "Abbreviations";
 321         case NARROW_FORMAT:
 322             return "Narrows";
 323         }
 324         return "Names";
 325     }
 326 }