1313// limitations under the License.
1414#include < jni.h>
1515
16+ #include < exception>
17+ #include < string>
18+
1619#include " processor/wetext_processor.h"
1720#include " utils/wetext_flags.h"
1821#include " utils/wetext_string.h"
@@ -22,28 +25,85 @@ namespace wetextprocessing {
2225std::shared_ptr<wetext::Processor> processorTN;
2326std::shared_ptr<wetext::Processor> processorITN;
2427
28+ namespace {
29+ class UtfStringChars {
30+ public:
31+ UtfStringChars (JNIEnv* env, jstring string)
32+ : env_(env),
33+ string_ (string),
34+ chars_(env->GetStringUTFChars (string, nullptr )) {}
35+
36+ UtfStringChars (const UtfStringChars&) = delete;
37+ UtfStringChars& operator =(const UtfStringChars&) = delete;
38+
39+ ~UtfStringChars () {
40+ if (chars_ != nullptr ) {
41+ env_->ReleaseStringUTFChars (string_, chars_);
42+ }
43+ }
44+
45+ const char * get () const { return chars_; }
46+
47+ private:
48+ JNIEnv* env_;
49+ jstring string_;
50+ const char * chars_;
51+ };
52+
53+ bool CopyJString (JNIEnv* env, jstring string, std::string* output) {
54+ UtfStringChars chars (env, string);
55+ if (chars.get () == nullptr ) {
56+ return false ;
57+ }
58+ *output = chars.get ();
59+ return true ;
60+ }
61+
62+ void ThrowIllegalState (JNIEnv* env, const char * message) {
63+ jclass exception_class = env->FindClass (" java/lang/IllegalStateException" );
64+ if (exception_class != nullptr ) {
65+ env->ThrowNew (exception_class, message);
66+ }
67+ }
68+ }
69+
2570void init (JNIEnv* env, jobject, jstring jModelDir) {
26- const char * pModelDir = env->GetStringUTFChars (jModelDir, nullptr );
71+ std::string model_dir;
72+ if (!CopyJString (env, jModelDir, &model_dir)) {
73+ return ;
74+ }
2775
28- std::string tnTagger = std::string (pModelDir) + " /zh_tn_tagger.fst" ;
29- std::string tnVerbalizer = std::string (pModelDir) + " /zh_tn_verbalizer.fst" ;
30- processorTN = std::make_shared<wetext::Processor>(tnTagger, tnVerbalizer);
76+ try {
77+ std::string tnTagger = model_dir + " /zh_tn_tagger.fst" ;
78+ std::string tnVerbalizer = model_dir + " /zh_tn_verbalizer.fst" ;
79+ processorTN = std::make_shared<wetext::Processor>(tnTagger, tnVerbalizer);
3180
32- std::string itnTagger = std::string (pModelDir) + " /zh_itn_tagger.fst" ;
33- std::string itnVerbalizer = std::string (pModelDir) + " /zh_itn_verbalizer.fst" ;
34- processorITN = std::make_shared<wetext::Processor>(itnTagger, itnVerbalizer);
81+ std::string itnTagger = model_dir + " /zh_itn_tagger.fst" ;
82+ std::string itnVerbalizer = model_dir + " /zh_itn_verbalizer.fst" ;
83+ processorITN = std::make_shared<wetext::Processor>(itnTagger, itnVerbalizer);
84+ } catch (const std::exception& error) {
85+ processorTN.reset ();
86+ processorITN.reset ();
87+ ThrowIllegalState (env, error.what ());
88+ }
3589}
3690
3791jstring normalize (JNIEnv* env, jobject, jstring input) {
38- std::string input_text = std::string (env->GetStringUTFChars (input, nullptr ));
92+ std::string input_text;
93+ if (!CopyJString (env, input, &input_text)) {
94+ return nullptr ;
95+ }
3996 std::string tagged_text = processorTN->Tag (input_text);
4097 std::string normalized_text = processorTN->Verbalize (tagged_text);
4198
4299 return env->NewStringUTF (normalized_text.c_str ());
43100}
44101
45102jstring inverse_normalize (JNIEnv* env, jobject, jstring input) {
46- std::string input_text = std::string (env->GetStringUTFChars (input, nullptr ));
103+ std::string input_text;
104+ if (!CopyJString (env, input, &input_text)) {
105+ return nullptr ;
106+ }
47107 std::string tagged_text = processorITN->Tag (input_text);
48108 std::string normalized_text = processorITN->Verbalize (tagged_text);
49109
0 commit comments