13aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes/* 23aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * Copyright (C) 2013 The Android Open Source Project 33aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * 43aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 53aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * you may not use this file except in compliance with the License. 63aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * You may obtain a copy of the License at 73aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * 83aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 93aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * 103aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * Unless required by applicable law or agreed to in writing, software 113aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 123aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * See the License for the specific language governing permissions and 143aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes * limitations under the License. 153aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes */ 163aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes 173aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#define LOG_TAG "IcuUtilities" 183aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes 196d8d1ea910c1ad026b48c87424da92a250664dd2Mark Salyzyn#include <android/log.h> 206d8d1ea910c1ad026b48c87424da92a250664dd2Mark Salyzyn 213aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "IcuUtilities.h" 223aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes 233aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "JniConstants.h" 243aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "JniException.h" 253aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "ScopedLocalRef.h" 263aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "ScopedUtfChars.h" 273aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "unicode/strenum.h" 283aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "unicode/ustring.h" 296d8d1ea910c1ad026b48c87424da92a250664dd2Mark Salyzyn#include "unicode/uloc.h" 303aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes 31a04b5c3d39232c7616591883ee2124520e3ab622Elliott HughesjobjectArray fromStringEnumeration(JNIEnv* env, UErrorCode& status, const char* provider, icu::StringEnumeration* se) { 32783a57a7ece339bc246925e328bc82b50cae427aElliott Hughes if (maybeThrowIcuException(env, provider, status)) { 333aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes return NULL; 343aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } 353aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes 363aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes int32_t count = se->count(status); 373aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes if (maybeThrowIcuException(env, "StringEnumeration::count", status)) { 383aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes return NULL; 393aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } 403aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes 413aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes jobjectArray result = env->NewObjectArray(count, JniConstants::stringClass, NULL); 423aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes for (int32_t i = 0; i < count; ++i) { 43a04b5c3d39232c7616591883ee2124520e3ab622Elliott Hughes const icu::UnicodeString* string = se->snext(status); 443aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes if (maybeThrowIcuException(env, "StringEnumeration::snext", status)) { 453aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes return NULL; 463aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } 473aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes ScopedLocalRef<jstring> javaString(env, env->NewString(string->getBuffer(), string->length())); 483aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes env->SetObjectArrayElement(result, i, javaString.get()); 493aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } 503aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes return result; 513aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes} 523aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes 533aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughesbool maybeThrowIcuException(JNIEnv* env, const char* function, UErrorCode error) { 543aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes if (U_SUCCESS(error)) { 553aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes return false; 563aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } 573aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes const char* exceptionClass = "java/lang/RuntimeException"; 583aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes if (error == U_ILLEGAL_ARGUMENT_ERROR) { 593aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes exceptionClass = "java/lang/IllegalArgumentException"; 603aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } else if (error == U_INDEX_OUTOFBOUNDS_ERROR || error == U_BUFFER_OVERFLOW_ERROR) { 613aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes exceptionClass = "java/lang/ArrayIndexOutOfBoundsException"; 623aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } else if (error == U_UNSUPPORTED_ERROR) { 633aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes exceptionClass = "java/lang/UnsupportedOperationException"; 64c96651d01dc87a20aa718d60feaff6611451852eNeil Fuller } else if (error == U_FORMAT_INEXACT_ERROR) { 65c96651d01dc87a20aa718d60feaff6611451852eNeil Fuller exceptionClass = "java/lang/ArithmeticException"; 663aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes } 673aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes jniThrowExceptionFmt(env, exceptionClass, "%s failed: %s", function, u_errorName(error)); 683aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes return true; 693aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes} 70