blob: 9a816a2cb113a653fea4f6af588fc092c57ecc8c (
plain)
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
|
#ifndef LIBCXML_CXML_H
#define LIBCXML_CXML_H
#include <string>
#include <list>
#include <stdint.h>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <glibmm.h>
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, " ");
return boost::lexical_cast<T> (s);
}
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, " ");
return boost::optional<T> (boost::lexical_cast<T> (t));
}
/** 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, " ");
return boost::lexical_cast<T> (s);
}
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, " ");
return boost::optional<T> (boost::lexical_cast<T> (t));
}
/** @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<Glib::ustring> _taken;
};
typedef boost::shared_ptr<cxml::Node> NodePtr;
class Document : public Node
{
public:
Document (std::string root_name);
void read_file (boost::filesystem::path);
void read_stream (std::istream &);
virtual ~Document ();
private:
void take_root_node ();
xmlpp::DomParser* _parser;
std::string _root_name;
};
}
#endif
|