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