jni_data_utils.h revision edc7d02d76d969a9074b5de1022fde226696dbcd
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 LATINIME_JNI_DATA_UTILS_H
18#define LATINIME_JNI_DATA_UTILS_H
19
20#include <vector>
21
22#include "defines.h"
23#include "jni.h"
24#include "suggest/core/policy/dictionary_header_structure_policy.h"
25#include "suggest/policyimpl/dictionary/header/header_read_write_utils.h"
26
27namespace latinime {
28
29class JniDataUtils {
30 public:
31    static void jintarrayToVector(JNIEnv *env, jintArray array, std::vector<int> *const outVector) {
32        if (!array) {
33            outVector->clear();
34            return;
35        }
36        const jsize arrayLength = env->GetArrayLength(array);
37        outVector->resize(arrayLength);
38        env->GetIntArrayRegion(array, 0 /* start */, arrayLength, outVector->data());
39    }
40
41    static DictionaryHeaderStructurePolicy::AttributeMap constructAttributeMap(JNIEnv *env,
42            jobjectArray attributeKeyStringArray, jobjectArray attributeValueStringArray) {
43        DictionaryHeaderStructurePolicy::AttributeMap attributeMap;
44        const int keyCount = env->GetArrayLength(attributeKeyStringArray);
45        for (int i = 0; i < keyCount; i++) {
46            jstring keyString = static_cast<jstring>(
47                    env->GetObjectArrayElement(attributeKeyStringArray, i));
48            const jsize keyUtf8Length = env->GetStringUTFLength(keyString);
49            char keyChars[keyUtf8Length + 1];
50            env->GetStringUTFRegion(keyString, 0, env->GetStringLength(keyString), keyChars);
51            keyChars[keyUtf8Length] = '\0';
52            DictionaryHeaderStructurePolicy::AttributeMap::key_type key;
53            HeaderReadWriteUtils::insertCharactersIntoVector(keyChars, &key);
54
55            jstring valueString = static_cast<jstring>(
56                    env->GetObjectArrayElement(attributeValueStringArray, i));
57            const jsize valueUtf8Length = env->GetStringUTFLength(valueString);
58            char valueChars[valueUtf8Length + 1];
59            env->GetStringUTFRegion(valueString, 0, env->GetStringLength(valueString), valueChars);
60            valueChars[valueUtf8Length] = '\0';
61            DictionaryHeaderStructurePolicy::AttributeMap::mapped_type value;
62            HeaderReadWriteUtils::insertCharactersIntoVector(valueChars, &value);
63            attributeMap[key] = value;
64        }
65        return attributeMap;
66    }
67
68 private:
69    DISALLOW_IMPLICIT_CONSTRUCTORS(JniDataUtils);
70};
71} // namespace latinime
72#endif // LATINIME_JNI_DATA_UTILS_H
73