Home | History | Annotate | Download | only in native
      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 "JNIHelp.h"
     20 #include "JniConstants.h"
     21 #include "JniException.h"
     22 #include "ScopedFd.h"
     23 #include "ScopedJavaUnicodeString.h"
     24 #include "ScopedLocalRef.h"
     25 #include "ScopedStringChars.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/gregocal.h"
     36 #include "unicode/locid.h"
     37 #include "unicode/numfmt.h"
     38 #include "unicode/strenum.h"
     39 #include "unicode/ubrk.h"
     40 #include "unicode/ucal.h"
     41 #include "unicode/uclean.h"
     42 #include "unicode/ucol.h"
     43 #include "unicode/ucurr.h"
     44 #include "unicode/udat.h"
     45 #include "unicode/uloc.h"
     46 #include "unicode/ustring.h"
     47 #include "ureslocs.h"
     48 #include "valueOf.h"
     49 
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <string>
     55 #include <sys/mman.h>
     56 #include <sys/stat.h>
     57 #include <sys/time.h>
     58 #include <sys/types.h>
     59 #include <time.h>
     60 #include <unistd.h>
     61 
     62 class ScopedResourceBundle {
     63 public:
     64     ScopedResourceBundle(UResourceBundle* bundle) : mBundle(bundle) {
     65     }
     66 
     67     ~ScopedResourceBundle() {
     68         if (mBundle != NULL) {
     69             ures_close(mBundle);
     70         }
     71     }
     72 
     73     UResourceBundle* get() {
     74         return mBundle;
     75     }
     76 
     77 private:
     78     UResourceBundle* mBundle;
     79 
     80     // Disallow copy and assignment.
     81     ScopedResourceBundle(const ScopedResourceBundle&);
     82     void operator=(const ScopedResourceBundle&);
     83 };
     84 
     85 Locale getLocale(JNIEnv* env, jstring localeName) {
     86     return Locale::createFromName(ScopedUtfChars(env, localeName).c_str());
     87 }
     88 
     89 static jstring ICU_addLikelySubtags(JNIEnv* env, jclass, jstring javaLocale) {
     90     UErrorCode status = U_ZERO_ERROR;
     91     ScopedUtfChars localeID(env, javaLocale);
     92     char maximizedLocaleID[ULOC_FULLNAME_CAPACITY];
     93     uloc_addLikelySubtags(localeID.c_str(), maximizedLocaleID, sizeof(maximizedLocaleID), &status);
     94     if (U_FAILURE(status)) {
     95         return javaLocale;
     96     }
     97     return env->NewStringUTF(maximizedLocaleID);
     98 }
     99 
    100 static jstring ICU_getScript(JNIEnv* env, jclass, jstring javaLocale) {
    101     UErrorCode status = U_ZERO_ERROR;
    102     ScopedUtfChars localeID(env, javaLocale);
    103     char script[ULOC_SCRIPT_CAPACITY];
    104     uloc_getScript(localeID.c_str(), script, sizeof(script), &status);
    105     if (U_FAILURE(status)) {
    106         return NULL;
    107     }
    108     return env->NewStringUTF(script);
    109 }
    110 
    111 static jint ICU_getCurrencyFractionDigits(JNIEnv* env, jclass, jstring javaCurrencyCode) {
    112     ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode);
    113     UnicodeString icuCurrencyCode(currencyCode.unicodeString());
    114     UErrorCode status = U_ZERO_ERROR;
    115     return ucurr_getDefaultFractionDigits(icuCurrencyCode.getTerminatedBuffer(), &status);
    116 }
    117 
    118 static jstring ICU_getCurrencyCode(JNIEnv* env, jclass, jstring javaCountryCode) {
    119     UErrorCode status = U_ZERO_ERROR;
    120     ScopedResourceBundle supplData(ures_openDirect(U_ICUDATA_CURR, "supplementalData", &status));
    121     if (U_FAILURE(status)) {
    122         return NULL;
    123     }
    124 
    125     ScopedResourceBundle currencyMap(ures_getByKey(supplData.get(), "CurrencyMap", NULL, &status));
    126     if (U_FAILURE(status)) {
    127         return NULL;
    128     }
    129 
    130     ScopedUtfChars countryCode(env, javaCountryCode);
    131     ScopedResourceBundle currency(ures_getByKey(currencyMap.get(), countryCode.c_str(), NULL, &status));
    132     if (U_FAILURE(status)) {
    133         return NULL;
    134     }
    135 
    136     ScopedResourceBundle currencyElem(ures_getByIndex(currency.get(), 0, NULL, &status));
    137     if (U_FAILURE(status)) {
    138         return env->NewStringUTF("None");
    139     }
    140 
    141     // Check if there's a 'to' date. If there is, the currency isn't used anymore.
    142     ScopedResourceBundle currencyTo(ures_getByKey(currencyElem.get(), "to", NULL, &status));
    143     if (!U_FAILURE(status)) {
    144         return NULL;
    145     }
    146     // Ignore the failure to find a 'to' date.
    147     status = U_ZERO_ERROR;
    148 
    149     ScopedResourceBundle currencyId(ures_getByKey(currencyElem.get(), "id", NULL, &status));
    150     if (U_FAILURE(status)) {
    151         // No id defined for this country
    152         return env->NewStringUTF("None");
    153     }
    154 
    155     int32_t charCount;
    156     const jchar* chars = ures_getString(currencyId.get(), &charCount, &status);
    157     return (charCount == 0) ? env->NewStringUTF("None") : env->NewString(chars, charCount);
    158 }
    159 
    160 static jstring ICU_getCurrencyDisplayName(JNIEnv* env, jclass, jstring javaLocaleName, jstring javaCurrencyCode) {
    161     ScopedUtfChars localeName(env, javaLocaleName);
    162     ScopedJavaUnicodeString currencyCode(env, javaCurrencyCode);
    163     UnicodeString icuCurrencyCode(currencyCode.unicodeString());
    164     UErrorCode status = U_ZERO_ERROR;
    165     UBool isChoiceFormat;
    166     int32_t charCount;
    167     const UChar* chars = ucurr_getName(icuCurrencyCode.getTerminatedBuffer(), localeName.c_str(),
    168             UCURR_LONG_NAME, &isChoiceFormat, &charCount, &status);
    169     if (status == U_USING_DEFAULT_WARNING) {
    170         // ICU's default is English. We want the ISO 4217 currency code instead.
    171         chars = icuCurrencyCode.getBuffer();
    172         charCount = icuCurrencyCode.length();
    173     }
    174     return (charCount == 0) ? NULL : env->NewString(chars, charCount);
    175 }
    176 
    177 static jstring ICU_getCurrencySymbol(JNIEnv* env, jclass, jstring locale, jstring currencyCode) {
    178     // We can't use ucurr_getName because it doesn't distinguish between using data root from
    179     // the root locale and parroting back the input because it's never heard of the currency code.
    180     ScopedUtfChars localeName(env, locale);
    181     UErrorCode status = U_ZERO_ERROR;
    182     ScopedResourceBundle currLoc(ures_open(U_ICUDATA_CURR, localeName.c_str(), &status));
    183     if (U_FAILURE(status)) {
    184         return NULL;
    185     }
    186 
    187     ScopedResourceBundle currencies(ures_getByKey(currLoc.get(), "Currencies", NULL, &status));
    188     if (U_FAILURE(status)) {
    189         return NULL;
    190     }
    191 
    192     ScopedUtfChars currency(env, currencyCode);
    193     ScopedResourceBundle currencyElems(ures_getByKey(currencies.get(), currency.c_str(), NULL, &status));
    194     if (U_FAILURE(status)) {
    195         return NULL;
    196     }
    197 
    198     int32_t charCount;
    199     const jchar* chars = ures_getStringByIndex(currencyElems.get(), 0, &charCount, &status);
    200     if (U_FAILURE(status)) {
    201         return NULL;
    202     }
    203     return (charCount == 0) ? NULL : env->NewString(chars, charCount);
    204 }
    205 
    206 static jstring ICU_getDisplayCountryNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
    207     Locale loc = getLocale(env, locale);
    208     Locale targetLoc = getLocale(env, targetLocale);
    209     UnicodeString str;
    210     targetLoc.getDisplayCountry(loc, str);
    211     return env->NewString(str.getBuffer(), str.length());
    212 }
    213 
    214 static jstring ICU_getDisplayLanguageNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
    215     Locale loc = getLocale(env, locale);
    216     Locale targetLoc = getLocale(env, targetLocale);
    217     UnicodeString str;
    218     targetLoc.getDisplayLanguage(loc, str);
    219     return env->NewString(str.getBuffer(), str.length());
    220 }
    221 
    222 static jstring ICU_getDisplayVariantNative(JNIEnv* env, jclass, jstring targetLocale, jstring locale) {
    223     Locale loc = getLocale(env, locale);
    224     Locale targetLoc = getLocale(env, targetLocale);
    225     UnicodeString str;
    226     targetLoc.getDisplayVariant(loc, str);
    227     return env->NewString(str.getBuffer(), str.length());
    228 }
    229 
    230 static jstring ICU_getISO3CountryNative(JNIEnv* env, jclass, jstring locale) {
    231     Locale loc = getLocale(env, locale);
    232     return env->NewStringUTF(loc.getISO3Country());
    233 }
    234 
    235 static jstring ICU_getISO3LanguageNative(JNIEnv* env, jclass, jstring locale) {
    236     Locale loc = getLocale(env, locale);
    237     return env->NewStringUTF(loc.getISO3Language());
    238 }
    239 
    240 static jobjectArray ICU_getISOCountriesNative(JNIEnv* env, jclass) {
    241     return toStringArray(env, Locale::getISOCountries());
    242 }
    243 
    244 static jobjectArray ICU_getISOLanguagesNative(JNIEnv* env, jclass) {
    245     return toStringArray(env, Locale::getISOLanguages());
    246 }
    247 
    248 static jobjectArray ICU_getAvailableLocalesNative(JNIEnv* env, jclass) {
    249     return toStringArray(env, uloc_countAvailable, uloc_getAvailable);
    250 }
    251 
    252 static jobjectArray ICU_getAvailableBreakIteratorLocalesNative(JNIEnv* env, jclass) {
    253     return toStringArray(env, ubrk_countAvailable, ubrk_getAvailable);
    254 }
    255 
    256 static jobjectArray ICU_getAvailableCalendarLocalesNative(JNIEnv* env, jclass) {
    257     return toStringArray(env, ucal_countAvailable, ucal_getAvailable);
    258 }
    259 
    260 static jobjectArray ICU_getAvailableCollatorLocalesNative(JNIEnv* env, jclass) {
    261     return toStringArray(env, ucol_countAvailable, ucol_getAvailable);
    262 }
    263 
    264 static jobjectArray ICU_getAvailableDateFormatLocalesNative(JNIEnv* env, jclass) {
    265     return toStringArray(env, udat_countAvailable, udat_getAvailable);
    266 }
    267 
    268 static jobjectArray ICU_getAvailableNumberFormatLocalesNative(JNIEnv* env, jclass) {
    269     return toStringArray(env, unum_countAvailable, unum_getAvailable);
    270 }
    271 
    272 static bool getDayIntVector(JNIEnv*, UResourceBundle* gregorian, int* values) {
    273     // get the First day of week and the minimal days in first week numbers
    274     UErrorCode status = U_ZERO_ERROR;
    275     ScopedResourceBundle gregorianElems(ures_getByKey(gregorian, "DateTimeElements", NULL, &status));
    276     if (U_FAILURE(status)) {
    277         return false;
    278     }
    279 
    280     int intVectSize;
    281     const int* result = ures_getIntVector(gregorianElems.get(), &intVectSize, &status);
    282     if (U_FAILURE(status) || intVectSize != 2) {
    283         return false;
    284     }
    285 
    286     values[0] = result[0];
    287     values[1] = result[1];
    288     return true;
    289 }
    290 
    291 // This allows you to leave extra space at the beginning or end of the array to support the
    292 // month names and day names arrays.
    293 static jobjectArray toStringArray(JNIEnv* env, UResourceBundle* rb, size_t size, int capacity, size_t offset) {
    294     if (capacity == -1) {
    295         capacity = size;
    296     }
    297     jobjectArray result = env->NewObjectArray(capacity, JniConstants::stringClass, NULL);
    298     if (result == NULL) {
    299         return NULL;
    300     }
    301     UErrorCode status = U_ZERO_ERROR;
    302     for (size_t i = 0; i < size; ++i) {
    303         int charCount;
    304         const jchar* chars = ures_getStringByIndex(rb, i, &charCount, &status);
    305         if (U_FAILURE(status)) {
    306             return NULL;
    307         }
    308         ScopedLocalRef<jstring> s(env, env->NewString(chars, charCount));
    309         if (env->ExceptionCheck()) {
    310             return NULL;
    311         }
    312         env->SetObjectArrayElement(result, offset + i, s.get());
    313         if (env->ExceptionCheck()) {
    314             return NULL;
    315         }
    316     }
    317     return result;
    318 }
    319 
    320 static jobjectArray getAmPmMarkers(JNIEnv* env, UResourceBundle* gregorian) {
    321     UErrorCode status = U_ZERO_ERROR;
    322     ScopedResourceBundle amPmMarkers(ures_getByKey(gregorian, "AmPmMarkers", NULL, &status));
    323     if (U_FAILURE(status)) {
    324         return NULL;
    325     }
    326     return toStringArray(env, amPmMarkers.get(), ures_getSize(amPmMarkers.get()), -1, 0);
    327 }
    328 
    329 static jobjectArray getEras(JNIEnv* env, UResourceBundle* gregorian) {
    330     UErrorCode status = U_ZERO_ERROR;
    331     ScopedResourceBundle eras(ures_getByKey(gregorian, "eras", NULL, &status));
    332     if (U_FAILURE(status)) {
    333         return NULL;
    334     }
    335     ScopedResourceBundle abbreviatedEras(ures_getByKey(eras.get(), "abbreviated", NULL, &status));
    336     if (U_FAILURE(status)) {
    337         return NULL;
    338     }
    339     return toStringArray(env, abbreviatedEras.get(), ures_getSize(abbreviatedEras.get()), -1, 0);
    340 }
    341 
    342 enum NameType { REGULAR, STAND_ALONE };
    343 enum NameWidth { LONG, SHORT };
    344 static jobjectArray getNames(JNIEnv* env, UResourceBundle* namesBundle, bool months, NameType type, NameWidth width) {
    345     const char* typeKey = (type == REGULAR) ? "format" : "stand-alone";
    346     const char* widthKey = (width == LONG) ? "wide" : "abbreviated";
    347     UErrorCode status = U_ZERO_ERROR;
    348     ScopedResourceBundle formatBundle(ures_getByKey(namesBundle, typeKey, NULL, &status));
    349     ScopedResourceBundle valuesBundle(ures_getByKey(formatBundle.get(), widthKey, NULL, &status));
    350     if (U_FAILURE(status)) {
    351         return NULL;
    352     }
    353 
    354     // The months array has a trailing empty string. The days array has a leading empty string.
    355     int count = ures_getSize(valuesBundle.get());
    356     int offset = months ? 0 : 1;
    357     jobjectArray result = toStringArray(env, valuesBundle.get(), count, count + 1, offset);
    358     ScopedLocalRef<jstring> emptyString(env, env->NewStringUTF(""));
    359     env->SetObjectArrayElement(result, months ? count : 0, emptyString.get());
    360     return result;
    361 }
    362 
    363 static void setIntegerField(JNIEnv* env, jobject obj, const char* fieldName, int value) {
    364     ScopedLocalRef<jobject> integerValue(env, integerValueOf(env, value));
    365     jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/Integer;");
    366     env->SetObjectField(obj, fid, integerValue.get());
    367 }
    368 
    369 static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, jstring value) {
    370     jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "Ljava/lang/String;");
    371     env->SetObjectField(obj, fid, value);
    372     env->DeleteLocalRef(value);
    373 }
    374 
    375 static void setStringArrayField(JNIEnv* env, jobject obj, const char* fieldName, jobjectArray value) {
    376     jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "[Ljava/lang/String;");
    377     env->SetObjectField(obj, fid, value);
    378 }
    379 
    380 static void setStringField(JNIEnv* env, jobject obj, const char* fieldName, UResourceBundle* bundle, int index) {
    381     UErrorCode status = U_ZERO_ERROR;
    382     int charCount;
    383     const UChar* chars = ures_getStringByIndex(bundle, index, &charCount, &status);
    384     if (U_SUCCESS(status)) {
    385         setStringField(env, obj, fieldName, env->NewString(chars, charCount));
    386     } else {
    387         LOGE("Error setting String field %s from ICU resource: %s", fieldName, u_errorName(status));
    388     }
    389 }
    390 
    391 static bool setStringField(JNIEnv* env, jobject obj, const char* key, const char* fieldName, UResourceBundle* bundle) {
    392     if (bundle == NULL) {
    393         return false;
    394     }
    395     UErrorCode status = U_ZERO_ERROR;
    396     int charCount;
    397     const UChar* chars = ures_getStringByKey(bundle, key, &charCount, &status);
    398     if (U_SUCCESS(status)) {
    399         setStringField(env, obj, fieldName, env->NewString(chars, charCount));
    400         return true;
    401     } else {
    402         // Missing item in current resource bundle but not an error.
    403         return false;
    404     }
    405 }
    406 
    407 static void setStringField(JNIEnv* env, jobject obj, const char* key, const char* fieldName,
    408     UResourceBundle* bundle, UResourceBundle* fallbackBundle) {
    409     if (!setStringField(env, obj, key, fieldName, bundle) && fallbackBundle != NULL) {
    410         setStringField(env, obj, key, fieldName, fallbackBundle);
    411     }
    412 }
    413 
    414 static bool setCharField(JNIEnv* env, jobject obj, const char* key, const char* fieldName,
    415     UResourceBundle* bundle) {
    416     if (bundle == NULL) {
    417         return false;
    418     }
    419     UErrorCode status = U_ZERO_ERROR;
    420     int charCount;
    421     const UChar* chars = ures_getStringByKey(bundle, key, &charCount, &status);
    422     if (U_SUCCESS(status)) {
    423         jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, fieldName, "C");
    424         env->SetCharField(obj, fid, chars[0]);
    425         return true;
    426     } else {
    427         // Missing item in current resource bundle but not an error.
    428         return false;
    429     }
    430 }
    431 
    432 static void setCharField(JNIEnv* env, jobject obj, const char* key, const char* fieldName,
    433     UResourceBundle* bundle, UResourceBundle* fallbackBundle) {
    434     if (!setCharField(env, obj, key, fieldName, bundle) && fallbackBundle != NULL) {
    435         setCharField(env, obj, key, fieldName, fallbackBundle);
    436     }
    437 }
    438 
    439 static void setNumberSymbols(JNIEnv* env, jobject obj, UResourceBundle* numberSymbols, UResourceBundle* fallbackNumberSymbols) {
    440     setCharField(env, obj, "decimal", "decimalSeparator", numberSymbols, fallbackNumberSymbols);
    441     setCharField(env, obj, "group", "groupingSeparator", numberSymbols, fallbackNumberSymbols);
    442     setCharField(env, obj, "list", "patternSeparator", numberSymbols, fallbackNumberSymbols);
    443     setCharField(env, obj, "percentSign", "percent", numberSymbols, fallbackNumberSymbols);
    444     setCharField(env, obj, "perMille", "perMill", numberSymbols, fallbackNumberSymbols);
    445     setCharField(env, obj, "decimal", "monetarySeparator", numberSymbols, fallbackNumberSymbols);
    446     setCharField(env, obj, "minusSign", "minusSign", numberSymbols, fallbackNumberSymbols);
    447     setStringField(env, obj, "exponential", "exponentSeparator", numberSymbols, fallbackNumberSymbols);
    448     setStringField(env, obj, "infinity", "infinity", numberSymbols, fallbackNumberSymbols);
    449     setStringField(env, obj, "nan", "NaN", numberSymbols, fallbackNumberSymbols);
    450 }
    451 
    452 static void setZeroDigitToDefault(JNIEnv* env, jobject obj) {
    453     static jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, "zeroDigit", "C");
    454     env->SetCharField(obj, fid, '0');
    455 }
    456 
    457 static void setZeroDigit(JNIEnv* env, jobject obj, bool isLatn, char* buffer) {
    458     if (isLatn || buffer == NULL || buffer[0] == '\0') {
    459         return setZeroDigitToDefault(env, obj);
    460     }
    461     UErrorCode status = U_ZERO_ERROR;
    462     ScopedResourceBundle numSystemRoot(ures_openDirect(NULL, "numberingSystems", &status));
    463     if (U_FAILURE(status)) {
    464         return setZeroDigitToDefault(env, obj);
    465     }
    466     ScopedResourceBundle numSystem(ures_getByKey(numSystemRoot.get(), "numberingSystems", NULL, &status));
    467     if (U_FAILURE(status)) {
    468         return setZeroDigitToDefault(env, obj);
    469     }
    470     ScopedResourceBundle nonLatnSystem(ures_getByKey(numSystem.get(), buffer, NULL, &status));
    471     if (U_FAILURE(status)) {
    472         return setZeroDigitToDefault(env, obj);
    473     }
    474     int32_t charCount = 0;
    475     const UChar* chars = ures_getStringByKey(nonLatnSystem.get(), "desc", &charCount, &status);
    476     if (charCount == 0) {
    477         setZeroDigitToDefault(env, obj);
    478     } else {
    479         static jfieldID fid = env->GetFieldID(JniConstants::localeDataClass, "zeroDigit", "C");
    480         env->SetCharField(obj, fid, chars[0]);
    481     }
    482 }
    483 
    484 static void setNumberElements(JNIEnv* env, jobject obj, UResourceBundle* numberElements) {
    485     UErrorCode status = U_ZERO_ERROR;
    486     ScopedResourceBundle latnNumberRB(ures_getByKey(numberElements, "latn", NULL, &status));
    487     if (U_FAILURE(status)) {
    488         LOGW("Error getting ICU latn number elements system value: %s", u_errorName(status));
    489         return;
    490     }
    491     ScopedResourceBundle patternsRB(ures_getByKey(latnNumberRB.get(), "patterns", NULL, &status));
    492     if (U_FAILURE(status)) {
    493         LOGW("Error getting ICU latn number patterns value: %s", u_errorName(status));
    494         return;
    495     }
    496     // Get the patterns from the 'latn' numberElements
    497     // This is a temporary workaround for ICU ticket#8611.
    498     UResourceBundle* bundle = patternsRB.get();
    499     setStringField(env, obj, "currencyFormat", "currencyPattern", bundle);
    500     setStringField(env, obj, "decimalFormat", "numberPattern", bundle);
    501     setStringField(env, obj, "percentFormat", "percentPattern", bundle);
    502 
    503     status = U_ZERO_ERROR;
    504     bool isLatn = false;
    505     char buffer[256];
    506     buffer[0] = '\0';
    507     ScopedResourceBundle defaultNumberElem(ures_getByKey(numberElements, "default", NULL, &status));
    508     if (U_SUCCESS(status)) {
    509         int32_t charCount = 256;
    510         ures_getUTF8String(defaultNumberElem.get(), buffer, &charCount, true, &status);
    511         buffer[charCount] = '\0';
    512         if (U_FAILURE(status)) {
    513             LOGW("Error getting ICU default number element system value: %s", u_errorName(status));
    514             // Use latn number symbols instead.
    515             isLatn = true;
    516         } else {
    517             isLatn = (strcmp(buffer, "latn") == 0);
    518         }
    519     } else {
    520         // Not default data, fallback to latn number elements.
    521         isLatn = true;
    522     }
    523 
    524     status = U_ZERO_ERROR;
    525     setZeroDigit(env, obj, isLatn, buffer);
    526     if (isLatn) {
    527         ScopedResourceBundle symbolsRB(ures_getByKey(latnNumberRB.get(), "symbols", NULL, &status));
    528         if (U_SUCCESS(status)) {
    529             setNumberSymbols(env, obj, symbolsRB.get(), NULL);
    530         } else {
    531             LOGW("Missing ICU latn symbols system value: %s", u_errorName(status));
    532         }
    533     } else {
    534         // Get every symbol item from default numbering system first. If it does not
    535         // exist, get the symbol from latn numbering system.
    536         ScopedResourceBundle defaultNumberRB(ures_getByKey(numberElements, (const char*)buffer, NULL, &status));
    537         ScopedResourceBundle defaultSymbolsRB(ures_getByKey(defaultNumberRB.get(), "symbols", NULL, &status));
    538         if (U_FAILURE(status)) {
    539             LOGW("Missing ICU %s symbols system value: %s", buffer, u_errorName(status));
    540             isLatn = true;  // Fallback to latn symbols.
    541             status = U_ZERO_ERROR;
    542         }
    543         ScopedResourceBundle latnSymbolsRB(ures_getByKey(latnNumberRB.get(), "symbols", NULL, &status));
    544         if (isLatn && U_FAILURE(status)) {
    545             return;
    546         }
    547         setNumberSymbols(env, obj, defaultSymbolsRB.get(), latnSymbolsRB.get());
    548     }
    549 }
    550 
    551 static jboolean ICU_initLocaleDataImpl(JNIEnv* env, jclass, jstring locale, jobject localeData) {
    552     ScopedUtfChars localeName(env, locale);
    553     if (localeName.c_str() == NULL) {
    554         return JNI_FALSE;
    555     }
    556 
    557     UErrorCode status = U_ZERO_ERROR;
    558     ScopedResourceBundle root(ures_open(NULL, localeName.c_str(), &status));
    559     if (U_FAILURE(status)) {
    560         LOGE("Error getting ICU resource bundle: %s", u_errorName(status));
    561         return JNI_FALSE;
    562     }
    563 
    564     ScopedResourceBundle calendar(ures_getByKey(root.get(), "calendar", NULL, &status));
    565     if (U_FAILURE(status)) {
    566         LOGE("Error getting ICU calendar resource bundle: %s", u_errorName(status));
    567         return JNI_FALSE;
    568     }
    569 
    570     ScopedResourceBundle gregorian(ures_getByKey(calendar.get(), "gregorian", NULL, &status));
    571     if (U_FAILURE(status)) {
    572         LOGE("Error getting ICU gregorian resource bundle: %s", u_errorName(status));
    573         return JNI_FALSE;
    574     }
    575 
    576     int firstDayVals[] = { 0, 0 };
    577     if (getDayIntVector(env, gregorian.get(), firstDayVals)) {
    578         setIntegerField(env, localeData, "firstDayOfWeek", firstDayVals[0]);
    579         setIntegerField(env, localeData, "minimalDaysInFirstWeek", firstDayVals[1]);
    580     }
    581 
    582     jobjectArray amPmMarkers = getAmPmMarkers(env, gregorian.get());
    583     setStringArrayField(env, localeData, "amPm", amPmMarkers);
    584     env->DeleteLocalRef(amPmMarkers);
    585 
    586     jobjectArray eras = getEras(env, gregorian.get());
    587     setStringArrayField(env, localeData, "eras", eras);
    588     env->DeleteLocalRef(eras);
    589 
    590     ScopedResourceBundle dayNames(ures_getByKey(gregorian.get(), "dayNames", NULL, &status));
    591     ScopedResourceBundle monthNames(ures_getByKey(gregorian.get(), "monthNames", NULL, &status));
    592 
    593     // Get the regular month and weekday names.
    594     jobjectArray longMonthNames = getNames(env, monthNames.get(), true, REGULAR, LONG);
    595     jobjectArray shortMonthNames = getNames(env, monthNames.get(), true, REGULAR, SHORT);
    596     jobjectArray longWeekdayNames = getNames(env, dayNames.get(), false, REGULAR, LONG);
    597     jobjectArray shortWeekdayNames = getNames(env, dayNames.get(), false, REGULAR, SHORT);
    598     setStringArrayField(env, localeData, "longMonthNames", longMonthNames);
    599     setStringArrayField(env, localeData, "shortMonthNames", shortMonthNames);
    600     setStringArrayField(env, localeData, "longWeekdayNames", longWeekdayNames);
    601     setStringArrayField(env, localeData, "shortWeekdayNames", shortWeekdayNames);
    602 
    603     // Get the stand-alone month and weekday names. If they're not available (as they aren't for
    604     // English), we reuse the regular names. If we returned null to Java, the usual fallback
    605     // mechanisms would come into play and we'd end up with the bogus stand-alone names from the
    606     // root locale ("1" for January, and so on).
    607     jobjectArray longStandAloneMonthNames = getNames(env, monthNames.get(), true, STAND_ALONE, LONG);
    608     if (longStandAloneMonthNames == NULL) {
    609         longStandAloneMonthNames = longMonthNames;
    610     }
    611     jobjectArray shortStandAloneMonthNames = getNames(env, monthNames.get(), true, STAND_ALONE, SHORT);
    612     if (shortStandAloneMonthNames == NULL) {
    613         shortStandAloneMonthNames = shortMonthNames;
    614     }
    615     jobjectArray longStandAloneWeekdayNames = getNames(env, dayNames.get(), false, STAND_ALONE, LONG);
    616     if (longStandAloneWeekdayNames == NULL) {
    617         longStandAloneWeekdayNames = longWeekdayNames;
    618     }
    619     jobjectArray shortStandAloneWeekdayNames = getNames(env, dayNames.get(), false, STAND_ALONE, SHORT);
    620     if (shortStandAloneWeekdayNames == NULL) {
    621         shortStandAloneWeekdayNames = shortWeekdayNames;
    622     }
    623     setStringArrayField(env, localeData, "longStandAloneMonthNames", longStandAloneMonthNames);
    624     setStringArrayField(env, localeData, "shortStandAloneMonthNames", shortStandAloneMonthNames);
    625     setStringArrayField(env, localeData, "longStandAloneWeekdayNames", longStandAloneWeekdayNames);
    626     setStringArrayField(env, localeData, "shortStandAloneWeekdayNames", shortStandAloneWeekdayNames);
    627 
    628     ScopedResourceBundle dateTimePatterns(ures_getByKey(gregorian.get(), "DateTimePatterns", NULL, &status));
    629     if (U_SUCCESS(status)) {
    630         setStringField(env, localeData, "fullTimeFormat", dateTimePatterns.get(), 0);
    631         setStringField(env, localeData, "longTimeFormat", dateTimePatterns.get(), 1);
    632         setStringField(env, localeData, "mediumTimeFormat", dateTimePatterns.get(), 2);
    633         setStringField(env, localeData, "shortTimeFormat", dateTimePatterns.get(), 3);
    634         setStringField(env, localeData, "fullDateFormat", dateTimePatterns.get(), 4);
    635         setStringField(env, localeData, "longDateFormat", dateTimePatterns.get(), 5);
    636         setStringField(env, localeData, "mediumDateFormat", dateTimePatterns.get(), 6);
    637         setStringField(env, localeData, "shortDateFormat", dateTimePatterns.get(), 7);
    638     }
    639     status = U_ZERO_ERROR;
    640 
    641     // For numberPatterns and symbols.
    642     ScopedResourceBundle numberElements(ures_getByKey(root.get(), "NumberElements", NULL, &status));
    643     if (U_SUCCESS(status)) {
    644         setNumberElements(env, localeData, numberElements.get());
    645     }
    646     status = U_ZERO_ERROR;
    647 
    648     jstring countryCode = env->NewStringUTF(Locale::createFromName(localeName.c_str()).getCountry());
    649     jstring internationalCurrencySymbol = ICU_getCurrencyCode(env, NULL, countryCode);
    650     env->DeleteLocalRef(countryCode);
    651     countryCode = NULL;
    652 
    653     jstring currencySymbol = NULL;
    654     if (internationalCurrencySymbol != NULL) {
    655         currencySymbol = ICU_getCurrencySymbol(env, NULL, locale, internationalCurrencySymbol);
    656     } else {
    657         internationalCurrencySymbol = env->NewStringUTF("XXX");
    658     }
    659     if (currencySymbol == NULL) {
    660         // This is the UTF-8 encoding of U+00A4 (CURRENCY SIGN).
    661         currencySymbol = env->NewStringUTF("\xc2\xa4");
    662     }
    663     setStringField(env, localeData, "currencySymbol", currencySymbol);
    664     setStringField(env, localeData, "internationalCurrencySymbol", internationalCurrencySymbol);
    665 
    666     return JNI_TRUE;
    667 }
    668 
    669 static jstring ICU_toLowerCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) {
    670     ScopedJavaUnicodeString scopedString(env, javaString);
    671     UnicodeString& s(scopedString.unicodeString());
    672     UnicodeString original(s);
    673     s.toLower(Locale::createFromName(ScopedUtfChars(env, localeName).c_str()));
    674     return s == original ? javaString : env->NewString(s.getBuffer(), s.length());
    675 }
    676 
    677 static jstring ICU_toUpperCase(JNIEnv* env, jclass, jstring javaString, jstring localeName) {
    678     ScopedJavaUnicodeString scopedString(env, javaString);
    679     UnicodeString& s(scopedString.unicodeString());
    680     UnicodeString original(s);
    681     s.toUpper(Locale::createFromName(ScopedUtfChars(env, localeName).c_str()));
    682     return s == original ? javaString : env->NewString(s.getBuffer(), s.length());
    683 }
    684 
    685 static jstring versionString(JNIEnv* env, const UVersionInfo& version) {
    686     char versionString[U_MAX_VERSION_STRING_LENGTH];
    687     u_versionToString(const_cast<UVersionInfo&>(version), &versionString[0]);
    688     return env->NewStringUTF(versionString);
    689 }
    690 
    691 static jstring ICU_getIcuVersion(JNIEnv* env, jclass) {
    692     UVersionInfo icuVersion;
    693     u_getVersion(icuVersion);
    694     return versionString(env, icuVersion);
    695 }
    696 
    697 static jstring ICU_getUnicodeVersion(JNIEnv* env, jclass) {
    698     UVersionInfo unicodeVersion;
    699     u_getUnicodeVersion(unicodeVersion);
    700     return versionString(env, unicodeVersion);
    701 }
    702 
    703 
    704 struct EnumerationCounter {
    705     const size_t count;
    706     EnumerationCounter(size_t count) : count(count) {}
    707     size_t operator()() { return count; }
    708 };
    709 struct EnumerationGetter {
    710     UEnumeration* e;
    711     UErrorCode* status;
    712     EnumerationGetter(UEnumeration* e, UErrorCode* status) : e(e), status(status) {}
    713     const UChar* operator()(int32_t* charCount) { return uenum_unext(e, charCount, status); }
    714 };
    715 static jobject ICU_getAvailableCurrencyCodes(JNIEnv* env, jclass) {
    716     UErrorCode status = U_ZERO_ERROR;
    717     UEnumeration* e(ucurr_openISOCurrencies(UCURR_COMMON|UCURR_NON_DEPRECATED, &status));
    718     EnumerationCounter counter(uenum_count(e, &status));
    719     EnumerationGetter getter(e, &status);
    720     jobject result = toStringArray16(env, &counter, &getter);
    721     maybeThrowIcuException(env, status);
    722     uenum_close(e);
    723     return result;
    724 }
    725 
    726 static JNINativeMethod gMethods[] = {
    727     NATIVE_METHOD(ICU, addLikelySubtags, "(Ljava/lang/String;)Ljava/lang/String;"),
    728     NATIVE_METHOD(ICU, getAvailableBreakIteratorLocalesNative, "()[Ljava/lang/String;"),
    729     NATIVE_METHOD(ICU, getAvailableCalendarLocalesNative, "()[Ljava/lang/String;"),
    730     NATIVE_METHOD(ICU, getAvailableCollatorLocalesNative, "()[Ljava/lang/String;"),
    731     NATIVE_METHOD(ICU, getAvailableCurrencyCodes, "()[Ljava/lang/String;"),
    732     NATIVE_METHOD(ICU, getAvailableDateFormatLocalesNative, "()[Ljava/lang/String;"),
    733     NATIVE_METHOD(ICU, getAvailableLocalesNative, "()[Ljava/lang/String;"),
    734     NATIVE_METHOD(ICU, getAvailableNumberFormatLocalesNative, "()[Ljava/lang/String;"),
    735     NATIVE_METHOD(ICU, getCurrencyCode, "(Ljava/lang/String;)Ljava/lang/String;"),
    736     NATIVE_METHOD(ICU, getCurrencyDisplayName, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
    737     NATIVE_METHOD(ICU, getCurrencyFractionDigits, "(Ljava/lang/String;)I"),
    738     NATIVE_METHOD(ICU, getCurrencySymbol, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
    739     NATIVE_METHOD(ICU, getDisplayCountryNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
    740     NATIVE_METHOD(ICU, getDisplayLanguageNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
    741     NATIVE_METHOD(ICU, getDisplayVariantNative, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
    742     NATIVE_METHOD(ICU, getISO3CountryNative, "(Ljava/lang/String;)Ljava/lang/String;"),
    743     NATIVE_METHOD(ICU, getISO3LanguageNative, "(Ljava/lang/String;)Ljava/lang/String;"),
    744     NATIVE_METHOD(ICU, getISOCountriesNative, "()[Ljava/lang/String;"),
    745     NATIVE_METHOD(ICU, getISOLanguagesNative, "()[Ljava/lang/String;"),
    746     NATIVE_METHOD(ICU, getIcuVersion, "()Ljava/lang/String;"),
    747     NATIVE_METHOD(ICU, getScript, "(Ljava/lang/String;)Ljava/lang/String;"),
    748     NATIVE_METHOD(ICU, getUnicodeVersion, "()Ljava/lang/String;"),
    749     NATIVE_METHOD(ICU, initLocaleDataImpl, "(Ljava/lang/String;Llibcore/icu/LocaleData;)Z"),
    750     NATIVE_METHOD(ICU, toLowerCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
    751     NATIVE_METHOD(ICU, toUpperCase, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
    752 };
    753 int register_libcore_icu_ICU(JNIEnv* env) {
    754     std::string path;
    755     path = u_getDataDirectory();
    756     path += "/";
    757     path += U_ICUDATA_NAME;
    758     path += ".dat";
    759 
    760     #define FAIL_WITH_STRERROR(s) \
    761         LOGE("Couldn't " s " '%s': %s", path.c_str(), strerror(errno)); \
    762         return -1;
    763     #define MAYBE_FAIL_WITH_ICU_ERROR(s) \
    764         if (status != U_ZERO_ERROR) {\
    765             LOGE("Couldn't initialize ICU (" s "): %s (%s)", u_errorName(status), path.c_str()); \
    766             return -1; \
    767         }
    768 
    769     // Open the file and get its length.
    770     ScopedFd fd(open(path.c_str(), O_RDONLY));
    771     if (fd.get() == -1) {
    772         FAIL_WITH_STRERROR("open");
    773     }
    774     struct stat sb;
    775     if (fstat(fd.get(), &sb) == -1) {
    776         FAIL_WITH_STRERROR("stat");
    777     }
    778 
    779     // Map it.
    780     void* data = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd.get(), 0);
    781     if (data == MAP_FAILED) {
    782         FAIL_WITH_STRERROR("mmap");
    783     }
    784 
    785     // Tell the kernel that accesses are likely to be random rather than sequential.
    786     if (madvise(data, sb.st_size, MADV_RANDOM) == -1) {
    787         FAIL_WITH_STRERROR("madvise(MADV_RANDOM)");
    788     }
    789 
    790     // Tell ICU to use our memory-mapped data.
    791     UErrorCode status = U_ZERO_ERROR;
    792     udata_setCommonData(data, &status);
    793     MAYBE_FAIL_WITH_ICU_ERROR("udata_setCommonData");
    794     // Tell ICU it can *only* use our memory-mapped data.
    795     udata_setFileAccess(UDATA_NO_FILES, &status);
    796     MAYBE_FAIL_WITH_ICU_ERROR("udata_setFileAccess");
    797 
    798     // Failures to find the ICU data tend to be somewhat obscure because ICU loads its data on first
    799     // use, which can be anywhere. Force initialization up front so we can report a nice clear error
    800     // and bail.
    801     u_init(&status);
    802     MAYBE_FAIL_WITH_ICU_ERROR("u_init");
    803     return jniRegisterNativeMethods(env, "libcore/icu/ICU", gMethods, NELEM(gMethods));
    804 }
    805