1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "ICU"
18
19 #include "IcuUtilities.h"
20 #include "JNIHelp.h"
21 #include "JniConstants.h"
22 #include "JniException.h"
23 #include "ScopedFd.h"
24 #include "ScopedJavaUnicodeString.h"
25 #include "ScopedLocalRef.h"
26 #include "ScopedUtfChars.h"
27 #include "UniquePtr.h"
28 #include "cutils/log.h"
29 #include "toStringArray.h"
30 #include "unicode/calendar.h"
31 #include "unicode/datefmt.h"
32 #include "unicode/dcfmtsym.h"
33 #include "unicode/decimfmt.h"
34 #include "unicode/dtfmtsym.h"
35 #include "unicode/dtptngen.h"
36 #include "unicode/gregocal.h"
37 #include "unicode/locid.h"
38 #include "unicode/numfmt.h"
39 #include "unicode/strenum.h"
40 #include "unicode/ubrk.h"
41 #include "unicode/ucal.h"
42 #include "unicode/uclean.h"
43 #include "unicode/ucol.h"
44 #include "unicode/ucurr.h"
45 #include "unicode/udat.h"
46 #include "unicode/uloc.h"
47 #include "unicode/ulocdata.h"
48 #include "unicode/ustring.h"
49 #include "ureslocs.h"
50 #include "valueOf.h"
51
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <string>
57 #include <sys/mman.h>
58 #include <sys/stat.h>
59 #include <sys/time.h>
60 #include <sys/types.h>
61 #include <time.h>
62 #include <unistd.h>
63
64 // TODO: put this in a header file and use it everywhere!
65 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
66 // It goes in the private: declarations in a class.
67 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
68 TypeName(const TypeName&); \
69 void operator=(const TypeName&)
70
71 class ScopedResourceBundle {
72 public:
ScopedResourceBundle(UResourceBundle * bundle)73 ScopedResourceBundle(UResourceBundle* bundle) : bundle_(bundle) {
74 }
75
~ScopedResourceBundle()76 ~ScopedResourceBundle() {
77 if (bundle_ != NULL) {
78 ures_close(bundle_);
79 }
80 }
81
get()82 UResourceBundle* get() {
83 return bundle_;
84 }
85
hasKey(const char * key)86 bool hasKey(const char* key) {
87 UErrorCode status = U_ZERO_ERROR;
88 ures_getStringByKey(bundle_, key, NULL, &status);
89 return U_SUCCESS(status);
90 }
91
92 private:
93 UResourceBundle* bundle_;
94 DISALLOW_COPY_AND_ASSIGN(ScopedResourceBundle);
95 };
96
ICU_addLikelySubtags(JNIEnv * env,jclass,jstring javaLocale)97 static jstring ICU_addLikelySubtags(JNIEnv* env, jclass, jstring javaLocale) {
98 UErrorCode status = U_ZERO_ERROR;
99 ScopedUtfChars localeID(env, javaLocale);
100 char maximizedLocaleID[ULOC_FULLNAME_CAPACITY];
101 uloc_addLikelySubtags(localeID.c_str(), maximizedLocaleID, sizeof(maximizedLocaleID), &status);
102 if (U_FAILURE(status)) {
103 return javaLocale;
104 }
105 return env->NewStringUTF(maximizedLocaleID);
106 }
107
ICU_getScript(JNIEnv * env,jclass,jstring javaLocale)108 static jstring ICU_getScript(JNIEnv* env, jclass, jstring javaLocale) {
109 UErrorCode status = U_ZERO_ERROR;
110 ScopedUtfChars localeID(env, javaLocale);
111 char script[ULOC_SCRIPT_CAPACITY];
112 uloc_getScript(localeID.c_str(), script, sizeof(script), &status);
113 if (U_FAILURE(status)) {
114 return NULL;
115 }
116 return env->NewStringUTF(script);
117 }
118
ICU_getCurrencyFractionDigits(JNIEnv * env,jclass,jstring javaCurrencyCode)119 static jint ICU_getCurrencyFractionDigits(JNIEnv* env, jclass, jstring javaCurrencyCode) {
120 ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode);
121 if (!currencyCode.valid()) {
122 return 0;
123 }
124 UnicodeString icuCurrencyCode(currencyCode.unicodeString());
125 UErrorCode status = U_ZERO_ERROR;
126 return ucurr_getDefaultFractionDigits(icuCurrencyCode.getTerminatedBuffer(), &status);
127 }
128
129 // TODO: rewrite this with int32_t ucurr_forLocale(const char* locale, UChar* buff, int32_t buffCapacity, UErrorCode* ec)...
ICU_getCurrencyCode(JNIEnv * env,jclass,jstring javaCountryCode)130 static jstring ICU_getCurrencyCode(JNIEnv* env, jclass, jstring javaCountryCode) {
131 UErrorCode status = U_ZERO_ERROR;
132 ScopedResourceBundle supplData(ures_openDirect(U_ICUDATA_CURR, "supplementalData", &status));
133 if (U_FAILURE(status)) {
134 return NULL;
135 }
136
137 ScopedResourceBundle currencyMap(ures_getByKey(supplData.get(), "CurrencyMap", NULL, &status));
138 if (U_FAILURE(status)) {
139 return NULL;
140 }
141
142 ScopedUtfChars countryCode(env, javaCountryCode);
143 ScopedResourceBundle currency(ures_getByKey(currencyMap.get(), countryCode.c_str(), NULL, &status));
144 if (U_FAILURE(status)) {
145 return NULL;
146 }
147
148 ScopedResourceBundle currencyElem(ures_getByIndex(currency.get(), 0, NULL, &status));
149 if (U_FAILURE(status)) {
150 return env->NewStringUTF("XXX");
151 }
152
153 // Check if there's a 'to' date. If there is, the currency isn't used anymore.
154 ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status));
155 if (!U_FAILURE(status)) {
156 return NULL;
157 }
158 // Ignore the failure to find a 'to' date.
159 status = U_ZERO_ERROR;
160
161 ScopedResourceBundle currencyId(ures_getByKey(currencyElem.get(), "id", NULL, &status));
162 if (U_FAILURE(status)) {
163 // No id defined for this country
164 return env->NewStringUTF("XXX");
165 }
166
167 int32_t charCount;
168 const jchar* chars = ures_getString(currencyId.get(), &charCount, &status);
169 return (charCount == 0) ? env->NewStringUTF("XXX") : env->NewString(chars, charCount);
170 }
171
getCurrencyName(JNIEnv * env,jstring javaLocaleName,jstring javaCurrencyCode,UCurrNameStyle nameStyle)172 static jstring getCurrencyName(JNIEnv* env, jstring javaLocaleName, jstring javaCurrencyCode, UCurrNameStyle nameStyle) {
173 ScopedUtfChars localeName(env, javaLocaleName);
174 if (localeName.c_str() == NULL) {
175 return NULL;
176 }
177 ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode);
178 if (!currencyCode.valid()) {
179 return NULL;
180 }
181 UnicodeString icuCurrencyCode(currencyCode.unicodeString());
182 UErrorCode status = U_ZERO_ERROR;
183 UBool isChoiceFormat = false;
184 int32_t charCount;
185 const UChar* chars = ucurr_getName(icuCurrencyCode.getTerminatedBuffer(), localeName.c_str(),
186 nameStyle, &isChoiceFormat, &charCount, &status);
187 if (status == U_USING_DEFAULT_WARNING) {
188 if (nameStyle == UCURR_SYMBOL_NAME) {
189 // ICU doesn't distinguish between falling back to the root locale and meeting a genuinely
190 // unknown currency. The Currency class does.
191 if (!ucurr_isAvailable(icuCurrencyCode.getTerminatedBuffer(), U_DATE_MIN, U_DATE_MAX, &status)) {
192 return NULL;
193 }
194 }
195 if (nameStyle == UCURR_LONG_NAME) {
196 // ICU's default is English. We want the ISO 4217 currency code instead.
197 chars = icuCurrencyCode.getBuffer();
198 charCount = icuCurrencyCode.length();
199 }
200 }
201 return (charCount == 0) ? NULL : env->NewString(chars, charCount);
202 }
203
ICU_getCurrencyDisplayName(JNIEnv * env,jclass,jstring javaLocaleName,jstring javaCurrencyCode)204 static jstring ICU_getCurrencyDisplayName(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) {
205 return getCurrencyName(env, javaLocaleName, javaCurrencyCode, UCURR_LONG_NAME);
206 }
207
ICU_getCurrencySymbol(JNIEnv * env,jclass,jstring javaLocaleName,jstring javaCurrencyCode)208 static jstring ICU_getCurrencySymbol(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) {
209 return getCurrencyName(env, javaLocaleName, javaCurrencyCode, UCURR_SYMBOL_NAME);
210 }
211
ICU_getDisplayCountryNative(JNIEnv * env,jclass,jstring targetLocale,jstring locale)212 static jstring ICU_getDisplayCountryNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
213 Locale loc = getLocale(env, locale);
214 Locale targetLoc = getLocale(env, targetLocale);
215 UnicodeString str;
216 targetLoc.getDisplayCountry(loc, str);
217 return env->NewString(str.getBuffer(), str.length());
218 }
219
ICU_getDisplayLanguageNative(JNIEnv * env,jclass,jstring targetLocale,jstring locale)220 static jstring ICU_getDisplayLanguageNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
221 Locale loc = getLocale(env, locale);
222 Locale targetLoc = getLocale(env, targetLocale);
223 UnicodeString str;
224 targetLoc.getDisplayLanguage(loc, str);
225 return env->NewString(str.getBuffer(), str.length());
226 }
227
ICU_getDisplayVariantNative(JNIEnv * env,jclass,jstring targetLocale,jstring locale)228 static jstring ICU_getDisplayVariantNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
229 Locale loc = getLocale(env, locale);
230 Locale targetLoc = getLocale(env, targetLocale);
231 UnicodeString str;
232 targetLoc.getDisplayVariant(loc, str);
233 return env->NewString(str.getBuffer(), str.length());
234 }
235
ICU_getISO3CountryNative(JNIEnv * env,jclass,jstring locale)236 static jstring ICU_getISO3CountryNative(JNIEnv* env, jclass, jstring locale) {
237 Locale loc = getLocale(env, locale);
238 return env->NewStringUTF(loc.getISO3Country());
239 }
240
ICU_getISO3LanguageNative(JNIEnv * env,jclass,jstring locale)241 static jstring ICU_getISO3LanguageNative(JNIEnv* env, jclass, jstring locale) {
242 Locale loc = getLocale(env, locale);
243 return env->NewStringUTF(loc.getISO3Language());
244 }
245
ICU_getISOCountriesNative(JNIEnv * env,jclass)246 static jobjectArray ICU_getISOCountriesNative(JNIEnv* env, jclass) {
247 return toStringArray(env, Locale::getISOCountries());
248 }
249
ICU_getISOLanguagesNative(JNIEnv * env,jclass)250 static jobjectArray ICU_getISOLanguagesNative(JNIEnv* env, jclass) {
251 return toStringArray(env, Locale::getISOLanguages());
252 }
253
ICU_getAvailableLocalesNative(JNIEnv * env,jclass)254 static jobjectArray ICU_getAvailableLocalesNative(JNIEnv* env, jclass) {
255 return toStringArray(env, uloc_countAvailable, uloc_getAvailable);
256 }
257
ICU_getAvailableBreakIteratorLocalesNative(JNIEnv * env,jclass)258 static jobjectArray ICU_getAvailableBreakIteratorLocalesNative(JNIEnv* env, jclass) {
259 return toStringArray(env, ubrk_countAvailable, ubrk_getAvailable);
260 }
261
ICU_getAvailableCalendarLocalesNative(JNIEnv * env,jclass)262 static jobjectArray ICU_getAvailableCalendarLocalesNative(JNIEnv* env, jclass) {
263 return toStringArray(env, ucal_countAvailable, ucal_getAvailable);
264 }
265
ICU_getAvailableCollatorLocalesNative(JNIEnv * env,jclass)266 static jobjectArray ICU_getAvailableCollatorLocalesNative(JNIEnv* env, jclass) {
267 return toStringArray(env, ucol_countAvailable, ucol_getAvailable);
268 }
269
ICU_getAvailableDateFormatLocalesNative(JNIEnv * env,jclass)270 static jobjectArray ICU_getAvailableDateFormatLocalesNative(JNIEnv* env, jclass) {
271 return toStringArray(env, udat_countAvailable, udat_getAvailable);
272 }
273
ICU_getAvailableNumberFormatLocalesNative(JNIEnv * env,jclass)274 static jobjectArray ICU_getAvailableNumberFormatLocalesNative(JNIEnv* env, jclass) {
275 return toStringArray(env, unum_countAvailable, unum_getAvailable);
276 }
277
setIntegerField(JNIEnv * env,jobject obj,const char * fieldName,int value)278 static void setIntegerField(JNIEnv* env, jobject obj, const char* fieldName, int value) {
279 ScopedLocalRef<jobject> integerValue(env, integerValueOf(env, value));
280 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/Integer;");
281 env->SetObjectField(obj, fid, integerValue.get());
282 }
283
setStringField(JNIEnv * env,jobject obj,const char * fieldName,jstring value)284 static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, jstring value) {
285 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/String;");
286 env->SetObjectField(obj, fid, value);
287 env->DeleteLocalRef(value);
288 }
289
setStringArrayField(JNIEnv * env,jobject obj,const char * fieldName,jobjectArray value)290 static void setStringArrayField(JNIEnv* env, jobject obj, const char* fieldName, jobjectArray value) {
291 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "[Ljava/lang/String;");
292 env->SetObjectField(obj, fid, value);
293 }
294
setStringArrayField(JNIEnv * env,jobject obj,const char * fieldName,const UnicodeString * valueArray,int32_t size)295 static void setStringArrayField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString* valueArray, int32_t size) {
296 ScopedLocalRef<jobjectArray> result(env, env->NewObjectArray(size, JniConstants::stringClass, NULL));
297 for (int32_t i = 0; i < size ; i++) {
298 ScopedLocalRef<jstring> s(env, env->NewString(valueArray[i].getBuffer(),valueArray[i].length()));
299 if (env->ExceptionCheck()) {
300 return;
301 }
302 env->SetObjectArrayElement(result.get(), i, s.get());
303 if (env->ExceptionCheck()) {
304 return;
305 }
306 }
307 setStringArrayField(env, obj, fieldName, result.get());
308 }
309
setStringField(JNIEnv * env,jobject obj,const char * fieldName,UResourceBundle * bundle,int index)310 static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, int index) {
311 UErrorCode status = U_ZERO_ERROR;
312 int charCount;
313 const UChar* chars = ures_getStringByIndex(bundle, index, &charCount, &status);
314 if (U_SUCCESS(status)) {
315 setStringField(env, obj, fieldName, env->NewString(chars, charCount));
316 } else {
317 ALOGE("Error setting String field %s from ICU resource (index %d): %s", fieldName, index, u_errorName(status));
318 }
319 }
320
setStringField(JNIEnv * env,jobject obj,const char * fieldName,UResourceBundle * bundle,const char * key)321 static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, const char* key) {
322 UErrorCode status = U_ZERO_ERROR;
323 int charCount;
324 const UChar* chars = ures_getStringByKey(bundle, key, &charCount, &status);
325 if (U_SUCCESS(status)) {
326 setStringField(env, obj, fieldName, env->NewString(chars, charCount));
327 } else {
328 ALOGE("Error setting String field %s from ICU resource (key %s): %s", fieldName, key, u_errorName(status));
329 }
330 }
331
setCharField(JNIEnv * env,jobject obj,const char * fieldName,const UnicodeString & value)332 static void setCharField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString& value) {
333 if (value.length() == 0) {
334 return;
335 }
336 jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "C");
337 env->SetCharField(obj, fid, value.charAt(0));
338 }
339
setStringField(JNIEnv * env,jobject obj,const char * fieldName,const UnicodeString & value)340 static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, const UnicodeString& value) {
341 const UChar* chars = value.getBuffer();
342 setStringField(env, obj, fieldName, env->NewString(chars, value.length()));
343 }
344
setNumberPatterns(JNIEnv * env,jobject obj,Locale & locale)345 static void setNumberPatterns(JNIEnv* env, jobject obj, Locale& locale) {
346 UErrorCode status = U_ZERO_ERROR;
347
348 UnicodeString pattern;
349 UniquePtr<DecimalFormat> fmt(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_CURRENCY, status)));
350 pattern = fmt->toPattern(pattern.remove());
351 setStringField(env, obj, "currencyPattern", pattern);
352
353 fmt.reset(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_DECIMAL, status)));
354 pattern = fmt->toPattern(pattern.remove());
355 setStringField(env, obj, "numberPattern", pattern);
356
357 fmt.reset(static_cast<DecimalFormat*>(NumberFormat::createInstance(locale, UNUM_PERCENT, status)));
358 pattern = fmt->toPattern(pattern.remove());
359 setStringField(env, obj, "percentPattern", pattern);
360 }
361
setDecimalFormatSymbolsData(JNIEnv * env,jobject obj,Locale & locale)362 static void setDecimalFormatSymbolsData(JNIEnv* env, jobject obj, Locale& locale) {
363 UErrorCode status = U_ZERO_ERROR;
364 DecimalFormatSymbols dfs(locale, status);
365
366 setCharField(env, obj, "decimalSeparator", dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
367 setCharField(env, obj, "groupingSeparator", dfs.getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
368 setCharField(env, obj, "patternSeparator", dfs.getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol));
369 setCharField(env, obj, "percent", dfs.getSymbol(DecimalFormatSymbols::kPercentSymbol));
370 setCharField(env, obj, "perMill", dfs.getSymbol(DecimalFormatSymbols::kPerMillSymbol));
371 setCharField(env, obj, "monetarySeparator", dfs.getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol));
372 setCharField(env, obj, "minusSign", dfs.getSymbol(DecimalFormatSymbols:: kMinusSignSymbol));
373 setStringField(env, obj, "exponentSeparator", dfs.getSymbol(DecimalFormatSymbols::kExponentialSymbol));
374 setStringField(env, obj, "infinity", dfs.getSymbol(DecimalFormatSymbols::kInfinitySymbol));
375 setStringField(env, obj, "NaN", dfs.getSymbol(DecimalFormatSymbols::kNaNSymbol));
376 setCharField(env, obj, "zeroDigit", dfs.getSymbol(DecimalFormatSymbols::kZeroDigitSymbol));
377 }
378
379
380 // Iterates up through the locale hierarchy. So "en_US" would return "en_US", "en", "".
381 class LocaleNameIterator {
382 public:
LocaleNameIterator(const char * locale_name,UErrorCode & status)383 LocaleNameIterator(const char* locale_name, UErrorCode& status) : status_(status), has_next_(true) {
384 strcpy(locale_name_, locale_name);
385 locale_name_length_ = strlen(locale_name_);
386 }
387
Get()388 const char* Get() {
389 return locale_name_;
390 }
391
HasNext()392 bool HasNext() {
393 return has_next_;
394 }
395
Up()396 void Up() {
397 locale_name_length_ = uloc_getParent(locale_name_, locale_name_, sizeof(locale_name_), &status_);
398 if (locale_name_length_ == 0) {
399 has_next_ = false;
400 }
401 }
402
403 private:
404 UErrorCode& status_;
405 bool has_next_;
406 char locale_name_[ULOC_FULLNAME_CAPACITY];
407 int32_t locale_name_length_;
408
409 DISALLOW_COPY_AND_ASSIGN(LocaleNameIterator);
410 };
411
getDateTimePatterns(JNIEnv * env,jobject localeData,const char * locale_name)412 static bool getDateTimePatterns(JNIEnv* env, jobject localeData, const char* locale_name) {
413 UErrorCode status = U_ZERO_ERROR;
414 ScopedResourceBundle root(ures_open(NULL, locale_name, &status));
415 if (U_FAILURE(status)) {
416 return false;
417 }
418 ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status));
419 if (U_FAILURE(status)) {
420 return false;
421 }
422 ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status));
423 if (U_FAILURE(status)) {
424 return false;
425 }
426 ScopedResourceBundle dateTimePatterns(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status));
427 if (U_FAILURE(status)) {
428 return false;
429 }
430 setStringField(env, localeData, "fullTimeFormat", dateTimePatterns.get(), 0);
431 setStringField(env, localeData, "longTimeFormat", dateTimePatterns.get(), 1);
432 setStringField(env, localeData, "mediumTimeFormat", dateTimePatterns.get(), 2);
433 setStringField(env, localeData, "shortTimeFormat", dateTimePatterns.get(), 3);
434 setStringField(env, localeData, "fullDateFormat", dateTimePatterns.get(), 4);
435 setStringField(env, localeData, "longDateFormat", dateTimePatterns.get(), 5);
436 setStringField(env, localeData, "mediumDateFormat", dateTimePatterns.get(), 6);
437 setStringField(env, localeData, "shortDateFormat", dateTimePatterns.get(), 7);
438 return true;
439 }
440
getTimeFormats12And24(JNIEnv * env,jobject localeData,Locale & locale)441 static bool getTimeFormats12And24(JNIEnv* env, jobject localeData, Locale& locale) {
442 UErrorCode status = U_ZERO_ERROR;
443 DateTimePatternGenerator* generator = DateTimePatternGenerator::createInstance(locale, status);
444 if (U_FAILURE(status)) {
445 return false;
446 }
447
448 UnicodeString pattern_Hm(generator->getBestPattern(UnicodeString("Hm", 2, US_INV), status));
449 if (U_FAILURE(status)) {
450 return false;
451 }
452
453 UnicodeString pattern_hm(generator->getBestPattern(UnicodeString("hm", 2, US_INV), status));
454 if (U_FAILURE(status)) {
455 return false;
456 }
457
458 setStringField(env, localeData, "timeFormat12", pattern_hm);
459 setStringField(env, localeData, "timeFormat24", pattern_Hm);
460 return true;
461 }
462
getYesterdayTodayAndTomorrow(JNIEnv * env,jobject localeData,const char * locale_name)463 static bool getYesterdayTodayAndTomorrow(JNIEnv* env, jobject localeData, const char* locale_name) {
464 UErrorCode status = U_ZERO_ERROR;
465 ScopedResourceBundle root(ures_open(NULL, locale_name, &status));
466 if (U_FAILURE(status)) {
467 return false;
468 }
469 ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status));
470 if (U_FAILURE(status)) {
471 return false;
472 }
473 ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status));
474 if (U_FAILURE(status)) {
475 return false;
476 }
477 ScopedResourceBundle fields(ures_getByKey(gregorian.get(), "fields", NULL, &status));
478 if (U_FAILURE(status)) {
479 return false;
480 }
481 ScopedResourceBundle day(ures_getByKey(fields.get(), "day", NULL, &status));
482 if (U_FAILURE(status)) {
483 return false;
484 }
485 ScopedResourceBundle relative(ures_getByKey(day.get(), "relative", NULL, &status));
486 if (U_FAILURE(status)) {
487 return false;
488 }
489 // bn_BD only has a "-2" entry.
490 if (relative.hasKey("-1") && relative.hasKey("0") && relative.hasKey("1")) {
491 setStringField(env, localeData, "yesterday", relative.get(), "-1");
492 setStringField(env, localeData, "today", relative.get(), "0");
493 setStringField(env, localeData, "tomorrow", relative.get(), "1");
494 return true;
495 }
496 return false;
497 }
498
ICU_initLocaleDataImpl(JNIEnv * env,jclass,jstring javaLocaleName,jobject localeData)499 static jboolean ICU_initLocaleDataImpl(JNIEnv* env, jclass, jstring javaLocaleName, jobject localeData) {
500 ScopedUtfChars localeName(env, javaLocaleName);
501 if (localeName.c_str() == NULL) {
502 return JNI_FALSE;
503 }
504 if (localeName.size() >= ULOC_FULLNAME_CAPACITY) {
505 return JNI_FALSE; // ICU has a fixed-length limit.
506 }
507
508 // Get the DateTimePatterns.
509 UErrorCode status = U_ZERO_ERROR;
510 bool foundDateTimePatterns = false;
511 for (LocaleNameIterator it(localeName.c_str(), status); it.HasNext(); it.Up()) {
512 if (getDateTimePatterns(env, localeData, it.Get())) {
513 foundDateTimePatterns = true;
514 break;
515 }
516 }
517 if (!foundDateTimePatterns) {
518 ALOGE("Couldn't find ICU DateTimePatterns for %s", localeName.c_str());
519 return JNI_FALSE;
520 }
521
522 // Get the "h:mm a" and "HH:mm" 12- and 24-hour time format strings.
523 Locale locale = getLocale(env, javaLocaleName);
524 if (!getTimeFormats12And24(env, localeData, locale)) {
525 ALOGE("Couldn't find ICU 12- and 24-hour time formats for %s", localeName.c_str());
526 return JNI_FALSE;
527 }
528
529 // Get the "Yesterday", "Today", and "Tomorrow" strings.
530 bool foundYesterdayTodayAndTomorrow = false;
531 for (LocaleNameIterator it(localeName.c_str(), status); it.HasNext(); it.Up()) {
532 if (getYesterdayTodayAndTomorrow(env, localeData, it.Get())) {
533 foundYesterdayTodayAndTomorrow = true;
534 break;
535 }
536 }
537 if (!foundYesterdayTodayAndTomorrow) {
538 ALOGE("Couldn't find ICU yesterday/today/tomorrow for %s", localeName.c_str());
539 return JNI_FALSE;
540 }
541
542 status = U_ZERO_ERROR;
543 UniquePtr<Calendar> cal(Calendar::createInstance(locale, status));
544 if (U_FAILURE(status)) {
545 return JNI_FALSE;
546 }
547
548 setIntegerField(env, localeData, "firstDayOfWeek", cal->getFirstDayOfWeek());
549 setIntegerField(env, localeData, "minimalDaysInFirstWeek", cal->getMinimalDaysInFirstWeek());
550
551 // Get DateFormatSymbols.
552 status = U_ZERO_ERROR;
553 DateFormatSymbols dateFormatSym(locale, status);
554 if (U_FAILURE(status)) {
555 return JNI_FALSE;
556 }
557
558 // Get AM/PM and BC/AD.
559 int32_t count = 0;
560 const UnicodeString* amPmStrs = dateFormatSym.getAmPmStrings(count);
561 setStringArrayField(env, localeData, "amPm", amPmStrs, count);
562 const UnicodeString* erasStrs = dateFormatSym.getEras(count);
563 setStringArrayField(env, localeData, "eras", erasStrs, count);
564
565 const UnicodeString* longMonthNames =
566 dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
567 setStringArrayField(env, localeData, "longMonthNames", longMonthNames, count);
568 const UnicodeString* shortMonthNames =
569 dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
570 setStringArrayField(env, localeData, "shortMonthNames", shortMonthNames, count);
571 const UnicodeString* tinyMonthNames =
572 dateFormatSym.getMonths(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
573 setStringArrayField(env, localeData, "tinyMonthNames", tinyMonthNames, count);
574 const UnicodeString* longWeekdayNames =
575 dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE);
576 setStringArrayField(env, localeData, "longWeekdayNames", longWeekdayNames, count);
577 const UnicodeString* shortWeekdayNames =
578 dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED);
579 setStringArrayField(env, localeData, "shortWeekdayNames", shortWeekdayNames, count);
580 const UnicodeString* tinyWeekdayNames =
581 dateFormatSym.getWeekdays(count, DateFormatSymbols::FORMAT, DateFormatSymbols::NARROW);
582 setStringArrayField(env, localeData, "tinyWeekdayNames", tinyWeekdayNames, count);
583
584 const UnicodeString* longStandAloneMonthNames =
585 dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
586 setStringArrayField(env, localeData, "longStandAloneMonthNames", longStandAloneMonthNames, count);
587 const UnicodeString* shortStandAloneMonthNames =
588 dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
589 setStringArrayField(env, localeData, "shortStandAloneMonthNames", shortStandAloneMonthNames, count);
590 const UnicodeString* tinyStandAloneMonthNames =
591 dateFormatSym.getMonths(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
592 setStringArrayField(env, localeData, "tinyStandAloneMonthNames", tinyStandAloneMonthNames, count);
593 const UnicodeString* longStandAloneWeekdayNames =
594 dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE);
595 setStringArrayField(env, localeData, "longStandAloneWeekdayNames", longStandAloneWeekdayNames, count);
596 const UnicodeString* shortStandAloneWeekdayNames =
597 dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED);
598 setStringArrayField(env, localeData, "shortStandAloneWeekdayNames", shortStandAloneWeekdayNames, count);
599 const UnicodeString* tinyStandAloneWeekdayNames =
600 dateFormatSym.getWeekdays(count, DateFormatSymbols::STANDALONE, DateFormatSymbols::NARROW);
601 setStringArrayField(env, localeData, "tinyStandAloneWeekdayNames", tinyStandAloneWeekdayNames, count);
602
603 status = U_ZERO_ERROR;
604
605 // For numberPatterns and symbols.
606 setNumberPatterns(env, localeData, locale);
607 setDecimalFormatSymbolsData(env, localeData, locale);
608
609 jstring countryCode = env->NewStringUTF(Locale::createFromName(localeName.c_str()).getCountry());
610 jstring internationalCurrencySymbol = ICU_getCurrencyCode(env, NULL, countryCode);
611 env->DeleteLocalRef(countryCode);
612 countryCode = NULL;
613
614 jstring currencySymbol = NULL;
615 if (internationalCurrencySymbol != NULL) {
616 currencySymbol = ICU_getCurrencySymbol(env, NULL, javaLocaleName, internationalCurrencySymbol);
617 } else {
618 internationalCurrencySymbol = env->NewStringUTF("XXX");
619 }
620 if (currencySymbol == NULL) {
621 // This is the UTF-8 encoding of U+00A4 (CURRENCY SIGN).
622 currencySymbol = env->NewStringUTF("\xc2\xa4");
623 }
624 setStringField(env, localeData, "currencySymbol", currencySymbol);
625 setStringField(env, localeData, "internationalCurrencySymbol", internationalCurrencySymbol);
626
627 return JNI_TRUE;
628 }
629
ICU_toLowerCase(JNIEnv * env,jclass,jstring javaString,jstring localeName)630 static jstring ICU_toLowerCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) {
631 ScopedJavaUnicodeString scopedString(env, javaString);
632 if (!scopedString.valid()) {
633 return NULL;
634 }
635 UnicodeString& s(scopedString.unicodeString());
636 UnicodeString original(s);
637 s.toLower(Locale::createFromName(ScopedUtfChars(env, localeName).c_str()));
638 return s == original ? javaString : env->NewString(s.getBuffer(), s.length());
639 }
640
ICU_toUpperCase(JNIEnv * env,jclass,jstring javaString,jstring localeName)641 static jstring ICU_toUpperCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) {
642 ScopedJavaUnicodeString scopedString(env, javaString);
643 if (!scopedString.valid()) {
644 return NULL;
645 }
646 UnicodeString& s(scopedString.unicodeString());
647 UnicodeString original(s);
648 s.toUpper(Locale::createFromName(ScopedUtfChars(env, localeName).c_str()));
649 return s == original ? javaString : env->NewString(s.getBuffer(), s.length());
650 }
651
versionString(JNIEnv * env,const UVersionInfo & version)652 static jstring versionString(JNIEnv* env, const UVersionInfo& version) {
653 char versionString[U_MAX_VERSION_STRING_LENGTH];
654 u_versionToString(const_cast<UVersionInfo&>(version), &versionString[0]);
655 return env->NewStringUTF(versionString);
656 }
657
ICU_getCldrVersion(JNIEnv * env,jclass)658 static jstring ICU_getCldrVersion(JNIEnv* env, jclass) {
659 UErrorCode status = U_ZERO_ERROR;
660 UVersionInfo cldrVersion;
661 ulocdata_getCLDRVersion(cldrVersion, &status);
662 return versionString(env, cldrVersion);
663 }
664
ICU_getIcuVersion(JNIEnv * env,jclass)665 static jstring ICU_getIcuVersion(JNIEnv* env, jclass) {
666 UVersionInfo icuVersion;
667 u_getVersion(icuVersion);
668 return versionString(env, icuVersion);
669 }
670
ICU_getUnicodeVersion(JNIEnv * env,jclass)671 static jstring ICU_getUnicodeVersion(JNIEnv* env, jclass) {
672 UVersionInfo unicodeVersion;
673 u_getUnicodeVersion(unicodeVersion);
674 return versionString(env, unicodeVersion);
675 }
676
677 struct EnumerationCounter {
678 const size_t count;
EnumerationCounterEnumerationCounter679 EnumerationCounter(size_t count) : count(count) {}
operator ()EnumerationCounter680 size_t operator()() { return count; }
681 };
682 struct EnumerationGetter {
683 UEnumeration* e;
684 UErrorCode* status;
EnumerationGetterEnumerationGetter685 EnumerationGetter(UEnumeration* e, UErrorCode* status) : e(e), status(status) {}
operator ()EnumerationGetter686 const UChar* operator()(int32_t* charCount) { return uenum_unext(e, charCount, status); }
687 };
ICU_getAvailableCurrencyCodes(JNIEnv * env,jclass)688 static jobject ICU_getAvailableCurrencyCodes(JNIEnv* env, jclass) {
689 UErrorCode status = U_ZERO_ERROR;
690 UEnumeration* e(ucurr_openISOCurrencies(UCURR_COMMON|UCURR_NON_DEPRECATED, &status));
691 EnumerationCounter counter(uenum_count(e, &status));
692 if (maybeThrowIcuException(env, "uenum_count", status)) {
693 return NULL;
694 }
695 EnumerationGetter getter(e, &status);
696 jobject result = toStringArray16(env, &counter, &getter);
697 maybeThrowIcuException(env, "uenum_unext", status);
698 uenum_close(e);
699 return result;
700 }
701
ICU_getBestDateTimePattern(JNIEnv * env,jclass,jstring javaPattern,jstring javaLocaleName)702 static jstring ICU_getBestDateTimePattern(JNIEnv* env, jclass, jstring javaPattern, jstring javaLocaleName) {
703 Locale locale = getLocale(env, javaLocaleName);
704 UErrorCode status = U_ZERO_ERROR;
705 DateTimePatternGenerator* generator = DateTimePatternGenerator::createInstance(locale, status);
706 if (maybeThrowIcuException(env, "DateTimePatternGenerator::createInstance", status)) {
707 return NULL;
708 }
709
710 ScopedJavaUnicodeString patternHolder(env, javaPattern);
711 if (!patternHolder.valid()) {
712 return NULL;
713 }
714 UnicodeString result(generator->getBestPattern(patternHolder.unicodeString(), status));
715 if (maybeThrowIcuException(env, "DateTimePatternGenerator::getBestPattern", status)) {
716 return NULL;
717 }
718
719 return env->NewString(result.getBuffer(), result.length());
720 }
721
722 static JNINativeMethod gMethods[] = {
723 NATIVE_METHOD(ICU, addLikelySubtags, "(Ljava/lang/String;)Ljava/lang/String;"),
724 NATIVE_METHOD(ICU, getAvailableBreakIteratorLocalesNative, "()[Ljava/lang/String;"),
725 NATIVE_METHOD(ICU, getAvailableCalendarLocalesNative, "()[Ljava/lang/String;"),
726 NATIVE_METHOD(ICU, getAvailableCollatorLocalesNative, "()[Ljava/lang/String;"),
727 NATIVE_METHOD(ICU, getAvailableCurrencyCodes, "()[Ljava/lang/String;"),
728 NATIVE_METHOD(ICU, getAvailableDateFormatLocalesNative, "()[Ljava/lang/String;"),
729 NATIVE_METHOD(ICU, getAvailableLocalesNative, "()[Ljava/lang/String;"),
730 NATIVE_METHOD(ICU, getAvailableNumberFormatLocalesNative, "()[Ljava/lang/String;"),
731 NATIVE_METHOD(ICU, getBestDateTimePattern, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
732 NATIVE_METHOD(ICU, getCldrVersion, "()Ljava/lang/String;"),
733 NATIVE_METHOD(ICU, getCurrencyCode, "(Ljava/lang/String;)Ljava/lang/String;"),
734 NATIVE_METHOD(ICU, getCurrencyDisplayName, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
735 NATIVE_METHOD(ICU, getCurrencyFractionDigits, "(Ljava/lang/String;)I"),
736 NATIVE_METHOD(ICU, getCurrencySymbol, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
737 NATIVE_METHOD(ICU, getDisplayCountryNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
738 NATIVE_METHOD(ICU, getDisplayLanguageNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
739 NATIVE_METHOD(ICU, getDisplayVariantNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
740 NATIVE_METHOD(ICU, getISO3CountryNative, "(Ljava/lang/String;)Ljava/lang/String;"),
741 NATIVE_METHOD(ICU, getISO3LanguageNative, "(Ljava/lang/String;)Ljava/lang/String;"),
742 NATIVE_METHOD(ICU, getISOCountriesNative, "()[Ljava/lang/String;"),
743 NATIVE_METHOD(ICU, getISOLanguagesNative, "()[Ljava/lang/String;"),
744 NATIVE_METHOD(ICU, getIcuVersion, "()Ljava/lang/String;"),
745 NATIVE_METHOD(ICU, getScript, "(Ljava/lang/String;)Ljava/lang/String;"),
746 NATIVE_METHOD(ICU, getUnicodeVersion, "()Ljava/lang/String;"),
747 NATIVE_METHOD(ICU, initLocaleDataImpl, "(Ljava/lang/String;Llibcore/icu/LocaleData;)Z"),
748 NATIVE_METHOD(ICU, toLowerCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
749 NATIVE_METHOD(ICU, toUpperCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
750 };
register_libcore_icu_ICU(JNIEnv * env)751 void register_libcore_icu_ICU(JNIEnv* env) {
752 std::string path;
753 path = u_getDataDirectory();
754 path += "/";
755 path += U_ICUDATA_NAME;
756 path += ".dat";
757
758 #define FAIL_WITH_STRERROR(s) \
759 ALOGE("Couldn't " s " '%s': %s", path.c_str(), strerror(errno)); \
760 abort();
761 #define MAYBE_FAIL_WITH_ICU_ERROR(s) \
762 if (status != U_ZERO_ERROR) {\
763 ALOGE("Couldn't initialize ICU (" s "): %s (%s)", u_errorName(status), path.c_str()); \
764 abort(); \
765 }
766
767 // Open the file and get its length.
768 ScopedFd fd(open(path.c_str(), O_RDONLY));
769 if (fd.get() == -1) {
770 FAIL_WITH_STRERROR("open");
771 }
772 struct stat sb;
773 if (fstat(fd.get(), &sb) == -1) {
774 FAIL_WITH_STRERROR("stat");
775 }
776
777 // Map it.
778 void* data = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd.get(), 0);
779 if (data == MAP_FAILED) {
780 FAIL_WITH_STRERROR("mmap");
781 }
782
783 // Tell the kernel that accesses are likely to be random rather than sequential.
784 if (madvise(data, sb.st_size, MADV_RANDOM) == -1) {
785 FAIL_WITH_STRERROR("madvise(MADV_RANDOM)");
786 }
787
788 // Tell ICU to use our memory-mapped data.
789 UErrorCode status = U_ZERO_ERROR;
790 udata_setCommonData(data, &status);
791 MAYBE_FAIL_WITH_ICU_ERROR("udata_setCommonData");
792 // Tell ICU it can *only* use our memory-mapped data.
793 udata_setFileAccess(UDATA_NO_FILES, &status);
794 MAYBE_FAIL_WITH_ICU_ERROR("udata_setFileAccess");
795
796 // Failures to find the ICU data tend to be somewhat obscure because ICU loads its data on first
797 // use, which can be anywhere. Force initialization up front so we can report a nice clear error
798 // and bail.
799 u_init(&status);
800 MAYBE_FAIL_WITH_ICU_ERROR("u_init");
801 jniRegisterNativeMethods(env, "libcore/icu/ICU", gMethods, NELEM(gMethods));
802 }
803