1 /* 2 * Copyright (c) 2012, 2014, 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 45 public CalendarNameProviderImpl(LocaleProviderAdapter.Type type) { 46 this.type = type; 47 } 48 49 @Override 50 public String getDisplayName(String calendarType, int field, int value, int style, Locale locale) { 51 return getDisplayNameImpl(calendarType, field, value, style, locale, false); 52 } 53 54 public String getJavaTimeDisplayName(String calendarType, int field, int value, int style, Locale locale) { 55 return getDisplayNameImpl(calendarType, field, value, style, locale, true); 56 } 57 58 public String getDisplayNameImpl(String calendarType, int field, int value, int style, Locale locale, boolean javatime) { 59 String name = null; 60 String key = getResourceKey(calendarType, field, style, javatime); 61 if (key != null) { 62 LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale); 63 String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.getCalendarNames(key); 64 if (strings != null && strings.length > 0) { 65 if (field == DAY_OF_WEEK || field == YEAR) { 66 --value; 67 } 68 if (value < 0 || value >= strings.length) { 69 return null; 70 } 71 name = strings[value]; 72 // If name is empty in standalone, try its `format' style. 73 if (name.length() == 0 74 && (style == SHORT_STANDALONE || style == LONG_STANDALONE 75 || style == NARROW_STANDALONE)) { 76 name = getDisplayName(calendarType, field, value, 77 getBaseStyle(style), 78 locale); 79 } 80 } 81 } 82 return name; 83 } 84 85 private static final int[] REST_OF_STYLES = { 86 SHORT_STANDALONE, LONG_FORMAT, LONG_STANDALONE, 87 NARROW_FORMAT, NARROW_STANDALONE 88 }; 89 90 @Override 91 public Map<String, Integer> getDisplayNames(String calendarType, int field, int style, Locale locale) { 92 Map<String, Integer> names; 93 if (style == ALL_STYLES) { 94 names = getDisplayNamesImpl(calendarType, field, SHORT_FORMAT, locale, false); 95 for (int st : REST_OF_STYLES) { 96 names.putAll(getDisplayNamesImpl(calendarType, field, st, locale, false)); 97 } 98 } else { 99 // specific style 100 names = getDisplayNamesImpl(calendarType, field, style, locale, false); 101 } 102 return names.isEmpty() ? null : names; 103 } 104 105 // NOTE: This method should be used ONLY BY JSR 310 classes. 106 public Map<String, Integer> getJavaTimeDisplayNames(String calendarType, int field, int style, Locale locale) { 107 Map<String, Integer> names; 108 names = getDisplayNamesImpl(calendarType, field, style, locale, true); 109 return names.isEmpty() ? null : names; 110 } 111 112 private Map<String, Integer> getDisplayNamesImpl(String calendarType, int field, 113 int style, Locale locale, boolean javatime) { 114 String key = getResourceKey(calendarType, field, style, javatime); 115 Map<String, Integer> map = new TreeMap<>(LengthBasedComparator.INSTANCE); 116 if (key != null) { 117 LocaleResources lr = LocaleProviderAdapter.forType(type).getLocaleResources(locale); 118 String[] strings = javatime ? lr.getJavaTimeNames(key) : lr.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(DateFormatProviderImpl.getLangTags(type)); 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 (DateFormatProviderImpl.getLangTags(type).contains(locale.toLanguageTag())) { 194 return true; 195 } 196 if (type == LocaleProviderAdapter.Type.JRE) { 197 String oldname = locale.toString().replace('_', '-'); 198 return DateFormatProviderImpl.getLangTags(type).contains(oldname); 199 } 200 return false; 201 } 202 203 @Override 204 public Set<String> getAvailableLanguageTags() { 205 return DateFormatProviderImpl.getLangTags(type); 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 javatime) { 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 javatime is true, use prefix "java.time.". 233 if (javatime) { 234 key.append("java.time."); 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 (javatime) { 249 if (baseStyle == LONG) { 250 key.append("long."); 251 } 252 } 253 if (baseStyle == SHORT) { 254 key.append("short."); 255 } 256 } else { // this.type == LocaleProviderAdapter.Type.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 }