2 Copyright (C) 2012-2016 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>
29 using boost::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 list<shared_ptr<cxml::Node> > 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 list<shared_ptr<cxml::Node> > n = node_children (name);
71 throw cxml::Error ("duplicate XML tag " + name);
72 } else if (n.empty ()) {
73 return shared_ptr<cxml::Node> ();
79 list<shared_ptr<cxml::Node> >
80 cxml::Node::node_children () const
83 throw Error ("No node to read children from");
85 xmlpp::Node::NodeList c = _node->get_children ();
87 list<shared_ptr<cxml::Node> > n;
88 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
89 n.push_back (shared_ptr<Node> (new Node (*i)));
95 list<shared_ptr<cxml::Node> >
96 cxml::Node::node_children (string name) const
98 /* XXX: using find / get_path should work here, but I can't follow
102 xmlpp::Node::NodeList c = _node->get_children ();
104 list<shared_ptr<cxml::Node> > n;
105 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
106 if ((*i)->get_name() == name) {
107 n.push_back (shared_ptr<Node> (new Node (*i)));
111 _taken.push_back (name);
116 cxml::Node::string_child (string c) const
118 return node_child(c)->content ();
122 cxml::Node::optional_string_child (string c) const
124 list<shared_ptr<Node> > nodes = node_children (c);
125 if (nodes.size() > 1) {
126 throw cxml::Error ("duplicate XML tag " + c);
129 if (nodes.empty ()) {
130 return optional<string> ();
133 return nodes.front()->content();
137 cxml::Node::bool_child (string c) const
139 string const s = string_child (c);
140 return (s == "1" || s == "yes" || s == "True");
144 cxml::Node::optional_bool_child (string c) const
146 optional<string> s = optional_string_child (c);
148 return optional<bool> ();
151 return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
155 cxml::Node::ignore_child (string name) const
157 _taken.push_back (name);
161 cxml::Node::string_attribute (string name) const
163 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
165 throw cxml::Error ("missing attribute " + name);
168 xmlpp::Attribute* a = e->get_attribute (name);
170 throw cxml::Error ("missing attribute " + name);
173 return a->get_value ();
177 cxml::Node::optional_string_attribute (string name) const
179 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
181 return optional<string> ();
184 xmlpp::Attribute* a = e->get_attribute (name);
186 return optional<string> ();
189 return string (a->get_value ());
193 cxml::Node::bool_attribute (string name) const
195 string const s = string_attribute (name);
196 return (s == "1" || s == "yes");
200 cxml::Node::optional_bool_attribute (string name) const
202 optional<string> s = optional_string_attribute (name);
204 return optional<bool> ();
207 return (s.get() == "1" || s.get() == "yes");
211 cxml::Node::done () const
213 xmlpp::Node::NodeList c = _node->get_children ();
214 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
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 xmlpp::Node::NodeList c = _node->get_children ();
227 for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
228 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
229 if (v && dynamic_cast<xmlpp::TextNode const *>(v)) {
230 content += v->get_content ();
238 cxml::Node::namespace_uri () const
240 return _node->get_namespace_uri ();
244 cxml::Node::namespace_prefix () const
246 return _node->get_namespace_prefix ();
249 cxml::Document::Document (string root_name)
250 : _root_name (root_name)
252 _parser = new xmlpp::DomParser;
255 cxml::Document::Document (string root_name, boost::filesystem::path file)
256 : _root_name (root_name)
258 _parser = new xmlpp::DomParser ();
262 cxml::Document::Document ()
264 _parser = new xmlpp::DomParser ();
267 cxml::Document::~Document ()
273 cxml::Document::read_file (boost::filesystem::path file)
275 if (!boost::filesystem::exists (file)) {
276 throw cxml::Error ("XML file " + file.string() + " does not exist");
279 _parser->parse_file (file.string ());
284 cxml::Document::read_string (string s)
286 _parser->parse_memory (s);
291 cxml::Document::take_root_node ()
294 throw cxml::Error ("could not parse XML");
297 _node = _parser->get_document()->get_root_node ();
298 if (!_root_name.empty() && _node->get_name() != _root_name) {
299 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
300 } else if (_root_name.empty ()) {
301 _root_name = _node->get_name ();
307 make_local (string v)
309 struct lconv* lc = localeconv ();
310 boost::algorithm::replace_all (v, ".", lc->decimal_point);
311 /* We hope it's ok not to add in thousands separators here */
315 template <typename P, typename Q>
319 /* We can't write a generic version of locale_convert; all required
320 versions must be specialised.
322 BOOST_STATIC_ASSERT (sizeof(Q) == 0);
327 locale_convert (string x)
330 sscanf (x.c_str(), "%d", &y);
336 locale_convert (string x)
339 sscanf (x.c_str(), "%u", &y);
345 locale_convert (string x)
348 sscanf (x.c_str(), "%ld", &y);
354 locale_convert (string x)
357 #ifdef LIBCXML_WINDOWS
358 __mingw_sscanf (x.c_str(), "%lld", &y);
360 sscanf (x.c_str(), "%lld", &y);
367 locale_convert (string x)
370 sscanf (x.c_str(), "%f", &y);
376 locale_convert (string x)
379 sscanf (x.c_str(), "%lf", &y);
385 cxml::raw_convert (string v)
387 return locale_convert<int> (make_local(v));
392 cxml::raw_convert (string v)
394 return locale_convert<long int> (make_local(v));
399 cxml::raw_convert (string v)
401 return locale_convert<long long> (make_local(v));
406 cxml::raw_convert (string v)
408 return locale_convert<unsigned int> (make_local(v));
413 cxml::raw_convert (string v)
415 return locale_convert<float> (make_local(v));
420 cxml::raw_convert (string v)
422 return locale_convert<double> (make_local(v));