2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief Some useful XML helper functions.
27 #include "exceptions.h"
28 #include <libcxml/cxml.h>
35 optional_type_child (cxml::Node const & node, std::string name)
37 std::list<boost::shared_ptr<cxml::Node> > n = node.node_children (name);
39 throw XMLError ("duplicate XML tag");
40 } else if (n.empty ()) {
41 return boost::shared_ptr<T> ();
44 return boost::shared_ptr<T> (new T (n.front ()));
48 boost::shared_ptr<T> type_child (boost::shared_ptr<const cxml::Node> node, std::string name) {
49 return boost::shared_ptr<T> (new T (node->node_child (name)));
54 optional_type_child (boost::shared_ptr<const cxml::Node> node, std::string name)
56 return optional_type_child<T> (*node.get(), name);
60 std::list<boost::shared_ptr<T> >
61 type_children (cxml::Node const & node, std::string name)
63 std::list<boost::shared_ptr<cxml::Node> > n = node.node_children (name);
64 std::list<boost::shared_ptr<T> > r;
65 for (typename std::list<boost::shared_ptr<cxml::Node> >::iterator i = n.begin(); i != n.end(); ++i) {
66 r.push_back (boost::shared_ptr<T> (new T (*i)));
72 std::list<boost::shared_ptr<T> >
73 type_children (boost::shared_ptr<const cxml::Node> node, std::string name)
75 return type_children<T> (*node.get(), name);
79 std::list<boost::shared_ptr<T> >
80 type_grand_children (cxml::Node const & node, std::string name, std::string sub)
82 boost::shared_ptr<const cxml::Node> p = node.node_child (name);
83 return type_children<T> (p, sub);
87 std::list<boost::shared_ptr<T> >
88 type_grand_children (boost::shared_ptr<const cxml::Node> node, std::string name, std::string sub)
90 return type_grand_children<T> (*node.get(), name, sub);