xmlwrapp
Lightweight C++ XML parsing library
Loading...
Searching...
No Matches
event_parser.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
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 the definition of the xml::event_parser class.
37 */
38
39#ifndef _xmlwrapp_event_parser_h_
40#define _xmlwrapp_event_parser_h_
41
42// xmlwrapp includes
43#include "xmlwrapp/init.h"
44#include "xmlwrapp/export.h"
45
46// standard includes
47#include <cstddef>
48#include <string>
49#include <iosfwd>
50#include <map>
51#include <memory>
52
53XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
54
55namespace xml
56{
57
58namespace impl
59{
60struct epimpl; // forward declaration of private implementation
61}
62
63/**
64 The xml::event_parser is used to parse an XML document by calling member
65 functions when certain things in the XML document are parsed. In order to
66 use this class you derive a sub-class from it and override the protected
67 virtual functions.
68 */
69class XMLWRAPP_API event_parser
70{
71public:
72 /// a type for holding XML node attributes
73 using attrs_type = std::map<std::string, std::string>;
74 /// size type
75 using size_type = std::size_t;
76
77 /// Default constructor.
79
80 virtual ~event_parser();
81
82 /**
83 Call this member function to parse the given file.
84
85 @param filename The name of the file to parse.
86 @return True if the file was successfully parsed; false otherwise.
87 */
88 bool parse_file(const char *filename);
89
90 /**
91 Parse what ever data that can be read from the given stream.
92
93 @param stream The stream to read data from.
94 @return True if the stream was successfully parsed; false otherwise.
95 */
96 bool parse_stream(std::istream& stream);
97
98 /**
99 Call this function to parse a chunk of xml data. When you are done
100 feeding the parser chucks of data you need to call the parse_finish
101 member function.
102
103 @param chunk The xml data chuck to parse.
104 @param length The size of the given data chunk
105 @return True if the chunk was parsed successfully; false otherwise.
106 */
107 bool parse_chunk(const char *chunk, size_type length);
108
109 /**
110 Finish parsing chunked data. You only need to call this member
111 function is you were parsing chunked xml data via the parse_chunk
112 member function.
113
114 @return True if all parsing was successful; false otherwise.
115 */
117
118 /**
119 If there was an error parsing the XML data, (indicated by one of the
120 parsing functions returning false), you can call this function to get
121 a message describing the error.
122
123 @return A description of the XML parsing error.
124 */
125 const std::string& get_error_message() const;
126
127protected:
128 /**
129 Override this member function to receive the start_element message.
130 This member function is called when the parser encounters an xml
131 element.
132
133 @param name The name of the element
134 @param attrs The element's attributes
135 @return You should return true to continue parsing; false to stop.
136 */
137 virtual bool start_element(const std::string& name, const attrs_type& attrs) = 0;
138
139 /**
140 Override this member function to receive the end_element message.
141 This member function is called when the parser encounters the closing
142 of an element.
143
144 @param name The name of the element that was closed.
145 @return You should return true to continue parsing; false to stop.
146 */
147 virtual bool end_element(const std::string& name) = 0;
148
149 /**
150 Override this member function to receive the text message. This
151 member function is called when the parser encounters text nodes.
152
153 @param contents The contents of the text node.
154 @return You should return true to continue parsing; false to stop.
155 */
156 virtual bool text(const std::string& contents) = 0;
157
158 /**
159 Override this member function to receive the cdata message. This
160 member function is called when the parser encounters a <![CDATA[]]>
161 section in the XML data.
162
163 The default implementation just calls the text() member function to
164 handle the text inside the CDATA section.
165
166 @param contents The contents of the CDATA section.
167 @return You should return true to continue parsing.
168 @return Return false if you want to stop.
169 */
170 virtual bool cdata(const std::string& contents);
171
172 /**
173 Override this member function to receive the processing_instruction
174 message. This member function will be called when the XML parser
175 encounters a processing instruction <?target data?>.
176
177 The default implementation will ignore processing instructions and
178 return true.
179
180 @param target The target of the processing instruction
181 @param data The data of the processing instruction.
182 @return You should return true to continue parsing.
183 @return Return false if you want to stop.
184 */
185 virtual bool processing_instruction(const std::string& target, const std::string& data);
186
187 /**
188 Override this member function to receive the comment message. This
189 member function will be called when the XML parser encounters a
190 comment <!-- contents -->.
191
192 The default implementation will ignore XML comments and return true.
193
194 @param contents The contents of the XML comment.
195 @return You should return true to continue parsing.
196 @return Return false if you want to stop.
197 */
198 virtual bool comment(const std::string& contents);
199
200 /**
201 Override this member function to receive parser warnings. The
202 default behaviour is to ignore warnings.
203
204 @param message The warning message from the compiler.
205 @return You should return true to continue parsing.
206 @return Return false if you want to stop.
207 */
208 virtual bool warning(const std::string& message);
209
210 /**
211 Set the error message that will be returned from the
212 get_error_message() member function. If one of your callback
213 functions returns false and does not first call this member
214 function, "Unknown Error" will be returned from get_error_message().
215
216 @param message The message to return from get_error_message().
217 */
218 void set_error_message(const char *message);
219
220private:
221 friend struct impl::epimpl;
222 std::unique_ptr<impl::epimpl> pimpl_;
223
224 // Don't allow anyone to copy construct an event_parser or to call the
225 // assignment operator. It does not make sense to copy a parser if it is
226 // half way done parsing. Plus, it would be a pain!
227 event_parser(const event_parser&) = delete;
228 event_parser& operator=(const event_parser&) = delete;
229};
230
231} // namespace xml
232
233XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
234
235#endif // _xmlwrapp_event_parser_h_
The xml::event_parser is used to parse an XML document by calling member functions when certain thing...
Definition event_parser.h:70
void set_error_message(const char *message)
Set the error message that will be returned from the get_error_message() member function.
std::size_t size_type
size type
Definition event_parser.h:75
event_parser()
Default constructor.
bool parse_chunk(const char *chunk, size_type length)
Call this function to parse a chunk of xml data.
virtual bool cdata(const std::string &contents)
Override this member function to receive the cdata message.
virtual bool processing_instruction(const std::string &target, const std::string &data)
Override this member function to receive the processing_instruction message.
bool parse_stream(std::istream &stream)
Parse what ever data that can be read from the given stream.
virtual bool text(const std::string &contents)=0
Override this member function to receive the text message.
bool parse_finish()
Finish parsing chunked data.
std::map< std::string, std::string > attrs_type
a type for holding XML node attributes
Definition event_parser.h:73
virtual bool comment(const std::string &contents)
Override this member function to receive the comment message.
virtual bool end_element(const std::string &name)=0
Override this member function to receive the end_element message.
virtual bool start_element(const std::string &name, const attrs_type &attrs)=0
Override this member function to receive the start_element message.
bool parse_file(const char *filename)
Call this member function to parse the given file.
virtual bool warning(const std::string &message)
Override this member function to receive parser warnings.
const std::string & get_error_message() const
If there was an error parsing the XML data, (indicated by one of the parsing functions returning fals...
XML library namespace.
Definition attributes.h:55
This file contains the definition of the xml::init class.