The Battle for Wesnoth  1.19.0-dev
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
schema_validation::schema_validator Class Reference

Realization of serialization/validator.hpp abstract validator. More...

#include <schema_validator.hpp>

Inheritance diagram for schema_validation::schema_validator:

Classes

struct  counter
 
struct  message_info
 Messages are cached. More...
 

Public Member Functions

virtual ~schema_validator ()
 
 schema_validator (const std::string &filename, bool validate_schema=false)
 Initializes validator from file. More...
 
void set_create_exceptions (bool value)
 
const std::vector< std::string > & get_errors () const
 
virtual void open_tag (const std::string &name, const config &parent, int start_line=0, const std::string &file="", bool addition=false) override
 Is called when parser opens tag. More...
 
virtual void close_tag () override
 As far as parser is built on stack, some realizations can store stack too. More...
 
virtual void validate (const config &cfg, const std::string &name, int start_line, const std::string &file) override
 Validates config. More...
 
virtual void validate_key (const config &cfg, const std::string &name, const config_attribute_value &value, int start_line, const std::string &file) override
 Checks if key is allowed and if its value is valid What exactly is validated depends on validator realization. More...
 
- Public Member Functions inherited from abstract_validator
 abstract_validator (const std::string &name)
 Constructor of validator can throw validator::error. More...
 
virtual ~abstract_validator ()
 

Protected Types

enum  {
  WRONG_TAG , EXTRA_TAG , MISSING_TAG , EXTRA_KEY ,
  MISSING_KEY , WRONG_VALUE , MISSING_SUPER , SUPER_CYCLE ,
  NEXT_ERROR
}
 
using message_type = int
 

Protected Member Functions

virtual void print (message_info &)
 
template<typename... T>
void queue_message (const config &cfg, T &&... args)
 
const wml_tagactive_tag () const
 
std::string active_tag_path () const
 
bool have_active_tag () const
 
bool is_valid () const
 
wml_type::ptr find_type (const std::string &type) const
 

Protected Attributes

bool create_exceptions_
 Controls the way to print errors. More...
 

Private Types

typedef std::map< std::string, countercnt_map
 Counters are mapped by tag name. More...
 
typedef std::stack< cnt_mapcnt_stack
 And counter maps are organize in stack. More...
 
typedef std::deque< message_infomessage_list
 
typedef std::map< const config *, message_listmessage_map
 
using derivation_graph_t = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, std::pair< const wml_tag *, std::string >, std::tuple< config, std::string, int > >
 
using link_graph_t = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, std::string >
 
using link_graph_map_t = std::map< const wml_type_alias *, link_graph_t::vertex_descriptor >
 

Private Member Functions

void print_cache ()
 
bool read_config_file (const std::string &filename)
 Reads config from input. More...
 
void detect_derivation_cycles ()
 
std::optional< std::map< std::string, wml_key > > find_mandatory_keys (const wml_tag *tag, const config &cfg) const
 Collects all mandatory keys for a tag, including the super keys and overrides. More...
 
std::optional< std::map< std::string, wml_key > > find_mandatory_keys (const wml_tag *tag, const config &cfg, std::vector< const wml_tag * > &visited) const
 
void validate_mandatory_keys (const wml_tag *tag, const config &cfg, const std::string &name, int start_line, const std::string &file)
 Validates that all mandatory keys for a tag are present. More...
 
void validate_mandatory_keys (const std::map< std::string, wml_key > &mandatory_keys, const wml_tag *tag, const config &cfg, const std::string &name, int start_line, const std::string &file, std::vector< const wml_tag * > &visited)
 
void detect_link_cycles (const std::string &filename)
 
void collect_link_source (link_graph_t &link_graph, link_graph_map_t &link_map, const std::string &type_name, const wml_type *type)
 
void collect_link_target (link_graph_t &link_graph, link_graph_map_t &link_map, const std::string &type_name, const wml_type *type, const wml_type_alias *alias)
 

