Displaying and editing the complex value types for attributes.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / utils / Utility.java
index 254f1f8..9c13390 100644 (file)
@@ -22,7 +22,10 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
+import org.oic.simulator.AttributeValue;
 import org.oic.simulator.SimulatorException;
+import org.oic.simulator.AttributeValue.TypeInfo;
+import org.oic.simulator.AttributeValue.ValueType;
 
 /**
  * This class has common utility methods.
@@ -93,4 +96,69 @@ public class Utility {
         }
         return detail;
     }
+
+    // This method only works for attributes whose values are of type int,
+    // double, bool, string and 1-D array of primitive types
+    public static String getAttributeValueAsString(AttributeValue val) {
+        if (null == val) {
+            return null;
+        }
+        Object value = val.get();
+        if (null == value) {
+            return null;
+        }
+        TypeInfo type = val.typeInfo();
+        if (type.mType == ValueType.RESOURCEMODEL
+                || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL)
+                || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
+            return null;
+        }
+        if (type.mType == ValueType.ARRAY) {
+            if (type.mBaseType == ValueType.INTEGER) {
+                Integer[] values = (Integer[]) value;
+                if (null == values || values.length < 1) {
+                    return null;
+                }
+                List<Integer> list = new ArrayList<Integer>();
+                for (Integer i : values) {
+                    list.add(i);
+                }
+                return list.toString();
+            } else if (type.mBaseType == ValueType.DOUBLE) {
+                Double[] values = (Double[]) value;
+                if (null == values || values.length < 1) {
+                    return null;
+                }
+                List<Double> list = new ArrayList<Double>();
+                for (Double i : values) {
+                    list.add(i);
+                }
+                return list.toString();
+            } else if (type.mBaseType == ValueType.BOOLEAN) {
+                Boolean[] values = (Boolean[]) value;
+                if (null == values || values.length < 1) {
+                    return null;
+                }
+                List<Boolean> list = new ArrayList<Boolean>();
+                for (Boolean i : values) {
+                    list.add(i);
+                }
+                return list.toString();
+            } else if (type.mBaseType == ValueType.STRING) {
+                String[] values = (String[]) value;
+                if (null == values || values.length < 1) {
+                    return null;
+                }
+                List<String> list = new ArrayList<String>();
+                for (String i : values) {
+                    list.add(i);
+                }
+                return list.toString();
+            } else {
+                return null;
+            }
+        } else {
+            return String.valueOf(value);
+        }
+    }
 }
\ No newline at end of file