replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / jsonSchemaParser / JsonSchema.cpp
index b62dcb5..bbd1aaf 100755 (executable)
@@ -90,9 +90,9 @@ namespace RAML
 \r
         cJSON *jsonAdditionalProperties = cJSON_GetObjectItem(m_cjson, "additionalProperties");\r
         if (jsonAdditionalProperties)\r
-            m_additionalProperties = jsonAdditionalProperties->type;\r
+            m_additionalProperties = (jsonAdditionalProperties->type == cJSON_True);\r
         else\r
-            m_additionalProperties = cJSON_True;\r
+            m_additionalProperties = true;\r
 \r
         cJSON *jsonReference = cJSON_GetObjectItem(m_cjson, "$ref");\r
         if (jsonReference)\r
@@ -230,11 +230,11 @@ namespace RAML
         std::string attType;\r
         if (propertyType)\r
         {\r
-            if (propertyType->type == 4)\r
+            if (propertyType->type == cJSON_String)\r
             {\r
                 attType = propertyType->valuestring;\r
             }\r
-            else if (propertyType->type == 5)\r
+            else if (propertyType->type == cJSON_Array)\r
             {\r
                 attType = cJSON_GetArrayItem(propertyType, 0)->valuestring;\r
             }\r
@@ -260,22 +260,22 @@ namespace RAML
     void JsonSchema::readDefaultValue(cJSON *defaultValue,  PropertiesPtr &property,\r
                                       const std::string &attType)\r
     {\r
-        if (defaultValue->type == 4)\r
+        if (defaultValue->type == cJSON_String)\r
         {\r
             property->setValue((std::string)defaultValue->valuestring);\r
         }\r
-        else if (defaultValue->type == 3)\r
+        else if (defaultValue->type == cJSON_Number)\r
         {\r
             if (attType == "number")\r
                 property->setValue((double)defaultValue->valuedouble);\r
             else\r
                 property->setValue((int)defaultValue->valueint );\r
         }\r
-        else if (defaultValue->type == 1)\r
+        else if (defaultValue->type == cJSON_True)\r
         {\r
             property->setValue((bool)true);\r
         }\r
-        else if (defaultValue->type == 0)\r
+        else if (defaultValue->type == cJSON_False)\r
         {\r
             property->setValue((bool)false);\r
         }\r
@@ -284,7 +284,7 @@ namespace RAML
     void JsonSchema::readAllowedValues(cJSON *allowedvalues,  PropertiesPtr &property,\r
                                        std::string &attType)\r
     {\r
-        if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 4)\r
+        if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == cJSON_String)\r
         {\r
             int size = cJSON_GetArraySize(allowedvalues);\r
             int idx = 0;\r
@@ -298,7 +298,7 @@ namespace RAML
             if (attType.empty())\r
                 attType = "string";\r
         }\r
-        else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 3)\r
+        else if ((cJSON_GetArrayItem(allowedvalues, 0)->type) == cJSON_Number)\r
         {\r
             int size = cJSON_GetArraySize(allowedvalues);\r
             int idx = 0;\r
@@ -325,15 +325,15 @@ namespace RAML
                     attType = "integer";\r
             }\r
         }\r
