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/>.
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string.hpp>
23 #include <libxml++/libxml++.h>
28 using boost::shared_ptr;
29 using boost::optional;
37 cxml::Node::Node (xmlpp::Node* node)
44 cxml::Node::name () const
47 throw Error ("No node to read name from");
49 return _node->get_name ();
52 shared_ptr<cxml::Node>
53 cxml::Node::node_child (string name) const
55 list<shared_ptr<cxml::Node> > n = node_children (name);
57 throw cxml::Error ("duplicate XML tag " + name);
58 } else if (n.empty ()) {
59 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
65 shared_ptr<cxml::Node>
66 cxml::Node::optional_node_child (string name) const
68 list<shared_ptr<cxml::Node> > n = node_children (name);
70 throw cxml::Error ("duplicate XML tag " + name);
71 } else if (n.empty ()) {
72 return shared_ptr<cxml::Node> ();
78 list<shared_ptr<cxml::Node> >
79 cxml::Node::node_children () const
82 throw Error ("No node to read children from");
84 xmlpp::Node::NodeList c = _node->get_children ();
86 list<shared_ptr<cxml::Node> > n;
87 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
88 n.push_back (shared_ptr<Node> (new Node (*i)));
94 list<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 xmlpp::Node::NodeList c = _node->get_children ();
103 list<shared_ptr<cxml::Node> > n;
104 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
105 if ((*i)->get_name() == name) {
106 n.push_back (shared_ptr<Node> (new Node (*i)));
110 _taken.push_back (name);
115 cxml::Node::string_child (string c) const
117 return node_child(c)->content ();
121 cxml::Node::optional_string_child (string c) const
123 list<shared_ptr<Node> > nodes = node_children (c);
124 if (nodes.size() > 1) {
125 throw cxml::Error ("duplicate XML tag " + c);
128 if (nodes.empty ()) {
129 return optional<string> ();
132 return nodes.front()->content();
136 cxml::Node::bool_child (string c) const
138 string const s = string_child (c);
139 return (s == "1" || s == "yes" || s == "True");
143 cxml::Node::optional_bool_child (string c) const
145 optional<string> s = optional_string_child (c);
147 return optional<bool> ();
150 return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
154 cxml::Node::ignore_child (string name) const
156 _taken.push_back (name);
160 cxml::Node::string_attribute (string name) const
162 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
164 throw cxml::Error ("missing attribute " + name);
167 xmlpp::Attribute* a = e->get_attribute (name);
169 throw cxml::Error ("missing attribute " + name);
172 return a->get_value ();
176 cxml::Node::optional_string_attribute (string name) const
178 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
180 return optional<string> ();
183 xmlpp::Attribute* a = e->get_attribute (name);
185 return optional<string> ();
188 return string (a->get_value ());
192 cxml::Node::bool_attribute (string name) const
194 string const s = string_attribute (name);
195 return (s == "1" || s == "yes");
199 cxml::Node::optional_bool_attribute (string name) const
201 optional<string> s = optional_string_attribute (name);
203 return optional<bool> ();
206 return (s.get() == "1" || s.get() == "yes");
210 cxml::Node::done () const
212 xmlpp::Node::NodeList c = _node->get_children ();
213 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
214 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
215 throw cxml::Error ("unexpected XML node " + (*i)->get_name());
221 cxml::Node::content () const
225 xmlpp::Node::NodeList c = _node->get_children ();
226 for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
227 xmlpp::ContentNode const * 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 struct lconv* 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)
356 sscanf (x.c_str(), "%f", &y);
362 locale_convert (string x)
365 sscanf (x.c_str(), "%lf", &y);
371 cxml::raw_convert (string v)
373 return locale_convert<int> (make_local(v));
378 cxml::raw_convert (string v)
380 return locale_convert<long int> (make_local(v));
385 cxml::raw_convert (string v)
387 return locale_convert<unsigned int> (make_local(v));
392 cxml::raw_convert (string v)
394 return locale_convert<float> (make_local(v));
399 cxml::raw_convert (string v)
401 return locale_convert<double> (make_local(v));