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
102 throw cxml::Error("Node has no internal xmlpp node; did you forget to call a read method on cxml::Document?");
105 vector<shared_ptr<cxml::Node>> n;
106 for (auto i: _node->get_children()) {
107 if (i->get_name() == name) {
108 n.push_back (shared_ptr<Node> (new Node (i)));
112 _taken.push_back (name);
117 cxml::Node::string_child (string c) const
119 return node_child(c)->content ();
123 cxml::Node::optional_string_child (string c) const
125 auto const nodes = node_children (c);
126 if (nodes.size() > 1) {
127 throw cxml::Error ("duplicate XML tag " + c);
130 if (nodes.empty ()) {
134 return nodes.front()->content();
138 cxml::Node::bool_child (string c) const
140 auto const s = string_child (c);
141 return (s == "1" || s == "yes" || s == "True");
145 cxml::Node::optional_bool_child (string c) const
147 auto const s = optional_string_child (c);
152 return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
156 cxml::Node::ignore_child (string name) const
158 _taken.push_back (name);
162 cxml::Node::string_attribute (string name) const
164 auto e = dynamic_cast<const xmlpp::Element *> (_node);
166 throw cxml::Error ("missing attribute " + name);
169 auto a = e->get_attribute (name);
171 throw cxml::Error ("missing attribute " + name);
174 return a->get_value ();
178 cxml::Node::optional_string_attribute (string name) const
180 auto e = dynamic_cast<const xmlpp::Element *> (_node);
185 auto a = e->get_attribute (name);
190 return string (a->get_value ());
194 cxml::Node::bool_attribute (string name) const
196 auto const s = string_attribute (name);
197 return (s == "1" || s == "yes");
201 cxml::Node::optional_bool_attribute (string name) const
203 auto s = optional_string_attribute (name);
208 return (s.get() == "1" || s.get() == "yes");
212 cxml::Node::done () const
214 for (auto i: _node->get_children()) {
215 if (dynamic_cast<xmlpp::Element *> (i) && find (_taken.begin(), _taken.end(), i->get_name()) == _taken.end ()) {
216 throw cxml::Error ("unexpected XML node " + i->get_name());
222 cxml::Node::content () const
226 for (auto i: _node->get_children()) {
227 auto v = dynamic_cast<xmlpp::ContentNode const *> (i);
228 if (v && dynamic_cast<xmlpp::TextNode const *>(v)) {
229 content += v->get_content ();
237 cxml::Node::namespace_uri () const
239 return _node->get_namespace_uri ();
243 cxml::Node::namespace_prefix () const
245 return _node->get_namespace_prefix ();
248 cxml::Document::Document (string root_name)
249 : _root_name (root_name)
251 _parser = new xmlpp::DomParser;
254 cxml::Document::Document (string root_name, boost::filesystem::path file)
255 : _root_name (root_name)
257 _parser = new xmlpp::DomParser ();
261 cxml::Document::Document ()
263 _parser = new xmlpp::DomParser ();
266 cxml::Document::~Document ()
272 cxml::Document::read_file (boost::filesystem::path file)
274 if (!boost::filesystem::exists (file)) {
275 throw cxml::Error ("XML file " + file.string() + " does not exist");
278 _parser->parse_file (file.string ());
283 cxml::Document::read_string (string s)
285 _parser->parse_memory (s);
290 cxml::Document::take_root_node ()
293 throw cxml::Error ("could not parse XML");
296 _node = _parser->get_document()->get_root_node ();
297 if (!_root_name.empty() && _node->get_name() != _root_name) {
298 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
299 } else if (_root_name.empty ()) {
300 _root_name = _node->get_name ();
306 make_local (string v)
308 auto lc = localeconv ();
309 boost::algorithm::replace_all (v, ".", lc->decimal_point);
310 /* We hope it's ok not to add in thousands separators here */
314 template <typename P, typename Q>
318 /* We can't write a generic version of locale_convert; all required
319 versions must be specialised.
321 BOOST_STATIC_ASSERT (sizeof(Q) == 0);
326 locale_convert (string x)
329 sscanf (x.c_str(), "%d", &y);
335 locale_convert (string x)
338 sscanf (x.c_str(), "%u", &y);
344 locale_convert (string x)
347 sscanf (x.c_str(), "%ld", &y);
353 locale_convert (string x)
355 long unsigned int y = 0;
356 #ifdef LIBCXML_WINDOWS
357 __mingw_sscanf (x.c_str(), "%lud", &y);
359 sscanf (x.c_str(), "%lud", &y);
366 locale_convert (string x)
369 #ifdef LIBCXML_WINDOWS
370 __mingw_sscanf (x.c_str(), "%lld", &y);
372 sscanf (x.c_str(), "%lld", &y);
379 locale_convert (string x)
381 long long unsigned y = 0;
382 #ifdef LIBCXML_WINDOWS
383 __mingw_sscanf (x.c_str(), "%llud", &y);
385 sscanf (x.c_str(), "%llud", &y);
392 locale_convert (string x)
395 sscanf (x.c_str(), "%f", &y);
401 locale_convert (string x)
404 sscanf (x.c_str(), "%lf", &y);
410 cxml::raw_convert (string v)
412 return locale_convert<int> (make_local(v));
417 cxml::raw_convert (string v)
419 return locale_convert<unsigned int> (make_local(v));
424 cxml::raw_convert (string v)
426 return locale_convert<long int> (make_local(v));
431 cxml::raw_convert (string v)
433 return locale_convert<long unsigned int> (make_local(v));
438 cxml::raw_convert (string v)
440 return locale_convert<long long> (make_local(v));
445 cxml::raw_convert (string v)
447 return locale_convert<long long unsigned> (make_local(v));
452 cxml::raw_convert (string v)
454 return locale_convert<float> (make_local(v));
459 cxml::raw_convert (string v)
461 return locale_convert<double> (make_local(v));