3 #include <boost/lexical_cast.hpp>
4 #include <boost/filesystem.hpp>
5 #include <libxml++/libxml++.h>
7 #include "exceptions.h"
11 using namespace boost;
12 using namespace libdcp;
20 XMLNode::XMLNode (xmlpp::Node const * node)
27 XMLNode::xml_node (string name)
29 list<xmlpp::Node*> n = xml_nodes (name);
31 throw XMLError ("duplicate XML tag " + name);
32 } else if (n.empty ()) {
33 throw XMLError ("missing XML tag " + name);
40 XMLNode::xml_nodes (string name)
42 /* XXX: using find / get_path should work here, but I can't follow
46 xmlpp::Node::NodeList c = _node->get_children ();
49 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
50 if ((*i)->get_name() == name) {
55 _taken.push_back (name);
60 XMLNode::string_node (string name)
62 xmlpp::Node* node = xml_node (name);
64 xmlpp::Node::NodeList c = node->get_children ();
66 throw XMLError ("unexpected content in XML node");
69 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (c.front());
71 throw XMLError ("missing content in XML node");
74 return v->get_content ();
78 XMLNode::optional_string_node (string name)
80 list<xmlpp::Node*> nodes = xml_nodes (name);
81 if (nodes.size() > 2) {
82 throw XMLError ("duplicate XML tag " + name);
89 return string_node (name);
93 XMLNode::kind_node (string name)
95 return content_kind_from_string (string_node (name));
99 XMLNode::fraction_node (string name)
101 return Fraction (string_node (name));
105 XMLNode::int64_node (string name)
107 return lexical_cast<int64_t> (string_node (name));
111 XMLNode::ignore_node (string name)
113 _taken.push_back (name);
119 xmlpp::Node::NodeList c = _node->get_children ();
120 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
121 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
122 throw XMLError ("unexpected XML node " + (*i)->get_name());
127 XMLFile::XMLFile (string file, string root_name)
129 if (!filesystem::exists (file)) {
130 throw FileError ("XML file does not exist", file);
133 _parser = new xmlpp::DomParser;
134 _parser->parse_file (file);
136 throw XMLError ("could not parse XML");
139 _node = _parser->get_document()->get_root_node ();
140 if (_node->get_name() != root_name) {
141 throw XMLError ("unrecognised root node");