1 /* 2 * Copyright (C) 2014 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 #ifndef SCOPED_ICU_LOCALE_H_included 18 #define SCOPED_ICU_LOCALE_H_included 19 20 #include <nativehelper/JNIHelp.h> 21 #include <nativehelper/ScopedUtfChars.h> 22 #include "unicode/locid.h" 23 24 class ScopedIcuLocale { 25 public: ScopedIcuLocale(JNIEnv * env,jstring javaLanguageTag)26 ScopedIcuLocale(JNIEnv* env, jstring javaLanguageTag) : mEnv(env) { 27 mLocale.setToBogus(); 28 29 if (javaLanguageTag == NULL) { 30 jniThrowNullPointerException(mEnv, "javaLanguageTag == null"); 31 return; 32 } 33 34 const ScopedUtfChars languageTag(env, javaLanguageTag); 35 if (languageTag.c_str() == NULL) { 36 return; 37 } 38 39 UErrorCode err; 40 icu::Locale locale = icu::Locale::forLanguageTag(languageTag.c_str(), err); 41 if (U_FAILURE(err)) { 42 return; 43 } 44 45 mLocale = locale; 46 } 47 ~ScopedIcuLocale()48 ~ScopedIcuLocale() { 49 } 50 valid()51 bool valid() const { 52 return !mLocale.isBogus(); 53 } 54 locale()55 icu::Locale& locale() { 56 return mLocale; 57 } 58 59 private: 60 JNIEnv* const mEnv; 61 icu::Locale mLocale; 62 63 // Disallow copy and assignment. 64 ScopedIcuLocale(const ScopedIcuLocale&); 65 void operator=(const ScopedIcuLocale&); 66 }; 67 68 #endif // SCOPED_ICU_LOCALE_H_included 69