Merge branch 'master' into simulator
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / jsonSchemaParser / JsonSchema.h
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 #ifndef JSON_SCHEMA_H_\r
22 #define JSON_SCHEMA_H_\r
23 \r
24 #include <string>\r
25 #include <vector>\r
26 #include <map>\r
27 #include "Properties.h"\r
28 #include "Items.h"\r
29 #include "Definitions.h"\r
30 #include "cJSON.h"\r
31 #include "Helpers.h"\r
32 #include "AllowedValues.h"\r
33 \r
34 #include "IncludeResolver.h"\r
35 \r
36 namespace RAML\r
37 {\r
38     class JsonSchema\r
39     {\r
40         public:\r
41             JsonSchema() : m_cjson(NULL), m_includeResolver(NULL)  {}\r
42             JsonSchema(const IncludeResolverPtr &includeResolver) : m_cjson(NULL),\r
43                 m_includeResolver(includeResolver) {}\r
44             JsonSchema(cJSON *cjson , const IncludeResolverPtr &includeResolver) : m_cjson(cjson),\r
45                 m_includeResolver(includeResolver)  { readJson(); }\r
46 \r
47             void setcJson(cJSON *cjson) {m_cjson = cjson; readJson(); }\r
48             int size() const\r
49             {\r
50                 return m_properties.size();\r
51             }\r
52             inline bool getProperty(const std::string &name, Properties *value)\r
53             {\r
54                 if (m_properties.end() != m_properties.find(name))\r
55                 {\r
56                     value = m_properties[name];\r
57                     return true;\r
58                 }\r
59                 return false;\r
60             }\r
61             inline std::map<std::string, Properties *> getProperties()\r
62             {\r
63                 return m_properties;\r
64             }\r
65             inline std::map<std::string, Definitions *> getDefinitions()\r
66             {\r
67                 return m_definition;\r
68             }\r
69             void addProperty(const std::string &name, Properties *property)\r
70             {\r
71                 if (m_properties.end() == m_properties.find(name))\r
72                 {\r
73                     m_properties[name] = property;\r
74                 }\r
75             }\r
76             void setRequiredValue(const std::string &reqValue)\r
77             {\r
78                 if (m_required.end() == std::find(m_required.begin(), m_required.end(), reqValue))\r
79                 {\r
80                     m_required.push_back(reqValue);\r
81                 }\r
82             }\r
83             std::vector<std::string> getRequiredValues()\r
84             {\r
85                 return m_required;\r
86             }\r
87             void addDefinition(const std::string &defName, Definitions *definition)\r
88             {\r
89                 if (m_definition.end() == m_definition.find(defName))\r
90                 {\r
91                     m_definition[defName] = definition;\r
92                 }\r
93             }\r
94             Definitions *getDefinition(const std::string &defName)\r
95             {\r
96                 if (m_definition.end() != m_definition.find(defName))\r
97                 {\r
98                     return m_definition[defName];\r
99                 }\r
100                 return nullptr;\r
101             }\r
102             std::string getType()\r
103             {\r
104                 return  m_type;\r
105             }\r
106             std::string getId()\r
107             {\r
108                 return  m_id;\r
109             }\r
110             std::string getSchema()\r
111             {\r
112                 return  m_schema;\r
113             }\r
114             std::string getDescription()\r
115             {\r
116                 return  m_description;\r
117             }\r
118             std::string getTitle()\r
119             {\r
120                 return  m_title;\r
121             }\r
122 \r
123             bool getAdditionalProperties()\r
124             {\r
125                 return  m_additionalProperties;\r
126             }\r
127             void setItem(Items *item)\r
128             {\r
129                 m_items.push_back(item);\r
130             }\r
131             std::vector<Items *> getItems()\r
132             {\r
133                 return m_items;\r
134             }\r
135 \r
136             void readJson();\r
137             Definitions *readDef(cJSON *childDefinitions, const std::string &defName);\r
138             Properties *readProp(cJSON *childProperties, const std::string &attName );\r
139             void readValues( cJSON *childProperties,  Properties *property , const std::string &attType);\r
140             void readString( cJSON *childProperties, Properties *property);\r
141             void readArray( cJSON *childProperties,  Properties *property);\r
142             void readNumber( cJSON *childProperties,  Properties *property);\r
143             Definitions *readRef(std::string m_ref);\r
144 \r
145 \r
146             void readJsonRef(cJSON *jsonReference);\r
147             void readDefRef(cJSON *defReference, Definitions *definition);\r
148             void readAllOf(cJSON *allofValues);\r
149             void readDefAllOf(cJSON *allofValues, Definitions *definition);\r
150             Items *readItems(cJSON *item);\r
151             void readItemRef(cJSON *itemReference, Items *item);\r
152             void readItemAllOf(cJSON *allofValues,  Items *item);\r
153 \r
154         private:\r
155             std::map<std::string, Properties *> m_properties;\r
156             std::map<std::string, Definitions *> m_definition;\r
157             std::string m_id;\r
158             std::string m_schema;\r
159             std::string m_title;\r
160             std::string m_description;\r
161             bool m_additionalProperties;\r
162             std::string m_type;\r
163             cJSON *m_cjson;\r
164             std::vector<std::string>  m_required;\r
165             std::vector<Items *> m_items;\r
166             IncludeResolverPtr m_includeResolver;\r
167     };\r
168     typedef std::shared_ptr<JsonSchema> JsonSchemaPtr;\r
169 \r
170 }\r
171 \r
172 #endif\r