-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
schema_validator.cpp
328 lines (293 loc) · 9.34 KB
/
schema_validator.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright (C) 2011 - 2018 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.
*/
#include "serialization/schema_validator.hpp"
#include "filesystem.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "serialization/preprocessor.hpp"
#include "serialization/string_utils.hpp"
#include "wml_exception.hpp"
namespace schema_validation
{
static lg::log_domain log_validation("validation");
#define ERR_VL LOG_STREAM(err, log_validation)
#define WRN_VL LOG_STREAM(warn, log_validation)
#define LOG_VL LOG_STREAM(info, log_validation)
static std::string at(const std::string& file, int line)
{
std::ostringstream ss;
ss << line << " " << file;
return "at " + ::lineno_string(ss.str());
}
static void print_output(const std::string& message, bool flag_exception = false)
{
#ifndef VALIDATION_ERRORS_LOG
if(flag_exception) {
throw wml_exception("Validation error occurred", message);
} else {
ERR_VL << message;
}
#else
// dirty hack to avoid "unused" error in case of compiling with definition on
flag_exception = true;
if(flag_exception) {
ERR_VL << message;
}
#endif
}
static void extra_tag_error(const std::string& file,
int line,
const std::string& name,
int n,
const std::string& parent,
bool flag_exception)
{
std::ostringstream ss;
ss << "Extra tag [" << name << "]; there may only be " << n << " [" << name << "] in [" << parent << "]\n"
<< at(file, line) << "\n";
print_output(ss.str(), flag_exception);
}
static void wrong_tag_error(
const std::string& file, int line, const std::string& name, const std::string& parent, bool flag_exception)
{
std::ostringstream ss;
ss << "Tag [" << name << "] may not be used in [" << parent << "]\n" << at(file, line) << "\n";
print_output(ss.str(), flag_exception);
}
static void missing_tag_error(const std::string& file,
int line,
const std::string& name,
int n,
const std::string& parent,
bool flag_exception)
{
std::ostringstream ss;
ss << "Missing tag [" << name << "]; there must be " << n << " [" << name << "]s in [" << parent << "]\n"
<< at(file, line) << "\n";
print_output(ss.str(), flag_exception);
}
static void extra_key_error(
const std::string& file, int line, const std::string& tag, const std::string& key, bool flag_exception)
{
std::ostringstream ss;
ss << "Invalid key '" << key << "=' in tag [" << tag << "]\n" << at(file, line) << "\n";
print_output(ss.str(), flag_exception);
}
static void missing_key_error(
const std::string& file, int line, const std::string& tag, const std::string& key, bool flag_exception)
{
std::ostringstream ss;
ss << "Missing key '" << key << "=' in tag [" << tag << "]\n" << at(file, line) << "\n";
print_output(ss.str(), flag_exception);
}
static void wrong_value_error(const std::string& file,
int line,
const std::string& tag,
const std::string& key,
const std::string& value,
bool flag_exception)
{
std::ostringstream ss;
ss << "Invalid value '" << value << "' in key '" << key << "=' in tag [" << tag << "]\n" << at(file, line) << "\n";
print_output(ss.str(), flag_exception);
}
static void wrong_type_error(const std::string & file, int line,
const std::string & tag,
const std::string & key,
const std::string & type,
bool flag_exception)
{
std::ostringstream ss;
ss << "Invalid type '" << type << "' in key '" << key << "=' in tag [" << tag << "]\n" << at(file, line) << "\n";
print_output(ss.str(), flag_exception);
}
schema_validator::~schema_validator()
{
}
schema_validator::schema_validator(const std::string& config_file_name)
: config_read_(false)
, create_exceptions_(strict_validation_enabled)
, root_()
, stack_()
, counter_()
, cache_()
, types_()
{
if(!read_config_file(config_file_name)) {
ERR_VL << "Schema file " << config_file_name << " was not read." << std::endl;
throw abstract_validator::error("Schema file " + config_file_name + " was not read.\n");
} else {
stack_.push(&root_);
counter_.emplace();
cache_.emplace();
root_.expand_all(root_);
LOG_VL << "Schema file " << config_file_name << " was read.\n"
<< "Validator initialized\n";
}
}
bool schema_validator::read_config_file(const std::string& filename)
{
config cfg;
try {
preproc_map preproc(game_config::config_cache::instance().get_preproc_map());
filesystem::scoped_istream stream = preprocess_file(filename, &preproc);
read(cfg, *stream);
} catch(config::error& e) {
ERR_VL << "Failed to read file " << filename << ":\n" << e.what() << "\n";
return false;
}
for(const config& g : cfg.child_range("wml_schema")) {
for(const config& schema : g.child_range("tag")) {
if(schema["name"].str() == "root") {
//@NOTE Don't know, maybe merging of roots needed.
root_ = class_tag(schema);
}
}
for(const config& type : g.child_range("type")) {
try {
types_[type["name"].str()] = class_type::from_config(type);
} catch(std::exception&) {
// Need to check all type values in schema-generator
}
}
}
config_read_ = true;
return true;
}
/*
* Please, @Note that there is some magic in pushing and poping to/from stacks.
* assume they all are on their place due to parser algorithm
* and validation logic
*/
void schema_validator::open_tag(const std::string& name, int start_line, const std::string& file, bool addittion)
{
if(!stack_.empty()) {
const class_tag* tag = nullptr;
if(stack_.top()) {
tag = stack_.top()->find_tag(name, root_);
if(!tag) {
wrong_tag_error(file, start_line, name, stack_.top()->get_name(), create_exceptions_);
} else {
if(!addittion) {
counter& cnt = counter_.top()[name];
++cnt.cnt;
}
}
}
stack_.push(tag);
} else {
stack_.push(nullptr);
}
counter_.emplace();
cache_.emplace();
}
void schema_validator::close_tag()
{
stack_.pop();
counter_.pop();
// cache_ is cleared in another place.
}
void schema_validator::validate(const config& cfg, const std::string& name, int start_line, const std::string& file)
{
// close previous errors and print them to output.
for(auto& m : cache_.top()) {
for(auto& list : m.second) {
print(list);
}
}
cache_.pop();
// clear cache
auto cache_it = cache_.top().find(&cfg);
if(cache_it != cache_.top().end()) {
cache_it->second.clear();
}
// Please note that validating unknown tag keys the result will be false
// Checking all elements counters.
if(!stack_.empty() && stack_.top() && config_read_) {
for(const auto& tag : stack_.top()->tags()) {
int cnt = counter_.top()[tag.first].cnt;
if(tag.second.get_min() > cnt) {
cache_.top()[&cfg].emplace_back(
MISSING_TAG, file, start_line, tag.second.get_min(), tag.first, "", name);
continue;
}
if(tag.second.get_max() < cnt) {
cache_.top()[&cfg].emplace_back(
EXTRA_TAG, file, start_line, tag.second.get_max(), tag.first, "", name);
}
}
// Checking if all mandatory keys are present
for(const auto& key : stack_.top()->keys()) {
if(key.second.is_mandatory()) {
if(cfg.get(key.first) == nullptr) {
cache_.top()[&cfg].emplace_back(MISSING_KEY, file, start_line, 0, name, key.first);
}
}
}
}
}
void schema_validator::validate_key(
const config& cfg, const std::string& name, const std::string& value, int start_line, const std::string& file)
{
if(!stack_.empty() && stack_.top() && !stack_.top()->get_name().empty() && config_read_) {
// checking existing keys
const class_key* key = stack_.top()->find_key(name);
if(key) {
bool matched = false;
for(auto& possible_type : utils::split(key->get_type())) {
auto itt = types_.find(possible_type);
if(itt == types_.end()) {
// TODO: This may not be a good place since it causes log spam when there's an invalid type
// Instead it should be checked when the schema is loaded!
cache_.top()[&cfg].emplace_back(
WRONG_TYPE, file, start_line, 0, stack_.top()->get_name(), name, key->get_type());
continue;
} else if(itt->second->matches(value, types_)) {
matched = true;
break;
}
}
if(!matched) {
cache_.top()[&cfg].emplace_back(
WRONG_VALUE, file, start_line, 0, stack_.top()->get_name(), name, value);
}
} else {
cache_.top()[&cfg].emplace_back(EXTRA_KEY, file, start_line, 0, stack_.top()->get_name(), name);
}
}
}
void schema_validator::print(message_info& el)
{
switch(el.type) {
case WRONG_TAG:
wrong_tag_error(el.file, el.line, el.tag, el.value, create_exceptions_);
break;
case EXTRA_TAG:
extra_tag_error(el.file, el.line, el.tag, el.n, el.value, create_exceptions_);
break;
case MISSING_TAG:
missing_tag_error(el.file, el.line, el.tag, el.n, el.value, create_exceptions_);
break;
case EXTRA_KEY:
extra_key_error(el.file, el.line, el.tag, el.key, create_exceptions_);
break;
case WRONG_VALUE:
wrong_value_error(el.file, el.line, el.tag, el.key, el.value, create_exceptions_);
break;
case MISSING_KEY:
missing_key_error(el.file, el.line, el.tag, el.key, create_exceptions_);
case WRONG_TYPE:
wrong_type_error(el.file, el.line, el.tag, el.key, el.value, create_exceptions_);
break;
}
}
} // namespace schema_validation{