Add cxml::add_child() and cxml::add_text_child().
[libcxml.git] / src / cxml.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libcxml.
5
6     libcxml is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libcxml is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libcxml.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "cxml.h"
22 #include <libxml++/libxml++.h>
23 #include <boost/filesystem.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <cstdio>
26
27
28 using std::make_shared;
29 using std::shared_ptr;
30 using std::string;
31 using std::vector;
32 using boost::optional;
33
34
35 cxml::Node::Node ()
36         : _node (nullptr)
37 {
38
39 }
40
41 cxml::Node::Node (xmlpp::Node* node)
42         : _node (node)
43 {
44
45 }
46
47 string
48 cxml::Node::name () const
49 {
50         if (!_node) {
51                 throw Error ("No node to read name from");
52         }
53         return _node->get_name ();
54 }
55
56 shared_ptr<cxml::Node>
57 cxml::Node::node_child (string name) const
58 {
59         auto const n = node_children (name);
60         if (n.size() > 1) {
61                 throw cxml::Error ("duplicate XML tag " + name);
62         } else if (n.empty ()) {
63                 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
64         }
65
66         return n.front ();
67 }
68
69 shared_ptr<cxml::Node>
70 cxml::Node::optional_node_child (string name) const
71 {
72         auto const n = node_children (name);
73         if (n.size() > 1) {
74                 throw cxml::Error ("duplicate XML tag " + name);
75         } else if (n.empty ()) {
76                 return {};
77         }
78
79         return n.front ();
80 }
81
82 vector<shared_ptr<cxml::Node>>
83 cxml::Node::node_children () const
84 {
85         if (!_node) {
86                 throw Error ("No node to read children from");
87         }
88
89         vector<shared_ptr<cxml::Node>> n;
90         for (auto i: _node->get_children()) {
91                 n.push_back(make_shared<Node>(i));
92         }
93
94         return n;
95 }
96
97 vector<shared_ptr<cxml::Node>>
98 cxml::Node::node_children (string name) const
99 {
100         /* XXX: using find / get_path should work here, but I can't follow
101            how get_path works.
102         */
103
104         if (!_node) {
105                 throw cxml::Error("Node has no internal xmlpp node; did you forget to call a read method on cxml::Document?");
106         }
107
108         vector<shared_ptr<cxml::Node>> n;
109         for (auto i: _node->get_children()) {
110                 if (i->get_name() == name) {
111                         n.push_back(make_shared<Node>(i));
112                 }
113         }
114
115         _taken.push_back (name);
116         return n;
117 }
118
119 string
120 cxml::Node::string_child (string c) const
121 {
122         return node_child(c)->content ();
123 }
124
125 optional<string>
126 cxml::Node::optional_string_child (string c) const
127 {
128         auto const nodes = node_children (c);
129         if (nodes.size() > 1) {
130                 throw cxml::Error ("duplicate XML tag " + c);
131         }
132
133         if (nodes.empty ()) {
134                 return {};
135         }
136
137         return nodes.front()->content();
138 }
139
140 bool
141 cxml::Node::bool_child (string c) const
142 {
143         auto const s = string_child (c);
144         return (s == "1" || s == "yes" || s == "True");
145 }
146
147 optional<bool>
148 cxml::Node::optional_bool_child (string c) const
149 {
150         auto const s = optional_string_child (c);
151         if (!s) {
152                 return {};
153         }
154
155         return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
156 }
157
158 void
159 cxml::Node::ignore_child (string name) const
160 {
161         _taken.push_back (name);
162 }
163
164 string
165 cxml::Node::string_attribute (string name) const
166 {
167         auto e = dynamic_cast<const xmlpp::Element *> (_node);
168         if (!e) {
169                 throw cxml::Error ("missing attribute " + name);
170         }
171
172         auto a = e->get_attribute (name);
173         if (!a) {
174                 throw cxml::Error ("missing attribute " + name);
175         }
176
177         return a->get_value ();
178 }
179
180 optional<string>
181 cxml::Node::optional_string_attribute (string name) const
182 {
183         auto e = dynamic_cast<const xmlpp::Element *> (_node);
184         if (!e) {
185                 return {};
186         }
187
188         auto a = e->get_attribute (name);
189         if (!a) {
190                 return {};
191         }
192
193         return string (a->get_value ());
194 }
195
196 bool
197 cxml::Node::bool_attribute (string name) const
198 {
199         auto const s = string_attribute (name);
200         return (s == "1" || s == "yes");
201 }
202
203 optional<bool>
204 cxml::Node::optional_bool_attribute (string name) const
205 {
206         auto s = optional_string_attribute (name);
207         if (!s) {
208                 return {};
209         }
210
211         return (s.get() == "1" || s.get() == "yes");
212 }
213
214 void
215 cxml::Node::done () const
216 {
217         for (auto i: _node->get_children()) {
218                 if (dynamic_cast<xmlpp::Element *> (i) && find (_taken.begin(), _taken.end(), i->get_name()) == _taken.end ()) {
219                         throw cxml::Error ("unexpected XML node " + i->get_name());
220                 }
221         }
222 }
223
224 string
225 cxml::Node::content () const
226 {
227         string content;
228
229         for (auto i: _node->get_children()) {
230                 auto v = dynamic_cast<xmlpp::ContentNode const *> (i);
231                 if (v && dynamic_cast<xmlpp::TextNode const *>(v)) {
232                         content += v->get_content ();
233                 }
234         }
235
236         return content;
237 }
238
239 string
240 cxml::Node::namespace_uri () const
241 {
242         return _node->get_namespace_uri ();
243 }
244
245 string
246 cxml::Node::namespace_prefix () const
247 {
248         return _node->get_namespace_prefix ();
249 }
250
251
252 bool
253 cxml::Node::is_text() const
254 {
255         return dynamic_cast<const xmlpp::TextNode*>(_node);
256 }
257
258 cxml::Document::Document (string root_name)
259         : _root_name (root_name)
260 {
261         _parser = new xmlpp::DomParser;
262 }
263
264 cxml::Document::Document (string root_name, boost::filesystem::path file)
265         : _root_name (root_name)
266 {
267         _parser = new xmlpp::DomParser ();
268         read_file (file);
269 }
270
271 cxml::Document::Document ()
272 {
273         _parser = new xmlpp::DomParser ();
274 }
275
276 cxml::Document::~Document ()
277 {
278         delete _parser;
279 }
280
281 void
282 cxml::Document::read_file (boost::filesystem::path file)
283 {
284         if (!boost::filesystem::exists (file)) {
285                 throw cxml::Error ("XML file " + file.string() + " does not exist");
286         }
287
288         _parser->parse_file (file.string ());
289         take_root_node ();
290 }
291
292 void
293 cxml::Document::read_string (string s)
294 {
295         _parser->parse_memory (s);
296         take_root_node ();
297 }
298
299 void
300 cxml::Document::take_root_node ()
301 {
302         if (!_parser) {
303                 throw cxml::Error ("could not parse XML");
304         }
305
306         _node = _parser->get_document()->get_root_node ();
307         if (!_root_name.empty() && _node->get_name() != _root_name) {
308                 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
309         } else if (_root_name.empty ()) {
310                 _root_name = _node->get_name ();
311         }
312 }
313
314 static
315 string
316 make_local (string v)
317 {
318         auto lc = localeconv ();
319         boost::algorithm::replace_all (v, ".", lc->decimal_point);
320         /* We hope it's ok not to add in thousands separators here */
321         return v;
322 }
323
324 template <typename P, typename Q>
325 P
326 locale_convert (Q x)
327 {
328         /* We can't write a generic version of locale_convert; all required
329            versions must be specialised.
330         */
331         BOOST_STATIC_ASSERT (sizeof(Q) == 0);
332 }
333
334 template<>
335 int
336 locale_convert (string x)
337 {
338         int y = 0;
339         sscanf (x.c_str(), "%d", &y);
340         return y;
341 }
342
343 template<>
344 unsigned int
345 locale_convert (string x)
346 {
347         unsigned int y = 0;
348         sscanf (x.c_str(), "%u", &y);
349         return y;
350 }
351
352 template<>
353 long int
354 locale_convert (string x)
355 {
356         long int y = 0;
357         sscanf (x.c_str(), "%ld", &y);
358         return y;
359 }
360
361 template<>
362 long unsigned int
363 locale_convert (string x)
364 {
365         long unsigned int y = 0;
366 #ifdef LIBCXML_WINDOWS
367         __mingw_sscanf (x.c_str(), "%lud", &y);
368 #else
369         sscanf (x.c_str(), "%lud", &y);
370 #endif
371         return y;
372 }
373
374 template<>
375 long long
376 locale_convert (string x)
377 {
378         long long y = 0;
379 #ifdef LIBCXML_WINDOWS
380         __mingw_sscanf (x.c_str(), "%lld", &y);
381 #else
382         sscanf (x.c_str(), "%lld", &y);
383 #endif
384         return y;
385 }
386
387 template<>
388 long long unsigned
389 locale_convert (string x)
390 {
391         long long unsigned y = 0;
392 #ifdef LIBCXML_WINDOWS
393         __mingw_sscanf (x.c_str(), "%llud", &y);
394 #else
395         sscanf (x.c_str(), "%llud", &y);
396 #endif
397         return y;
398 }
399
400 template<>
401 float
402 locale_convert (string x)
403 {
404         float y = 0;
405         sscanf (x.c_str(), "%f", &y);
406         return y;
407 }
408
409 template <>
410 double
411 locale_convert (string x)
412 {
413         double y = 0;
414         sscanf (x.c_str(), "%lf", &y);
415         return y;
416 }
417
418 template <>
419 int
420 cxml::raw_convert (string v)
421 {
422         return locale_convert<int> (make_local(v));
423 }
424
425 template <>
426 unsigned int
427 cxml::raw_convert (string v)
428 {
429         return locale_convert<unsigned int> (make_local(v));
430 }
431
432 template <>
433 long int
434 cxml::raw_convert (string v)
435 {
436         return locale_convert<long int> (make_local(v));
437 }
438
439 template <>
440 long unsigned int
441 cxml::raw_convert (string v)
442 {
443         return locale_convert<long unsigned int> (make_local(v));
444 }
445
446 template <>
447 long long
448 cxml::raw_convert (string v)
449 {
450         return locale_convert<long long> (make_local(v));
451 }
452
453 template <>
454 long long unsigned
455 cxml::raw_convert (string v)
456 {
457         return locale_convert<long long unsigned> (make_local(v));
458 }
459
460 template <>
461 float
462 cxml::raw_convert (string v)
463 {
464         return locale_convert<float> (make_local(v));
465 }
466
467 template <>
468 double
469 cxml::raw_convert (string v)
470 {
471         return locale_convert<double> (make_local(v));
472 }
473
474
475 xmlpp::Element*
476 cxml::add_child(xmlpp::Element* parent, string const& name)
477 {
478         return parent->add_child(name);
479 }
480
481
482 void
483 cxml::add_text_child(xmlpp::Element* parent, string const& name, string const& text)
484 {
485         parent->add_child(name)->add_child_text(text);
486 }