Fix for SVACE and Klocwork issues in RAML parser module.
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / jsonSchemaParser / JsonSchema.cpp
1 /******************************************************************\r
2  *\r
3  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 /**\r
22  * @file   JsonSchema.cpp\r
23  *\r
24  * @brief   This file reads data from Json Schema.\r
25  */\r
26 \r
27 #include "JsonSchema.h"\r
28 #include <iostream>\r
29 namespace RAML\r
30 {\r
31 \r
32     void JsonSchema::readJson()\r
33     {\r
34         if (! m_cjson)\r
35             return;\r
36 \r
37         cJSON *jsonId = cJSON_GetObjectItem(m_cjson, "id");\r
38         if (jsonId)\r
39         {\r
40             m_id = jsonId->valuestring;\r
41         }\r
42         cJSON *jsonSchema = cJSON_GetObjectItem(m_cjson, "$schema");\r
43         if (jsonSchema)\r
44         {\r
45             m_schema = jsonSchema->valuestring;\r
46         }\r
47         cJSON *jsonTitle = cJSON_GetObjectItem(m_cjson, "title");\r
48         if (jsonTitle)\r
49         {\r
50             m_title = jsonTitle->valuestring;\r
51         }\r
52         cJSON *jsonType = cJSON_GetObjectItem(m_cjson, "type");\r
53         if (jsonType)\r
54         {\r
55             m_type = jsonType->valuestring;\r
56         }\r
57         cJSON *jsonDescription = cJSON_GetObjectItem(m_cjson, "description");\r
58         if (jsonDescription)\r
59         {\r
60             m_description = jsonDescription->valuestring;\r
61         }\r
62         cJSON *jsonDefinitions = cJSON_GetObjectItem(m_cjson, "definitions");\r
63         if (jsonDefinitions)\r
64         {\r
65             cJSON *childDefinitions = jsonDefinitions->child;\r
66             while (childDefinitions)\r
67             {\r
68                 std::string defName = childDefinitions->string;\r
69                 addDefinition(defName, readDef(childDefinitions, defName));\r
70                 childDefinitions = childDefinitions->next;\r
71             }\r
72         }\r
73         cJSON *jsonProperties = cJSON_GetObjectItem(m_cjson, "properties");\r
74         if (jsonProperties)\r
75         {\r
76             cJSON *childProperties = jsonProperties->child;\r
77             while (childProperties)\r
78             {\r
79                 std::string attName = childProperties->string;\r
80                 addProperty(attName, readProp(childProperties, attName));\r
81                 childProperties = childProperties->next;\r
82             }\r
83         }\r
84         if (m_type == "array")\r
85         {\r
86             PropertiesPtr property = std::make_shared<Properties>("array");\r
87             readArray(m_cjson, property);\r
88             addProperty("array" , property);\r
89         }\r
90 \r
91         cJSON *jsonAdditionalProperties = cJSON_GetObjectItem(m_cjson, "additionalProperties");\r
92         if (jsonAdditionalProperties)\r
93             m_additionalProperties = jsonAdditionalProperties->type;\r
94         else\r
95             m_additionalProperties = cJSON_True;\r
96 \r
97         cJSON *jsonReference = cJSON_GetObjectItem(m_cjson, "$ref");\r
98         if (jsonReference)\r
99         {\r
100             JsonParameters param;\r
101             readJsonRef(jsonReference, param);\r
102 \r
103             for (auto it : param.getProperties())\r
104             {\r
105                 addProperty(it.first, it.second);\r
106             }\r
107             for ( auto it : param.getRequired())\r
108             {\r
109                 setRequiredValue(it);\r
110             }\r
111             if (m_type.empty())\r
112                 m_type = param.getType();\r
113         }\r
114         cJSON *jsonAllOf = cJSON_GetObjectItem(m_cjson, "allOf");\r
115         if (jsonAllOf)\r
116         {\r
117             JsonParameters param;\r
118 \r
119             readAllOf(jsonAllOf, param);\r
120 \r
121             for (auto it : param.getProperties())\r
122             {\r
123                 addProperty(it.first, it.second);\r
124             }\r
125             for ( auto it : param.getRequired())\r
126             {\r
127                 setRequiredValue(it);\r
128             }\r
129             if (m_type.empty())\r
130                 m_type = param.getType();\r
131         }\r
132         cJSON *jsonRequiredValues = cJSON_GetObjectItem(m_cjson, "required");\r
133         if (jsonRequiredValues)\r
134         {\r
135             int size = cJSON_GetArraySize(jsonRequiredValues);\r
136             int index = 0;\r
137             do\r
138             {\r
139                 setRequiredValue(cJSON_GetArrayItem(jsonRequiredValues, index)->valuestring);\r
140             }\r
141             while ( ++index < size);\r
142         }\r
143     }\r
144 \r
145     DefinitionsPtr JsonSchema::readDef(cJSON *childDefinitions, const std::string &defName)\r
146     {\r
147         DefinitionsPtr definition = std::make_shared<Definitions>(defName);\r
148 \r
149         cJSON *defType = cJSON_GetObjectItem(childDefinitions, "type");\r
150         if (defType)\r
151         {\r
152             std::string type = defType->valuestring;\r
153             definition->setType(type);\r
154             if (type == "array")\r
155             {\r
156                 PropertiesPtr property = std::make_shared<Properties>("array");\r
157                 readArray(childDefinitions, property);\r
158                 definition->addProperty("array" , property);\r
159             }\r
160         }\r
161         cJSON *defProperties = cJSON_GetObjectItem(childDefinitions, "properties");\r
162         if (defProperties)\r
163         {\r
164             cJSON *childProperties = defProperties->child;\r
165             while (childProperties)\r
166             {\r
167                 std::string attName = childProperties->string;\r
168                 definition->addProperty(attName, readProp(childProperties, attName));\r
169                 childProperties = childProperties->next;\r
170             }\r
171         }\r
172         cJSON *defRequiredValues = cJSON_GetObjectItem(childDefinitions, "required");\r
173         if (defRequiredValues)\r
174         {\r
175             int size = cJSON_GetArraySize(defRequiredValues);\r
176             int index = 0;\r
177             do\r
178             {\r
179                 definition->setRequiredValue(cJSON_GetArrayItem(defRequiredValues, index)->valuestring);\r
180             }\r
181             while ( ++index < size);\r
182         }\r
183         cJSON *defReference = cJSON_GetObjectItem(childDefinitions, "$ref");\r
184         if (defReference)\r
185         {\r
186             JsonParameters param;\r
187             readJsonRef(defReference, param);\r
188 \r
189             for (auto it : param.getProperties())\r
190             {\r
191                 definition->addProperty(it.first, it.second);\r
192             }\r
193             for ( auto it : param.getRequired())\r
194             {\r
195                 definition->setRequiredValue(it);\r
196             }\r
197             if (definition->getType().empty())\r
198                 definition->setType(param.getType());\r
199         }\r
200         cJSON *defAllOf = cJSON_GetObjectItem(childDefinitions, "allOf");\r
201         if (defAllOf)\r
202         {\r
203             JsonParameters param;\r
204             readAllOf(defAllOf, param);\r
205 \r
206             for (auto it : param.getProperties())\r
207             {\r
208                 definition->addProperty(it.first, it.second);\r
209             }\r
210             for ( auto it : param.getRequired())\r
211             {\r
212                 definition->setRequiredValue(it);\r
213             }\r
214             if (definition->getType().empty())\r
215                 definition->setType(param.getType());\r
216         }\r
217         return definition;\r
218     }\r
219 \r
220     PropertiesPtr JsonSchema::readProp(cJSON *childProperties, const std::string &attName )\r
221     {\r
222         PropertiesPtr property = std::make_shared<Properties>(attName);\r
223 \r
224         cJSON *propertyDescription = cJSON_GetObjectItem(childProperties, "description");\r
225         if (propertyDescription)\r
226         {\r
227             property->setDescription(propertyDescription->valuestring);\r
228         }\r
229         cJSON *propertyType = cJSON_GetObjectItem(childProperties, "type");\r
230         std::string attType;\r
231         if (propertyType)\r
232         {\r
233             if (propertyType->type == 4)\r
234             {\r
235                 attType = propertyType->valuestring;\r
236             }\r
237             else if (propertyType->type == 5)\r
238             {\r
239                 attType = cJSON_GetArrayItem(propertyType, 0)->valuestring;\r
240             }\r
241         }\r
242         if (!(attType == "array") && !(attType == "object"))\r
243         {\r
244             cJSON *defaultValue = cJSON_GetObjectItem(childProperties, "default");\r
245             if (defaultValue)\r
246             {\r
247                 readDefaultValue(defaultValue, property, attType);\r
248             }\r
249         }\r
250         readValues(childProperties, property, attType);\r
251         cJSON *allowedvalues = cJSON_GetObjectItem(childProperties, "enum");\r
252         if (allowedvalues)\r
253         {\r
254             readAllowedValues(allowedvalues, property, attType);\r
255         }\r
256         property->setTypeString(attType);\r
257         return property;\r
258     }\r
259 \r
260     void JsonSchema::readDefaultValue(cJSON *defaultValue,  PropertiesPtr &property,\r
261                                       const std::string &attType)\r
262     {\r
263         if (defaultValue->type == 4)\r
264         {\r
265             property->setValue((std::string)defaultValue->valuestring);\r
266         }\r
267         else if (defaultValue->type == 3)\r
268         {\r
269             if (attType == "number")\r
270                 property->setValue((double)defaultValue->valuedouble);\r
271             else\r
272                 property->setValue((int)defaultValue->valueint );\r
273         }\r
274         else if (defaultValue->type == 1)\r
275         {\r
276             property->setValue((bool)true);\r
277         }\r
278         else if (defaultValue->type == 0)\r
279         {\r
280             property->setValue((bool)false);\r
281         }\r
282     }\r
283 \r
284     void JsonSchema::readAllowedValues(cJSON *allowedvalues,  PropertiesPtr &property,\r
285                                        std::string &attType)\r
286     {\r
287         if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4)\r
288         {\r
289             int size = cJSON_GetArraySize(allowedvalues);\r
290             int idx = 0;\r
291             std::vector<std::string> allwdValues;\r
292             do\r
293             {\r
294                 allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuestring);\r
295             }\r
296             while ( ++idx < size);\r
297             property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
298             if (attType.empty())\r
299                 attType = "string";\r
300         }\r
301         else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3)\r
302         {\r
303             int size = cJSON_GetArraySize(allowedvalues);\r
304             int idx = 0;\r
305             if (attType == "number")\r
306             {\r
307                 std::vector<double> allwdValues;\r
308                 do\r
309                 {\r
310                     allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valuedouble);\r
311                 }\r
312                 while ( ++idx < size);\r
313                 property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
314             }\r
315             else\r
316             {\r
317                 std::vector<int> allwdValues;\r
318                 do\r
319                 {\r
320                     allwdValues.push_back(cJSON_GetArrayItem(allowedvalues, idx)->valueint);\r
321                 }\r
322                 while ( ++idx < size);\r
323                 property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
324                 if (attType.empty())\r
325                     attType = "integer";\r
326             }\r
327         }\r
328         else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1)\r
329                  || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0))\r
330         {\r
331             int size = cJSON_GetArraySize(allowedvalues);\r
332             int idx = 0;\r
333             std::vector<bool> allwdValues;\r
334             do\r
335             {\r
336                 if (cJSON_GetArrayItem(allowedvalues, idx)->type)\r
337                     allwdValues.push_back(true);\r
338                 else\r
339                     allwdValues.push_back(false);\r
340             }\r
341             while ( ++idx < size);\r
342             property->setValueProperty(std::make_shared<ValueProperty>(allwdValues));\r
343             if (attType.empty())\r
344                 attType = "boolean";\r
345         }\r
346     }\r
347 \r
348     void JsonSchema::readValues(cJSON *childProperties,  PropertiesPtr &property ,\r
349                                 const std::string &attType)\r
350     {\r
351         if (attType == "string")\r
352         {\r
353             readString(childProperties, property);\r
354         }\r
355         else if (attType == "integer")\r
356         {\r
357             readInteger(childProperties, property);\r
358         }\r
359         else if (attType == "number")\r
360         {\r
361             readDouble(childProperties, property);\r
362         }\r
363         else if (attType == "array")\r
364         {\r
365             readArray(childProperties, property);\r
366         }\r
367         else if (attType == "object")\r
368         {\r
369             readObject(childProperties, property);\r
370         }\r
371     }\r
372 \r
373     void JsonSchema::readString(cJSON *childProperties, PropertiesPtr &property)\r
374     {\r
375         cJSON *stringMax = cJSON_GetObjectItem(childProperties, "maxLength");\r
376         int min = INT_MIN, max = INT_MAX;\r
377         if (stringMax)\r
378         {\r
379             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
380             if (exclusiveMax)\r
381             {\r
382                 if (exclusiveMax->type == cJSON_True)\r
383                     max = --(stringMax->valueint);\r
384                 else\r
385                     max = stringMax->valueint;\r
386             }\r
387             else\r
388                 max = stringMax->valueint;\r
389         }\r
390         cJSON *stringMin = cJSON_GetObjectItem(childProperties, "minLength");\r
391         if (stringMin)\r
392         {\r
393             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
394             if (exclusiveMin)\r
395             {\r
396                 if (exclusiveMin->type == cJSON_True)\r
397                     min = ++(stringMin->valueint);\r
398                 else\r
399                     min = stringMin->valueint;\r
400             }\r
401             else\r
402                 min = stringMin->valueint;\r
403         }\r
404         if (min != INT_MIN || max != INT_MAX)\r
405             property->setValueProperty(std::make_shared<ValueProperty>(min, max, 0));\r
406 \r
407         cJSON *stringFormat = cJSON_GetObjectItem(childProperties, "format");\r
408         if (stringFormat)\r
409         {\r
410             property->setValueProperty(std::make_shared<ValueProperty>\r
411                                        (ValueProperty::Type::FORMAT, (stringFormat->valuestring)));\r
412         }\r
413 \r
414         cJSON *stringPattern = cJSON_GetObjectItem(childProperties, "pattern");\r
415         if (stringPattern)\r
416         {\r
417             property->setValueProperty(std::make_shared<ValueProperty>\r
418                                        (ValueProperty::Type::PATTERN, (stringPattern->valuestring)));\r
419         }\r
420     }\r
421 \r
422     void JsonSchema::readInteger(cJSON *childProperties,  PropertiesPtr &property)\r
423     {\r
424         cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum");\r
425         int min = INT_MIN, max = INT_MAX, multipleOf = INT_MAX;\r
426         if (Max)\r
427         {\r
428             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
429             if (exclusiveMax)\r
430             {\r
431                 if (exclusiveMax->type == cJSON_True)\r
432                     max = --(Max->valueint);\r
433                 else\r
434                     max = Max->valueint;\r
435             }\r
436             else\r
437                 max = Max->valueint;\r
438         }\r
439         cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum");\r
440         if (Min)\r
441         {\r
442             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
443             if (exclusiveMin)\r
444             {\r
445                 if (exclusiveMin->type == cJSON_True)\r
446                     min = ++(Min->valueint);\r
447                 else\r
448                     min = Min->valueint;\r
449             }\r
450             else\r
451                 min = Min->valueint;\r
452         }\r
453         cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf");\r
454         if (MultipleOff)\r
455         {\r
456             multipleOf = MultipleOff->valueint;\r
457         }\r
458         if (min != INT_MIN || max != INT_MAX)\r
459             property->setValueProperty(std::make_shared<ValueProperty>(min, max, multipleOf));\r
460 \r
461     }\r
462 \r
463     void JsonSchema::readDouble(cJSON *childProperties,  PropertiesPtr &property)\r
464     {\r
465         cJSON *Max = cJSON_GetObjectItem(childProperties, "maximum");\r
466         double min = INT_MIN, max = INT_MAX;\r
467         int multipleOf = INT_MAX;\r
468         if (Max)\r
469         {\r
470             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
471             if (exclusiveMax)\r
472             {\r
473                 if (exclusiveMax->type == cJSON_True)\r
474                     max = --(Max->valuedouble);\r
475                 else\r
476                     max = Max->valuedouble;\r
477             }\r
478             else\r
479                 max = Max->valuedouble;\r
480         }\r
481         cJSON *Min = cJSON_GetObjectItem(childProperties, "minimum");\r
482         if (Min)\r
483         {\r
484             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
485             if (exclusiveMin)\r
486             {\r
487                 if (exclusiveMin->type == cJSON_True)\r
488                     min = ++(Min->valuedouble);\r
489                 else\r
490                     min = Min->valuedouble;\r
491             }\r
492             else\r
493                 min = Min->valuedouble;\r
494         }\r
495 \r
496         cJSON *MultipleOff = cJSON_GetObjectItem(childProperties, "multipleOf");\r
497         if (MultipleOff)\r
498         {\r
499             multipleOf = MultipleOff->valueint;\r
500         }\r
501         if (min != INT_MIN || max != INT_MAX)\r
502             property->setValueProperty(std::make_shared<ValueProperty>(min, max, multipleOf));\r
503 \r
504     }\r
505 \r
506     void JsonSchema::readArray(cJSON *childProperties,  PropertiesPtr &property)\r
507     {\r
508         cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items");\r
509         if (itemValues)\r
510         {\r
511             if (itemValues->type == 5)\r
512             {\r
513                 //int item_size = cJSON_GetArraySize(itemValues);\r
514                 int item_index = 0;\r
515                 //do\r
516                 //{\r
517                 cJSON *item = cJSON_GetArrayItem(itemValues, item_index);\r
518                 readItems(item, property);\r
519                 //break;\r
520                 //}\r
521                 //while ( ++item_index < item_size);\r
522             }\r
523             else\r
524             {\r
525                 readItems(itemValues, property);\r
526             }\r
527         }\r
528         cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems");\r
529         int min = INT_MIN, max = INT_MAX;\r
530         bool unique = cJSON_False, addItems = cJSON_False;\r
531         if (itemsMax)\r
532         {\r
533             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
534             if (exclusiveMax)\r
535             {\r
536                 if (exclusiveMax->type == cJSON_True)\r
537                     max = --(itemsMax->valueint);\r
538                 else\r
539                     max = itemsMax->valueint;\r
540             }\r
541             else\r
542                 max = itemsMax->valueint;\r
543         }\r
544         cJSON *itemsMin = cJSON_GetObjectItem(childProperties, "minItems");\r
545         if (itemsMin)\r
546         {\r
547             cJSON *exclusiveMin = cJSON_GetObjectItem(childProperties, "exclusiveMinimum");\r
548             if (exclusiveMin)\r
549             {\r
550                 if (exclusiveMin->type == cJSON_True)\r
551                     min = ++(itemsMin->valueint);\r
552                 else\r
553                     min = itemsMin->valueint;\r
554             }\r
555             else\r
556                 min = itemsMin->valueint;\r
557         }\r
558         cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems");\r
559         if (uniqueItems)\r
560         {\r
561             unique = uniqueItems->type;\r
562         }\r
563         cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems");\r
564         if (additionalItems)\r
565         {\r
566             addItems = additionalItems->type;\r
567         }\r
568         property->setValueProperty(std::make_shared<ValueProperty>\r
569                                    (ValueProperty::Type::ARRAY, min, max, unique, addItems));\r
570     }\r
571 \r
572     void JsonSchema::readItems(cJSON *item, PropertiesPtr &property)\r
573     {\r
574         std::string type;\r
575         JsonParameters param;\r
576         cJSON *itemType = cJSON_GetObjectItem(item, "type");\r
577         if (itemType)\r
578         {\r
579             type = itemType->valuestring;\r
580         }\r
581 \r
582         cJSON *itemAllOf = cJSON_GetObjectItem(item, "allOf");\r
583         if (itemAllOf)\r
584         {\r
585             readAllOf(itemAllOf , param);\r
586         }\r
587         cJSON *itemReference = cJSON_GetObjectItem(item, "$ref");\r
588         if (itemReference)\r
589         {\r
590             readJsonRef(itemReference , param);\r
591         }\r
592 \r
593         if (type == "object")\r
594         {\r
595             cJSON *itemProperties = cJSON_GetObjectItem(item, "properties");\r
596             if (itemProperties)\r
597             {\r
598                 cJSON *childProperties = itemProperties->child;\r
599                 std::vector<Properties> propertyVector;\r
600                 while (childProperties)\r
601                 {\r
602                     std::string attName = childProperties->string;\r
603                     PropertiesPtr prop = std::make_shared<Properties>(attName);\r
604                     readProp(childProperties, attName);\r
605                     propertyVector.push_back(*prop);\r
606                     childProperties = childProperties->next;\r
607                 }\r
608                 property->setValue(propertyVector);\r
609             }\r
610 \r
611             cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required");\r
612             if (itemRequiredValues)\r
613             {\r
614                 int size = cJSON_GetArraySize(itemRequiredValues);\r
615                 int index = 0;\r
616                 do\r
617                 {\r
618                     property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring);\r
619                 }\r
620                 while ( ++index < size);\r
621             }\r
622         }\r
623 \r
624         else if (param.getType() == "object")\r
625         {\r
626             std::vector<Properties> propertyVector;\r
627             for (auto prop : param.getProperties())\r
628             {\r
629                 propertyVector.push_back(*(prop.second));\r
630             }\r
631             property->setValue(propertyVector);\r
632 \r
633             for (auto req : param.getRequired())\r
634             {\r
635                 property->setRequiredValue(req);\r
636             }\r
637         }\r
638         else\r
639         {\r
640             PropertiesPtr prop = std::make_shared<Properties>("property");\r
641 \r
642             cJSON *defaultValue = cJSON_GetObjectItem(item, "default");\r
643             if (defaultValue)\r
644             {\r
645                 readDefaultValue(defaultValue, prop, type);\r
646             }\r
647             cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum");\r
648             if (allowedvalues)\r
649             {\r
650                 readAllowedValues(allowedvalues, prop, type);\r
651             }\r
652             readValues(item, prop, type);\r
653             prop->setTypeString(type);\r
654             property->setValue(*prop);\r
655         }\r
656     }\r
657 \r
658     void JsonSchema::readObject(cJSON *childProperties,  PropertiesPtr &property)\r
659     {\r
660         property->setTypeString("object");\r
661 \r
662         cJSON *subProperties = cJSON_GetObjectItem(childProperties, "properties");\r
663         cJSON *itemRequiredValues = cJSON_GetObjectItem(childProperties, "required");\r
664         if (subProperties)\r
665         {\r
666             cJSON *childProperties = subProperties->child;\r
667             std::vector<Properties> propertyVector;\r
668             while (childProperties)\r
669             {\r
670                 std::string attName = childProperties->string;\r
671                 PropertiesPtr prop = std::make_shared<Properties>(attName);\r
672                 readProp(childProperties, attName);\r
673                 propertyVector.push_back(*prop);\r
674                 childProperties = childProperties->next;\r
675             }\r
676             property->setValue(propertyVector);\r
677             if (itemRequiredValues)\r
678             {\r
679                 int size = cJSON_GetArraySize(itemRequiredValues);\r
680                 int index = 0;\r
681                 do\r
682                 {\r
683                     property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring);\r
684                 }\r
685                 while ( ++index < size);\r
686             }\r
687         }\r
688         else\r
689         {\r
690             JsonParameters param;\r
691 \r
692             cJSON *itemAllOf = cJSON_GetObjectItem(childProperties, "allOf");\r
693             if (itemAllOf)\r
694             {\r
695                 readAllOf(itemAllOf , param);\r
696             }\r
697             cJSON *itemReference = cJSON_GetObjectItem(childProperties, "$ref");\r
698             if (itemReference)\r
699             {\r
700                 readJsonRef(itemReference , param);\r
701             }\r
702 \r
703             if (param.getType() == "object")\r
704             {\r
705                 std::vector<Properties> propertyVector;\r
706                 for (auto prop : param.getProperties())\r
707                 {\r
708                     propertyVector.push_back(*(prop.second));\r
709                 }\r
710                 property->setValue(propertyVector);\r
711 \r
712                 for (auto req : param.getRequired())\r
713                 {\r
714                     property->setRequiredValue(req);\r
715                 }\r
716             }\r
717         }\r
718     }\r
719 \r
720     void JsonSchema::readFile(std::string &fileName ,  JsonParameters &param)\r
721     {\r
722         std::string name = fileName;\r
723         std::transform(name.begin(), name.end(), name.begin(), ::tolower);\r
724         if (name.compare("oic.baseresource.json") == 0)\r
725             return;\r
726 \r
727         cJSON *json = m_includeResolver->readToJson(fileName);\r
728         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
729 \r
730         param.addProperties(Refparser->getProperties());\r
731         param.addRequired(Refparser->getRequiredValues());\r
732         param.setType(Refparser->getType());\r
733     }\r
734 \r
735     void JsonSchema::readFile(std::string &fileName , std::string &defName ,  JsonParameters &param)\r
736     {\r
737         std::string name = fileName;\r
738         std::transform(name.begin(), name.end(), name.begin(), ::tolower);\r
739         if (name.compare("oic.baseresource.json") == 0)\r
740             return;\r
741 \r
742         cJSON *json = m_includeResolver->readToJson(fileName);\r
743         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
744 \r
745         DefinitionsPtr definition = Refparser->getDefinition(defName);\r
746         if (definition == nullptr)\r
747             throw JsonException("Definition Name Incorrect");\r
748 \r
749         param.addProperties(definition->getProperties());\r
750         param.addRequired(definition->getRequiredValues());\r
751         param.setType(definition->getType());\r
752     }\r
753 \r
754     void JsonSchema::readRef(std::string ref ,  JsonParameters &param)\r
755     {\r
756         std::string delimiter1 = "#";\r
757         std::string delimiter2 = "/";\r
758         std::string fileName;\r
759         if (! ref.empty())\r
760         {\r
761             std::size_t pos = std::string::npos;\r
762             if ( (pos = ref.find(delimiter1)) != std::string::npos)\r
763             {\r
764                 fileName = ref.substr(0, pos);\r
765                 ref.erase(0, pos);\r
766             }\r
767             ref.erase(0, delimiter1 .length());\r
768             std::string defName;\r
769 \r
770             if (! ref.empty())\r
771             {\r
772                 ref.erase(0, delimiter2 .length());\r
773                 std::string keyName;\r
774                 if ( (pos = ref.find(delimiter2)) != std::string::npos)\r
775                 {\r
776                     keyName = ref.substr(0, pos);\r
777                     ref.erase(0, pos + delimiter2.length());\r
778                     if (keyName == "definitions")\r
779                     {\r
780                         if ( (pos = ref.find(delimiter2)) != std::string::npos)\r
781                         {\r
782                             defName = ref.substr(0, pos);\r
783                         }\r
784                         else if (! ref.empty())\r
785                         {\r
786                             defName = ref;\r
787                         }\r
788                     }\r
789                 }\r
790             }\r
791             if (!fileName.empty())\r
792             {\r
793                 if (!(defName.empty()))\r
794                 {\r
795                     readFile(fileName, defName, param);\r
796                 }\r
797                 else\r
798                 {\r
799                     throw JsonException("Definition Name Empty");\r
800                 }\r
801             }\r
802             else\r
803             {\r
804                 if (!(defName.empty()))\r
805                 {\r
806                     if (getDefinition(defName) == nullptr)\r
807                         throw JsonException("Definition Name Incorrect");\r
808                     param.addProperties(getDefinition(defName)->getProperties());\r
809                     param.addRequired(getDefinition(defName)->getRequiredValues());\r
810                     param.setType(getDefinition(defName)->getType());\r
811                 }\r
812                 else\r
813                 {\r
814                     throw JsonException("Definition Name Empty");\r
815                 }\r
816             }\r
817         }\r
818     }\r
819 \r
820     void JsonSchema::readJsonRef(cJSON *jsonReference , JsonParameters &param)\r
821     {\r
822         std::string ref = jsonReference->valuestring;\r
823 \r
824         std::string web = "http://";\r
825         std::string delimiter = "#";\r
826         std::size_t pos = ref.find(web);\r
827 \r
828         if (pos == std::string::npos)   // If Web Link Is GIVEN TO READ\r
829         {\r
830             pos = ref.find(delimiter);\r
831             if ( pos ==  (ref.length() - 1) )\r
832             {\r
833                 std::string fileName = ref.substr(0, pos);\r
834                 readFile(fileName, param);\r
835             }\r
836             else\r
837             {\r
838                 readRef(ref, param);\r
839             }\r
840         }\r
841     }\r
842 \r
843     void JsonSchema::readAllOf(cJSON *allofValues ,  JsonParameters &allParams)\r
844     {\r
845         int size = cJSON_GetArraySize(allofValues);\r
846         int index = 0;\r
847         do\r
848         {\r
849             JsonParameters param;\r
850 \r
851             cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index);\r
852             cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref");\r
853             if (jsonReference)\r
854             {\r
855                 readJsonRef(jsonReference, param);\r
856                 allParams.addProperties(param.getProperties());\r
857                 allParams.addRequired(param.getRequired());\r
858                 allParams.setType(param.getType());\r
859             }\r
860             cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required");\r
861             if (jsonRequiredValues)\r
862             {\r
863                 int len = cJSON_GetArraySize(jsonRequiredValues);\r
864                 int idx = 0;\r
865                 do\r
866                 {\r
867                     allParams.addRequired(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring);\r
868                 }\r
869                 while ( ++idx < len);\r
870             }\r
871         }\r
872         while ( ++index < size);\r
873     }\r
874 }\r
875 \r