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