Private Attributes

bool config_read_
 Shows, if validator is initialized with schema file. More...
 
wml_tag root_
 Root of schema information. More...
 
std::stack< const wml_tag * > stack_
 
cnt_stack counter_
 Contains number of children. More...
 
std::stack< message_mapcache_
 Caches error messages. More...
 
wml_type::map types_
 Type validators. More...
 
bool validate_schema_
 
std::vector< std::string > errors_
 
derivation_graph_t derivation_graph_
 
std::map< const wml_tag *, derivation_graph_t::vertex_descriptor > derivation_map_
 

Additional Inherited Members

- Public Attributes inherited from abstract_validator
const std::string name_
 

Detailed Description

Realization of serialization/validator.hpp abstract validator.

Based on stack. Uses some stacks to store different info.

Definition at line 39 of file schema_validator.hpp.

Member Typedef Documentation

◆ cnt_map

typedef std::map<std::string, counter> schema_validation::schema_validator::cnt_map
private

Counters are mapped by tag name.

Definition at line 78 of file schema_validator.hpp.

◆ cnt_stack

And counter maps are organize in stack.

Definition at line 81 of file schema_validator.hpp.

◆ derivation_graph_t

using schema_validation::schema_validator::derivation_graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, std::pair<const wml_tag*, std::string>, std::tuple<config, std::string, int> >
private

Definition at line 168 of file schema_validator.hpp.

◆ link_graph_map_t

using schema_validation::schema_validator::link_graph_map_t = std::map<const wml_type_alias*, link_graph_t::vertex_descriptor>
private

Definition at line 206 of file schema_validator.hpp.

◆ link_graph_t

using schema_validation::schema_validator::link_graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, std::string>
private

Definition at line 201 of file schema_validator.hpp.

◆ message_list

Definition at line 127 of file schema_validator.hpp.

◆ message_map

Definition at line 128 of file schema_validator.hpp.

◆ message_type

Definition at line 84 of file schema_validator.hpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protected
Enumerator
WRONG_TAG 
EXTRA_TAG 
MISSING_TAG 
EXTRA_KEY 
MISSING_KEY 
WRONG_VALUE 
MISSING_SUPER 
SUPER_CYCLE 
NEXT_ERROR 

Definition at line 85 of file schema_validator.hpp.

Constructor & Destructor Documentation

◆ ~schema_validator()

schema_validation::schema_validator::~schema_validator ( )
virtual

Definition at line 246 of file schema_validator.cpp.

◆ schema_validator()

schema_validation::schema_validator::schema_validator ( const std::string &  filename,
bool  validate_schema = false 
)

Initializes validator from file.

Throws abstract_validator::error if any error.

Definition at line 250 of file schema_validator.cpp.

References cache_, counter_, ERR_VL, schema_validation::wml_tag::expand_all(), LOG_VL, read_config_file(), root_, and stack_.

Member Function Documentation

◆ active_tag()

const wml_tag & schema_validation::schema_validator::active_tag ( ) const
protected

◆ active_tag_path()

std::string schema_validation::schema_validator::active_tag_path ( ) const
protected

Definition at line 652 of file schema_validator.cpp.

References utils::join(), game_config::path, and stack_.

Referenced by validate().

◆ close_tag()

void schema_validation::schema_validator::close_tag ( )
overridevirtual

As far as parser is built on stack, some realizations can store stack too.

So they need to know if tag was closed.

Implements abstract_validator.

Reimplemented in schema_validation::schema_self_validator.

Definition at line 408 of file schema_validator.cpp.

References counter_, print_cache(), and stack_.

Referenced by schema_validation::schema_self_validator::close_tag().

◆ collect_link_source()

void schema_validation::schema_validator::collect_link_source ( link_graph_t link_graph,
link_graph_map_t link_map,
const std::string &  type_name,
const wml_type type 
)
private

Definition at line 336 of file schema_validator.cpp.

