3 #include <boost/lexical_cast.hpp>
4 #include <libxml++/libxml++.h>
6 #include "exceptions.h"
10 using namespace boost;
11 using namespace libdcp;
19 XMLNode::XMLNode (xmlpp::Node const * node)
26 XMLNode::xml_node (string name)
28 list<xmlpp::Node*> n = xml_nodes (name);
30 throw XMLError ("duplicate XML tag " + name);
31 } else if (n.empty ()) {
32 throw XMLError ("missing XML tag " + name);
39 XMLNode::xml_nodes (string name)
41 /* XXX: using find / get_path should work here, but I can't follow
45 xmlpp::Node::NodeList c = _node->get_children ();
48 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
49 if ((*i)->get_name() == name) {
54 _taken.push_back (name);
59 XMLNode::string_node (string name)
61 xmlpp::Node* node = xml_node (name);
63 xmlpp::Node::NodeList c = node->get_children ();
65 throw XMLError ("unexpected content in XML node");
68 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (c.front());
70 throw XMLError ("missing content in XML node");
73 return v->get_content ();
77 XMLNode::optional_string_node (string name)
79 list<xmlpp::Node*> nodes = xml_nodes (name);
80 if (nodes.size() > 2) {
81 throw XMLError ("duplicate XML tag " + name);
88 return string_node (name);
92 XMLNode::kind_node (string name)
94 return content_kind_from_string (string_node (name));
98 XMLNode::fraction_node (string name)
100 return Fraction (string_node (name));
104 XMLNode::int64_node (string name)
106 return lexical_cast<int64_t> (string_node (name));
110 XMLNode::ignore_node (string name)
112 _taken.push_back (name);
118 xmlpp::Node::NodeList c = _node->get_children ();
119 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
120 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
121 throw XMLError ("unexpected XML node " + (*i)->get_name());
126 XMLFile::XMLFile (string file, string root_name)
128 _parser = new xmlpp::DomParser;
129 _parser->parse_file (file);
131 throw XMLError ("could not parse XML");
134 _node = _parser->get_document()->get_root_node ();
135 if (_node->get_name() != root_name) {
136 throw XMLError ("unrecognised root node");