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 return _node->get_name ();
50 shared_ptr<cxml::Node>
51 cxml::Node::node_child (string name) const
53 list<shared_ptr<cxml::Node> > n = node_children (name);
55 throw cxml::Error ("duplicate XML tag " + name);
56 } else if (n.empty ()) {
57 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
63 shared_ptr<cxml::Node>
64 cxml::Node::optional_node_child (string name) const
66 list<shared_ptr<cxml::Node> > n = node_children (name);
68 throw cxml::Error ("duplicate XML tag " + name);
69 } else if (n.empty ()) {
70 return shared_ptr<cxml::Node> ();
76 list<shared_ptr<cxml::Node> >
77 cxml::Node::node_children () const
79 xmlpp::Node::NodeList c = _node->get_children ();
81 list<shared_ptr<cxml::Node> > n;
82 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
83 n.push_back (shared_ptr<Node> (new Node (*i)));
89 list<shared_ptr<cxml::Node> >
90 cxml::Node::node_children (string name) const
92 /* XXX: using find / get_path should work here, but I can't follow
96 xmlpp::Node::NodeList c = _node->get_children ();
98 list<shared_ptr<cxml::Node> > n;
99 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
100 if ((*i)->get_name() == name) {
101 n.push_back (shared_ptr<Node> (new Node (*i)));
105 _taken.push_back (name);
110 cxml::Node::string_child (string c) const
112 return node_child(c)->content ();
116 cxml::Node::optional_string_child (string c) const
118 list<shared_ptr<Node> > nodes = node_children (c);
119 if (nodes.size() > 1) {
120 throw cxml::Error ("duplicate XML tag " + c);
123 if (nodes.empty ()) {
124 return optional<string> ();
127 return nodes.front()->content();
131 cxml::Node::bool_child (string c) const
133 string const s = string_child (c);
134 return (s == "1" || s == "yes" || s == "True");
138 cxml::Node::optional_bool_child (string c) const
140 optional<string> s = optional_string_child (c);
142 return optional<bool> ();
145 return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
149 cxml::Node::ignore_child (string name) const
151 _taken.push_back (name);
155 cxml::Node::string_attribute (string name) const
157 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
159 throw cxml::Error ("missing attribute " + name);
162 xmlpp::Attribute* a = e->get_attribute (name);
164 throw cxml::Error ("missing attribute " + name);
167 return a->get_value ();
171 cxml::Node::optional_string_attribute (string name) const
173 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
175 return optional<string> ();
178 xmlpp::Attribute* a = e->get_attribute (name);
180 return optional<string> ();
183 return string (a->get_value ());
187 cxml::Node::bool_attribute (string name) const
189 string const s = string_attribute (name);
190 return (s == "1" || s == "yes");
194 cxml::Node::optional_bool_attribute (string name) const
196 optional<string> s = optional_string_attribute (name);
198 return optional<bool> ();
201 return (s.get() == "1" || s.get() == "yes");
205 cxml::Node::done () const
207 xmlpp::Node::NodeList c = _node->get_children ();
208 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
209 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
210 throw cxml::Error ("unexpected XML node " + (*i)->get_name());
216 cxml::Node::content () const
220 xmlpp::Node::NodeList c = _node->get_children ();
221 for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
222 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
223 if (v && dynamic_cast<xmlpp::TextNode const *>(v)) {
224 content += v->get_content ();
232 cxml::Node::namespace_uri () const
234 return _node->get_namespace_uri ();
238 cxml::Node::namespace_prefix () const
240 return _node->get_namespace_prefix ();
243 cxml::Document::Document (string root_name)
244 : _root_name (root_name)
246 _parser = new xmlpp::DomParser;
249 cxml::Document::Document (string root_name, boost::filesystem::path file)
250 : _root_name (root_name)
252 _parser = new xmlpp::DomParser ();
256 cxml::Document::Document ()
258 _parser = new xmlpp::DomParser ();
261 cxml::Document::~Document ()
267 cxml::Document::read_file (boost::filesystem::path file)
269 if (!boost::filesystem::exists (file)) {
270 throw cxml::Error ("XML file " + file.string() + " does not exist");
273 _parser->parse_file (file.string ());
278 cxml::Document::read_string (string s)
280 _parser->parse_memory (s);
285 cxml::Document::take_root_node ()
288 throw cxml::Error ("could not parse XML");
291 _node = _parser->get_document()->get_root_node ();
292 if (!_root_name.empty() && _node->get_name() != _root_name) {
293 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
294 } else if (_root_name.empty ()) {
295 _root_name = _node->get_name ();