1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef LIBCXML_CXML_H
#define LIBCXML_CXML_H
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <stdint.h>
#include <string>
#include <sstream>
#include <list>
/* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
#ifdef check
#undef check
#endif
namespace xmlpp {
class Node;
class DomParser;
}
namespace cxml {
/** @brief An error */
class Error : public std::exception
{
public:
/** Construct an Error exception.
* @param message Error message.
*/
Error (std::string const & message) : _message (message) {}
/** Error destructor */
~Error () throw () {}
/** @return error message. Caller must not free the returned
* value.
*/
char const * what () const throw () {
return _message.c_str ();
}
private:
/** error message */
std::string _message;
};
/** @brief A wrapper for a xmlpp::Node which simplifies parsing */
class Node
{
public:
Node ();
/** Construct a Node from an xmlpp::Node. This class will
* not destroy the xmlpp::Node.
* @param node xmlpp::Node.
*/
Node (xmlpp::Node* node);
std::string name () const;
/* A set of methods which look up a child of this node by
* its name, and return its contents as some type or other.
*
* If, for example, this object has been created with
* a node named "Fred", we might have the following XML:
*
* <Fred>
* <Jim>42</Jim>
* </Fred>
*
* string_child ("Jim") would return "42"
* number_child<int64_t> ("Jim") would return 42.
* ...and so on.
*
* The methods not marked "optional" will throw an exception
* if the child node is not present. The "optional" methods
* will return an empty boost::optional<> in that case.
*
* All methods will also throw an exception if there is more
* than one of the specified child node.
*/
std::string string_child (std::string c) const;
boost::optional<std::string> optional_string_child (std::string) const;
bool bool_child (std::string) const;
boost::optional<bool> optional_bool_child (std::string) const;
template <class T>
T number_child (std::string c) const
{
std::string s = string_child (c);
boost::erase_all (s, " ");
std::stringstream t;
t.imbue (std::locale::classic ());
t << s;
T n;
t >> n;
return n;
}
template <class T>
boost::optional<T> optional_number_child (std::string c) const
{
boost::optional<std::string> s = optional_string_child (c);
if (!s) {
return boost::optional<T> ();
}
std::string t = s.get ();
boost::erase_all (t, " ");
std::stringstream u;
u.imbue (std::locale::classic ());
u << t;
T n;
u >> n;
return n;
}
/** This will mark a child as to be ignored when calling done() */
void ignore_child (std::string) const;
/** Check whether all children of this Node have been looked up
* or passed to ignore_child(). If not, an exception is thrown.
*/
void done () const;
/* These methods look for an attribute of this node, in the
* same way as the child methods do.
*/
std::string string_attribute (std::string) const;
boost::optional<std::string> optional_string_attribute (std::string) const;
bool bool_attribute (std::string) const;
boost::optional<bool> optional_bool_attribute (std::string) const;
template <class T>
T number_attribute (std::string c) const
{
std::string s = string_attribute (c);
boost::erase_all (s, " ");
std::stringstream t;
t.imbue (std::locale::classic ());
t << s;
T n;
t >> n;
return n;
}
template <class T>
boost::optional<T> optional_number_attribute (std::string c) const
{
boost::optional<std::string> s = optional_string_attribute (c);
if (!s) {
return boost::optional<T> ();
}
std::string t = s.get ();
boost::erase_all (t, " ");
std::stringstream u;
u.imbue (std::locale::classic ());
u << t;
T n;
u >> n;
return n;
}
/** @return The content of this node */
std::string content () const;
/** @return namespace URI of this node */
std::string namespace_uri () const;
/** @return namespace prefix of this node */
std::string namespace_prefix () const;
boost::shared_ptr<Node> node_child (std::string) const;
boost::shared_ptr<Node> optional_node_child (std::string) const;
std::list<boost::shared_ptr<Node> > node_children (std::string) const;
xmlpp::Node* node () const {
return _node;
}
protected:
xmlpp::Node* _node;
private:
mutable std::list<std::string> _taken;
};
typedef boost::shared_ptr<cxml::Node> NodePtr;
typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
class Document : public Node
{
public:
Document ();
Document (std::string root_name);
Document (std::string root_name, boost::filesystem::path);
virtual ~Document ();
void read_file (boost::filesystem::path);
void read_stream (std::istream &);
void read_string (std::string);
std::string root_name () const {
return _root_name;
}
private:
void take_root_node ();
xmlpp::DomParser* _parser;
std::string _root_name;
};
}
#endif
|