Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
SchemaParseContext.h
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 * SchemaParseContext.h
17 * Stores the state required as we walk the JSON schema document.
18 * Copyright (C) 2014 Simon Newton
19 */
20
21#ifndef COMMON_WEB_SCHEMAPARSECONTEXT_H_
22#define COMMON_WEB_SCHEMAPARSECONTEXT_H_
23
24#include <ola/base/Macro.h>
25
26#include <map>
27#include <memory>
28#include <set>
29#include <string>
30#include <vector>
31
32#include "common/web/PointerTracker.h"
33#include "common/web/SchemaErrorLogger.h"
34#include "common/web/SchemaKeywords.h"
35#include "ola/web/JsonSchema.h"
36#include "ola/web/JsonParser.h"
37#include "ola/web/OptionalItem.h"
38
39namespace ola {
40namespace web {
41
42class ArrayOfSchemaContext;
43class ArrayOfStringsContext;
44class DefinitionsParseContext;
45class DependencyParseContext;
46class JsonValueContext;
47class PropertiesParseContext;
48class SchemaParseContext;
49
54 public:
57
58 virtual void String(SchemaErrorLogger *logger, const std::string &value) = 0;
59 virtual void Number(SchemaErrorLogger *logger, uint32_t value) = 0;
60 virtual void Number(SchemaErrorLogger *logger, int32_t value) = 0;
61 virtual void Number(SchemaErrorLogger *logger, uint64_t value) = 0;
62 virtual void Number(SchemaErrorLogger *logger, int64_t value) = 0;
63 virtual void Number(SchemaErrorLogger *logger, double value) = 0;
64 virtual void Bool(SchemaErrorLogger *logger, bool value) = 0;
65 virtual void Null(SchemaErrorLogger *logger) = 0;
66 virtual SchemaParseContextInterface* OpenArray(SchemaErrorLogger *logger) = 0;
67 virtual void CloseArray(SchemaErrorLogger *logger) = 0;
68 virtual SchemaParseContextInterface* OpenObject(
69 SchemaErrorLogger *logger) = 0;
70 virtual void ObjectKey(SchemaErrorLogger *logger, const std::string &key) = 0;
71 virtual void CloseObject(SchemaErrorLogger *logger) = 0;
72};
73
79 public:
81
85 void ObjectKey(SchemaErrorLogger*, const std::string &keyword) {
86 m_keyword.Set(keyword);
87 }
88
89 protected:
93 std::string TakeKeyword() {
94 std::string keyword = m_keyword.Value();
95 m_keyword.Reset();
96 return keyword;
97 }
98
102 const std::string& Keyword() const { return m_keyword.Value(); }
103
104 private:
105 OptionalItem<std::string> m_keyword;
106};
107
115 public:
117
118 void String(SchemaErrorLogger *logger, const std::string &value);
119 void Number(SchemaErrorLogger *logger, uint32_t value);
120 void Number(SchemaErrorLogger *logger, int32_t value);
121 void Number(SchemaErrorLogger *logger, uint64_t value);
122 void Number(SchemaErrorLogger *logger, int64_t value);
123 void Number(SchemaErrorLogger *logger, double value);
124 void Bool(SchemaErrorLogger *logger, bool value);
125 void Null(SchemaErrorLogger *logger);
127 void CloseArray(SchemaErrorLogger *logger);
129 void CloseObject(SchemaErrorLogger *logger);
130
131 private:
132 void ReportErrorForType(SchemaErrorLogger *logger, JsonType type);
133
135 const StrictTypedParseContext &operator=(const StrictTypedParseContext &) = delete;
136};
137
145 public:
155 m_schema_defs(definitions) {
156 }
157
159 void CloseObject(SchemaErrorLogger *logger);
160
161 private:
162 SchemaDefinitions *m_schema_defs;
163 std::auto_ptr<SchemaParseContext> m_current_schema;
164
166 const DefinitionsParseContext &operator=(const DefinitionsParseContext &) = delete;
167};
168
173 public:
180 : m_schema_defs(definitions),
181 m_keyword(SCHEMA_UNKNOWN),
182 m_type(JSON_UNDEFINED) {
183 }
184
193
194 void ObjectKey(SchemaErrorLogger *logger, const std::string &keyword);
195
196 void String(SchemaErrorLogger *logger, const std::string &value);
197 void Number(SchemaErrorLogger *logger, uint32_t value);
198 void Number(SchemaErrorLogger *logger, int32_t value);
199 void Number(SchemaErrorLogger *logger, uint64_t value);
200 void Number(SchemaErrorLogger *logger, int64_t value);
201 void Number(SchemaErrorLogger *logger, double value);
202 void Bool(SchemaErrorLogger *logger, bool value);
203 void Null(SchemaErrorLogger *logger);
205 void CloseArray(SchemaErrorLogger *logger);
207 void CloseObject(SchemaErrorLogger *logger);
208
209 private:
210 SchemaDefinitions *m_schema_defs;
211 // Set to the last keyword reported to ObjectKey()
212 SchemaKeyword m_keyword;
213
214 // Members are arranged according to the order in which they appear in the
215 // draft standard.
216
217 // Common keywords
220
221 // 5.1 Number / integer keywords
222 OptionalItem<bool> m_exclusive_maximum;
223 OptionalItem<bool> m_exclusive_minimum;
224 std::auto_ptr<JsonNumber> m_maximum;
225 std::auto_ptr<JsonNumber> m_minimum;
226 std::auto_ptr<JsonNumber> m_multiple_of;
227
228 // 5.2 String keywords
229 // TODO(simon): Implement pattern support?
231 OptionalItem<uint64_t> m_max_length;
232 OptionalItem<uint64_t> m_min_length;
233
234 // 5.3 Array keywords
235 // 'additionalItems' can be either a bool or a schema
236 OptionalItem<bool> m_additional_items;
237 std::auto_ptr<SchemaParseContext> m_additional_items_context;
238
239 // 'items' can be either a json schema, or an array of json schema.
240 std::auto_ptr<SchemaParseContext> m_items_single_context;
241 std::auto_ptr<ArrayOfSchemaContext> m_items_context_array;
242
243 OptionalItem<uint64_t> m_max_items;
244 OptionalItem<uint64_t> m_min_items;
245 OptionalItem<bool> m_unique_items;
246
247 // 5.4 Object keywords
248 OptionalItem<uint64_t> m_max_properties;
249 OptionalItem<uint64_t> m_min_properties;
250 std::auto_ptr<ArrayOfStringsContext> m_required_items;
251 std::auto_ptr<DependencyParseContext> m_dependency_context;
252
253 // 5.5 Keywords for multiple instance types
254 JsonType m_type;
255 std::auto_ptr<class ArrayOfJsonValuesContext> m_enum_context;
256 std::auto_ptr<class ArrayOfSchemaContext> m_allof_context;
257 std::auto_ptr<class ArrayOfSchemaContext> m_anyof_context;
258 std::auto_ptr<class ArrayOfSchemaContext> m_oneof_context;
259 std::auto_ptr<class SchemaParseContext> m_not_context;
260
261 // 6. Metadata keywords
262 OptionalItem<std::string> m_description;
264 std::auto_ptr<const JsonValue> m_default_value;
265 std::auto_ptr<JsonValueContext> m_default_value_context;
266
267 OptionalItem<std::string> m_ref_schema;
268
269 // TODO(simon): Implement format support?
271
272 std::auto_ptr<DefinitionsParseContext> m_definitions_context;
273 std::auto_ptr<PropertiesParseContext> m_properties_context;
274 OptionalItem<bool> m_additional_properties;
275 std::auto_ptr<SchemaParseContext> m_additional_properties_context;
276
277 void ProcessPositiveInt(SchemaErrorLogger *logger, uint64_t value);
278
279 template <typename T>
280 void ProcessInt(SchemaErrorLogger *logger, T t);
281
282 bool AddNumberConstraints(IntegerValidator *validator,
283 SchemaErrorLogger *logger);
284
285 BaseValidator* BuildArrayValidator(SchemaErrorLogger *logger);
286 BaseValidator* BuildObjectValidator(SchemaErrorLogger *logger);
287 BaseValidator* BuildStringValidator(SchemaErrorLogger *logger);
288
289 static bool ValidTypeForKeyword(SchemaErrorLogger *logger,
290 SchemaKeyword keyword,
291 JsonType type);
292
293 // Verify that type == expected_type. If it doesn't report an error to the
294 // logger.
295 static bool CheckTypeAndLog(SchemaErrorLogger *logger, SchemaKeyword keyword,
296 JsonType type, JsonType expected_type);
297 // Same as above but the type can be either expected_type1 or expected_type2
298 static bool CheckTypeAndLog(SchemaErrorLogger *logger, SchemaKeyword keyword,
299 JsonType type, JsonType expected_type1,
300 JsonType expected_type2);
301
302 SchemaParseContext(const SchemaParseContext &) = delete;
303 const SchemaParseContext &operator=(const SchemaParseContext &) = delete;
304};
305
306
311 public:
312 explicit PropertiesParseContext(SchemaDefinitions *definitions)
314 m_schema_defs(definitions) {
315 }
317
318 void AddPropertyValidators(ObjectValidator *object_validator,
319 SchemaErrorLogger *logger);
320
322
323 private:
324 typedef std::map<std::string, SchemaParseContext*> SchemaMap;
325
326 SchemaDefinitions *m_schema_defs;
327 SchemaMap m_property_contexts;
328
330 const PropertiesParseContext &operator=(const PropertiesParseContext &) = delete;
331};
332
333
338 public:
339 explicit ArrayOfSchemaContext(SchemaDefinitions *definitions)
340 : m_schema_defs(definitions) {
341 }
342
344
351 void GetValidators(SchemaErrorLogger *logger,
353
355
356 private:
357 typedef std::vector<SchemaParseContext*> ItemSchemas;
358
359 SchemaDefinitions *m_schema_defs;
360 ItemSchemas m_item_schemas;
361
363 const ArrayOfSchemaContext &operator=(const ArrayOfSchemaContext &) = delete;
364};
365
366
373 public:
374 typedef std::set<std::string> StringSet;
375
377
381 void GetStringSet(StringSet *stringd);
382
383 void String(SchemaErrorLogger *logger, const std::string &value);
384
385 private:
386 StringSet m_items;
387
389 const ArrayOfStringsContext &operator=(const ArrayOfStringsContext &) = delete;
390};
391
400 public:
402
403 const JsonValue* ClaimValue(SchemaErrorLogger *logger);
404
405 void String(SchemaErrorLogger *logger, const std::string &value);
406 void Number(SchemaErrorLogger *logger, uint32_t value);
407 void Number(SchemaErrorLogger *logger, int32_t value);
408 void Number(SchemaErrorLogger *logger, uint64_t value);
409 void Number(SchemaErrorLogger *logger, int64_t value);
410 void Number(SchemaErrorLogger *logger, double value);
411 void Bool(SchemaErrorLogger *logger, bool value);
412 void Null(SchemaErrorLogger *logger);
414 void CloseArray(SchemaErrorLogger *logger);
416 void ObjectKey(SchemaErrorLogger *logger, const std::string &key);
417 void CloseObject(SchemaErrorLogger *logger);
418
419 private:
420 JsonParser m_parser;
421
422 JsonValueContext(const JsonValueContext &) = delete;
423 const JsonValueContext &operator=(const JsonValueContext &) = delete;
424};
425
432 public:
435
436 void AddEnumsToValidator(BaseValidator *validator);
437
438 void String(SchemaErrorLogger *logger, const std::string &value);
439 void Number(SchemaErrorLogger *logger, uint32_t value);
440 void Number(SchemaErrorLogger *logger, int32_t value);
441 void Number(SchemaErrorLogger *logger, uint64_t value);
442 void Number(SchemaErrorLogger *logger, int64_t value);
443 void Number(SchemaErrorLogger *logger, double value);
444 void Bool(SchemaErrorLogger *logger, bool value);
445 void Null(SchemaErrorLogger *logger);
447 void CloseArray(SchemaErrorLogger *logger);
449 void ObjectKey(SchemaErrorLogger*, const std::string &) {}
450 void CloseObject(SchemaErrorLogger *logger);
451
452 bool Empty() const { return m_enums.empty(); }
453
454 private:
455 std::vector<const JsonValue*> m_enums;
456 std::auto_ptr<JsonValueContext> m_value_context;
457
458 void CheckForDuplicateAndAdd(SchemaErrorLogger *logger,
459 const JsonValue *value);
460
462 const ArrayOfJsonValuesContext &operator=(const ArrayOfJsonValuesContext &) = delete;
463};
464
465
474 public:
475 explicit DependencyParseContext(SchemaDefinitions *definitions)
476 : m_schema_defs(definitions) {}
478
479 void AddDependenciesToValidator(ObjectValidator *validator);
480
482 void CloseArray(SchemaErrorLogger *logger);
484 void CloseObject(SchemaErrorLogger *logger);
485
486 private:
487 typedef std::set<std::string> StringSet;
488 typedef std::map<std::string, ValidatorInterface*> SchemaDependencies;
489 typedef std::map<std::string, StringSet> PropertyDependencies;
490
491 SchemaDefinitions *m_schema_defs;
492
493 std::auto_ptr<ArrayOfStringsContext> m_property_context;
494 std::auto_ptr<SchemaParseContext> m_schema_context;
495
496 PropertyDependencies m_property_dependencies;
497 SchemaDependencies m_schema_dependencies;
498
500 const DependencyParseContext &operator=(const DependencyParseContext &) = delete;
501};
502} // namespace web
503} // namespace ola
504#endif // COMMON_WEB_SCHEMAPARSECONTEXT_H_
A JsonParserInterface implementation that builds a parse tree.
A Json Schema, see www.json-schema.org.
Helper macros.
The context for an array of JsonValues.
Definition SchemaParseContext.h:431
Parse the array of objects in an 'items' property.
Definition SchemaParseContext.h:337
void GetValidators(SchemaErrorLogger *logger, ValidatorInterface::ValidatorList *validators)
Populate a vector with validators for the elements in 'items'.
Definition SchemaParseContext.cpp:827
The context for an array of strings.
Definition SchemaParseContext.h:372
void GetStringSet(StringSet *stringd)
Return the strings in the string array.
Definition SchemaParseContext.cpp:844
The base class for Json BaseValidators. All Visit methods return false.
Definition JsonSchema.h:121
The context for schema definitions.
Definition SchemaParseContext.h:144
DefinitionsParseContext(SchemaDefinitions *definitions)
Create a new DefinitionsParseContext.
Definition SchemaParseContext.h:153
The context for a dependency object.
Definition SchemaParseContext.h:473
The validator for Json integers.
Definition JsonSchema.h:476
A JsonParserInterface implementation that builds a tree of JsonValues.
Definition JsonParser.h:57
The context for a default value.
Definition SchemaParseContext.h:399
The base class for JSON values.
Definition Json.h:119
A SchemaParseContext that keeps track of the last keyword / property seen.
Definition SchemaParseContext.h:78
void ObjectKey(SchemaErrorLogger *, const std::string &keyword)
Called when we encounter a property.
Definition SchemaParseContext.h:85
The validator for JsonObject.
Definition JsonSchema.h:531
Definition OptionalItem.h:31
Definition SchemaParseContext.h:310
Definition JsonSchema.h:960
The SchemaErrorLogger captures errors while parsing the schema.
Definition SchemaErrorLogger.h:41
Definition SchemaParseContext.h:172
SchemaParseContext(SchemaDefinitions *definitions)
Create a new SchemaParseContext.
Definition SchemaParseContext.h:179
ValidatorInterface * GetValidator(SchemaErrorLogger *logger)
Return the ValidatorInterface for this context.
Definition SchemaParseContext.cpp:149
The interface all SchemaParseContext classes inherit from.
Definition SchemaParseContext.h:53
A SchemaParseContext that reports errors for all types.
Definition SchemaParseContext.h:114
The interface Json Schema Validators.
Definition JsonSchema.h:56
std::vector< ValidatorInterface * > ValidatorList
a list of Validators.
Definition JsonSchema.h:61
JsonType
The type of JSON data element.
Definition JsonTypes.h:44
@ JSON_UNDEFINED
Definition JsonTypes.h:52
SchemaKeyword
The list of valid JSon Schema keywords.
Definition SchemaKeywords.h:32
@ SCHEMA_UNKNOWN
Definition SchemaKeywords.h:33
The namespace containing all OLA symbols.
Definition Credentials.cpp:44