1 /*
   2  * Copyright (c) 2012, 2013, 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 
  35 /**
  36  * Concrete implementation of the  {@link java.util.spi.CalendarDataProvider
  37  * CalendarDataProvider} class for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Masayoshi Okutsu
  40  * @author Naoto Sato
  41  */
  42 public class CalendarNameProviderImpl extends CalendarNameProvider implements AvailableLanguageTags {
  43     private final LocaleProviderAdapter.Type type;
  44     private final Set<String> langtags;
  45 
  46     public CalendarNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  47         this.type = type;
  48         this.langtags = langtags;
  49     }
  50 
  51     @Override
  52     public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) {
  53         String name = null;
  54         String key = getResourceKey(calendarType, field, style);
  55         if (key != null) {
  56             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
  57             if (strings != null && strings.length > 0) {
  58                 if (field == DAY_OF_WEEK || field == YEAR) {
  59                     --value;
  60                 }
  61                 name = strings[value];
  62                 // If name is empty in standalone, try its `format' style.
  63                 if (name.length() == 0
  64                         && (style == SHORT_STANDALONE || style == LONG_STANDALONE
  65                             || style == NARROW_STANDALONE)) {
  66                     name = getDisplayName(calendarType, field, value,
  67                                           getBaseStyle(style),
  68                                           locale);
  69                 }
  70             }
  71         }
  72         return name;
  73     }
  74 
  75     private static int[] REST_OF_STYLES = {
  76         SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE,
  77         NARROW_FORMAT, NARROW_STANDALONE
  78     };
  79     @Override
  80     public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) {
  81         Map<String, Integer> names;
  82         if (style == ALL_STYLES) {
  83             names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale);
  84             for (int st : REST_OF_STYLES) {
  85                 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale));
  86             }
  87         } else {
  88             // specific style
  89             names = getDisplayNamesImpl(calendarType, field, style, locale);
  90         }
  91         return names.isEmpty() ? null : names;
  92     }
  93 
  94     private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field,
  95                                                      int style, Locale locale) {
  96         String key = getResourceKey(calendarType, field, style);
  97         Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE);
  98         if (key != null) {
  99             String[] strings = LocaleProviderAdapter.forType(type).getLocaleResources(locale).getCalendarNames(key);
 100             if (strings != null) {
 101                 if (!hasDuplicates(strings)) {
 102                     if (field == YEAR) {
 103                         if (strings.length > 0) {
 104                             map.put(strings[0], 1);
 105                         }
 106                     } else {
 107                         int base = (field == DAY_OF_WEEK) ? 1 : 0;
 108                         for (int i = 0; i < strings.length; i++) {
 109                             String name = strings[i];
 110                             // Ignore any empty string (some standalone month names
 111                             // are not defined)
 112                             if (name.length() == 0) {
 113                                 continue;
 114                             }
 115                             map.put(name, base + i);
 116                         }
 117                     }
 118                 }
 119             }
 120         }
 121         return map;
 122     }
 123 
 124     private int getBaseStyle(int style) {
 125         return style & ~(SHORT_STANDALONE - SHORT_FORMAT);
 126     }
 127 
 128     /**
 129      * Comparator implementation for TreeMap which iterates keys from longest
 130      * to shortest.
 131      */
 132     private static class LengthBasedComparator implements Comparator<String> {
 133         private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();
 134 
 135         private LengthBasedComparator() {
 136         }
 137 
 138         @Override
 139         public int compare(String o1, String o2) {
 140             int n = o2.length() - o1.length();
 141             return (n == 0) ? o1.compareTo(o2) : n;
 142         }
 143     }
 144 
 145     @Override
 146     public Locale[] getAvailableLocales() {
 147         return LocaleProviderAdapter.toLocaleArray(langtags);
 148     }
 149 
 150     @Override
 151     public boolean isSupportedLocale(Locale locale) {
 152         if (Locale.ROOT.equals(locale)) {
 153             return true;
 154         }
 155         String calendarType = null;
 156         if (locale.hasExtensions()) {
 157             calendarType = locale.getUnicodeLocaleType("ca");
 158             locale = locale.stripExtensions();
 159         }
 160 
 161         if (calendarType != null) {
 162             switch (calendarType) {
 163             case "buddhist":
 164             case "japanese":
 165             case "gregory":
 166             case "islamic":
 167             case "roc":
 168                 break;
 169             default:
 170                 // Unknown calendar type
 171                 return false;
 172             }
 173         }
 174         if (langtags.contains(locale.toLanguageTag())) {
 175             return true;
 176         }
 177         if (type == LocaleProviderAdapter.Type.JRE) {
 178             String oldname = locale.toString().replace('_', '-');
 179             return langtags.contains(oldname);
 180         }
 181         return false;
 182     }
 183 
 184     @Override
 185     public Set<String> getAvailableLanguageTags() {
 186         return langtags;
 187     }
 188 
 189     private boolean hasDuplicates(String[] strings) {
 190         int len = strings.length;
 191         for (int i = 0; i < len - 1; i++) {
 192             String a = strings[i];
 193             if (a != null) {
 194                 for (int j = i + 1; j < len; j++) {
 195                     if (a.equals(strings[j]))  {
 196                         return true;
 197                     }
 198                 }
 199             }
 200         }
 201         return false;
 202     }
 203 
 204     private String getResourceKey(String type, int field, int style) {
 205         int baseStyle = getBaseStyle(style);
 206         boolean isStandalone = (style != baseStyle);
 207 
 208         if ("gregory".equals(type)) {
 209             type = null;
 210         }
 211         boolean isNarrow = (baseStyle == NARROW_FORMAT);
 212         StringBuilder key = new StringBuilder();
 213         switch (field) {
 214         case ERA:
 215             if (type != null) {
 216                 key.append(type).append('.');
 217             }
 218             if (isNarrow) {
 219                 key.append("narrow.");
 220             } else {
 221                 // JRE and CLDR use different resource key conventions
 222                 // due to historical reasons. (JRE DateFormatSymbols.getEras returns
 223                 // abbreviations while other getShort*() return abbreviations.)
 224                 if (this.type == LocaleProviderAdapter.Type.JRE) {
 225                     if (baseStyle == SHORT) {
 226                         key.append("short.");
 227                     }
 228                 } else { // CLDR
 229                     if (baseStyle == LONG) {
 230                         key.append("long.");
 231                     }
 232                 }
 233             }
 234             key.append("Eras");
 235             break;
 236 
 237         case YEAR:
 238             if (!isNarrow) {
 239                 key.append(type).append(".FirstYear");
 240             }
 241             break;
 242 
 243         case MONTH:
 244             if ("islamic".equals(type)) {
 245                 key.append(type).append('.');
 246             }
 247             if (isStandalone) {
 248                 key.append("standalone.");
 249             }
 250             key.append("Month").append(toStyleName(baseStyle));
 251             break;
 252 
 253         case DAY_OF_WEEK:
 254             // support standalone narrow day names
 255             if (isStandalone && isNarrow) {
 256                 key.append("standalone.");
 257             }
 258             key.append("Day").append(toStyleName(baseStyle));
 259             break;
 260 
 261         case AM_PM:
 262             if (isNarrow) {
 263                 key.append("narrow.");
 264             }
 265             key.append("AmPmMarkers");
 266             break;
 267         }
 268         return key.length() > 0 ? key.toString() : null;
 269     }
 270 
 271     private String toStyleName(int baseStyle) {
 272         switch (baseStyle) {
 273         case SHORT:
 274             return "Abbreviations";
 275         case NARROW_FORMAT:
 276             return "Narrows";
 277         }
 278         return "Names";
 279     }
 280 }