xmlwrapp Lightweight C++ XML parsing library

errors.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2010-2013 Vaclav Slavik <vslavik@gmail.com>
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /**
34 @file
35
36 This file contains errors-handling classes: xml::exception and
37 xml::error_handler and derived classes.
38 */
39
40 #ifndef _xmlwrapp_errors_h_
41 #define _xmlwrapp_errors_h_
42
43 // xmlwrapp includes
44 #include "xmlwrapp/export.h"
45
46 #include <stdexcept>
47 #include <string>
48 #include <list>
49
50 /// XML library namespace
51 namespace xml
52 {
53
54 class error_messages;
55
56 /**
57 This exception class is thrown by xmlwrapp for all runtime XML-related
58 errors.
59
60 @note C++ runtime may still thrown other errors when used from xmlwrapp.
61 Also, std::bad_alloc() is thrown in out-of-memory situations.
62
63 @since 0.7.0
64 */
65 class XMLWRAPP_API exception : public std::runtime_error
66 {
67 public:
68 explicit exception(const std::string& what);
69 explicit exception(const error_messages& what);
70 };
71
72
73 /**
74 The xml::error_handler class is used to handle libxml2 errors and warnings
75 emitted during parsing, validation etc.
76
77 Although you can derive a custom handler from it, the specializations
78 included in xmlwrapp should suffice for most uses: throw_on_error and
79 throw_on_error_or_warning throw on issues, error_messages collects errors
80 and warnings without throwing.
81
82 @since 0.7.0
83 */
84 class XMLWRAPP_API error_handler
85 {
86 public:
87 virtual ~error_handler() {}
88
89 /// Called by xmlwrapp to report an error.
90 virtual void on_error(const std::string& msg) = 0;
91
92 /// Called by xmlwrapp to report a warning.
93 virtual void on_warning(const std::string& msg) = 0;
94 };
95
96
97 /**
98 An error handler that ignores both errors and warnings.
99
100 @see ignore_errors
101 */
102 class error_handler_ignore_errors : public error_handler
103 {
104 public:
105 void on_error(const std::string&) {}
106 void on_warning(const std::string&) {}
107 };
108
109 /**
110 Specialization of error_handler that throws on any error.
111
112 @see throw_on_error
113 */
114 class error_handler_throw_on_error : public error_handler
115 {
116 public:
117 void on_error(const std::string& msg) { throw exception(msg); }
118 void on_warning(const std::string&) {}
119 };
120
121 /**
122 Specialization of error_handler that throws on any error or warning.
123
124 @see throw_on_error_or_warning
125 */
126 class error_handler_throw_on_error_or_warning : public error_handler_throw_on_error
127 {
128 public:
129 void on_warning(const std::string& msg) { on_error(msg); }
130 };
131
132 /// Error handler ignoring all errors, its use is strongly discouraged.
133 extern XMLWRAPP_API error_handler_ignore_errors ignore_errors;
134
135 /// Error handler object that throws on any error.
136 extern XMLWRAPP_API error_handler_throw_on_error throw_on_error;
137
138 /// Error handler object that throws on any error or warning.
139 extern XMLWRAPP_API error_handler_throw_on_error_or_warning throw_on_error_or_warning;
140
141
142 /**
143 Single message in error_messages.
144
145 @since 0.7.0
146 */
147 class XMLWRAPP_API error_message
148 {
149 public:
150 /// A type for different type of errors
151 enum message_type
152 {
153 type_error, ///< error
154 type_warning ///< warning
155 };
156
157 /**
158 Create a new xml::error_message object.
159
160 @param err_msg The error message.
161 @param msg_type The error type.
162 */
163 error_message(const std::string& err_msg, message_type msg_type)
164 : message_(err_msg), type_(msg_type)
165 {}
166
167 /// Get the error message type.
168 message_type type() const { return type_; }
169
170 /// Get the error message.
171 const std::string& message() const { return message_; }
172
173 private:
174 std::string message_;
175 message_type type_;
176 };
177
178
179 /**
180 The xml::error_messages class is used to store all the error messages
181 which are collected while parsing or validating an XML document.
182
183 @since 0.7.0
184 */
185 class XMLWRAPP_API error_messages : public error_handler
186 {
187 public:
188 /// A type to store multiple messages
189 typedef std::list<error_message> messages_type;
190
191 error_messages() : has_errors_(false), has_warnings_(false) {}
192
193 /// Get the error messages.
194 const messages_type& messages() const { return messages_; }
195
196 /**
197 Convenience function to find if there are any messages at all.
198 */
199 bool empty() const
200 {
201 return messages_.empty();
202 }
203
204 /**
205 Check if there are warnings in the error messages.
206
207 @return true if there is at least one warning in the error messages.
208 It does not consider errors.
209 */
210 bool has_warnings() const { return has_warnings_; }
211
212 /**
213 Check if there are any errors.
214 */
215 bool has_errors() const { return has_errors_; }
216
217
218 /**
219 Convert error messages into a single printable string.
220
221 The returned string is typically multiline, with the messages
222 separated with newlines ('\n').
223 */
224 std::string print() const;
225
226
227
228 // Implementation of error_handler methods:
229 void on_error(const std::string& msg);
230 void on_warning(const std::string& msg);
231
232 protected:
233 /// Called by print() to format a single message.
234 virtual std::string format_for_print(const error_message& msg) const;
235
236 private:
237 messages_type messages_;
238 bool has_errors_;
239 bool has_warnings_;
240 };
241
242
243 } // namespace xml
244
245 #endif // _xmlwrapp_errors_h_
xml::error_message::type
message_type type() const
Get the error message type.
Definition: errors.h:168
xml::error_messages::on_error
void on_error(const std::string &msg)
Called by xmlwrapp to report an error.
xml::error_handler_throw_on_error_or_warning
Specialization of error_handler that throws on any error or warning.
Definition: errors.h:127
xml::error_messages::messages_type
std::list< error_message > messages_type
A type to store multiple messages.
Definition: errors.h:189
xml::error_message::message
const std::string & message() const
Get the error message.
Definition: errors.h:171
xml::error_messages::has_errors
bool has_errors() const
Check if there are any errors.
Definition: errors.h:215
xml::error_message
Single message in error_messages.
Definition: errors.h:148
xml::error_messages::print
std::string print() const
Convert error messages into a single printable string.
xml::error_handler_ignore_errors::on_error
void on_error(const std::string &)
Called by xmlwrapp to report an error.
Definition: errors.h:105
xml::error_handler
The xml::error_handler class is used to handle libxml2 errors and warnings emitted during parsing,...
Definition: errors.h:85
xml::error_messages::has_warnings
bool has_warnings() const
Check if there are warnings in the error messages.
Definition: errors.h:210
xml::error_handler::on_error
virtual void on_error(const std::string &msg)=0
Called by xmlwrapp to report an error.
xml::error_messages::on_warning
void on_warning(const std::string &msg)
Called by xmlwrapp to report a warning.
xml::error_handler_ignore_errors::on_warning
void on_warning(const std::string &)
Called by xmlwrapp to report a warning.
Definition: errors.h:106
xml::throw_on_error
error_handler_throw_on_error throw_on_error
Error handler object that throws on any error.
xml::error_handler_ignore_errors
An error handler that ignores both errors and warnings.
Definition: errors.h:103
xml::error_handler::on_warning
virtual void on_warning(const std::string &msg)=0
Called by xmlwrapp to report a warning.
xml::error_messages::messages
const messages_type & messages() const
Get the error messages.
Definition: errors.h:194
xml::ignore_errors
error_handler_ignore_errors ignore_errors
Error handler ignoring all errors, its use is strongly discouraged.
xml::error_message::error_message
error_message(const std::string &err_msg, message_type msg_type)
Create a new xml::error_message object.
Definition: errors.h:163
xml::error_messages
The xml::error_messages class is used to store all the error messages which are collected while parsi...
Definition: errors.h:186
xml::error_message::type_error
@ type_error
error
Definition: errors.h:153
xml
XML library namespace.
Definition: attributes.h:52
xml::error_handler_throw_on_error::on_error
void on_error(const std::string &msg)
Called by xmlwrapp to report an error.
Definition: errors.h:117
xml::error_messages::empty
bool empty() const
Convenience function to find if there are any messages at all.
Definition: errors.h:199
xml::exception
This exception class is thrown by xmlwrapp for all runtime XML-related errors.
Definition: errors.h:66
xml::error_handler_throw_on_error::on_warning
void on_warning(const std::string &)
Called by xmlwrapp to report a warning.
Definition: errors.h:118
xml::throw_on_error_or_warning
error_handler_throw_on_error_or_warning throw_on_error_or_warning
Error handler object that throws on any error or warning.
xml::error_message::message_type
message_type
A type for different type of errors.
Definition: errors.h:152
xml::error_messages::format_for_print
virtual std::string format_for_print(const error_message &msg) const
Called by print() to format a single message.
xml::error_handler_throw_on_error
Specialization of error_handler that throws on any error.
Definition: errors.h:115
xml::error_handler_throw_on_error_or_warning::on_warning
void on_warning(const std::string &msg)
Called by xmlwrapp to report a warning.
Definition: errors.h:129