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 jobjectArray fromStringEnumeration(JNIEnv* env, UErrorCode& status, const char* provider, StringEnumeration* se) {
     32   if (maybeThrowIcuException(env, provider, status)) {
     33     return NULL;
     34   }
     35 
     36   int32_t count = se->count(status);
     37   if (maybeThrowIcuException(env, "StringEnumeration::count", status)) {
     38     return NULL;
     39   }
     40 
     41   jobjectArray result = env->NewObjectArray(count, JniConstants::stringClass, NULL);
     42   for (int32_t i = 0; i < count; ++i) {
     43     const UnicodeString* string = se->snext(status);
     44     if (maybeThrowIcuException(env, "StringEnumeration::snext", status)) {
     45       return NULL;
     46     }
     47     ScopedLocalRef<jstring> javaString(env, env->NewString(string->getBuffer(), string->length()));
     48     env->SetObjectArrayElement(result, i, javaString.get());
     49   }
     50   return result;
     51 }
     52 
     53 bool maybeThrowIcuException(JNIEnv* env, const char* function, UErrorCode error) {
     54   if (U_SUCCESS(error)) {
     55     return false;
     56   }
     57   const char* exceptionClass = "java/lang/RuntimeException";
     58   if (error == U_ILLEGAL_ARGUMENT_ERROR) {
     59     exceptionClass = "java/lang/IllegalArgumentException";
     60   } else if (error == U_INDEX_OUTOFBOUNDS_ERROR || error == U_BUFFER_OVERFLOW_ERROR) {
     61     exceptionClass = "java/lang/ArrayIndexOutOfBoundsException";
     62   } else if (error == U_UNSUPPORTED_ERROR) {
     63     exceptionClass = "java/lang/UnsupportedOperationException";
     64   } else if (error == U_FORMAT_INEXACT_ERROR) {
     65     exceptionClass = "java/lang/ArithmeticException";
     66   }
     67   jniThrowExceptionFmt(env, exceptionClass, "%s failed: %s", function, u_errorName(error));
     68   return true;
     69 }
     70