blob: c6f39502d3eca75d7996d49191eaab2d4879d235 [file] [log] [blame]
Elliott Hughes3aac4dd2013-02-04 15:37:52 -08001/*
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
31Locale getLocale(JNIEnv* env, jstring localeName) {
32 return Locale::createFromName(ScopedUtfChars(env, localeName).c_str());
33}
34
Elliott Hughes783a57a2013-08-26 14:59:01 -070035jobjectArray fromStringEnumeration(JNIEnv* env, UErrorCode& status, const char* provider, StringEnumeration* se) {
36 if (maybeThrowIcuException(env, provider, status)) {
Elliott Hughes3aac4dd2013-02-04 15:37:52 -080037 return NULL;
38 }
39
Elliott Hughes3aac4dd2013-02-04 15:37:52 -080040 int32_t count = se->count(status);
41 if (maybeThrowIcuException(env, "StringEnumeration::count", status)) {
42 return NULL;
43 }
44
45 jobjectArray result = env->NewObjectArray(count, JniConstants::stringClass, NULL);
46 for (int32_t i = 0; i < count; ++i) {
47 const UnicodeString* string = se->snext(status);
48 if (maybeThrowIcuException(env, "StringEnumeration::snext", status)) {
49 return NULL;
50 }
51 ScopedLocalRef<jstring> javaString(env, env->NewString(string->getBuffer(), string->length()));
52 env->SetObjectArrayElement(result, i, javaString.get());
53 }
54 return result;
55}
56
57bool maybeThrowIcuException(JNIEnv* env, const char* function, UErrorCode error) {
58 if (U_SUCCESS(error)) {
59 return false;
60 }
61 const char* exceptionClass = "java/lang/RuntimeException";
62 if (error == U_ILLEGAL_ARGUMENT_ERROR) {
63 exceptionClass = "java/lang/IllegalArgumentException";
64 } else if (error == U_INDEX_OUTOFBOUNDS_ERROR || error == U_BUFFER_OVERFLOW_ERROR) {
65 exceptionClass = "java/lang/ArrayIndexOutOfBoundsException";
66 } else if (error == U_UNSUPPORTED_ERROR) {
67 exceptionClass = "java/lang/UnsupportedOperationException";
Neil Fullerc96651d2014-04-09 10:58:08 +010068 } else if (error == U_FORMAT_INEXACT_ERROR) {
69 exceptionClass = "java/lang/ArithmeticException";
Elliott Hughes3aac4dd2013-02-04 15:37:52 -080070 }
71 jniThrowExceptionFmt(env, exceptionClass, "%s failed: %s", function, u_errorName(error));
72 return true;
73}