summaryrefslogtreecommitdiff
path: root/src/xml.cc
blob: 4ab6a2e7bcd5460009447ff83b5b31ed76b2422a (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
#include <sstream>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/filesystem.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::xml_node (string name)
{
	list<xmlpp::Node*> n = xml_nodes (name);
	if (n.size() > 1) {
		throw XMLError ("duplicate XML tag " + name);
	} else if (n.empty ()) {
		throw XMLError ("missing XML tag " + name);
	}
	
	return n.front ();
}

list<xmlpp::Node*>
XMLNode::xml_nodes (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_node (string name)
{
	xmlpp::Node* node = xml_node (name);
	
        xmlpp::Node::NodeList c = node->get_children ();
	if (c.size() != 1) {
		throw XMLError ("unexpected content in XML node");
	}
	
        xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (c.front());
	if (!v) {
		throw XMLError ("missing content in XML node");
	}
	
	return v->get_content ();
}

string
XMLNode::optional_string_node (string name)
{
	list<xmlpp::Node*> nodes = xml_nodes (name);
	if (nodes.size() > 2) {
		throw XMLError ("duplicate XML tag " + name);
	}

	if (nodes.empty ()) {
		return "";
	}

	return string_node (name);
}

ContentKind
XMLNode::kind_node (string name)
{
	return content_kind_from_string (string_node (name));
}

Fraction
XMLNode::fraction_node (string name)
{
	return Fraction (string_node (name));
}

int64_t
XMLNode::int64_node (string name)
{
	return lexical_cast<int64_t> (string_node (name));
}

void
XMLNode::ignore_node (string name)
{
	_taken.push_back (name);
}

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 ()) {
			throw XMLError ("unexpected XML node " + (*i)->get_name());
		}
	}
}

XMLFile::XMLFile (string file, string root_name)
{
	if (!filesystem::exists (file)) {
		throw FileError ("XML file does not exist", file);
	}
	
	_parser = new xmlpp::DomParser;
	_parser->parse_file (file);
	if (!_parser) {
		throw XMLError ("could not parse XML");
	}

	_node = _parser->get_document()->get_root_node ();
	if (_node->get_name() != root_name) {
		throw XMLError ("unrecognised root node");
	}
}

XMLFile::~XMLFile ()
{
	delete _parser;
}