xmlwrapp
Lightweight C++ XML parsing library
Loading...
Searching...
No Matches
tree_parser.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2001-2003 Peter J Jones (pjones@pmade.org)
3 * Copyright (C) 2013 Vaclav Slavik <vslavik@gmail.com>
4 * All Rights Reserved
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of the Author nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
24 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/**
35 @file
36
37 This file contains the definition of the xml::tree_parser class.
38 */
39
40#ifndef _xmlwrapp_tree_parser_h_
41#define _xmlwrapp_tree_parser_h_
42
43// xmlwrapp includes
44#include "xmlwrapp/init.h"
45#include "xmlwrapp/export.h"
46#include "xmlwrapp/errors.h"
47
48// standard includes
49#include <cstddef>
50#include <memory>
51#include <string>
52
53XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
54
55namespace xml
56{
57
58// forward declarations
59class document;
60
61namespace impl
62{
63struct tree_impl;
64}
65
66/**
67 The xml::tree_parser class is used to parse an XML document and generate
68 a tree like structure of xml::node objects.
69
70 After constructing a tree_parser, with either a file to parse or some in
71 memory data to parse, you can walk the tree using the xml::node interface.
72
73 @note You probably don't need to use this class directly anymore and
74 can just use the corresponding xml::document constructors.
75 */
76class XMLWRAPP_API tree_parser
77{
78public:
79 using size_type = std::size_t;
80 /**
81 xml::tree_parser class constructor. Given the name of a file, this
82 constructor will parse that file.
83
84 @param filename The name of the file to parse.
85 @param on_error Handler called to process errors and warnings.
86
87 @since 0.7.0
88 */
89 tree_parser(const char *filename, error_handler& on_error = throw_on_error);
90
91 /**
92 xml::tree_parser class constructor. Given some data and the size of
93 that data, parse that data as XML.
94
95 @param data The XML data to parse.
96 @param size The size of the XML data to parse.
97 @param on_error Handler called to process errors and warnings.
98
99 @since 0.7.0
100 */
101 tree_parser(const char *data, size_type size, error_handler& on_error = throw_on_error);
102
103 /**
104 xml::tree_parser class constructor. Given the name of a file, this
105 constructor will parse that file.
106
107 There are two options for dealing with XML parsing errors. The
108 default it to throw an exception (xml::exception). The other
109 option is to pass false for the allow_exceptions flag. This will
110 prevent an exception from being thrown, instead, a flag will be set
111 that you can test with the operator! member function.
112
113 No matter what option you choose, this constructor may still throw
114 exceptions for memory failure or other non-parsing related failures.
115
116 @param filename The name of the file to parse.
117 @param allow_exceptions Whether or not you want an exception for parsing errors.
118 */
119 tree_parser(const char *filename, bool allow_exceptions);
120
121 /**
122 xml::tree_parser class constructor. Given some data and the size of
123 that data, parse that data as XML. To see if the data was parsed
124 successfully use operator!.
125
126 @param data The XML data to parse.
127 @param size The size of the XML data to parse.
128 @param allow_exceptions Whether or not you want an exception
129 for parsing errors.
130 */
131 tree_parser(const char *data, size_type size, bool allow_exceptions);
132
133 ~tree_parser();
134
135 /**
136 Return error_messages object with errors and warnings collected
137 during parsing.
138
139 @since 0.7.0
140 */
141 const error_messages& messages() const;
142
143 /**
144 Check to see if a xml::tree_parser class is valid. That is, check to
145 see if parsing XML data was successful and the tree_parser holds a
146 good XML node tree.
147
148 @return True if the tree_parser is NOT VALID; false if it is valid.
149 */
150 bool operator!() const;
151
152 /**
153 If operator! indicates that there was an error parsing your XML data,
154 you can use this member function to get the error message that was
155 generated during parsing.
156
157 @return The error message generated during XML parsing.
158
159 @deprecated Use messages() instead.
160 */
161 XMLWRAPP_DEPRECATED("use messages() instead")
162 const std::string& get_error_message() const;
163
164 /**
165 Check to see if there were any warnings from the parser while
166 processing the given XML data. If there were, you may want to send
167 the same document through xmllint or the event_parser to catch and
168 review the warning messages.
169
170 @return True if there were any warnings.
171 @return False if there were no warnings.
172 */
173 bool had_warnings() const;
174
175 /**
176 Get a reference to the xml::document that was generated during the
177 XML parsing. You should make sure to only use a reference to the
178 document to avoid a deep copy.
179
180 @return A reference to the xml::document.
181 */
182 xml::document& get_document();
183
184 /**
185 Get a const reference to the xml::document that was generate during
186 the XML parsing. You should make sure to only use a reference to the
187 document to avoid a deep copy.
188
189 @return A const reference to the xml::document.
190 */
191 const xml::document& get_document() const;
192
193private:
194 void init(const char *filename, error_handler *on_error);
195 void init(const char *data, size_type size, error_handler *on_error);
196
197 std::unique_ptr<impl::tree_impl> pimpl_;
198
199 // Don't allow anyone to copy construct a xml::tree_parser or allow the
200 // assignment operator to be called. It is not very useful to copy a
201 // parser that has already parsed half a document.
202 tree_parser(const tree_parser&) = delete;
203 tree_parser& operator=(const tree_parser&) = delete;
204};
205
206} // end xml namespace
207
208XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
209
210#endif // _xmlwrapp_tree_parser_h_
The xml::document class is used to hold the XML tree and various bits of information about it.
Definition document.h:88
The xml::error_handler class is used to handle libxml2 errors and warnings emitted during parsing,...
Definition errors.h:89
The xml::error_messages class is used to store all the error messages which are collected while parsi...
Definition errors.h:190
The xml::init class is used to configure the XML parser.
Definition init.h:64
The xml::tree_parser class is used to parse an XML document and generate a tree like structure of xml...
Definition tree_parser.h:77
tree_parser(const char *data, size_type size, bool allow_exceptions)
xml::tree_parser class constructor.
tree_parser(const char *filename, error_handler &on_error=throw_on_error)
xml::tree_parser class constructor.
tree_parser(const char *filename, bool allow_exceptions)
xml::tree_parser class constructor.
bool operator!() const
Check to see if a xml::tree_parser class is valid.
const error_messages & messages() const
Return error_messages object with errors and warnings collected during parsing.
tree_parser(const char *data, size_type size, error_handler &on_error=throw_on_error)
xml::tree_parser class constructor.
This file contains errors-handling classes: xml::exception and xml::error_handler and derived classes...
XML library namespace.
Definition attributes.h:55
This file contains the definition of the xml::init class.