333e5715d276c4de39c07ff839eb2c6c62e674de
[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 /**\r
22  * @file   JsonSchema.h\r
23  *\r
24  * @brief   This file provides data Model for Json Schema file.\r
25  */\r
26 \r
27 #ifndef JSON_SCHEMA_H_\r
28 #define JSON_SCHEMA_H_\r
29 \r
30 #include <string>\r
31 #include <vector>\r
32 #include <map>\r
33 #include "Properties.h"\r
34 #include "Definitions.h"\r
35 #include "cJSON.h"\r
36 #include <memory>\r
37 \r
38 #include "IncludeResolver.h"\r
39 \r
40 namespace RAML\r
41 {\r
42     /**\r
43      * @class   JsonSchema\r
44      * @brief   This class provides data Model for Json Schema file.\r
45      */\r
46     class JsonSchema\r
47     {\r
48         public:\r
49             /**\r
50                   * Constructor of JsonSchema.\r
51                   */\r
52             JsonSchema() : m_additionalProperties(cJSON_True), m_cjson(NULL), m_includeResolver(NULL)  {}\r
53 \r
54             /**\r
55                   * Constructor of JsonSchema.\r
56                   *\r
57                   * @param includeResolver - Reference to IncludeResolver for reading external files\r
58                   */\r
59             JsonSchema(const IncludeResolverPtr &includeResolver) : m_additionalProperties(cJSON_True),\r
60                 m_cjson(NULL),\r
61                 m_includeResolver(includeResolver) {}\r
62 \r
63             /**\r
64                   * Constructor of JsonSchema.\r
65                   *\r
66                   * @param cjson - pointer to cjson\r
67                   * @param includeResolver - Reference to IncludeResolver for reading external files\r
68                   */\r
69             JsonSchema(cJSON *cjson , const IncludeResolverPtr &includeResolver) : m_additionalProperties(\r
70                     cJSON_True), m_cjson(cjson),\r
71                 m_includeResolver(includeResolver)  { readJson(); }\r
72 \r
73 \r
74             /**\r
75                  * This method is for setting cJson pointer to JsonSchema.\r
76                  *\r
77                  * @param cjson -pointer to cJson\r
78                  */\r
79             void setcJson(cJSON *cjson) {m_cjson = cjson; readJson(); }\r
80 \r
81             /**\r
82                  * This method is for getting size of Properties from JsonSchema.\r
83                  *\r
84                  * @return  size of Properties\r
85                  */\r
86             int size() const\r
87             {\r
88                 return m_properties.size();\r
89             }\r
90 \r
91             /**\r
92                  * This method is for getting Properties from JsonSchema.\r
93                  *\r
94                  * @param name -name of property as string\r
95                  *\r
96                  * @return pointer to Properties\r
97                  */\r
98             PropertiesPtr getProperty(const std::string &name)\r
99             {\r
100                 if (m_properties.end() != m_properties.find(name))\r
101                 {\r
102                     return m_properties[name];\r
103                 }\r
104                 return nullptr;\r
105             }\r
106 \r
107             /**\r
108                  * This method is for getting Properties from JsonSchema.\r
109                  *\r
110                  * @return map of Properties name and pointer to Properties\r
111                  */\r
112             std::map<std::string, PropertiesPtr > const &getProperties()\r
113             {\r
114                 return m_properties;\r
115             }\r
116 \r
117             /**\r
118                  * This method is for getting Definitions from JsonSchema.\r
119                  *\r
120                  * @return map of Definitions name and pointer to Definitions\r
121                  */\r
122             std::map<std::string, DefinitionsPtr > const &getDefinitions()\r
123             {\r
124                 return m_definition;\r
125             }\r
126 \r
127             /**\r
128                  * This method is for setting Properties to JsonSchema.\r
129                  *\r
130                  * @param name -name of property as string\r
131                  * @param property -pointer to Properties\r
132                  */\r
133             void addProperty(const std::string &name, const PropertiesPtr &property)\r
134             {\r
135                 if (m_properties.end() == m_properties.find(name))\r
136                 {\r
137                     m_properties[name] = property;\r
138                 }\r
139             }\r
140 \r
141             /**\r
142                  * This method is for setting RequiredValue to JsonSchema.\r
143                  *\r
144                  * @param reqValue -name of RequiredValue as string\r
145                  */\r
146             void setRequiredValue(const std::string &reqValue)\r
147             {\r
148                 if (m_required.end() == std::find(m_required.begin(), m_required.end(), reqValue))\r
149                 {\r
150                     m_required.push_back(reqValue);\r
151                 }\r
152             }\r
153 \r
154             /**\r
155                  * This method is for getting RequiredValues from JsonSchema.\r
156                  *\r
157                  * @return vector of RequiredValues as string\r
158                  */\r
159             std::vector<std::string> const &getRequiredValues()\r
160             {\r
161                 return m_required;\r
162             }\r
163 \r
164             /**\r
165                  * This method is for setting Definitions to JsonSchema.\r
166                  *\r
167                  * @param defName -name of Definitions as string\r
168                  * @param definition -pointer to Definitions\r
169                  */\r
170             void addDefinition(const std::string &defName, const DefinitionsPtr &definition)\r
171             {\r
172                 if (m_definition.end() == m_definition.find(defName))\r
173                 {\r
174                     m_definition[defName] = definition;\r
175                 }\r
176             }\r
177 \r
178             /**\r
179                  * This method is for getting Definitions from JsonSchema.\r
180                  *\r
181                  * @param defName -Definition name  as string\r
182                  *\r
183                  * @return pointer to Definitions\r
184                  */\r
185             DefinitionsPtr getDefinition(const std::string &defName)\r
186             {\r
187                 if (m_definition.end() != m_definition.find(defName))\r
188                 {\r
189                     return m_definition[defName];\r
190                 }\r
191                 return nullptr;\r
192             }\r
193 \r
194             /**\r
195                  * This method is for getting Type from JsonSchema.\r
196                  *\r
197                  * @return JsonSchema Type as string\r
198                  */\r
199             std::string getType()\r
200             {\r
201                 return  m_type;\r
202             }\r
203 \r
204             /**\r
205                  * This method is for getting Id from JsonSchema.\r
206                  *\r
207                  * @return JsonSchema Id as string\r
208                  */\r
209             std::string getId()\r
210             {\r
211                 return  m_id;\r
212             }\r
213 \r
214             /**\r
215                  * This method is for getting Schema from JsonSchema.\r
216                  *\r
217                  * @return  Schema as string\r
218                  */\r
219             std::string getSchema()\r
220             {\r
221                 return  m_schema;\r
222             }\r
223 \r
224             /**\r
225                  * This method is for getting Description from JsonSchema.\r
226                  *\r
227                  * @return JsonSchema Description as string\r
228                  */\r
229             std::string getDescription()\r
230             {\r
231                 return  m_description;\r
232             }\r
233 \r
234             /**\r
235                  * This method is for getting Title from JsonSchema.\r
236                  *\r
237                  * @return JsonSchema Title as string\r
238                  */\r
239             std::string getTitle()\r
240             {\r
241                 return  m_title;\r
242             }\r
243 \r
244             /**\r
245                  * This method is for getting AdditionalProperties from JsonSchema.\r
246                  *\r
247                  * @return AdditionalProperties as bool\r
248                  */\r
249             bool getAdditionalProperties()\r
250             {\r
251                 return  m_additionalProperties;\r
252             }\r
253 \r
254         private:\r
255             class JsonParameters\r
256             {\r
257                 public:\r
258                     std::map<std::string, PropertiesPtr > getProperties() const { return m_properties; }\r
259                     void addProperties(const std::string &name, const PropertiesPtr &prop) { m_properties[name] = prop; }\r
260                     void addProperties(const std::map<std::string, PropertiesPtr > &properties)\r
261                     {\r
262                         for (auto prop : properties)\r
263                             m_properties[prop.first] = prop.second;\r
264                     }\r
265                     std::vector<std::string> getRequired() const { return m_required; }\r
266                     void addRequired(const std::string &req) { m_required.push_back(req); }\r
267                     void addRequired(const std::vector<std::string> &required)\r
268                     {\r
269                         for (auto req : required)\r
270                             m_required.push_back(req);\r
271                     }\r
272                     std::string getType() const { return m_type; }\r
273                     void setType(const std::string &type)\r
274                     {\r
275                         if (m_type.empty())\r
276                             m_type = type;\r
277                     }\r
278 \r
279                 private:\r
280                     std::map<std::string, PropertiesPtr > m_properties;\r
281                     std::vector<std::string> m_required;\r
282                     std::string m_type;\r
283             };\r
284 \r
285             void readJson();\r
286             DefinitionsPtr readDef(cJSON *childDefinitions, const std::string &defName);\r
287             PropertiesPtr readProp(cJSON *childProperties, const std::string &attName );\r
288             void readDefaultValue(cJSON *defaultValue,  PropertiesPtr &property, const std::string &attType);\r
289             void readAllowedValues(cJSON *allowedvalues,  PropertiesPtr &property, std::string &attType);\r
290             void readValues( cJSON *childProperties,  PropertiesPtr &property ,\r
291                              const std::string &attType);\r
292             void readString( cJSON *childProperties, PropertiesPtr &property);\r
293             void readInteger( cJSON *childProperties,  PropertiesPtr &property);\r
294             void readDouble( cJSON *childProperties,  PropertiesPtr &property);\r
295             void readArray( cJSON *childProperties,  PropertiesPtr &property);\r
296             void readItems(cJSON *item, PropertiesPtr &property);\r
297             void readObject(cJSON *childProperties,  PropertiesPtr &property);\r
298 \r
299             void readFile(std::string &fileName , JsonParameters &param);\r
300             void readFile(std::string &fileName , std::string &defName , JsonParameters &param);\r
301             void readRef(std::string ref , JsonParameters &param);\r
302             void readJsonRef(cJSON *jsonReference , JsonParameters &param);\r
303             void readAllOf(cJSON *allofValues ,  JsonParameters &allParams);\r
304 \r
305         private:\r
306             std::map<std::string, PropertiesPtr > m_properties;\r
307             std::map<std::string, DefinitionsPtr > m_definition;\r
308             std::string m_id;\r
309             std::string m_schema;\r
310             std::string m_title;\r
311             std::string m_description;\r
312             bool m_additionalProperties;\r
313             std::string m_type;\r
314             cJSON *m_cjson;\r
315             std::vector<std::string>  m_required;\r
316             PropertiesPtr m_property;\r
317             IncludeResolverPtr m_includeResolver;\r
318 \r
319     };\r
320 \r
321     /** JsonSchemaPtr - shared Ptr to JsonSchema.*/\r
322     typedef std::shared_ptr<JsonSchema> JsonSchemaPtr;\r
323 \r
324 }\r
325 \r
326 #endif\r