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 "Transliterator"
     18 
     19 #include "IcuUtilities.h"
     20 #include "JNIHelp.h"
     21 #include "JniConstants.h"
     22 #include "JniException.h"
     23 #include "ScopedJavaUnicodeString.h"
     24 #include "ScopedStringChars.h"
     25 #include "unicode/translit.h"
     26 
     27 static Transliterator* fromPeer(jlong peer) {
     28   return reinterpret_cast<Transliterator*>(static_cast<uintptr_t>(peer));
     29 }
     30 
     31 static jlong Transliterator_create(JNIEnv* env, jclass, jstring javaId) {
     32   ScopedJavaUnicodeString id(env, javaId);
     33   if (!id.valid()) {
     34     return 0;
     35   }
     36   UErrorCode status = U_ZERO_ERROR;
     37   Transliterator* t = Transliterator::createInstance(id.unicodeString(), UTRANS_FORWARD, status);
     38   if (maybeThrowIcuException(env, "Transliterator::createInstance", status)) {
     39     return 0;
     40   }
     41   return reinterpret_cast<uintptr_t>(t);
     42 }
     43 
     44 static void Transliterator_destroy(JNIEnv*, jclass, jlong peer) {
     45   delete fromPeer(peer);
     46 }
     47 
     48 static jobjectArray Transliterator_getAvailableIDs(JNIEnv* env, jclass) {
     49   UErrorCode status = U_ZERO_ERROR;
     50   return fromStringEnumeration(env, Transliterator::getAvailableIDs(status));
     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