References collect_link_target(), and types_.

Referenced by detect_link_cycles().

◆ collect_link_target()

void schema_validation::schema_validator::collect_link_target ( link_graph_t link_graph,
link_graph_map_t link_map,
const std::string &  type_name,
const wml_type type,
const wml_type_alias alias 
)
private

Definition at line 350 of file schema_validator.cpp.

References schema_validation::wml_type_alias::link().

Referenced by collect_link_source().

◆ detect_derivation_cycles()

void schema_validation::schema_validator::detect_derivation_cycles ( )
private

◆ detect_link_cycles()

void schema_validation::schema_validator::detect_link_cycles ( const std::string &  filename)
private

◆ find_mandatory_keys() [1/2]

std::optional< std::map< std::string, wml_key > > schema_validation::schema_validator::find_mandatory_keys ( const wml_tag tag,
const config cfg 
) const
private

Collects all mandatory keys for a tag, including the super keys and overrides.

The returned map can contain non-mandatory keys if they are overriden, please check the is_mandatory() result.

Definition at line 503 of file schema_validator.cpp.

Referenced by find_mandatory_keys(), and validate_mandatory_keys().

◆ find_mandatory_keys() [2/2]

std::optional< std::map< std::string, wml_key > > schema_validation::schema_validator::find_mandatory_keys ( const wml_tag tag,
const config cfg,
std::vector< const wml_tag * > &  visited 
) const
private

◆ find_type()

wml_type::ptr schema_validation::schema_validator::find_type ( const std::string &  type) const
protected

Definition at line 638 of file schema_validator.cpp.

References types_.

Referenced by validate_key().

◆ get_errors()

const std::vector<std::string>& schema_validation::schema_validator::get_errors ( ) const
inline

Definition at line 55 of file schema_validator.hpp.

References errors_.

◆ have_active_tag()

bool schema_validation::schema_validator::have_active_tag ( ) const
protected

◆ is_valid()

bool schema_validation::schema_validator::is_valid ( ) const
inlineprotected

◆ open_tag()

void schema_validation::schema_validator::open_tag ( const std::string &  name,
const config parent,
int  start_line = 0,
const std::string &  file = "",
bool  addition = false 
)
overridevirtual

Is called when parser opens tag.

Parameters
nameName of tag
parentThe parent config
start_lineLine in file
fileName of file
addition

Implements abstract_validator.

Reimplemented in schema_validation::schema_self_validator.

Definition at line 377 of file schema_validator.cpp.

References active_tag(), cache_, schema_validation::schema_validator::counter::cnt, counter_, create_exceptions_, errors_, schema_validation::wml_tag::find_tag(), root_, stack_, and schema_validation::wrong_tag_error().

Referenced by schema_validation::schema_self_validator::open_tag().

◆ print()

void schema_validation::schema_validator::print ( message_info el)
protectedvirtual

◆ print_cache()

void schema_validation::schema_validator::print_cache ( )
private

Definition at line 419 of file schema_validator.cpp.

References cache_, and print().

Referenced by close_tag(), and validate().

◆ queue_message()

template<typename... T>
void schema_validation::schema_validator::queue_message ( const config cfg,
T &&...  args 
)
inlineprotected

◆ read_config_file()

bool schema_validation::schema_validator::read_config_file ( const std::string &  filename)
private

◆ set_create_exceptions()

void schema_validation::schema_validator::set_create_exceptions ( bool  value)
inline

Definition at line 50 of file schema_validator.hpp.

References create_exceptions_.

Referenced by process_command_args().

◆ validate()

void schema_validation::schema_validator::validate ( const config cfg,
const std::string &  name,
int  start_line,
const std::string &  file 
)
overridevirtual

◆ validate_key()

void schema_validation::schema_validator::validate_key ( const config cfg,
const std::string &  name,
const config_attribute_value value,
int  start_line,
const std::string &  file 
)
overridevirtual

