NCBI C++ ToolKit
errors.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: errors.hpp 72932 2016-06-08 18:03:19Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Sergey Satskiy, NCBI
27  * Credits: Denis Vakatov, NCBI (help with the original design)
28  *
29  */
30 
31 
32 /** @file
33  * Various XML parser's and validator's errors related code for XmlWrapp.
34 **/
35 
36 #ifndef _xmlwrapp_errors_hpp_
37 #define _xmlwrapp_errors_hpp_
38 
39 // standard includes
40 #include <string>
41 #include <stdexcept>
42 #include <list>
43 
44 namespace xml {
45 
46 /**
47  * The xml::error_message class is used to store a single error message
48  * which may appear while parsing or validating an XML document.
49  *
50  * @author Sergey Satskiy, NCBI
51 **/
53 public:
54  /// A type for different type of errors
55  enum message_type {
56  type_fatal_error, ///< fatal error
57  type_error, ///< error
58  type_warning ///< warning
59  };
60 
61  /**
62  * Convert an error type to a string.
63  *
64  * @param mt The error type.
65  * @return The string representation of the error type.
66  * @author Sergey Satskiy, NCBI
67  **/
69 
70  /**
71  * Create a new xml::error_message object.
72  *
73  * @param message The error message.
74  * @param msg_type The error type.
75  * @param line file line realting to error message. 0 if none.
76  * @param filename file name if available or an empty string.
77  * @author Sergey Satskiy, NCBI
78  **/
79  error_message(const std::string& message,
80  message_type msg_type,
81  int line,
82  const std::string& filename);
83 
84  /**
85  * Get the error message type.
86  *
87  * @return The error type.
88  * @author Sergey Satskiy, NCBI
89  **/
90  message_type get_message_type (void) const;
91 
92  /**
93  * Get the error message.
94  *
95  * @return The error message.
96  * @author Sergey satskiy, NCBI
97  **/
98  std::string get_message (void) const;
99 
100  /**
101  * Get the line number.
102  *
103  * @return The line number, 0 if not available.
104  **/
105  int get_line (void) const;
106 
107  /**
108  * Get the file name.
109  *
110  * @return The file name, empty string if not available.
111  **/
112  std::string get_filename (void) const;
113 
114 private:
117  int line_;
119 };
120 
121 
122 
123 /**
124  * The xml::error_messages class is used to store all the error message
125  * which are collected while parsing or validating an XML document.
126  *
127  * @author Sergey Satskiy, NCBI
128 **/
130 {
131 public:
132  /// A type to store multiple messages
133  typedef std::list<error_message> error_messages_type;
134 
135  /**
136  * Get the error messages.
137  *
138  * @return The error messages.
139  * @author Sergey Satskiy, NCBI
140  **/
141  const error_messages_type& get_messages (void) const;
142 
143  /**
144  * Get the error messages.
145  *
146  * @return The error messages.
147  * @author Sergey Satskiy, NCBI
148  **/
149  error_messages_type& get_messages (void);
150 
151  /**
152  * Check if there are warnings in the error messages.
153  *
154  * @return true if there is at least one warning in the error messages.
155  * It does not consider errors and fatal errors.
156  * @author Sergey Satskiy, NCBI
157  **/
158  bool has_warnings (void) const;
159 
160  /**
161  * Check if there are errors in the error messages.
162  *
163  * @return true if there is at least one error in the error messages.
164  * It does not consider fatal errors.
165  * @author Sergey Satskiy, NCBI
166  **/
167  bool has_errors (void) const;
168 
169  /**
170  * Check if there are fatal errors in the error messages.
171  *
172  * @return true if there is at least one fatal error in the error messages.
173  * @author Sergey Satskiy, NCBI
174  **/
175  bool has_fatal_errors (void) const;
176 
177  /**
178  * Convert error messages into a single printable string.
179  *
180  * @return string representation of the errors list ('\n' separated)
181  * @author Sergey Satskiy, NCBI
182  **/
183  std::string print (void) const;
184 
185  /**
186  * Appends the messages from the other container.
187  * @author Sergey Satskiy, NCBI
188  **/
189  void append_messages(const error_messages & other);
190 
191 
192 private:
193  error_messages_type error_messages_;
195 };
196 
197 
198 
199 
200 /**
201  * The xml::parser_exception class is used to store parsing and validating
202  * exception information.
203  *
204  * @author Sergey Satskiy, NCBI
205 **/
206 class parser_exception : public std::exception
207 {
208 public:
209 
210  /**
211  * Convert error messages into a printable C-style string.
212  *
213  * @return string representation of the errors list ('\n' separated).
214  * @author Sergey Satskiy, NCBI
215  **/
216  virtual const char* what() const throw ();
217 
218  /**
219  * Get error messages.
220  *
221  * @return The error messages.
222  * @author Sergey Satskiy, NCBI
223  **/
224  const error_messages& get_messages(void) const;
225 
226  /**
227  * Create a new object using the given list of error messages.
228  *
229  * @param msgs The error messages.
230  * @author Sergey Satskiy, NCBI
231  **/
232  parser_exception(const error_messages& msgs);
233 
234  /**
235  * Destroy the object.
236  *
237  * @author Sergey Satskiy, NCBI
238  **/
239  virtual ~parser_exception() throw () {}
240 
241 private:
243  mutable std::string buffer; // used for list convertion in what()
244 };
245 
246 
247 /// A type for different approaches to process warnings
249  type_warnings_are_errors, ///< Treat warnings as errors
250  type_warnings_not_errors ///< Do not treat warnings as errors
251 };
252 
253 } // xml namespace
254 
255 #endif
256 
std::string get_filename(void) const
Get the file name.
Definition: errors.cpp:73
Do not treat warnings as errors.
Definition: errors.hpp:250
error_message(const std::string &message, message_type msg_type, int line, const std::string &filename)
Create a new xml::error_message object.
Definition: errors.cpp:53
void append_messages(const error_messages &other)
Appends the messages from the other container.
Definition: errors.cpp:129
std::string print(void) const
Convert error messages into a single printable string.
Definition: errors.cpp:112
std::string filename_
Definition: errors.hpp:118
The xml::parser_exception class is used to store parsing and validating exception information...
Definition: errors.hpp:206
virtual const char * what() const
Convert error messages into a printable C-style string.
Definition: errors.cpp:142
The xml::error_message class is used to store a single error message which may appear while parsing o...
Definition: errors.hpp:52
error_messages_type error_messages_
Definition: errors.hpp:193
std::string message_
Definition: errors.hpp:116
int get_line(void) const
Get the line number.
Definition: errors.cpp:69
string
Definition: cgiapp.hpp:437
bool has_errors(void) const
Check if there are errors in the error messages.
Definition: errors.cpp:104
static std::string message_type_str(message_type mt)
Convert an error type to a string.
Definition: errors.cpp:43
bool has_fatal_errors(void) const
Check if there are fatal errors in the error messages.
Definition: errors.cpp:108
bool has_messages_of_type(error_message::message_type type) const
Definition: errors.cpp:90
parser_exception(const error_messages &msgs)
Create a new object using the given list of error messages.
Definition: errors.cpp:138
virtual ~parser_exception()
Destroy the object.
Definition: errors.hpp:239
XML library namespace.
Definition: attributes.hpp:57
Definition: type.c:8
warnings_as_errors_type
A type for different approaches to process warnings.
Definition: errors.hpp:248
Treat warnings as errors.
Definition: errors.hpp:249
bool has_warnings(void) const
Check if there are warnings in the error messages.
Definition: errors.cpp:100
std::string buffer
Definition: errors.hpp:243
The xml::error_messages class is used to store all the error message which are collected while parsin...
Definition: errors.hpp:129
const error_messages_type & get_messages(void) const
Get the error messages.
Definition: errors.cpp:80
message_type get_message_type(void) const
Get the error message type.
Definition: errors.cpp:60
const error_messages & get_messages(void) const
Get error messages.
Definition: errors.cpp:147
message_type message_type_
Definition: errors.hpp:115
std::list< error_message > error_messages_type
A type to store multiple messages.
Definition: errors.hpp:133
error_messages messages_
Definition: errors.hpp:242
std::string get_message(void) const
Get the error message.
Definition: errors.cpp:64
message_type
A type for different type of errors.
Definition: errors.hpp:55
Modified on Wed Sep 28 19:27:08 2016 by modify_doxy.py rev. 506947