2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libcxml.
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.
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.
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/>.
22 #include <libxml++/libxml++.h>
23 #include <boost/filesystem.hpp>
24 #include <boost/algorithm/string.hpp>
27 using std::shared_ptr;
30 using boost::optional;
38 cxml::Node::Node (xmlpp::Node* node)
45 cxml::Node::name () const
48 throw Error ("No node to read name from");
50 return _node->get_name ();
53 shared_ptr<cxml::Node>
54 cxml::Node::node_child (string name) const
56 auto const n = node_children (name);
58 throw cxml::Error ("duplicate XML tag " + name);
59 } else if (n.empty ()) {
60 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
66 shared_ptr<cxml::Node>
67 cxml::Node::optional_node_child (string name) const
69 auto const n = node_children (name);
71 throw cxml::Error ("duplicate XML tag " + name);
72 } else if (n.empty ()) {
79 vector<shared_ptr<cxml::Node>>
80 cxml::Node::node_children () const
83 throw Error ("No node to read children from");
86 vector<shared_ptr<cxml::Node>> n;
87 for (auto i: _node->get_children()) {
88 n.push_back (shared_ptr<Node> (new Node (i)));
94 vector<shared_ptr<cxml::Node>>
95 cxml::Node::node_children (string name) const
97 /* XXX: using find / get_path should work here, but I can't follow
101 vector<shared_ptr<cxml::Node>> n;
102 for (auto i: _node->get_children()) {
103 if (i->get_name() == name) {
104 n.push_back (shared_ptr<Node> (new Node (i)));
108 _taken.push_back (name);
113 cxml::Node::string_child (string c) const
115 return node_child(c)->content ();
119 cxml::Node::optional_string_child (string c) const
121 auto const nodes = node_children (c);
122 if (nodes.size() > 1) {
123 throw cxml::Error ("duplicate XML tag " + c);
126 if (nodes.empty ()) {
130 return nodes.front()->content();
134 cxml::Node::bool_child (string c) const
136 auto const s = string_child (c);
137 return (s == "1" || s == "yes" || s == "True");
141 cxml::Node::optional_bool_child (string c) const
143 auto const s = optional_string_child (c);
148 return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
152 cxml::Node::ignore_child (string name) const
154 _taken.push_back (name);
158 cxml::Node::string_attribute (string name) const
160 auto e = dynamic_cast<const xmlpp::Element *> (_node);
162 throw cxml::Error ("missing attribute " + name);
165 auto a = e->get_attribute (name);
167 throw cxml::Error ("missing attribute " + name);
170 return a->get_value ();
174 cxml::Node::optional_string_attribute (string name) const
176 auto e = dynamic_cast<const xmlpp::Element *> (_node);
181 auto a = e->get_attribute (name);
186 return string (a->get_value ());
190 cxml::Node::bool_attribute (string name) const
192 auto const s = string_attribute (name);
193 return (s == "1" || s == "yes");
197 cxml::Node::optional_bool_attribute (string name) const
199 auto s = optional_string_attribute (name);
204 return (s.get() == "1" || s.get() == "yes");
208 cxml::Node::done () const
210 for (auto i: _node->get_children()) {
211 if (dynamic_cast<xmlpp::Element *> (i) && find (_taken.begin(), _taken.end(), i->get_name()) == _taken.end ()) {
212 throw cxml::Error ("unexpected XML node " + i->get_name());
218 cxml::Node::content () const
222 for (auto i: _node->get_children()) {
223 auto v = dynamic_cast<xmlpp::ContentNode const *> (i);
224 if (v && dynamic_cast<xmlpp::TextNode const *>(v)) {
225 content += v->get_content ();
233 cxml::Node::namespace_uri () const
235 return _node->get_namespace_uri ();
239 cxml::Node::namespace_prefix () const
241 return _node->get_namespace_prefix ();
244 cxml::Document::Document (string root_name)
245 : _root_name (root_name)
247 _parser = new xmlpp::DomParser;
250 cxml::Document::Document (string root_name, boost::filesystem::path file)
251 : _root_name (root_name)
253 _parser = new xmlpp::DomParser ();
257 cxml::Document::Document ()
259 _parser = new xmlpp::DomParser ();
262 cxml::Document::~Document ()
268 cxml::Document::read_file (boost::filesystem::path file)
270 if (!boost::filesystem::exists (file)) {
271 throw cxml::Error ("XML file " + file.string() + " does not exist");
274 _parser->parse_file (file.string ());
279 cxml::Document::read_string (string s)
281 _parser->parse_memory (s);
286 cxml::Document::take_root_node ()
289 throw cxml::Error ("could not parse XML");
292 _node = _parser->get_document()->get_root_node ();
293 if (!_root_name.empty() && _node->get_name() != _root_name) {
294 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
295 } else if (_root_name.empty ()) {
296 _root_name = _node->get_name ();
302 make_local (string v)
304 auto lc = localeconv ();
305 boost::algorithm::replace_all (v, ".", lc->decimal_point);
306 /* We hope it's ok not to add in thousands separators here */
310 template <typename P, typename Q>
314 /* We can't write a generic version of locale_convert; all required
315 versions must be specialised.
317 BOOST_STATIC_ASSERT (sizeof(Q) == 0);
322 locale_convert (string x)
325 sscanf (x.c_str(), "%d", &y);
331 locale_convert (string x)
334 sscanf (x.c_str(), "%u", &y);
340 locale_convert (string x)
343 sscanf (x.c_str(), "%ld", &y);
349 locale_convert (string x)
351 long unsigned int y = 0;
352 #ifdef LIBCXML_WINDOWS
353 __mingw_sscanf (x.c_str(), "%lud", &y);
355 sscanf (x.c_str(), "%lud", &y);
362 locale_convert (string x)
365 #ifdef LIBCXML_WINDOWS
366 __mingw_sscanf (x.c_str(), "%lld", &y);
368 sscanf (x.c_str(), "%lld", &y);
375 locale_convert (string x)
377 long long unsigned y = 0;
378 #ifdef LIBCXML_WINDOWS
379 __mingw_sscanf (x.c_str(), "%llud", &y);
381 sscanf (x.c_str(), "%llud", &y);
388 locale_convert (string x)
391 sscanf (x.c_str(), "%f", &y);
397 locale_convert (string x)
400 sscanf (x.c_str(), "%lf", &y);
406 cxml::raw_convert (string v)
408 return locale_convert<int> (make_local(v));
413 cxml::raw_convert (string v)
415 return locale_convert<unsigned int> (make_local(v));
420 cxml::raw_convert (string v)
422 return locale_convert<long int> (make_local(v));
427 cxml::raw_convert (string v)
429 return locale_convert<long unsigned int> (make_local(v));
434 cxml::raw_convert (string v)
436 return locale_convert<long long> (make_local(v));
441 cxml::raw_convert (string v)
443 return locale_convert<long long unsigned> (make_local(v));
448 cxml::raw_convert (string v)
450 return locale_convert<float> (make_local(v));
455 cxml::raw_convert (string v)
457 return locale_convert<double> (make_local(v));