Bv2.1 7.2.3: Check that subtitle <StartTime> exists and is 0.
[libdcp.git] / src / xml.h
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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.
10
11     libdcp 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.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #ifndef LIBDCP_XML_H
35 #define LIBDCP_XML_H
36
37 #include "exceptions.h"
38 #include <libcxml/cxml.h>
39
40 namespace dcp
41 {
42
43 template <class T>
44 std::shared_ptr<T>
45 optional_type_child (cxml::Node const & node, std::string name)
46 {
47         auto n = node.node_children (name);
48         if (n.size() > 1) {
49                 throw XMLError ("duplicate XML tag");
50         } else if (n.empty ()) {
51                 return {};
52         }
53
54         return std::make_shared<T>(n.front());
55 }
56
57 template <class T>
58 std::shared_ptr<T> type_child (std::shared_ptr<const cxml::Node> node, std::string name) {
59         return std::make_shared<T>(node->node_child(name));
60 }
61
62 template <class T>
63 std::shared_ptr<T>
64 optional_type_child (std::shared_ptr<const cxml::Node> node, std::string name)
65 {
66         return optional_type_child<T> (*node.get(), name);
67 }
68
69 template <class T>
70 std::list<std::shared_ptr<T>>
71 type_children (cxml::Node const & node, std::string name)
72 {
73         std::list<std::shared_ptr<T> > r;
74         for (auto i: node.node_children(name)) {
75                 r.push_back (std::make_shared<T>(i));
76         }
77         return r;
78 }
79
80 template <class T>
81 std::list<std::shared_ptr<T>>
82 type_children (std::shared_ptr<const cxml::Node> node, std::string name)
83 {
84         return type_children<T> (*node.get(), name);
85 }
86
87 template <class T>
88 std::list<std::shared_ptr<T>>
89 type_grand_children (cxml::Node const & node, std::string name, std::string sub)
90 {
91         return type_children<T> (node.node_child(name), sub);
92 }
93
94 template <class T>
95 std::list<std::shared_ptr<T>>
96 type_grand_children (std::shared_ptr<const cxml::Node> node, std::string name, std::string sub)
97 {
98         return type_grand_children<T> (*node.get(), name, sub);
99 }
100
101 }
102
103 #endif