summaryrefslogtreecommitdiff
path: root/src/cxml.h
blob: 3bb28caf1c19f85de2820f1c0cd9013a4f6aeaec (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
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
272
273
274
275
276
277
278
279
280
281
/*
    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>

    This file is part of libcxml.

    libcxml 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.

    libcxml 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 libcxml.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef LIBCXML_CXML_H
#define LIBCXML_CXML_H

#include <glibmm/ustring.h>
#include <boost/optional.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <exception>
#include <memory>
#include <string>
#include <vector>

/* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
#ifdef check
#undef check
#endif

namespace xmlpp {
	class DomParser;
	class Element;
	class Node;
}

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 noexcept override {
		return _message.c_str ();
	}

private:
	/** error message */
	std::string _message;
};

/** A sort-of version of boost::lexical_cast that does uses the "C"
 *  locale (i.e. no thousands separators and a . for the decimal separator).
 */
template <typename P, typename Q>
P
raw_convert (Q)
{
	/* We can't write a generic version of raw_convert; all required
	   versions must be specialised.
	*/
	BOOST_STATIC_ASSERT (sizeof(Q) == 0);
}

template <>
int
raw_convert (std::string v);

template <>
unsigned int
raw_convert (std::string v);

template <>
long int
raw_convert (std::string v);

template <>
long unsigned int
raw_convert (std::string v);

template <>
long long
raw_convert (std::string v);

template <>
long long unsigned
raw_convert (std::string v);

template <>
float
raw_convert (std::string v);

template <>
double
raw_convert (std::string v);

/** @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 const* 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
	{
		auto s = string_child (c);
		boost::erase_all (s, " ");
		return raw_convert<T> (s);
	}

	template <class T>
	boost::optional<T> optional_number_child (std::string c) const
	{
		auto s = optional_string_child (c);
		if (!s) {
			return {};
		}

		auto t = s.get ();
		boost::erase_all (t, " ");
		return raw_convert<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 raw_convert<T> (s);
	}

	template <class T>
	boost::optional<T> optional_number_attribute (std::string c) const
	{
		auto s = optional_string_attribute (c);
		if (!s) {
			return boost::optional<T> ();
		}

		auto t = s.get ();
		boost::erase_all (t, " ");
		return raw_convert<T> (t);
	}

	/** @return The text content of this node (excluding comments or CDATA) */
	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;

	std::shared_ptr<Node> node_child (std::string) const;
	std::shared_ptr<Node> optional_node_child (std::string) const;

	std::vector<std::shared_ptr<Node>> node_children () const;
	std::vector<std::shared_ptr<Node>> node_children (std::string) const;

	xmlpp::Node const* node() const {
		return _node;
	}

	bool is_text() const;

protected:
	xmlpp::Node const* _node;

private:
	mutable std::vector<Glib::ustring> _taken;
};

typedef std::shared_ptr<cxml::Node> NodePtr;
typedef std::shared_ptr<const cxml::Node> ConstNodePtr;

class Document : public Node
{
public:
	Document ();
	explicit Document(std::string root_name);
	Document (std::string root_name, boost::filesystem::path);

	Document (Document const&) = delete;
	Document& operator= (Document const&) = delete;

	virtual ~Document ();

	void read_file (boost::filesystem::path);
	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;
};


xmlpp::Element* add_child(xmlpp::Element* parent, std::string const& name, std::string const& ns_prefix = {});
void add_text_child(xmlpp::Element* parent, std::string const& name, std::string const& text);


}

#endif