New test.
[libsub.git] / src / xml.h
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/xml.h
21  *  @brief Some useful XML helper functions.
22  */
23
24 #ifndef LIBSUB_XML_H
25 #define LIBSUB_XML_H
26
27 #include "exceptions.h"
28 #include <libcxml/cxml.h>
29
30 namespace sub
31 {
32
33 template <class T>
34 boost::shared_ptr<T>
35 optional_type_child (cxml::Node const & node, std::string name)
36 {
37         std::list<boost::shared_ptr<cxml::Node> > n = node.node_children (name);
38         if (n.size() > 1) {
39                 throw XMLError ("duplicate XML tag");
40         } else if (n.empty ()) {
41                 return boost::shared_ptr<T> ();
42         }
43
44         return boost::shared_ptr<T> (new T (n.front ()));
45 }
46
47 template <class T>
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)));
50 }
51
52 template <class T>
53 boost::shared_ptr<T>
54 optional_type_child (boost::shared_ptr<const cxml::Node> node, std::string name)
55 {
56         return optional_type_child<T> (*node.get(), name);
57 }
58
59 template <class T>
60 std::list<boost::shared_ptr<T> >
61 type_children (cxml::Node const & node, std::string name)
62 {
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)));
67         }
68         return r;
69 }
70
71 template <class T>
72 std::list<boost::shared_ptr<T> >
73 type_children (boost::shared_ptr<const cxml::Node> node, std::string name)
74 {
75         return type_children<T> (*node.get(), name);
76 }
77
78 template <class T>
79 std::list<boost::shared_ptr<T> >
80 type_grand_children (cxml::Node const & node, std::string name, std::string sub)
81 {
82         boost::shared_ptr<const cxml::Node> p = node.node_child (name);
83         return type_children<T> (p, sub);
84 }
85
86 template <class T>
87 std::list<boost::shared_ptr<T> >
88 type_grand_children (boost::shared_ptr<const cxml::Node> node, std::string name, std::string sub)
89 {
90         return type_grand_children<T> (*node.get(), name, sub);
91 }
92
93 }
94
95 #endif