Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (C) 2013 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 "IcuUtilities"
     18 
     19 #include "IcuUtilities.h"
     20 
     21 #include "JniConstants.h"
     22 #include "JniException.h"
     23 #include "ScopedLocalRef.h"
     24 #include "ScopedUtfChars.h"
     25 #include "UniquePtr.h"
     26 #include "cutils/log.h"
     27 #include "unicode/strenum.h"
     28 #include "unicode/uloc.h"
     29 #include "unicode/ustring.h"
     30 
     31 Locale getLocale(JNIEnv* env, jstring localeName) {
     32   return Locale::createFromName(ScopedUtfChars(env, localeName).c_str());
     33 }
     34 
     35 jobjectArray fromStringEnumeration(JNIEnv* env, StringEnumeration* se) {
     36   UniquePtr<StringEnumeration> deleter(se);
     37   if (se == NULL) {
     38     return NULL;
     39   }
     40 
     41   UErrorCode status = U_ZERO_ERROR;
     42   int32_t count = se->count(status);
     43   if (maybeThrowIcuException(env, "StringEnumeration::count", status)) {
     44     return NULL;
     45   }
     46 
     47   jobjectArray result = env->NewObjectArray(count, JniConstants::stringClass, NULL);
     48   for (int32_t i = 0; i < count; ++i) {
     49     const UnicodeString* string = se->snext(status);
     50     if (maybeThrowIcuException(env, "StringEnumeration::snext", status)) {
     51       return NULL;
     52     }
     53     ScopedLocalRef<jstring> javaString(env, env->NewString(string->getBuffer(), string->length()));
     54     env->SetObjectArrayElement(result, i, javaString.get());
     55   }
     56   return result;
     57 }
     58 
     59 bool maybeThrowIcuException(JNIEnv* env, const char* function, UErrorCode error) {
     60   if (U_SUCCESS(error)) {
     61     return false;
     62   }
     63   const char* exceptionClass = "java/lang/RuntimeException";
     64   if (error == U_ILLEGAL_ARGUMENT_ERROR) {
     65     exceptionClass = "java/lang/IllegalArgumentException";
     66   } else if (error == U_INDEX_OUTOFBOUNDS_ERROR || error == U_BUFFER_OVERFLOW_ERROR) {
     67     exceptionClass = "java/lang/ArrayIndexOutOfBoundsException";
     68   } else if (error == U_UNSUPPORTED_ERROR) {
     69     exceptionClass = "java/lang/UnsupportedOperationException";
     70   }
     71   jniThrowExceptionFmt(env, exceptionClass, "%s failed: %s", function, u_errorName(error));
     72   return true;
     73 }
     74