serialization/schema_validator.hpp

Go to the documentation of this file.
00001 /* $Id: schema_validator.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2011 - 2012 by Sytyi Nick <nsytyi@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef SCHEMA_VALIDATOR_HPP
00017 #define SCHEMA_VALIDATOR_HPP
00018 
00019 #include "serialization/validator.hpp"
00020 #include "tools/schema/tag.hpp"
00021 
00022 #include "config.hpp"
00023 #include "config_cache.hpp"
00024 #include "serialization/parser.hpp"
00025 
00026 
00027 
00028 #include "boost/regex.hpp"
00029 
00030 #include <iostream>
00031 #include <queue>
00032 #include <string>
00033 #include <stack>
00034 
00035 /** @file
00036  *  One of the realizations of serialization/validator.hpp abstract validator.
00037  */
00038 namespace schema_validation{
00039 /**
00040  * Realization of serialization/validator.hpp abstract validator.
00041  * Based on stack. Uses some stacks to store different info.
00042  */
00043 class schema_validator : public abstract_validator{
00044 public:
00045     virtual ~schema_validator();
00046     /**
00047      * Initializes validator from file.
00048      * Throws abstract_validator::error if any error.
00049      */
00050     schema_validator(const std::string & filename);
00051     void set_create_exceptions(bool value){
00052         create_exceptions_ = value;
00053     }
00054 
00055     virtual void open_tag(const std::string & name,
00056                           int start_line=0,
00057                           const std::string &file="",
00058                           bool addittion = false);
00059     virtual void close_tag();
00060     virtual void validate(const config & cfg,
00061                           const std::string & name,
00062                           int start_line,
00063                           const std::string &file);
00064     virtual void validate_key(const config & cfg,
00065                               const std::string & name,
00066                               const std::string & value,
00067                               int start_line,
00068                               const std::string &file);
00069 private:
00070 // types section
00071     // Just some magic to ensure zero initialization.
00072     struct counter{
00073         int cnt;
00074         counter(): cnt(0){}
00075     };
00076     /**
00077      * Counters are mapped by tag name
00078      */
00079     typedef std::map<std::string,counter> cnt_map;
00080 
00081     /**
00082      * And counter maps are organize in stack.
00083      */
00084     typedef std::stack<cnt_map> cnt_stack;
00085 
00086     enum message_type{WRONG_TAG,EXTRA_TAG,MISSING_TAG,
00087                     EXTRA_KEY,MISSING_KEY,WRONG_VALUE};
00088     //error_cache
00089     /**
00090       * Messages are cached.
00091       * The reason is next: in file where [tag]...[/tag][+tag]...[/tag]
00092       * config object will be validated each [/tag]. So message can be as wrong
00093       * (when [+tag] section contains missed elements) as duplicated.
00094       *
00095       * Messages are mapped by config*. That ensures uniqueness.
00096       * Also message-maps are organized in stack to avoid memory leaks.
00097       */
00098     struct message_info{
00099         message_type type;
00100         std::string file;
00101         int line;
00102         int n;
00103         std::string tag;
00104         std::string key;
00105         std::string value;
00106         message_info(message_type t,
00107                    const std::string& file,
00108                    int line = 0,
00109                    int n = 0,
00110                    const std::string& tag = "",
00111                    const std::string& key = "",
00112                    const std::string& value = "")
00113                        :type(t),file(file),line(line),n(n),tag(tag),key(key),
00114                        value(value){}
00115     };
00116     typedef std::deque<message_info> message_list;
00117     typedef std::map<const config *, message_list> message_map;
00118 
00119     void print(message_info &);
00120     /**
00121      * Reads config from input.
00122      */
00123     bool read_config_file(const std::string & filename);
00124     /**
00125      * Shows, if validator is intialized with schema file;
00126      */
00127     bool config_read_;
00128     /**
00129      * Controls the way to print errors.
00130      */
00131     bool create_exceptions_;
00132     /**
00133      * Root of schema information
00134      */
00135     class_tag root_;
00136 
00137     std::stack<const class_tag *> stack_;
00138     /**
00139      * Contains number of children
00140      */
00141     cnt_stack counter_;
00142     /**
00143      * Caches error messages.
00144      */
00145     std::stack<message_map> cache_;
00146     /**
00147      * Type validators.
00148      */
00149      std::map<std::string,boost::regex> types_;
00150 };
00151 }//namespace schema_validation{
00152 
00153 #endif // SCHEMA_VALIDATOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:09 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs