-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
schema_validator.hpp
150 lines (136 loc) · 3.81 KB
/
schema_validator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Copyright (C) 2011 - 2016 by Sytyi Nick <nsytyi@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef SCHEMA_VALIDATOR_HPP
#define SCHEMA_VALIDATOR_HPP
#include "serialization/validator.hpp"
#include "tools/schema/tag.hpp"
#include "config_cache.hpp"
#include "serialization/parser.hpp"
#include <iostream>
#include <queue>
#include <regex>
#include <string>
#include <stack>
class config;
/** @file
* One of the realizations of serialization/validator.hpp abstract validator.
*/
namespace schema_validation{
/**
* Realization of serialization/validator.hpp abstract validator.
* Based on stack. Uses some stacks to store different info.
*/
class schema_validator : public abstract_validator{
public:
virtual ~schema_validator();
/**
* Initializes validator from file.
* Throws abstract_validator::error if any error.
*/
schema_validator(const std::string & filename);
void set_create_exceptions(bool value){
create_exceptions_ = value;
}
virtual void open_tag(const std::string & name,
int start_line=0,
const std::string &file="",
bool addittion = false);
virtual void close_tag();
virtual void validate(const config & cfg,
const std::string & name,
int start_line,
const std::string &file);
virtual void validate_key(const config & cfg,
const std::string & name,
const std::string & value,
int start_line,
const std::string &file);
private:
// types section
// Just some magic to ensure zero initialization.
struct counter{
int cnt;
counter(): cnt(0){}
};
/**
* Counters are mapped by tag name
*/
typedef std::map<std::string,counter> cnt_map;
/**
* And counter maps are organize in stack.
*/
typedef std::stack<cnt_map> cnt_stack;
enum message_type{WRONG_TAG,EXTRA_TAG,MISSING_TAG,
EXTRA_KEY,MISSING_KEY,WRONG_VALUE};
//error_cache
/**
* Messages are cached.
* The reason is next: in file where [tag]...[/tag][+tag]...[/tag]
* config object will be validated each [/tag]. So message can be as wrong
* (when [+tag] section contains missed elements) as duplicated.
*
* Messages are mapped by config*. That ensures uniqueness.
* Also message-maps are organized in stack to avoid memory leaks.
*/
struct message_info{
message_type type;
std::string file;
int line;
int n;
std::string tag;
std::string key;
std::string value;
message_info(message_type t,
const std::string& file,
int line = 0,
int n = 0,
const std::string& tag = "",
const std::string& key = "",
const std::string& value = "")
:type(t),file(file),line(line),n(n),tag(tag),key(key),
value(value){}
};
typedef std::deque<message_info> message_list;
typedef std::map<const config *, message_list> message_map;
void print(message_info &);
/**
* Reads config from input.
*/
bool read_config_file(const std::string & filename);
/**
* Shows, if validator is intialized with schema file;
*/
bool config_read_;
/**
* Controls the way to print errors.
*/
bool create_exceptions_;
/**
* Root of schema information
*/
class_tag root_;
std::stack<const class_tag *> stack_;
/**
* Contains number of children
*/
cnt_stack counter_;
/**
* Caches error messages.
*/
std::stack<message_map> cache_;
/**
* Type validators.
*/
std::map<std::string,std::regex> types_;
};
}//namespace schema_validation{
#endif // SCHEMA_VALIDATOR_HPP