Checks if key is allowed and if its value is valid What exactly is validated depends on validator realization.

Parameters
cfgConfig to be validated.
nameName of tag
valueThe key's value
start_lineLine in file
fileName of file

Implements abstract_validator.

Reimplemented in schema_validation::schema_self_validator.

Definition at line 607 of file schema_validator.cpp.

References active_tag(), EXTRA_KEY, schema_validation::wml_tag::find_key(), find_type(), schema_validation::wml_key::get_type(), have_active_tag(), is_valid(), queue_message(), utils::split(), types_, and WRONG_VALUE.

Referenced by schema_validation::schema_self_validator::validate_key().

◆ validate_mandatory_keys() [1/2]

void schema_validation::schema_validator::validate_mandatory_keys ( const std::map< std::string, wml_key > &  mandatory_keys,
const wml_tag tag,
const config cfg,
const std::string &  name,
int  start_line,
const std::string &  file,
std::vector< const wml_tag * > &  visited 
)
private

Definition at line 559 of file schema_validator.cpp.

References config::get(), MISSING_KEY, and queue_message().

◆ validate_mandatory_keys() [2/2]

void schema_validation::schema_validator::validate_mandatory_keys ( const wml_tag tag,
const config cfg,
const std::string &  name,
int  start_line,
const std::string &  file 
)
private

Validates that all mandatory keys for a tag are present.

Definition at line 545 of file schema_validator.cpp.

References find_mandatory_keys().

Referenced by validate().

Member Data Documentation

◆ cache_

std::stack<message_map> schema_validation::schema_validator::cache_
private

Caches error messages.

Definition at line 145 of file schema_validator.hpp.

Referenced by open_tag(), print_cache(), queue_message(), schema_validator(), and validate().

◆ config_read_

bool schema_validation::schema_validator::config_read_
private

Shows, if validator is initialized with schema file.

Definition at line 134 of file schema_validator.hpp.

Referenced by is_valid(), and read_config_file().

◆ counter_

cnt_stack schema_validation::schema_validator::counter_
private

Contains number of children.

Definition at line 142 of file schema_validator.hpp.

Referenced by close_tag(), open_tag(), schema_validator(), and validate().

◆ create_exceptions_

bool schema_validation::schema_validator::create_exceptions_
protected

Controls the way to print errors.

Definition at line 121 of file schema_validator.hpp.

Referenced by open_tag(), print(), schema_validation::schema_self_validator::print(), and set_create_exceptions().

◆ derivation_graph_

derivation_graph_t schema_validation::schema_validator::derivation_graph_
private

Definition at line 174 of file schema_validator.hpp.

Referenced by detect_derivation_cycles(), and validate().

◆ derivation_map_

std::map<const wml_tag*, derivation_graph_t::vertex_descriptor> schema_validation::schema_validator::derivation_map_
private

Definition at line 175 of file schema_validator.hpp.

Referenced by detect_derivation_cycles(), and validate().

◆ errors_

std::vector<std::string> schema_validation::schema_validator::errors_
private

Definition at line 152 of file schema_validator.hpp.

Referenced by get_errors(), open_tag(), and print().

◆ root_

wml_tag schema_validation::schema_validator::root_
private

Root of schema information.

Definition at line 137 of file schema_validator.hpp.

Referenced by open_tag(), read_config_file(), schema_validator(), and validate().

◆ stack_

std::stack<const wml_tag*> schema_validation::schema_validator::stack_
private

◆ types_

wml_type::map schema_validation::schema_validator::types_
private

Type validators.

Definition at line 148 of file schema_validator.hpp.

Referenced by collect_link_source(), detect_link_cycles(), find_type(), read_config_file(), and validate_key().

◆ validate_schema_

bool schema_validation::schema_validator::validate_schema_
private

Definition at line 150 of file schema_validator.hpp.

Referenced by read_config_file().


The documentation for this class was generated from the following files: