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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
Copyright (C) 2012-2014 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef LIBCXML_CXML_H
#define LIBCXML_CXML_H
#include <string>
#include <sstream>
#include <list>
#include <map>
#include <stdint.h>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <boost/enable_shared_from_this.hpp>
/* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
#ifdef check
#undef check
#endif
namespace xmlpp {
class Node;
class Document;
}
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;
};
class Node;
typedef boost::shared_ptr<cxml::Node> NodePtr;
typedef std::list<NodePtr> NodeList;
typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
typedef std::list<std::pair<std::string, std::string> > KeyValueList;
class Node : public boost::enable_shared_from_this<Node>
{
public:
Node ();
Node (xmlpp::Node 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;
}
/* 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;
}
/* Access to child nodes */
boost::shared_ptr<Node> child (std::string) const;
boost::shared_ptr<Node> optional_child (std::string) const;
NodeList children () const;
NodeList children (std::string) const;
/** Add a child node with a given name */
NodePtr add_child (std::string name, std::string namespace_prefix = "")
{
NodePtr n (new cxml::Node ());
n->set_namespace_prefix (namespace_prefix);
n->set_name (name);
_children.push_back (n);
return n;
}
void set_namespace_declaration (std::string uri, std::string prefix = "");
KeyValueList namespace_declarations () const {
return _namespace_declarations;
}
void set_content (std::string c) {
_content = c;
}
/** @return The content of this node */
std::string content () const;
void set_attribute (std::string name, std::string value);
void set_namespace_prefix (std::string p) {
_namespace_prefix = p;
}
/** @return namespace prefix of this node */
std::string namespace_prefix () const;
/** 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;
/** Set the name of the node.
* @param n New name.
*/
void set_name (std::string n) {
_name = n;
}
/** @return Node name */
std::string name () const {
return _name;
}
/* We use xmlpp for parsing and writing XML out; these
methods help with that.
*/
void read (xmlpp::Node const *);
void write (xmlpp::Node *, std::map<cxml::ConstNodePtr, xmlpp::Node*>* mapping = 0) const;
protected:
NodeList _children;
private:
std::string _name;
std::string _content;
std::string _namespace_prefix;
KeyValueList _attributes;
KeyValueList _namespace_declarations;
mutable std::list<std::string> _taken;
};
cxml::NodePtr read_file (boost::filesystem::path);
cxml::NodePtr read_stream (std::istream &);
cxml::NodePtr read_string (std::string);
void write_to_xmlpp_document (cxml::ConstNodePtr, xmlpp::Document &, std::map<cxml::ConstNodePtr, xmlpp::Node*>* mapping = 0);
void write_to_file (cxml::ConstNodePtr, boost::filesystem::path);
void write_to_file_formatted (cxml::ConstNodePtr, boost::filesystem::path);
void write_to_stream_formatted (cxml::ConstNodePtr, std::ostream& stream);
std::string write_to_string (cxml::ConstNodePtr);
std::string write_to_string_formatted (cxml::ConstNodePtr);
}
#endif
|