Open Lighting Architecture: common/web/SchemaParseContext.h Source File

| 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
53 class SchemaParseContextInterface {
54 public:
55 SchemaParseContextInterface() {}
56 virtual ~SchemaParseContextInterface() {}
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
78 class ObjectParseContext : public SchemaParseContextInterface {
79 public:
80 ObjectParseContext() {}
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
114 class StrictTypedParseContext : public ObjectParseContext {
115 public:
116 StrictTypedParseContext() {}
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);
126 SchemaParseContextInterface* OpenArray(SchemaErrorLogger *logger);
127 void CloseArray(SchemaErrorLogger *logger);
128 SchemaParseContextInterface* OpenObject(SchemaErrorLogger *logger);
129 void CloseObject(SchemaErrorLogger *logger);
130
131 private:
132 void ReportErrorForType(SchemaErrorLogger *logger, JsonType type);
133
134 StrictTypedParseContext(const StrictTypedParseContext &) = delete;
135 const StrictTypedParseContext &operator=(const StrictTypedParseContext &) = delete;
136};
137
144 class DefinitionsParseContext : public StrictTypedParseContext {
145 public:
153 explicit DefinitionsParseContext(SchemaDefinitions *definitions)
154 : StrictTypedParseContext(),
155 m_schema_defs(definitions) {
156 }
157
158 SchemaParseContextInterface* OpenObject(SchemaErrorLogger *logger);
159 void CloseObject(SchemaErrorLogger *logger);
160
161 private:
162 SchemaDefinitions *m_schema_defs;
163 std::auto_ptr<SchemaParseContext> m_current_schema;
164
165 DefinitionsParseContext(const DefinitionsParseContext &) = delete;
166 const DefinitionsParseContext &operator=(const DefinitionsParseContext &) = delete;
167};
168
172 class SchemaParseContext : public SchemaParseContextInterface {
173 public:
179 explicit SchemaParseContext(SchemaDefinitions *definitions)
180 : m_schema_defs(definitions),
181 m_keyword(SCHEMA_UNKNOWN),
182 m_type(JSON_UNDEFINED) {
183 }
184
192 ValidatorInterface* GetValidator(SchemaErrorLogger *logger);
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);
204 SchemaParseContextInterface* OpenArray(SchemaErrorLogger *logger);
205 void CloseArray(SchemaErrorLogger *logger);
206 SchemaParseContextInterface* OpenObject(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
218 OptionalItem<std::string> m_id;
219 OptionalItem<std::string> m_schema;
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?
230 OptionalItem<std::string> m_pattern;
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;
263 OptionalItem<std::string> m_title;
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?
270 OptionalItem<std::string> m_format;
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
310 class PropertiesParseContext : public StrictTypedParseContext {
311 public:
312 explicit PropertiesParseContext(SchemaDefinitions *definitions)
313 : StrictTypedParseContext(),
314 m_schema_defs(definitions) {
315 }
316 ~PropertiesParseContext();
317
318 void AddPropertyValidators(ObjectValidator *object_validator,
319 SchemaErrorLogger *logger);
320
321 SchemaParseContextInterface* OpenObject(SchemaErrorLogger *logger);
322
323 private:
324 typedef std::map<std::string, SchemaParseContext*> SchemaMap;
325
326 SchemaDefinitions *m_schema_defs;
327 SchemaMap m_property_contexts;
328
329 PropertiesParseContext(const PropertiesParseContext &) = delete;
330 const PropertiesParseContext &operator=(const PropertiesParseContext &) = delete;
331};
332
333
337 class ArrayOfSchemaContext : public StrictTypedParseContext {
338 public:
339 explicit ArrayOfSchemaContext(SchemaDefinitions *definitions)
340 : m_schema_defs(definitions) {
341 }
342
343 ~ArrayOfSchemaContext();
344
351 void GetValidators(SchemaErrorLogger *logger,
352 ValidatorInterface::ValidatorList *validators);
353
354 SchemaParseContextInterface* OpenObject(SchemaErrorLogger *logger);
355
356 private:
357 typedef std::vector<SchemaParseContext*> ItemSchemas;
358
359 SchemaDefinitions *m_schema_defs;
360 ItemSchemas m_item_schemas;
361
362 ArrayOfSchemaContext(const ArrayOfSchemaContext &) = delete;
363 const ArrayOfSchemaContext &operator=(const ArrayOfSchemaContext &) = delete;
364};
365
366
372 class ArrayOfStringsContext : public StrictTypedParseContext {
373 public:
374 typedef std::set<std::string> StringSet;
375
376 ArrayOfStringsContext() {}
377
381 void GetStringSet(StringSet *stringd);
382
383 void String(SchemaErrorLogger *logger, const std::string &value);
384
385 private:
386 StringSet m_items;
387
388 ArrayOfStringsContext(const ArrayOfStringsContext &) = delete;
389 const ArrayOfStringsContext &operator=(const ArrayOfStringsContext &) = delete;
390};
391
399 class JsonValueContext : public SchemaParseContextInterface {
400 public:
401 JsonValueContext();
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);
413 SchemaParseContextInterface* OpenArray(SchemaErrorLogger *logger);
414 void CloseArray(SchemaErrorLogger *logger);
415 SchemaParseContextInterface* OpenObject(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
431 class ArrayOfJsonValuesContext : public SchemaParseContextInterface {
432 public:
433 ArrayOfJsonValuesContext() {}
434 ~ArrayOfJsonValuesContext();
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);
446 SchemaParseContextInterface* OpenArray(SchemaErrorLogger *logger);
447 void CloseArray(SchemaErrorLogger *logger);
448 SchemaParseContextInterface* OpenObject(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
461 ArrayOfJsonValuesContext(const ArrayOfJsonValuesContext &) = delete;
462 const ArrayOfJsonValuesContext &operator=(const ArrayOfJsonValuesContext &) = delete;
463};
464
465
473 class DependencyParseContext : public StrictTypedParseContext {
474 public:
475 explicit DependencyParseContext(SchemaDefinitions *definitions)
476 : m_schema_defs(definitions) {}
477 ~DependencyParseContext();
478
479 void AddDependenciesToValidator(ObjectValidator *validator);
480
481 SchemaParseContextInterface* OpenArray(SchemaErrorLogger *logger);
482 void CloseArray(SchemaErrorLogger *logger);
483 SchemaParseContextInterface* OpenObject(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
499 DependencyParseContext(const DependencyParseContext &) = delete;
500 const DependencyParseContext &operator=(const DependencyParseContext &) = delete;
501};
502} // namespace web
503} // namespace ola
504#endif // COMMON_WEB_SCHEMAPARSECONTEXT_H_
JsonParser.h
A JsonParserInterface implementation that builds a parse tree.
JsonSchema.h
A Json Schema, see www.json-schema.org.
Macro.h
Helper macros.
ola::web::ArrayOfSchemaContext
Parse the array of objects in an 'items' property.
Definition SchemaParseContext.h:337
ola::web::ArrayOfSchemaContext::GetValidators
void GetValidators(SchemaErrorLogger *logger, ValidatorInterface::ValidatorList *validators)
Populate a vector with validators for the elements in 'items'.
Definition SchemaParseContext.cpp:827
ola::web::ArrayOfStringsContext
The context for an array of strings.
Definition SchemaParseContext.h:372
ola::web::ArrayOfStringsContext::GetStringSet
void GetStringSet(StringSet *stringd)
Return the strings in the string array.
Definition SchemaParseContext.cpp:844
ola::web::BaseValidator
The base class for Json BaseValidators. All Visit methods return false.
Definition JsonSchema.h:121
ola::web::DefinitionsParseContext
The context for schema definitions.
Definition SchemaParseContext.h:144
ola::web::DefinitionsParseContext::DefinitionsParseContext
DefinitionsParseContext(SchemaDefinitions *definitions)
Create a new DefinitionsParseContext.
Definition SchemaParseContext.h:153
ola::web::DependencyParseContext
The context for a dependency object.
Definition SchemaParseContext.h:473
ola::web::IntegerValidator
The validator for Json integers.
Definition JsonSchema.h:476
ola::web::JsonParser
A JsonParserInterface implementation that builds a tree of JsonValues.
Definition JsonParser.h:57
ola::web::JsonValueContext
The context for a default value.
Definition SchemaParseContext.h:399
ola::web::JsonValue
The base class for JSON values.
Definition Json.h:119
ola::web::ObjectParseContext::ObjectKey
void ObjectKey(SchemaErrorLogger *, const std::string &keyword)
Called when we encounter a property.
Definition SchemaParseContext.h:85
ola::web::ObjectValidator
The validator for JsonObject.
Definition JsonSchema.h:531
ola::web::OptionalItem
Definition OptionalItem.h:31
ola::web::PropertiesParseContext
Definition SchemaParseContext.h:310
ola::web::SchemaDefinitions
Definition JsonSchema.h:960
ola::web::SchemaErrorLogger
The SchemaErrorLogger captures errors while parsing the schema.
Definition SchemaErrorLogger.h:41
ola::web::SchemaParseContext
Definition SchemaParseContext.h:172
ola::web::SchemaParseContext::SchemaParseContext
SchemaParseContext(SchemaDefinitions *definitions)
Create a new SchemaParseContext.
Definition SchemaParseContext.h:179
ola::web::SchemaParseContext::GetValidator
ValidatorInterface * GetValidator(SchemaErrorLogger *logger)
Return the ValidatorInterface for this context.
Definition SchemaParseContext.cpp:149
ola::web::SchemaParseContextInterface
The interface all SchemaParseContext classes inherit from.
Definition SchemaParseContext.h:53
ola::web::ValidatorInterface
The interface Json Schema Validators.
Definition JsonSchema.h:56
ola::web::ValidatorInterface::ValidatorList
std::vector< ValidatorInterface * > ValidatorList
a list of Validators.
Definition JsonSchema.h:61
ola::web
Classes to deal with web services.
Definition Json.cpp:29
ola::web::JsonType
JsonType
The type of JSON data element.
Definition JsonTypes.h:44
ola::web::JSON_UNDEFINED
@ JSON_UNDEFINED
Definition JsonTypes.h:52
ola::web::SchemaKeyword
SchemaKeyword
The list of valid JSon Schema keywords.
Definition SchemaKeywords.h:32
ola::web::SCHEMA_UNKNOWN
@ SCHEMA_UNKNOWN
Definition SchemaKeywords.h:33
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44