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
26 package sun.util.locale.provider;
27
28 import static java.util.Calendar.*;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.spi.CalendarDataProvider;
32 import java.util.spi.CalendarNameProvider;
33
34 /**
35 * {@code CalendarDataUtility} is a utility class for calling the
36 * {@link CalendarDataProvider} methods.
37 *
38 * @author Masayoshi Okutsu
39 * @author Naoto Sato
40 */
41 public class CalendarDataUtility {
42 public static final String FIRST_DAY_OF_WEEK = "firstDayOfWeek";
43 public static final String MINIMAL_DAYS_IN_FIRST_WEEK = "minimalDaysInFirstWeek";
44
45 // No instantiation
46 private CalendarDataUtility() {
47 }
48
49 public static int retrieveFirstDayOfWeek(Locale locale) {
50 LocaleServiceProviderPool pool =
51 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
52 Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
53 locale, true, FIRST_DAY_OF_WEEK);
54 return (value != null && (value >= SUNDAY && value <= SATURDAY)) ? value : SUNDAY;
55 }
56
57 public static int retrieveMinimalDaysInFirstWeek(Locale locale) {
58 LocaleServiceProviderPool pool =
59 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
60 Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
61 locale, true, MINIMAL_DAYS_IN_FIRST_WEEK);
62 return (value != null && (value >= 1 && value <= 7)) ? value : 1;
63 }
64
65 public static String retrieveFieldValueName(String id, int field, int value, int style, Locale locale) {
66 LocaleServiceProviderPool pool =
67 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
68 return pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id),
69 field, value, style, false);
70 }
71
72 public static String retrieveJavaTimeFieldValueName(String id, int field, int value, int style, Locale locale) {
73 LocaleServiceProviderPool pool =
74 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
75 String name;
76 name = pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id),
77 field, value, style, true);
78 if (name == null) {
79 name = pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id),
80 field, value, style, false);
81 }
85 public static Map<String, Integer> retrieveFieldValueNames(String id, int field, int style, Locale locale) {
86 LocaleServiceProviderPool pool =
87 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
88 return pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale,
89 normalizeCalendarType(id), field, style, false);
90 }
91
92 public static Map<String, Integer> retrieveJavaTimeFieldValueNames(String id, int field, int style, Locale locale) {
93 LocaleServiceProviderPool pool =
94 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
95 Map<String, Integer> map;
96 map = pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale,
97 normalizeCalendarType(id), field, style, true);
98 if (map == null) {
99 map = pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale,
100 normalizeCalendarType(id), field, style, false);
101 }
102 return map;
103 }
104
105 static String normalizeCalendarType(String requestID) {
106 String type;
107 if (requestID.equals("gregorian") || requestID.equals("iso8601")) {
108 type = "gregory";
109 } else if (requestID.startsWith("islamic")) {
110 type = "islamic";
111 } else {
112 type = requestID;
113 }
114 return type;
115 }
116
117 /**
118 * Obtains a localized field value string from a CalendarDataProvider
119 * implementation.
120 */
121 private static class CalendarFieldValueNameGetter
122 implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider,
123 String> {
124 private static final CalendarFieldValueNameGetter INSTANCE =
|
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
26 package sun.util.locale.provider;
27
28 import static java.util.Calendar.*;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.spi.CalendarDataProvider;
33 import java.util.spi.CalendarNameProvider;
34
35 /**
36 * {@code CalendarDataUtility} is a utility class for calling the
37 * {@link CalendarDataProvider} methods.
38 *
39 * @author Masayoshi Okutsu
40 * @author Naoto Sato
41 */
42 public class CalendarDataUtility {
43 public static final String FIRST_DAY_OF_WEEK = "firstDayOfWeek";
44 public static final String MINIMAL_DAYS_IN_FIRST_WEEK = "minimalDaysInFirstWeek";
45
46 // No instantiation
47 private CalendarDataUtility() {
48 }
49
50 public static int retrieveFirstDayOfWeek(Locale locale) {
51 // Look for the Unicode Extension in the locale parameter
52 if (locale.hasExtensions()) {
53 String fw = locale.getUnicodeLocaleType("fw");
54 if (fw != null) {
55 switch (fw.toLowerCase(Locale.ROOT)) {
56 case "mon":
57 return MONDAY;
58 case "tue":
59 return TUESDAY;
60 case "wed":
61 return WEDNESDAY;
62 case "thu":
63 return THURSDAY;
64 case "fri":
65 return FRIDAY;
66 case "sat":
67 return SATURDAY;
68 case "sun":
69 return SUNDAY;
70 }
71 }
72 }
73
74 LocaleServiceProviderPool pool =
75 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
76 Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
77 findRegionOverride(locale).orElse(locale),
78 true, FIRST_DAY_OF_WEEK);
79 return (value != null && (value >= SUNDAY && value <= SATURDAY)) ? value : SUNDAY;
80 }
81
82 public static int retrieveMinimalDaysInFirstWeek(Locale locale) {
83 LocaleServiceProviderPool pool =
84 LocaleServiceProviderPool.getPool(CalendarDataProvider.class);
85 Integer value = pool.getLocalizedObject(CalendarWeekParameterGetter.INSTANCE,
86 findRegionOverride(locale).orElse(locale),
87 true, MINIMAL_DAYS_IN_FIRST_WEEK);
88 return (value != null && (value >= 1 && value <= 7)) ? value : 1;
89 }
90
91 public static String retrieveFieldValueName(String id, int field, int value, int style, Locale locale) {
92 LocaleServiceProviderPool pool =
93 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
94 return pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id),
95 field, value, style, false);
96 }
97
98 public static String retrieveJavaTimeFieldValueName(String id, int field, int value, int style, Locale locale) {
99 LocaleServiceProviderPool pool =
100 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
101 String name;
102 name = pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id),
103 field, value, style, true);
104 if (name == null) {
105 name = pool.getLocalizedObject(CalendarFieldValueNameGetter.INSTANCE, locale, normalizeCalendarType(id),
106 field, value, style, false);
107 }
111 public static Map<String, Integer> retrieveFieldValueNames(String id, int field, int style, Locale locale) {
112 LocaleServiceProviderPool pool =
113 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
114 return pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale,
115 normalizeCalendarType(id), field, style, false);
116 }
117
118 public static Map<String, Integer> retrieveJavaTimeFieldValueNames(String id, int field, int style, Locale locale) {
119 LocaleServiceProviderPool pool =
120 LocaleServiceProviderPool.getPool(CalendarNameProvider.class);
121 Map<String, Integer> map;
122 map = pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale,
123 normalizeCalendarType(id), field, style, true);
124 if (map == null) {
125 map = pool.getLocalizedObject(CalendarFieldValueNamesMapGetter.INSTANCE, locale,
126 normalizeCalendarType(id), field, style, false);
127 }
128 return map;
129 }
130
131 /**
132 * Utility to look for a region override extension
133 */
134 public static Optional<Locale> findRegionOverride(Locale l) {
135 String rg = l.getUnicodeLocaleType("rg");
136 Locale override = null;
137
138 if (rg != null && rg.length() == 6) {
139 // UN M.49 code should not be allowed here
140 // cannot use regex here, as it could be a recursive call
141 rg = rg.toUpperCase(Locale.ROOT);
142 if (rg.charAt(0) >= 0x0041 &&
143 rg.charAt(0) <= 0x005A &&
144 rg.charAt(1) >= 0x0041 &&
145 rg.charAt(1) <= 0x005A &&
146 rg.substring(2).equals("ZZZZ")) {
147 override = new Locale.Builder().setLocale(l)
148 .setRegion(rg.substring(0, 2))
149 .build();
150 }
151 }
152
153 return Optional.ofNullable(override);
154 }
155
156 static String normalizeCalendarType(String requestID) {
157 String type;
158 if (requestID.equals("gregorian") || requestID.equals("iso8601")) {
159 type = "gregory";
160 } else if (requestID.startsWith("islamic")) {
161 type = "islamic";
162 } else {
163 type = requestID;
164 }
165 return type;
166 }
167
168 /**
169 * Obtains a localized field value string from a CalendarDataProvider
170 * implementation.
171 */
172 private static class CalendarFieldValueNameGetter
173 implements LocaleServiceProviderPool.LocalizedObjectGetter<CalendarNameProvider,
174 String> {
175 private static final CalendarFieldValueNameGetter INSTANCE =
|