The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
schema_validator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Sytyi Nick <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef SCHEMA_VALIDATOR_HPP
16 #define SCHEMA_VALIDATOR_HPP
17 
19 #include "tools/schema/tag.hpp"
20 
21 #include "config_cache.hpp"
22 #include "serialization/parser.hpp"
23 
24 
25 
26 #include <boost/regex.hpp>
27 
28 #include <iostream>
29 #include <queue>
30 #include <string>
31 #include <stack>
32 
33 class config;
34 
35 /** @file
36  * One of the realizations of serialization/validator.hpp abstract validator.
37  */
38 namespace schema_validation{
39 /**
40  * Realization of serialization/validator.hpp abstract validator.
41  * Based on stack. Uses some stacks to store different info.
42  */
44 public:
45  virtual ~schema_validator();
46  /**
47  * Initializes validator from file.
48  * Throws abstract_validator::error if any error.
49  */
50  schema_validator(const std::string & filename);
53  }
54 
55  virtual void open_tag(const std::string & name,
56  int start_line=0,
57  const std::string &file="",
58  bool addittion = false);
59  virtual void close_tag();
60  virtual void validate(const config & cfg,
61  const std::string & name,
62  int start_line,
63  const std::string &file);
64  virtual void validate_key(const config & cfg,
65  const std::string & name,
66  const std::string & value,
67  int start_line,
68  const std::string &file);
69 private:
70 // types section
71  // Just some magic to ensure zero initialization.
72  struct counter{
73  int cnt;
74  counter(): cnt(0){}
75  };
76  /**
77  * Counters are mapped by tag name
78  */
79  typedef std::map<std::string,counter> cnt_map;
80 
81  /**
82  * And counter maps are organize in stack.
83  */
84  typedef std::stack<cnt_map> cnt_stack;
85 
88  //error_cache
89  /**
90  * Messages are cached.
91  * The reason is next: in file where [tag]...[/tag][+tag]...[/tag]
92  * config object will be validated each [/tag]. So message can be as wrong
93  * (when [+tag] section contains missed elements) as duplicated.
94  *
95  * Messages are mapped by config*. That ensures uniqueness.
96  * Also message-maps are organized in stack to avoid memory leaks.
97  */
98  struct message_info{
101  int line;
102  int n;
107  const std::string& file,
108  int line = 0,
109  int n = 0,
110  const std::string& tag = "",
111  const std::string& key = "",
112  const std::string& value = "")
113  :type(t),file(file),line(line),n(n),tag(tag),key(key),
114  value(value){}
115  };
116  typedef std::deque<message_info> message_list;
117  typedef std::map<const config *, message_list> message_map;
118 
119  void print(message_info &);
120  /**
121  * Reads config from input.
122  */
123  bool read_config_file(const std::string & filename);
124  /**
125  * Shows, if validator is intialized with schema file;
126  */
128  /**
129  * Controls the way to print errors.
130  */
132  /**
133  * Root of schema information
134  */
136 
137  std::stack<const class_tag *> stack_;
138  /**
139  * Contains number of children
140  */
141  cnt_stack counter_;
142  /**
143  * Caches error messages.
144  */
145  std::stack<message_map> cache_;
146  /**
147  * Type validators.
148  */
149  std::map<std::string,boost::regex> types_;
150 };
151 }//namespace schema_validation{
152 
153 #endif // SCHEMA_VALIDATOR_HPP
std::stack< cnt_map > cnt_stack
And counter maps are organize in stack.
bool create_exceptions_
Controls the way to print errors.
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="")
cnt_stack counter_
Contains number of children.
virtual void close_tag()
As far as parser is built on stack, some realizations can store stack too.
virtual void validate(const config &cfg, const std::string &name, int start_line, const std::string &file)
Validates config.
std::deque< message_info > message_list
std::map< std::string, boost::regex > types_
Type validators.
std::map< const config *, message_list > message_map
This file contains information about validation abstract level interface.
GLdouble GLdouble t
Definition: glew.h:1366
bool config_read_
Shows, if validator is intialized with schema file;.
class_tag root_
Root of schema information.
This file contains objects "tag" and "key", which are used to store information about tags and keys w...
Used in parsing config file.
Definition: validator.hpp:36
std::stack< message_map > cache_
Caches error messages.
schema_validator(const std::string &filename)
Initializes validator from file.
GLsizei const GLfloat * value
Definition: glew.h:1817
virtual void validate_key(const config &cfg, const std::string &name, const std::string &value, int start_line, const std::string &file)
Checks if key is allowed and if its value is valid What exactly is validated depends on validator rea...
virtual void open_tag(const std::string &name, int start_line=0, const std::string &file="", bool addittion=false)
Is called when parser opens tag.
std::map< std::string, counter > cnt_map
Counters are mapped by tag name.
bool read_config_file(const std::string &filename)
Reads config from input.
std::stack< const class_tag * > stack_
GLuint const GLchar * name
Definition: glew.h:1782
Realization of serialization/validator.hpp abstract validator.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Stores information about tag.
Definition: tag.hpp:116