Resolved issues and concerns found during overall functionality testing.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / utils / AttributeValueBuilder.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
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 package oic.simulator.serviceprovider.utils;
18
19 import java.util.Vector;
20
21 import org.oic.simulator.AttributeValue;
22
23 public class AttributeValueBuilder {
24     public static AttributeValue build(String valueString,
25             AttributeValue.ValueType valueType) throws Exception {
26         int depth = findDepth(valueString);
27         if (0 == depth) {
28             return handleDepth0(valueString, valueType);
29         } else if (1 == depth) {
30             return handleDepth1(valueString, valueType);
31         } else if (2 == depth) {
32             return handleDepth2(valueString, valueType);
33         } else if (3 == depth) {
34             return handleDepth3(valueString, valueType);
35         }
36
37         return null;
38     }
39
40     private static int findDepth(String value) {
41         int depth = 0;
42         for (char ch : value.toCharArray()) {
43             if (ch == '[')
44                 depth++;
45             else
46                 break;
47         }
48
49         return depth;
50     }
51
52     private static boolean isValidSyntax(String value) {
53         int count = 0;
54         for (char ch : value.toCharArray()) {
55             if (ch == '[')
56                 count++;
57             if (ch == ']')
58                 count--;
59         }
60
61         if (count == 0)
62             return true;
63         return false;
64
65     }
66
67     private static AttributeValue handleDepth0(String valueString,
68             AttributeValue.ValueType valueType) {
69         valueString = valueString.trim();
70         if (0 != findDepth(valueString))
71             return null;
72
73         try {
74             if (valueType == AttributeValue.ValueType.INTEGER)
75                 return new AttributeValue(Integer.parseInt(valueString));
76             else if (valueType == AttributeValue.ValueType.DOUBLE) {
77                 Double value = Double.parseDouble(valueString);
78                 if (!value.isInfinite()) {
79                     return new AttributeValue(value);
80                 }
81             } else if (valueType == AttributeValue.ValueType.BOOLEAN) {
82                 if (valueString.equalsIgnoreCase("true")
83                         || valueString.equalsIgnoreCase("false"))
84                     return new AttributeValue(Boolean.parseBoolean(valueString));
85             } else if (valueType == AttributeValue.ValueType.STRING)
86                 return new AttributeValue(valueString);
87         } catch (Exception e) {
88             return null;
89         }
90         return null;
91     }
92
93     private static String[] splitIntoArrays(String value) {
94         Vector<String> values = new Vector<String>();
95         String valueString = new String(value);
96         valueString = valueString.substring(valueString.indexOf('[') + 1,
97                 valueString.lastIndexOf(']'));
98
99         int count = 0;
100         int startPos = 0;
101         char[] charArray = valueString.toCharArray();
102         for (int index = 0; index < charArray.length; index++) {
103             if (charArray[index] == '[' && 0 == count++) {
104                 startPos = index;
105             }
106
107             if (charArray[index] == ']' && 0 == --count) {
108                 values.add(valueString.substring(startPos, index + 1));
109             }
110         }
111
112         String[] result = new String[values.size()];
113         values.toArray(result);
114         return result;
115     }
116
117     private static AttributeValue handleDepth1(String valueString,
118             AttributeValue.ValueType valueType) {
119         valueString = valueString.trim();
120         if (1 != findDepth(valueString) || false == isValidSyntax(valueString))
121             return null;
122
123         valueString = valueString.substring(valueString.indexOf('[') + 1,
124                 valueString.lastIndexOf(']'));
125         String[] valuesString = valueString.split(",");
126         if (null == valuesString || 0 == valuesString.length)
127             return null;
128
129         if (valueType == AttributeValue.ValueType.INTEGER) {
130             if (1 == valuesString.length && valuesString[0].isEmpty()) {
131                 return new AttributeValue(new Integer[0]);
132             }
133
134             Integer[] result = new Integer[valuesString.length];
135             for (int index = 0; index < valuesString.length; index++) {
136                 if (null != valuesString[index]
137                         && !valuesString[index].isEmpty()) {
138                     Integer value = (Integer) handleDepth0(valuesString[index],
139                             valueType).get();
140                     if (null == value)
141                         return null;
142                     result[index] = value;
143                 }
144             }
145             return new AttributeValue(result);
146         } else if (valueType == AttributeValue.ValueType.DOUBLE) {
147             if (1 == valuesString.length && valuesString[0].isEmpty()) {
148                 return new AttributeValue(new Double[0]);
149             }
150
151             Double[] result = new Double[valuesString.length];
152             for (int index = 0; index < valuesString.length; index++) {
153                 if (null != valuesString[index]
154                         && !valuesString[index].isEmpty()) {
155                     Double value = (Double) handleDepth0(valuesString[index],
156                             valueType).get();
157                     if (null == value)
158                         return null;
159                     result[index] = value;
160                 }
161             }
162             return new AttributeValue(result);
163         } else if (valueType == AttributeValue.ValueType.BOOLEAN) {
164             if (1 == valuesString.length && valuesString[0].isEmpty()) {
165                 return new AttributeValue(new Boolean[0]);
166             }
167
168             Boolean[] result = new Boolean[valuesString.length];
169             for (int index = 0; index < valuesString.length; index++) {
170                 if (null != valuesString[index]
171                         && !valuesString[index].isEmpty()) {
172                     Boolean value = (Boolean) handleDepth0(valuesString[index],
173                             valueType).get();
174                     if (null == value)
175                         return null;
176                     result[index] = value;
177                 }
178             }
179             return new AttributeValue(result);
180         } else if (valueType == AttributeValue.ValueType.STRING) {
181             if (1 == valuesString.length && valuesString[0].isEmpty()) {
182                 return new AttributeValue(new String[0]);
183             }
184
185             for (int index = 0; index < valuesString.length; index++) {
186                 if (null != valuesString[index]
187                         && !valuesString[index].isEmpty()) {
188                     valuesString[index] = valuesString[index].trim();
189                 }
190             }
191             return new AttributeValue(valuesString);
192         }
193
194         return null;
195     }
196
197     private static AttributeValue handleDepth2(String valueString,
198             AttributeValue.ValueType valueType) {
199         valueString = valueString.trim();
200         if (2 != findDepth(valueString) || false == isValidSyntax(valueString))
201             return null;
202
203         String[] valuesString = splitIntoArrays(valueString);
204         if (null == valuesString || 0 == valuesString.length)
205             return null;
206
207         if (valueType == AttributeValue.ValueType.INTEGER) {
208             Integer[][] result = new Integer[valuesString.length][];
209             for (int index = 0; index < valuesString.length; index++) {
210                 Integer[] value = (Integer[]) handleDepth1(valuesString[index],
211                         valueType).get();
212                 if (null == value)
213                     return null;
214                 result[index] = value;
215             }
216             return new AttributeValue(result);
217         } else if (valueType == AttributeValue.ValueType.DOUBLE) {
218             Double[][] result = new Double[valuesString.length][];
219             for (int index = 0; index < valuesString.length; index++) {
220                 Double[] value = (Double[]) handleDepth1(valuesString[index],
221                         valueType).get();
222                 if (null == value)
223                     return null;
224                 result[index] = value;
225             }
226             return new AttributeValue(result);
227         } else if (valueType == AttributeValue.ValueType.BOOLEAN) {
228             Boolean[][] result = new Boolean[valuesString.length][];
229             for (int index = 0; index < valuesString.length; index++) {
230                 Boolean[] value = (Boolean[]) handleDepth1(valuesString[index],
231                         valueType).get();
232                 if (null == value)
233                     return null;
234                 result[index] = value;
235             }
236             return new AttributeValue(result);
237         } else if (valueType == AttributeValue.ValueType.STRING) {
238             String[][] result = new String[valuesString.length][];
239             for (int index = 0; index < valuesString.length; index++) {
240                 String[] value = (String[]) handleDepth1(valuesString[index],
241                         valueType).get();
242                 if (null == value)
243                     return null;
244                 result[index] = value;
245             }
246             return new AttributeValue(result);
247         }
248
249         return null;
250     }
251
252     public static AttributeValue handleDepth3(String valueString,
253             AttributeValue.ValueType valueType) {
254         valueString = valueString.trim();
255         if (3 != findDepth(valueString) || false == isValidSyntax(valueString))
256             return null;
257
258         String[] valuesString = splitIntoArrays(valueString);
259         if (null == valuesString || 0 == valuesString.length)
260             return null;
261
262         if (valueType == AttributeValue.ValueType.INTEGER) {
263             Integer[][][] result = new Integer[valuesString.length][][];
264             for (int index = 0; index < valuesString.length; index++) {
265                 Integer[][] value = (Integer[][]) handleDepth2(
266                         valuesString[index], valueType).get();
267                 if (null == value)
268                     return null;
269                 result[index] = value;
270             }
271             return new AttributeValue(result);
272         } else if (valueType == AttributeValue.ValueType.DOUBLE) {
273             Double[][][] result = new Double[valuesString.length][][];
274             for (int index = 0; index < valuesString.length; index++) {
275                 Double[][] value = (Double[][]) handleDepth2(
276                         valuesString[index], valueType).get();
277                 if (null == value)
278                     return null;
279                 result[index] = value;
280             }
281             return new AttributeValue(result);
282         } else if (valueType == AttributeValue.ValueType.BOOLEAN) {
283             Boolean[][][] result = new Boolean[valuesString.length][][];
284             for (int index = 0; index < valuesString.length; index++) {
285                 Boolean[][] value = (Boolean[][]) handleDepth2(
286                         valuesString[index], valueType).get();
287                 if (null == value)
288                     return null;
289                 result[index] = value;
290             }
291             return new AttributeValue(result);
292         } else if (valueType == AttributeValue.ValueType.STRING) {
293             String[][][] result = new String[valuesString.length][][];
294             for (int index = 0; index < valuesString.length; index++) {
295                 String[][] value = (String[][]) handleDepth2(
296                         valuesString[index], valueType).get();
297                 if (null == value)
298                     return null;
299                 result[index] = value;
300             }
301             return new AttributeValue(result);
302         }
303
304         return null;
305     }
306 }