Imported Upstream version 1.1.0
[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         else\r
588         {\r
589             cJSON *itemOneOf = cJSON_GetObjectItem(item, "oneOf");\r
590             if (itemOneOf)\r
591             {\r
592                 readAllOf(itemOneOf , param);\r
593             }\r
594         }\r
595 \r
596         cJSON *itemReference = cJSON_GetObjectItem(item, "$ref");\r
597         if (itemReference)\r
598         {\r
599             readJsonRef(itemReference , param);\r
600         }\r
601 \r
602         if (type == "object")\r
603         {\r
604             cJSON *itemProperties = cJSON_GetObjectItem(item, "properties");\r
605             if (itemProperties)\r
606             {\r
607                 cJSON *childProperties = itemProperties->child;\r
608                 std::vector<Properties> propertyVector;\r
609                 while (childProperties)\r
610                 {\r
611                     std::string attName = childProperties->string;\r
612                     PropertiesPtr prop = std::make_shared<Properties>(attName);\r
613                     readProp(childProperties, attName);\r
614                     propertyVector.push_back(*prop);\r
615                     childProperties = childProperties->next;\r
616                 }\r
617                 property->setValue(propertyVector);\r
618             }\r
619 \r
620             cJSON *itemRequiredValues = cJSON_GetObjectItem(item, "required");\r
621             if (itemRequiredValues)\r
622             {\r
623                 int size = cJSON_GetArraySize(itemRequiredValues);\r
624                 int index = 0;\r
625                 do\r
626                 {\r
627                     property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring);\r
628                 }\r
629                 while ( ++index < size);\r
630             }\r
631         }\r
632 \r
633         else if (param.getType() == "object")\r
634         {\r
635             std::vector<Properties> propertyVector;\r
636             for (auto prop : param.getProperties())\r
637             {\r
638                 propertyVector.push_back(*(prop.second));\r
639             }\r
640             property->setValue(propertyVector);\r
641 \r
642             for (auto req : param.getRequired())\r
643             {\r
644                 property->setRequiredValue(req);\r
645             }\r
646         }\r
647         else\r
648         {\r
649             PropertiesPtr prop = std::make_shared<Properties>("property");\r
650 \r
651             cJSON *defaultValue = cJSON_GetObjectItem(item, "default");\r
652             if (defaultValue)\r
653             {\r
654                 readDefaultValue(defaultValue, prop, type);\r
655             }\r
656             cJSON *allowedvalues = cJSON_GetObjectItem(item, "enum");\r
657             if (allowedvalues)\r
658             {\r
659                 readAllowedValues(allowedvalues, prop, type);\r
660             }\r
661             readValues(item, prop, type);\r
662             prop->setTypeString(type);\r
663             property->setValue(*prop);\r
664         }\r
665     }\r
666 \r
667     void JsonSchema::readObject(cJSON *childProperties,  PropertiesPtr &property)\r
668     {\r
669         property->setTypeString("object");\r
670 \r
671         cJSON *subProperties = cJSON_GetObjectItem(childProperties, "properties");\r
672         cJSON *itemRequiredValues = cJSON_GetObjectItem(childProperties, "required");\r
673         if (subProperties)\r
674         {\r
675             cJSON *childProperties = subProperties->child;\r
676             std::vector<Properties> propertyVector;\r
677             while (childProperties)\r
678             {\r
679                 std::string attName = childProperties->string;\r
680                 PropertiesPtr prop = std::make_shared<Properties>(attName);\r
681                 readProp(childProperties, attName);\r
682                 propertyVector.push_back(*prop);\r
683                 childProperties = childProperties->next;\r
684             }\r
685             property->setValue(propertyVector);\r
686             if (itemRequiredValues)\r
687             {\r
688                 int size = cJSON_GetArraySize(itemRequiredValues);\r
689                 int index = 0;\r
690                 do\r
691                 {\r
692                     property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring);\r
693                 }\r
694                 while ( ++index < size);\r
695             }\r
696         }\r
697         else\r
698         {\r
699             JsonParameters param;\r
700 \r
701             cJSON *itemAllOf = cJSON_GetObjectItem(childProperties, "allOf");\r
702             if (itemAllOf)\r
703             {\r
704                 readAllOf(itemAllOf , param);\r
705             }\r
706             cJSON *itemReference = cJSON_GetObjectItem(childProperties, "$ref");\r
707             if (itemReference)\r
708             {\r
709                 readJsonRef(itemReference , param);\r
710             }\r
711 \r
712             if (param.getType() == "object")\r
713             {\r
714                 std::vector<Properties> propertyVector;\r
715                 for (auto prop : param.getProperties())\r
716                 {\r
717                     propertyVector.push_back(*(prop.second));\r
718                 }\r
719                 property->setValue(propertyVector);\r
720 \r
721                 for (auto req : param.getRequired())\r
722                 {\r
723                     property->setRequiredValue(req);\r
724                 }\r
725             }\r
726         }\r
727     }\r
728 \r
729     void JsonSchema::readFile(std::string &fileName ,  JsonParameters &param)\r
730     {\r
731         std::string name = fileName;\r
732         std::transform(name.begin(), name.end(), name.begin(), ::tolower);\r
733         if (name.compare("oic.baseresource.json") == 0)\r
734             return;\r
735 \r
736         cJSON *json = m_includeResolver->readToJson(fileName);\r
737         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
738 \r
739         param.addProperties(Refparser->getProperties());\r
740         param.addRequired(Refparser->getRequiredValues());\r
741         param.setType(Refparser->getType());\r
742     }\r
743 \r
744     void JsonSchema::readFile(std::string &fileName , std::string &defName ,  JsonParameters &param)\r
745     {\r
746         std::string name = fileName;\r
747         std::transform(name.begin(), name.end(), name.begin(), ::tolower);\r
748         if (name.compare("oic.baseresource.json") == 0)\r
749             return;\r
750 \r
751         cJSON *json = m_includeResolver->readToJson(fileName);\r
752         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
753 \r
754         DefinitionsPtr definition = Refparser->getDefinition(defName);\r
755         if (definition == nullptr)\r
756             throw JsonException("Definition Name Incorrect");\r
757 \r
758         param.addProperties(definition->getProperties());\r
759         param.addRequired(definition->getRequiredValues());\r
760         param.setType(definition->getType());\r
761     }\r
762 \r
763     void JsonSchema::readRef(std::string ref ,  JsonParameters &param)\r
764     {\r
765         std::string delimiter1 = "#";\r
766         std::string delimiter2 = "/";\r
767         std::string fileName;\r
768         if (! ref.empty())\r
769         {\r
770             std::size_t pos = std::string::npos;\r
771             if ( (pos = ref.find(delimiter1)) != std::string::npos)\r
772             {\r
773                 fileName = ref.substr(0, pos);\r
774                 ref.erase(0, pos);\r
775             }\r
776             ref.erase(0, delimiter1 .length());\r
777             std::string defName;\r
778 \r
779             if (! ref.empty())\r
780             {\r
781                 ref.erase(0, delimiter2 .length());\r
782                 std::string keyName;\r
783                 if ( (pos = ref.find(delimiter2)) != std::string::npos)\r
784                 {\r
785                     keyName = ref.substr(0, pos);\r
786                     ref.erase(0, pos + delimiter2.length());\r
787                     if (keyName == "definitions")\r
788                     {\r
789                         if ( (pos = ref.find(delimiter2)) != std::string::npos)\r
790                         {\r
791                             defName = ref.substr(0, pos);\r
792                         }\r
793                         else if (! ref.empty())\r
794                         {\r
795                             defName = ref;\r
796                         }\r
797                     }\r
798                 }\r
799             }\r
800             if (!fileName.empty())\r
801             {\r
802                 if (!(defName.empty()))\r
803                 {\r
804                     readFile(fileName, defName, param);\r
805                 }\r
806                 else\r
807                 {\r
808                     throw JsonException("Definition Name Empty");\r
809                 }\r
810             }\r
811             else\r
812             {\r
813                 if (!(defName.empty()))\r
814                 {\r
815                     if (getDefinition(defName) == nullptr)\r
816                         throw JsonException("Definition Name Incorrect");\r
817                     param.addProperties(getDefinition(defName)->getProperties());\r
818                     param.addRequired(getDefinition(defName)->getRequiredValues());\r
819                     param.setType(getDefinition(defName)->getType());\r
820                 }\r
821                 else\r
822                 {\r
823                     throw JsonException("Definition Name Empty");\r
824                 }\r
825             }\r
826         }\r
827     }\r
828 \r
829     void JsonSchema::readJsonRef(cJSON *jsonReference , JsonParameters &param)\r
830     {\r
831         std::string ref = jsonReference->valuestring;\r
832 \r
833         std::string web = "http://";\r
834         std::string delimiter = "#";\r
835         std::size_t pos = ref.find(web);\r
836 \r
837         if (pos == std::string::npos)   // If Web Link Is GIVEN TO READ\r
838         {\r
839             pos = ref.find(delimiter);\r
840             if ( pos ==  (ref.length() - 1) )\r
841             {\r
842                 std::string fileName = ref.substr(0, pos);\r
843                 readFile(fileName, param);\r
844             }\r
845             else\r
846             {\r
847                 readRef(ref, param);\r
848             }\r
849         }\r
850     }\r
851 \r
852     void JsonSchema::readAllOf(cJSON *allofValues ,  JsonParameters &allParams)\r
853     {\r
854         int size = cJSON_GetArraySize(allofValues);\r
855         int index = 0;\r
856         do\r
857         {\r
858             JsonParameters param;\r
859 \r
860             cJSON *childAllOf = cJSON_GetArrayItem(allofValues, index);\r
861             cJSON *jsonReference = cJSON_GetObjectItem(childAllOf, "$ref");\r
862             if (jsonReference)\r
863             {\r
864                 readJsonRef(jsonReference, param);\r
865                 allParams.addProperties(param.getProperties());\r
866                 allParams.addRequired(param.getRequired());\r
867                 allParams.setType(param.getType());\r
868             }\r
869             cJSON *jsonRequiredValues = cJSON_GetObjectItem(childAllOf, "required");\r
870             if (jsonRequiredValues)\r
871             {\r
872                 int len = cJSON_GetArraySize(jsonRequiredValues);\r
873                 int idx = 0;\r
874                 do\r
875                 {\r
876                     allParams.addRequired(cJSON_GetArrayItem(jsonRequiredValues, idx)->valuestring);\r
877                 }\r
878                 while ( ++idx < len);\r
879             }\r
880         }\r
881         while ( ++index < size);\r
882     }\r
883 }\r
884 \r