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