Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcRepresentation.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22 #include "JniOcRepresentation.h"
23 #include "JniUtils.h"
24
25 using namespace OC;
26
27 OCRepresentation* JniOcRepresentation::getOCRepresentationPtr(JNIEnv *env, jobject thiz)
28 {
29     OCRepresentation *rep = GetHandle<OCRepresentation>(env, thiz);
30     if (env->ExceptionCheck())
31     {
32         LOGE("Failed to get native handle from OcRepresentation");
33     }
34     if (!rep)
35     {
36         ThrowOcException(JNI_NO_NATIVE_POINTER, "");
37     }
38     return rep;
39 }
40
41 /*
42 * Class:     org_iotivity_base_OcRepresentation
43 * Method:    getValueN
44 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
45 */
46 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getValueN
47 (JNIEnv *env, jobject thiz, jstring jKey)
48 {
49     LOGD("OcRepresentation_getValue");
50     if (!jKey)
51     {
52         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
53         return nullptr;
54     }
55     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
56     if (!rep) return nullptr;
57
58     std::string key = env->GetStringUTFChars(jKey, nullptr);
59
60     AttributeValue attrValue;
61     if (!rep->getAttributeValue(key.c_str(), attrValue))
62     {
63         ThrowOcException(JNI_NO_SUCH_KEY, " attribute key does not exist");
64         return nullptr;
65     }
66     return boost::apply_visitor(JObjectConverter(env), attrValue);
67 }
68
69 /*
70 * Class:     org_iotivity_base_OcRepresentation
71 * Method:    setValueInteger
72 * Signature: (Ljava/lang/String;I)V
73 */
74 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger
75 (JNIEnv *env, jobject thiz, jstring jKey, jint jValue)
76 {
77     LOGD("OcRepresentation_setValueInteger");
78     if (!jKey)
79     {
80         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
81         return;
82     }
83     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
84     if (!rep) return;
85
86     std::string str = env->GetStringUTFChars(jKey, nullptr);
87     rep->setValue(str, static_cast<int>(jValue));
88 }
89
90 /*
91 * Class:     org_iotivity_base_OcRepresentation
92 * Method:    setValueDouble
93 * Signature: (Ljava/lang/String;D)V
94 */
95 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble
96 (JNIEnv *env, jobject thiz, jstring jKey, jdouble jValue)
97 {
98     LOGD("OcRepresentation_setValueDouble");
99     if (!jKey)
100     {
101         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
102         return;
103     }
104     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
105     if (!rep) return;
106
107     std::string str = env->GetStringUTFChars(jKey, nullptr);
108     rep->setValue(str, static_cast<double>(jValue));
109 }
110
111 /*
112 * Class:     org_iotivity_base_OcRepresentation
113 * Method:    setValueBoolean
114 * Signature: (Ljava/lang/String;Z)V
115 */
116 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean
117 (JNIEnv *env, jobject thiz, jstring jKey, jboolean jValue)
118 {
119     LOGD("OcRepresentation_setValueBoolean");
120     if (!jKey)
121     {
122         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
123         return;
124     }
125     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
126     if (!rep) return;
127
128     std::string str = env->GetStringUTFChars(jKey, nullptr);
129     rep->setValue(str, static_cast<bool>(jValue));
130 }
131
132 /*
133 * Class:     org_iotivity_base_OcRepresentation
134 * Method:    setValueStringN
135 * Signature: (Ljava/lang/String;Ljava/lang/String;)V
136 */
137 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueStringN
138 (JNIEnv *env, jobject thiz, jstring jKey, jstring jValue)
139 {
140     LOGD("OcRepresentation_setValueString");
141     if (!jKey)
142     {
143         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
144         return;
145     }
146     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
147     if (!rep) return;
148
149     std::string key = env->GetStringUTFChars(jKey, nullptr);
150     std::string value = env->GetStringUTFChars(jValue, nullptr);
151
152     rep->setValue(key, value);
153 }
154
155 /*
156 * Class:     org_iotivity_base_OcRepresentation
157 * Method:    setValueRepresentation
158 * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;)V
159 */
160 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation
161 (JNIEnv *env, jobject thiz, jstring jKey, jobject jValue)
162 {
163     LOGD("OcRepresentation_setValueRepresentation");
164     if (!jKey)
165     {
166         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
167         return;
168     }
169     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
170     if (!rep) return;
171
172     std::string key = env->GetStringUTFChars(jKey, nullptr);
173
174     if (jValue)
175     {
176         OCRepresentation *value = JniOcRepresentation::getOCRepresentationPtr(env, jValue);
177         if (!value) return;
178         rep->setValue(key, *value);
179     }
180     else
181     {
182         rep->setNULL(key);
183     }
184 }
185
186 /*
187 * Class:     org_iotivity_base_OcRepresentation
188 * Method:    setValueIntegerArray
189 * Signature: (Ljava/lang/String;[I)V
190 */
191 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueIntegerArray
192 (JNIEnv *env, jobject thiz, jstring jKey, jintArray jValue)
193 {
194     LOGD("OcRepresentation_setValueIntegerArray");
195     if (!jKey)
196     {
197         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
198         return;
199     }
200
201     const jsize len = env->GetArrayLength(jValue);
202     jint* ints = env->GetIntArrayElements(jValue, nullptr);
203
204     std::vector<int> value;
205     for (jsize i = 0; i < len; ++i)
206     {
207         value.push_back(static_cast<int>(ints[i]));
208     }
209     env->ReleaseIntArrayElements(jValue, ints, JNI_ABORT);
210
211     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
212     if (!rep) return;
213
214     std::string key = env->GetStringUTFChars(jKey, nullptr);
215     rep->setValue(key, value);
216 }
217
218 /*
219 * Class:     org_iotivity_base_OcRepresentation
220 * Method:    setValueInteger2DArray
221 * Signature: (Ljava/lang/String;[[I)V
222 */
223 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger2DArray
224 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
225 {
226     LOGD("OcRepresentation__setValueInteger2DArray");
227     if (!jKey)
228     {
229         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
230         return;
231     }
232     std::vector<std::vector<int>> value;
233     const jsize lenOuter = env->GetArrayLength(jValue);
234     for (jsize j = 0; j < lenOuter; ++j)
235     {
236         jintArray jInnerArray = static_cast<jintArray>(env->GetObjectArrayElement(jValue, j));
237         jint* ints = env->GetIntArrayElements(jInnerArray, nullptr);
238         std::vector<int> innerVector;
239         const jsize lenInner = env->GetArrayLength(jInnerArray);
240         for (jsize i = 0; i < lenInner; ++i)
241         {
242             innerVector.push_back(static_cast<int>(ints[i]));
243         }
244         env->ReleaseIntArrayElements(jInnerArray, ints, JNI_ABORT);
245         env->DeleteLocalRef(jInnerArray);
246         value.push_back(innerVector);
247     }
248
249     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
250     if (!rep) return;
251
252     std::string key = env->GetStringUTFChars(jKey, nullptr);
253     rep->setValue(key, value);
254 }
255
256 /*
257 * Class:     org_iotivity_base_OcRepresentation
258 * Method:    setValueInteger3DArray
259 * Signature: (Ljava/lang/String;[[[I)V
260 */
261 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger3DArray
262 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
263 {
264     LOGD("OcRepresentation_setValueInteger3DArray");
265     if (!jKey)
266     {
267         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
268         return;
269     }
270     std::vector<std::vector<std::vector<int>>> value;
271     const jsize lenOuter = env->GetArrayLength(jValue);
272     for (jsize k = 0; k < lenOuter; ++k)
273     {
274         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
275         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
276         std::vector<std::vector<int>> middleArray;
277         for (jsize j = 0; j < lenMiddle; ++j)
278         {
279             jintArray jInnerArray = static_cast<jintArray>(env->GetObjectArrayElement(jMiddleArray, j));
280             jint* ints = env->GetIntArrayElements(jInnerArray, nullptr);
281             std::vector<int> innerVector;
282             const jsize lenInner = env->GetArrayLength(jInnerArray);
283             for (jsize i = 0; i < lenInner; ++i)
284             {
285                 innerVector.push_back(static_cast<int>(ints[i]));
286             }
287             env->ReleaseIntArrayElements(jInnerArray, ints, JNI_ABORT);
288             env->DeleteLocalRef(jInnerArray);
289             middleArray.push_back(innerVector);
290         }
291         env->DeleteLocalRef(jMiddleArray);
292         value.push_back(middleArray);
293     }
294
295     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
296     if (!rep) return;
297
298     std::string key = env->GetStringUTFChars(jKey, nullptr);
299     rep->setValue(key, value);
300 }
301
302 /*
303 * Class:     org_iotivity_base_OcRepresentation
304 * Method:    setValueDoubleArray
305 * Signature: (Ljava/lang/String;[D)V
306 */
307 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDoubleArray
308 (JNIEnv *env, jobject thiz, jstring jKey, jdoubleArray jValue)
309 {
310     LOGD("OcRepresentation_setValueDoubleArray");
311     if (!jKey)
312     {
313         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
314         return;
315     }
316
317     const jsize len = env->GetArrayLength(jValue);
318     jdouble* doubles = env->GetDoubleArrayElements(jValue, nullptr);
319
320     std::vector<double> value;
321     for (jsize i = 0; i < len; ++i)
322     {
323         value.push_back(static_cast<double>(doubles[i]));
324     }
325     env->ReleaseDoubleArrayElements(jValue, doubles, JNI_ABORT);
326
327     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
328     if (!rep) return;
329
330     std::string key = env->GetStringUTFChars(jKey, nullptr);
331     rep->setValue(key, value);
332 }
333
334 /*
335 * Class:     org_iotivity_base_OcRepresentation
336 * Method:    setValueDouble2DArray
337 * Signature: (Ljava/lang/String;[[D)V
338 */
339 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble2DArray
340 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
341 {
342     LOGD("OcRepresentation_setValueDouble2DArray");
343     if (!jKey)
344     {
345         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
346         return;
347     }
348     std::vector<std::vector<double>> value;
349     const jsize lenOuter = env->GetArrayLength(jValue);
350     for (jsize j = 0; j < lenOuter; ++j)
351     {
352         jdoubleArray jInnerArray = static_cast<jdoubleArray>(env->GetObjectArrayElement(jValue, j));
353         jdouble* doubles = env->GetDoubleArrayElements(jInnerArray, nullptr);
354         std::vector<double> innerVector;
355         const jsize lenInner = env->GetArrayLength(jInnerArray);
356         for (jsize i = 0; i < lenInner; ++i)
357         {
358             innerVector.push_back(static_cast<double>(doubles[i]));
359         }
360         env->ReleaseDoubleArrayElements(jInnerArray, doubles, JNI_ABORT);
361         env->DeleteLocalRef(jInnerArray);
362         value.push_back(innerVector);
363     }
364
365     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
366     if (!rep) return;
367
368     std::string key = env->GetStringUTFChars(jKey, nullptr);
369     rep->setValue(key, value);
370 }
371
372 /*
373 * Class:     org_iotivity_base_OcRepresentation
374 * Method:    setValueDouble3DArray
375 * Signature: (Ljava/lang/String;[[[D)V
376 */
377 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble3DArray
378 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
379 {
380     LOGD("OcRepresentation_setValueDouble3DArray");
381     if (!jKey)
382     {
383         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
384         return;
385     }
386     std::vector<std::vector<std::vector<double>>> value;
387     const jsize lenOuter = env->GetArrayLength(jValue);
388     for (jsize k = 0; k < lenOuter; ++k)
389     {
390         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
391         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
392         std::vector<std::vector<double>> middleArray;
393         for (jsize j = 0; j < lenMiddle; ++j)
394         {
395             jdoubleArray jInnerArray = static_cast<jdoubleArray>(env->GetObjectArrayElement(jMiddleArray, j));
396             jdouble* doubles = env->GetDoubleArrayElements(jInnerArray, nullptr);
397             std::vector<double> innerVector;
398             const jsize lenInner = env->GetArrayLength(jInnerArray);
399             for (jsize i = 0; i < lenInner; ++i)
400             {
401                 innerVector.push_back(static_cast<double>(doubles[i]));
402             }
403             env->ReleaseDoubleArrayElements(jInnerArray, doubles, JNI_ABORT);
404             env->DeleteLocalRef(jInnerArray);
405             middleArray.push_back(innerVector);
406         }
407         env->DeleteLocalRef(jMiddleArray);
408         value.push_back(middleArray);
409     }
410
411     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
412     if (!rep) return;
413
414     std::string key = env->GetStringUTFChars(jKey, nullptr);
415     rep->setValue(key, value);
416 }
417
418 /*
419 * Class:     org_iotivity_base_OcRepresentation
420 * Method:    setValueBooleanArray
421 * Signature: (Ljava/lang/String;[Z)V
422 */
423 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBooleanArray
424 (JNIEnv *env, jobject thiz, jstring jKey, jbooleanArray jValue)
425 {
426     LOGD("OcRepresentation_setValueBooleanArray");
427     if (!jKey)
428     {
429         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
430         return;
431     }
432
433     const jsize len = env->GetArrayLength(jValue);
434     jboolean* booleans = env->GetBooleanArrayElements(jValue, nullptr);
435
436     std::vector<bool> value;
437     for (jsize i = 0; i < len; ++i)
438     {
439         value.push_back(static_cast<bool>(booleans[i]));
440     }
441     env->ReleaseBooleanArrayElements(jValue, booleans, JNI_ABORT);
442
443     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
444     if (!rep) return;
445
446     std::string key = env->GetStringUTFChars(jKey, nullptr);
447     rep->setValue(key, value);
448 }
449
450 /*
451 * Class:     org_iotivity_base_OcRepresentation
452 * Method:    setValueBoolean2DArray
453 * Signature: (Ljava/lang/String;[[Z)V
454 */
455 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean2DArray
456 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
457 {
458     LOGD("OcRepresentation_setValueBoolean2DArray");
459     if (!jKey)
460     {
461         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
462         return;
463     }
464     std::vector<std::vector<bool>> value;
465     const jsize lenOuter = env->GetArrayLength(jValue);
466     for (jsize j = 0; j < lenOuter; ++j)
467     {
468         jbooleanArray jInnerArray = static_cast<jbooleanArray>(env->GetObjectArrayElement(jValue, j));
469         const jsize lenInner = env->GetArrayLength(jInnerArray);
470         jboolean* booleans = env->GetBooleanArrayElements(jInnerArray, nullptr);
471
472         std::vector<bool> innerVector;
473         for (jsize i = 0; i < lenInner; ++i)
474         {
475             innerVector.push_back(static_cast<bool>(booleans[i]));
476         }
477         env->ReleaseBooleanArrayElements(jInnerArray, booleans, JNI_ABORT);
478         env->DeleteLocalRef(jInnerArray);
479         value.push_back(innerVector);
480     }
481
482     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
483     if (!rep) return;
484
485     std::string key = env->GetStringUTFChars(jKey, nullptr);
486     rep->setValue(key, value);
487 }
488
489 /*
490 * Class:     org_iotivity_base_OcRepresentation
491 * Method:    setValueBoolean3DArray
492 * Signature: (Ljava/lang/String;[[[Z)V
493 */
494 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean3DArray
495 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
496 {
497     LOGD("OcRepresentation_setValueBoolean3DArray");
498     if (!jKey)
499     {
500         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
501         return;
502     }
503     std::vector<std::vector<std::vector<bool>>> value;
504     const jsize lenOuter = env->GetArrayLength(jValue);
505     for (jsize k = 0; k < lenOuter; ++k)
506     {
507         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
508         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
509         std::vector<std::vector<bool>> middleArray;
510         for (jsize j = 0; j < lenMiddle; ++j)
511         {
512             jbooleanArray jInnerArray = static_cast<jbooleanArray>(env->GetObjectArrayElement(jMiddleArray, j));
513             const jsize lenInner = env->GetArrayLength(jInnerArray);
514             jboolean* booleans = env->GetBooleanArrayElements(jInnerArray, nullptr);
515
516             std::vector<bool> innerVector;
517             for (jsize i = 0; i < lenInner; ++i)
518             {
519                 innerVector.push_back(static_cast<bool>(booleans[i]));
520             }
521             env->ReleaseBooleanArrayElements(jInnerArray, booleans, JNI_ABORT);
522             env->DeleteLocalRef(jInnerArray);
523             middleArray.push_back(innerVector);
524         }
525         env->DeleteLocalRef(jMiddleArray);
526         value.push_back(middleArray);
527     }
528
529     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
530     if (!rep) return;
531
532     std::string key = env->GetStringUTFChars(jKey, nullptr);
533     rep->setValue(key, value);
534 }
535
536 /*
537 * Class:     org_iotivity_base_OcRepresentation
538 * Method:    setValueStringArray
539 * Signature: (Ljava/lang/String;[Ljava/lang/String;)V
540 */
541 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueStringArray
542 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
543 {
544     LOGD("OcRepresentation_setValueStringArray");
545     if (!jKey)
546     {
547         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
548         return;
549     }
550
551     std::vector<std::string> value;
552     JniUtils::convertJavaStrArrToStrVector(env, jValue, value);
553
554     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
555     if (!rep) return;
556
557     std::string key = env->GetStringUTFChars(jKey, nullptr);
558     rep->setValue(key, value);
559 }
560
561 /*
562 * Class:     org_iotivity_base_OcRepresentation
563 * Method:    setValueString2DArray
564 * Signature: (Ljava/lang/String;[[Ljava/lang/String;)V
565 */
566 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueString2DArray
567 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
568 {
569     LOGD("OcRepresentation_setValueString2DArray");
570     if (!jKey)
571     {
572         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
573         return;
574     }
575     std::vector<std::vector<std::string>> value;
576     const jsize lenOuter = env->GetArrayLength(jValue);
577     for (jsize j = 0; j < lenOuter; ++j)
578     {
579         jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, j));
580         std::vector<std::string> innerVector;
581         JniUtils::convertJavaStrArrToStrVector(env, jInnerArray, innerVector);
582         env->DeleteLocalRef(jInnerArray);
583         value.push_back(innerVector);
584     }
585
586     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
587     if (!rep) return;
588
589     std::string key = env->GetStringUTFChars(jKey, nullptr);
590     rep->setValue(key, value);
591 }
592
593 /*
594 * Class:     org_iotivity_base_OcRepresentation
595 * Method:    setValueString3DArray
596 * Signature: (Ljava/lang/String;[[[Ljava/lang/String;)V
597 */
598 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueString3DArray
599 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
600 {
601     LOGD("OcRepresentation_setValueString3DArray");
602     if (!jKey)
603     {
604         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
605         return;
606     }
607     std::vector<std::vector<std::vector<std::string>>> value;
608     const jsize lenOuter = env->GetArrayLength(jValue);
609     for (jsize k = 0; k < lenOuter; ++k)
610     {
611         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
612         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
613         std::vector<std::vector<std::string>> middleArray;
614         for (jsize j = 0; j < lenMiddle; ++j)
615         {
616             jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jMiddleArray, j));
617             std::vector<std::string> innerVector;
618             JniUtils::convertJavaStrArrToStrVector(env, jInnerArray, innerVector);
619             env->DeleteLocalRef(jInnerArray);
620             middleArray.push_back(innerVector);
621         }
622         env->DeleteLocalRef(jMiddleArray);
623         value.push_back(middleArray);
624     }
625
626     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
627     if (!rep) return;
628
629     std::string key = env->GetStringUTFChars(jKey, nullptr);
630     rep->setValue(key, value);
631 }
632
633 /*
634 * Class:     org_iotivity_base_OcRepresentation
635 * Method:    setValueRepresentationArray
636 * Signature: (Ljava/lang/String;[Lorg/iotivity/base/OcRepresentation;)V
637 */
638 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentationArray
639 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
640 {
641     LOGD("OcRepresentation_setValueRepresentationArray");
642     if (!jKey)
643     {
644         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
645         return;
646     }
647
648     std::vector<OCRepresentation> value;
649     JniUtils::convertJavaRepresentationArrToVector(env, jValue, value);
650
651     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
652     if (!rep) return;
653
654     std::string key = env->GetStringUTFChars(jKey, nullptr);
655     rep->setValue(key, value);
656 }
657
658 /*
659 * Class:     org_iotivity_base_OcRepresentation
660 * Method:    setValueRepresentation2DArray
661 * Signature: (Ljava/lang/String;[[Lorg/iotivity/base/OcRepresentation;)V
662 */
663 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation2DArray
664 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
665 {
666     LOGD("OcRepresentation_setValueRepresentation2DArray");
667     if (!jKey)
668     {
669         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
670         return;
671     }
672     std::vector<std::vector<OCRepresentation>> value;
673     const jsize lenOuter = env->GetArrayLength(jValue);
674     for (jsize j = 0; j < lenOuter; ++j)
675     {
676         jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, j));
677         std::vector<OCRepresentation> innerVector;
678         JniUtils::convertJavaRepresentationArrToVector(env, jInnerArray, innerVector);
679         env->DeleteLocalRef(jInnerArray);
680         value.push_back(innerVector);
681     }
682
683     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
684     if (!rep) return;
685
686     std::string key = env->GetStringUTFChars(jKey, nullptr);
687     rep->setValue(key, value);
688 }
689
690 /*
691 * Class:     org_iotivity_base_OcRepresentation
692 * Method:    setValueRepresentation3DArray
693 * Signature: (Ljava/lang/String;[[[Lorg/iotivity/base/OcRepresentation;)V
694 */
695 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation3DArray
696 (JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
697 {
698     LOGD("OcRepresentation_setValueRepresentation3DArray");
699     if (!jKey)
700     {
701         ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
702         return;
703     }
704     std::vector<std::vector<std::vector<OCRepresentation>>> value;
705     const jsize lenOuter = env->GetArrayLength(jValue);
706     for (jsize k = 0; k < lenOuter; ++k)
707     {
708         jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
709         const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
710         std::vector<std::vector<OCRepresentation>> middleArray;
711         for (jsize j = 0; j < lenMiddle; ++j)
712         {
713             jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jMiddleArray, j));
714             std::vector<OCRepresentation> innerVector;
715             JniUtils::convertJavaRepresentationArrToVector(env, jInnerArray, innerVector);
716             env->DeleteLocalRef(jInnerArray);
717             middleArray.push_back(innerVector);
718         }
719         env->DeleteLocalRef(jMiddleArray);
720         value.push_back(middleArray);
721     }
722
723     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
724     if (!rep) return;
725
726     std::string key = env->GetStringUTFChars(jKey, nullptr);
727     rep->setValue(key, value);
728 }
729
730 /*
731 * Class:     org_iotivity_base_OcRepresentation
732 * Method:    addChild
733 * Signature: (Lorg/iotivity/base/OcRepresentation;)V
734 */
735 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_addChild
736 (JNIEnv *env, jobject thiz, jobject jOcRepresentation)
737 {
738     LOGD("OcRepresentation_addChild");
739     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
740     if (!rep) return;
741
742     OCRepresentation *child = JniOcRepresentation::getOCRepresentationPtr(env, jOcRepresentation);
743     if (!child) return;
744
745     rep->addChild(*child);
746 }
747
748 /*
749 * Class:     org_iotivity_base_OcRepresentation
750 * Method:    clearChildren
751 * Signature: ()V
752 */
753 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_clearChildren
754 (JNIEnv *env, jobject thiz)
755 {
756     LOGD("OcRepresentation_clearChildren");
757     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
758     if (!rep) return;
759
760     rep->clearChildren();
761 }
762
763 /*
764 * Class:     org_iotivity_base_OcRepresentation
765 * Method:    getChildrenArray
766 * Signature: ()[Lorg/iotivity/base/OcRepresentation;
767 */
768 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcRepresentation_getChildrenArray
769 (JNIEnv *env, jobject thiz)
770 {
771     LOGD("OcRepresentation_getChildrenArray");
772     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
773     if (!rep) return nullptr;
774
775     return JniUtils::convertRepresentationVectorToJavaArray(env, rep->getChildren());
776 }
777
778 /*
779 * Class:     org_iotivity_base_OcRepresentation
780 * Method:    getUri
781 * Signature: ()Ljava/lang/String;
782 */
783 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcRepresentation_getUri
784 (JNIEnv *env, jobject thiz)
785 {
786     LOGD("OcRepresentation_getUri");
787     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
788     if (!rep) return nullptr;
789
790     std::string uri(rep->getUri());
791     return env->NewStringUTF(uri.c_str());
792 }
793
794 /*
795 * Class:     org_iotivity_base_OcRepresentation
796 * Method:    getHost
797 * Signature: ()Ljava/lang/String;
798 */
799 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcRepresentation_getHost
800 (JNIEnv *env, jobject thiz)
801 {
802     LOGD("OcRepresentation_getHost");
803     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
804     if (!rep) return nullptr;
805
806     std::string uri(rep->getHost());
807     return env->NewStringUTF(uri.c_str());
808 }
809
810 /*
811 * Class:     org_iotivity_base_OcRepresentation
812 * Method:    setUri
813 * Signature: (Ljava/lang/String;)V
814 */
815 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setUri
816 (JNIEnv *env, jobject thiz, jstring jUri)
817 {
818     LOGD("OcRepresentation_setUri");
819     if (!jUri)
820     {
821         ThrowOcException(OC_STACK_INVALID_PARAM, "uri cannot be null");
822         return;
823     }
824     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
825     if (!rep) return;
826
827     rep->setUri(env->GetStringUTFChars(jUri, nullptr));
828 }
829
830 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_hasAttribute
831 (JNIEnv *env, jobject thiz, jstring jstr)
832 {
833     LOGD("OcRepresentation_hasAttribute");
834     if (!jstr)
835     {
836         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
837         return false;
838     }
839     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
840     if (!rep) return false;
841
842     std::string str = env->GetStringUTFChars(jstr, nullptr);
843     return rep->hasAttribute(str);
844 }
845
846 /*
847 * Class:     org_iotivity_base_OcRepresentation
848 * Method:    getResourceTypes
849 * Signature: ()Ljava/util/List;
850 */
851 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getResourceTypes
852 (JNIEnv *env, jobject thiz)
853 {
854     LOGD("OcRepresentation_getResourceTypes");
855     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
856     if (!rep) return nullptr;
857
858     std::vector<std::string> resourceTypes = rep->getResourceTypes();
859     return JniUtils::convertStrVectorToJavaStrList(env, resourceTypes);
860 }
861
862 /*
863 * Class:     org_iotivity_base_OcRepresentation
864 * Method:    setResourceTypeArray
865 * Signature: ([Ljava/lang/String;)V
866 */
867 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setResourceTypeArray
868 (JNIEnv *env, jobject thiz, jobjectArray jResourceTypeArray)
869 {
870     LOGD("OcRepresentation_setResourceTypeArray");
871     if (!jResourceTypeArray)
872     {
873         ThrowOcException(OC_STACK_INVALID_PARAM, "resourceTypeList cannot be null");
874         return;
875     }
876     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
877     if (!rep) return;
878
879     std::vector<std::string> resourceTypes;
880     JniUtils::convertJavaStrArrToStrVector(env, jResourceTypeArray, resourceTypes);
881     rep->setResourceTypes(resourceTypes);
882 }
883 /*
884 * Class:     org_iotivity_base_OcRepresentation
885 * Method:    getResourceInterfaces
886 * Signature: ()Ljava/util/List;
887 */
888 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getResourceInterfaces
889 (JNIEnv *env, jobject thiz)
890 {
891     LOGD("OcRepresentation_getResourceInterfaces");
892     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
893     if (!rep) return nullptr;
894
895     std::vector<std::string> resourceInterfaces = rep->getResourceInterfaces();
896     return JniUtils::convertStrVectorToJavaStrList(env, resourceInterfaces);
897 }
898
899 /*
900 * Class:     org_iotivity_base_OcRepresentation
901 * Method:    setResourceInterfaceArray
902 * Signature: ([Ljava/lang/String;)V
903 */
904 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setResourceInterfaceArray
905 (JNIEnv *env, jobject thiz, jobjectArray jResourceInterfaceArray)
906 {
907     LOGD("OcRepresentation_setResourceInterfaceArray");
908     if (!jResourceInterfaceArray)
909     {
910         ThrowOcException(OC_STACK_INVALID_PARAM, "resourceInterfaceList cannot be null");
911         return;
912     }
913     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
914     if (!rep) return;
915
916     std::vector<std::string> resourceInterfaces;
917     JniUtils::convertJavaStrArrToStrVector(env, jResourceInterfaceArray, resourceInterfaces);
918     rep->setResourceInterfaces(resourceInterfaces);
919 }
920
921 /*
922 * Class:     org_iotivity_base_OcRepresentation
923 * Method:    isEmpty
924 * Signature: ()Z
925 */
926 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_isEmpty
927 (JNIEnv *env, jobject thiz)
928 {
929     LOGD("OcRepresentation_isEmpty");
930     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
931     if (!rep) return false;
932
933     return static_cast<jboolean>(rep->empty());
934 }
935
936 /*
937 * Class:     org_iotivity_base_OcRepresentation
938 * Method:    size
939 * Signature: ()I
940 */
941 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcRepresentation_size
942 (JNIEnv *env, jobject thiz)
943 {
944     LOGD("OcRepresentation_size");
945     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
946     if (!rep) return -1;
947
948     return static_cast<jint>(rep->numberOfAttributes());
949 }
950
951 /*
952 * Class:     org_iotivity_base_OcRepresentation
953 * Method:    remove
954 * Signature: (Ljava/lang/String;)Z
955 */
956 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_remove
957 (JNIEnv *env, jobject thiz, jstring jAttributeKey)
958 {
959     LOGD("OcRepresentation_remove");
960     if (!jAttributeKey)
961     {
962         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
963         return false;
964     }
965     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
966     if (!rep) return false;
967
968     std::string attributeKey = env->GetStringUTFChars(jAttributeKey, nullptr);
969     return static_cast<jboolean>(rep->erase(attributeKey));
970 }
971
972 /*
973 * Class:     org_iotivity_base_OcRepresentation
974 * Method:    setNull
975 * Signature: (Ljava/lang/String;)V
976 */
977 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setNull
978 (JNIEnv *env, jobject thiz, jstring jAttributeKey)
979 {
980     LOGD("OcRepresentation_setNull");
981     if (!jAttributeKey)
982     {
983         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
984         return;
985     }
986     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
987     if (!rep) return;
988
989     std::string attributeKey = env->GetStringUTFChars(jAttributeKey, nullptr);
990     rep->setNULL(attributeKey);
991 }
992
993 /*
994 * Class:     org_iotivity_base_OcRepresentation
995 * Method:    isNull
996 * Signature: (Ljava/lang/String;)Z
997 */
998 JNIEXPORT jboolean JNICALL Java_org_iotivity_base_OcRepresentation_isNull
999 (JNIEnv *env, jobject thiz, jstring jAttributeKey)
1000 {
1001     LOGD("OcRepresentation_isNull");
1002     if (!jAttributeKey)
1003     {
1004         ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
1005         return false;
1006     }
1007     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1008     if (!rep) return false;
1009
1010     std::string attributeKey = env->GetStringUTFChars(jAttributeKey, nullptr);
1011     return static_cast<jboolean>(rep->isNULL(attributeKey));
1012 }
1013
1014 /*
1015 * Class:     org_iotivity_base_OcRepresentation
1016 * Method:    create
1017 * Signature: ()V
1018 */
1019 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_create
1020 (JNIEnv *env, jobject thiz)
1021 {
1022     LOGD("OcRepresentation_create");
1023     OCRepresentation *rep = new OCRepresentation();
1024     SetHandle<OCRepresentation>(env, thiz, rep);
1025     if (env->ExceptionCheck())
1026     {
1027         LOGE("Failed to set native handle for OcRepresentation");
1028         delete rep;
1029     }
1030 }
1031
1032 /*
1033 * Class:     org_iotivity_base_OcRepresentation
1034 * Method:    dispose
1035 * Signature: ()V
1036 */
1037 JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_dispose
1038 (JNIEnv *env, jobject thiz, jboolean jNeedsDelete)
1039 {
1040     LOGD("OcRepresentation_dispose");
1041     OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
1042
1043     if (jNeedsDelete)
1044     {
1045         delete rep;
1046     }
1047 }