blob: 508790abd43af74a5d9330588534185eef669604 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#include <sstream>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <libxml++/libxml++.h>
#include "xml.h"
#include "exceptions.h"
#include "util.h"
using namespace std;
using namespace boost;
using namespace libdcp;
XMLNode::XMLNode ()
: _node (0)
{
}
XMLNode::XMLNode (xmlpp::Node const * node)
: _node (node)
{
}
xmlpp::Node *
XMLNode::node_child (string name)
{
list<xmlpp::Node*> n = node_children (name);
if (n.size() > 1) {
boost::throw_exception (XMLError ("duplicate XML tag " + name));
} else if (n.empty ()) {
boost::throw_exception (XMLError ("missing XML tag " + name + " in " + _node->get_name()));
}
return n.front ();
}
list<xmlpp::Node*>
XMLNode::node_children (string name)
{
/* XXX: using find / get_path should work here, but I can't follow
how get_path works.
*/
xmlpp::Node::NodeList c = _node->get_children ();
list<xmlpp::Node*> n;
for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
if ((*i)->get_name() == name) {
n.push_back (*i);
}
}
_taken.push_back (name);
return n;
}
string
XMLNode::string_child (string name)
{
return XMLNode (node_child (name)).content ();
}
string
XMLNode::optional_string_child (string name)
{
list<xmlpp::Node*> nodes = node_children (name);
if (nodes.size() > 2) {
boost::throw_exception (XMLError ("duplicate XML tag " + name));
}
if (nodes.empty ()) {
return "";
}
return string_child (name);
}
ContentKind
XMLNode::kind_child (string name)
{
return content_kind_from_string (string_child (name));
}
Fraction
XMLNode::fraction_child (string name)
{
return Fraction (string_child (name));
}
int64_t
XMLNode::int64_child (string name)
{
string s = string_child (name);
erase_all (s, " ");
return lexical_cast<int64_t> (s);
}
int64_t
XMLNode::optional_int64_child (string name)
{
list<xmlpp::Node*> nodes = node_children (name);
if (nodes.size() > 2) {
boost::throw_exception (XMLError ("duplicate XML tag " + name));
}
if (nodes.empty ()) {
return 0;
}
return int64_child (name);
}
float
XMLNode::float_child (string name)
{
return lexical_cast<float> (string_child (name));
}
void
XMLNode::ignore_child (string name)
{
_taken.push_back (name);
}
Time
XMLNode::time_attribute (string name)
{
return Time (string_attribute (name));
}
string
XMLNode::string_attribute (string name)
{
xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
if (!e) {
boost::throw_exception (XMLError ("missing attribute"));
}
xmlpp::Attribute* a = e->get_attribute (name);
if (!a) {
boost::throw_exception (XMLError ("missing attribute"));
}
return a->get_value ();
}
string
XMLNode::optional_string_attribute (string name)
{
xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
if (!e) {
return "";
}
xmlpp::Attribute* a = e->get_attribute (name);
if (!a) {
return "";
}
return a->get_value ();
}
float
XMLNode::float_attribute (string name)
{
return lexical_cast<float> (string_attribute (name));
}
int64_t
XMLNode::int64_attribute (string name)
{
return lexical_cast<int64_t> (string_attribute (name));
}
int64_t
XMLNode::optional_int64_attribute (string name)
{
string const s = optional_string_attribute (name);
if (s.empty ()) {
return 0;
}
return lexical_cast<int64_t> (s);
}
optional<bool>
XMLNode::optional_bool_attribute (string name)
{
string const s = optional_string_attribute (name);
if (s.empty ()) {
return optional<bool> ();
}
if (s == "1" || s == "yes") {
return optional<bool> (true);
}
return optional<bool> (false);
}
optional<Color>
XMLNode::optional_color_attribute (string name)
{
string const s = optional_string_attribute (name);
if (s.empty ()) {
return optional<Color> ();
}
return optional<Color> (Color (s));
}
void
XMLNode::done ()
{
xmlpp::Node::NodeList c = _node->get_children ();
for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
boost::throw_exception (XMLError ("unexpected XML node " + (*i)->get_name()));
}
}
}
string
XMLNode::content ()
{
string content;
xmlpp::Node::NodeList c = _node->get_children ();
for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
if (v) {
content += v->get_content ();
}
}
return content;
}
XMLFile::XMLFile (string file, string root_name)
{
if (!filesystem::exists (file)) {
boost::throw_exception (FileError ("XML file does not exist", file));
}
_parser = new xmlpp::DomParser;
_parser->parse_file (file);
if (!_parser) {
boost::throw_exception (XMLError ("could not parse XML"));
}
_node = _parser->get_document()->get_root_node ();
if (_node->get_name() != root_name) {
boost::throw_exception (XMLError ("unrecognised root node"));
}
}
XMLFile::~XMLFile ()
{
delete _parser;
}
|