-        else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == 1)\r
-                 || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == 0))\r
+        else if (((cJSON_GetArrayItem(allowedvalues, 0)->type) == cJSON_True)\r
+                 || ((cJSON_GetArrayItem(allowedvalues, 0)->type) == cJSON_False))\r
         {\r
             int size = cJSON_GetArraySize(allowedvalues);\r
             int idx = 0;\r
             std::vector<bool> allwdValues;\r
             do\r
             {\r
-                if (cJSON_GetArrayItem(allowedvalues, idx)->type)\r
+                if (cJSON_GetArrayItem(allowedvalues, idx)->type != cJSON_False)\r
                     allwdValues.push_back(true);\r
                 else\r
                     allwdValues.push_back(false);\r
@@ -364,6 +364,10 @@ namespace RAML
         {\r
             readArray(childProperties, property);\r
         }\r
+        else if (attType == "object")\r
+        {\r
+            readObject(childProperties, property);\r
+        }\r
     }\r
 \r
     void JsonSchema::readString(cJSON *childProperties, PropertiesPtr &property)\r
@@ -504,17 +508,17 @@ namespace RAML
         cJSON *itemValues = cJSON_GetObjectItem(childProperties, "items");\r
         if (itemValues)\r
         {\r
-            if (itemValues->type == 5)\r
+            if (itemValues->type == cJSON_Array)\r
             {\r
-                int item_size = cJSON_GetArraySize(itemValues);\r
+                //int item_size = cJSON_GetArraySize(itemValues);\r
                 int item_index = 0;\r
-                do\r
-                {\r
-                    cJSON *item = cJSON_GetArrayItem(itemValues, item_index);\r
-                    readItems(item, property);\r
-                    break; \r
-                }\r
-                while ( ++item_index < item_size);\r
+                //do\r
+                //{\r
+                cJSON *item = cJSON_GetArrayItem(itemValues, item_index);\r
+                readItems(item, property);\r
+                //break;\r
+                //}\r
+                //while ( ++item_index < item_size);\r
             }\r
             else\r
             {\r
@@ -523,7 +527,7 @@ namespace RAML
         }\r
         cJSON *itemsMax = cJSON_GetObjectItem(childProperties, "maxItems");\r
         int min = INT_MIN, max = INT_MAX;\r
-        bool unique = cJSON_True, addItems = cJSON_True;\r
+        bool unique = false, addItems = false;\r
         if (itemsMax)\r
         {\r
             cJSON *exclusiveMax = cJSON_GetObjectItem(childProperties, "exclusiveMaximum");\r
@@ -554,12 +558,12 @@ namespace RAML
         cJSON *uniqueItems = cJSON_GetObjectItem(childProperties, "uniqueItems");\r
         if (uniqueItems)\r
         {\r
-            unique = uniqueItems->type;\r
+            unique = (uniqueItems->type == cJSON_True);\r
         }\r
         cJSON *additionalItems = cJSON_GetObjectItem(childProperties, "additionalItems");\r
         if (additionalItems)\r
         {\r
-            addItems = additionalItems->type;\r
+            addItems = (additionalItems->type == cJSON_True);\r
         }\r
         property->setValueProperty(std::make_shared<ValueProperty>\r
                                    (ValueProperty::Type::ARRAY, min, max, unique, addItems));\r
@@ -580,6 +584,15 @@ namespace RAML
         {\r
             readAllOf(itemAllOf , param);\r
         }\r
+        else\r
+        {\r
+            cJSON *itemOneOf = cJSON_GetObjectItem(item, "oneOf");\r
+            if (itemOneOf)\r
+            {\r
+                readAllOf(itemOneOf , param);\r
+            }\r
+        }\r
+\r
         cJSON *itemReference = cJSON_GetObjectItem(item, "$ref");\r
         if (itemReference)\r
         {\r
@@ -645,13 +658,81 @@ namespace RAML
             {\r
                 readAllowedValues(allowedvalues, prop, type);\r
             }\r
+            readValues(item, prop, type);\r
             prop->setTypeString(type);\r
             property->setValue(*prop);\r
         }\r
     }\r
 \r
+    void JsonSchema::readObject(cJSON *childProperties,  PropertiesPtr &property)\r
+    {\r
+        property->setTypeString("object");\r
+\r
+        cJSON *subProperties = cJSON_GetObjectItem(childProperties, "properties");\r
+        cJSON *itemRequiredValues = cJSON_GetObjectItem(childProperties, "required");\r
+        if (subProperties)\r
+        {\r
+            cJSON *childProperties = subProperties->child;\r
+            std::vector<Properties> propertyVector;\r
+            while (childProperties)\r
+            {\r
+                std::string attName = childProperties->string;\r
+                PropertiesPtr prop = std::make_shared<Properties>(attName);\r
+                readProp(childProperties, attName);\r
+                propertyVector.push_back(*prop);\r
+                childProperties = childProperties->next;\r
+            }\r
+            property->setValue(propertyVector);\r
+            if (itemRequiredValues)\r
+            {\r
+                int size = cJSON_GetArraySize(itemRequiredValues);\r
+                int index = 0;\r
+                do\r
+                {\r
+                    property->setRequiredValue(cJSON_GetArrayItem(itemRequiredValues, index)->valuestring);\r
+                }\r
+                while ( ++index < size);\r
+            }\r
+        }\r
+        else\r
+        {\r
+            JsonParameters param;\r
+\r
+            cJSON *itemAllOf = cJSON_GetObjectItem(childProperties, "allOf");\r
+            if (itemAllOf)\r
+            {\r
+                readAllOf(itemAllOf , param);\r
+            }\r
+            cJSON *itemReference = cJSON_GetObjectItem(childProperties, "$ref");\r
+            if (itemReference)\r
+            {\r
+                readJsonRef(itemReference , param);\r
+            }\r
+\r
+            if (param.getType() == "object")\r
+            {\r
+                std::vector<Properties> propertyVector;\r
+                for (auto prop : param.getProperties())\r
+                {\r
+                    propertyVector.push_back(*(prop.second));\r
+                }\r
+                property->setValue(propertyVector);\r
+\r
+                for (auto req : param.getRequired())\r
+                {\r
+                    property->setRequiredValue(req);\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
     void JsonSchema::readFile(std::string &fileName ,  JsonParameters &param)\r
     {\r
+        std::string name = fileName;\r
+        std::transform(name.begin(), name.end(), name.begin(), ::tolower);\r
+        if (name.compare("oic.baseresource.json") == 0)\r
+            return;\r
+\r
         cJSON *json = m_includeResolver->readToJson(fileName);\r
         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
 \r
@@ -662,6 +743,11 @@ namespace RAML
 \r
     void JsonSchema::readFile(std::string &fileName , std::string &defName ,  JsonParameters &param)\r
     {\r
+        std::string name = fileName;\r
+        std::transform(name.begin(), name.end(), name.begin(), ::tolower);\r
+        if (name.compare("oic.baseresource.json") == 0)\r
+            return;\r
+\r
         cJSON *json = m_includeResolver->readToJson(fileName);\r
         JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(json, m_includeResolver);\r
 \r
@@ -681,7 +767,7 @@ namespace RAML
         std::string fileName;\r
         if (! ref.empty())\r
         {\r
-            std::size_t pos = ref.find(delimiter1);\r
+            std::size_t pos = std::string::npos;\r
             if ( (pos = ref.find(delimiter1)) != std::string::npos)\r
             {\r
                 fileName = ref.substr(0, pos);\r