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 "Transliterator" 18 19 #include "IcuUtilities.h" 20 #include "JNIHelp.h" 21 #include "JniConstants.h" 22 #include "JniException.h" 23 #include "ScopedJavaUnicodeString.h" 24 #include "unicode/translit.h" 25 26 static Transliterator* fromPeer(jlong peer) { 27 return reinterpret_cast<Transliterator*>(static_cast<uintptr_t>(peer)); 28 } 29 30 static jlong Transliterator_create(JNIEnv* env, jclass, jstring javaId) { 31 ScopedJavaUnicodeString id(env, javaId); 32 if (!id.valid()) { 33 return 0; 34 } 35 UErrorCode status = U_ZERO_ERROR; 36 Transliterator* t = Transliterator::createInstance(id.unicodeString(), UTRANS_FORWARD, status); 37 if (maybeThrowIcuException(env, "Transliterator::createInstance", status)) { 38 return 0; 39 } 40 return reinterpret_cast<uintptr_t>(t); 41 } 42 43 static void Transliterator_destroy(JNIEnv*, jclass, jlong peer) { 44 delete fromPeer(peer); 45 } 46 47 static jobjectArray Transliterator_getAvailableIDs(JNIEnv* env, jclass) { 48 UErrorCode status = U_ZERO_ERROR; 49 StringEnumeration* e = Transliterator::getAvailableIDs(status); 50 return fromStringEnumeration(env, status, "Transliterator::getAvailableIDs", e); 51 } 52 53 static jstring Transliterator_transliterate(JNIEnv* env, jclass, jlong peer, jstring javaString) { 54 Transliterator* t = fromPeer(peer); 55 ScopedJavaUnicodeString string(env, javaString); 56 if (!string.valid()) { 57 return NULL; 58 } 59 60 UnicodeString& s(string.unicodeString()); 61 t->transliterate(s); 62 return env->NewString(s.getBuffer(), s.length()); 63 } 64 65 static JNINativeMethod gMethods[] = { 66 NATIVE_METHOD(Transliterator, create, "(Ljava/lang/String;)J"), 67 NATIVE_METHOD(Transliterator, destroy, "(J)V"), 68 NATIVE_METHOD(Transliterator, getAvailableIDs, "()[Ljava/lang/String;"), 69 NATIVE_METHOD(Transliterator, transliterate, "(JLjava/lang/String;)Ljava/lang/String;"), 70 }; 71 void register_libcore_icu_Transliterator(JNIEnv* env) { 72 jniRegisterNativeMethods(env, "libcore/icu/Transliterator", gMethods, NELEM(gMethods)); 73 } 74