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