1 /******************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "simulator_resource_attribute_jni.h"
22 #include "simulator_resource_model_jni.h"
23 #include "simulator_resource_model_schema_jni.h"
24 #include "simulator_utils_jni.h"
25 #include "jni_string.h"
28 jobject SimulatorResourceAttributeToJava(JNIEnv *env,
29 const SimulatorResourceAttribute &attribute)
31 static jmethodID simulatorResAttributeCtor = env->GetMethodID(
32 gSimulatorClassRefs.simulatorResourceAttributeCls, "<init>",
33 "(Ljava/lang/String;Lorg/oic/simulator/AttributeValue;Lorg/oic/simulator/AttributeProperty;)V");
35 jstring jAttrName = env->NewStringUTF(attribute.getName().c_str());
36 jobject jAttrValue = AttributeValueToJava(env, attribute.getValue());
37 jobject jAttrProperty = AttributePropertyToJava(env, attribute.getProperty());
39 return env->NewObject(gSimulatorClassRefs.simulatorResourceAttributeCls,
40 simulatorResAttributeCtor, jAttrName, jAttrValue, jAttrProperty);
43 bool SimulatorResourceAttributeToCpp(JNIEnv *env, jobject jAttribute,
44 SimulatorResourceAttribute &attribute)
49 static jfieldID nameFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
50 "mName", "Ljava/lang/String;");
51 static jfieldID valueFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
52 "mValue", "Lorg/oic/simulator/AttributeValue;");
53 static jfieldID propertyFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
54 "mProperty", "Lorg/oic/simulator/AttributeProperty;");
56 jstring jAttrName = (jstring) env->GetObjectField(jAttribute, nameFID);
57 jobject jAttrValue = env->GetObjectField(jAttribute, valueFID);
58 jobject jAttrProperty = env->GetObjectField(jAttribute, propertyFID);
60 if (!jAttrName || !jAttrValue)
63 JniString attrName(env, jAttrName);
64 AttributeValueVariant value;
65 AttributeValueToCpp(env, jAttrValue, value);
67 attribute.setName(attrName.get());
68 attribute.setValue(value);
71 std::shared_ptr<AttributeProperty> property = AttributePropertyToCpp(env, jAttrProperty);
72 attribute.setProperty(property);
78 jobject SimulatorResourceAttributesToJava(JNIEnv *env,
79 const std::map<std::string, SimulatorResourceAttribute> &attributes)
81 JniMap jAttributesMap(env);
83 // Add attribute values for Attributes table
84 for (auto &attributeEntry : attributes)
86 jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
87 jobject jAttribute = SimulatorResourceAttributeToJava(env, attributeEntry.second);
89 jAttributesMap.put(jAttrName, jAttribute);
92 return jAttributesMap.get();