Some maths operations with Time.
[libdcp.git] / src / xml.cc
index 62e231d15dedbf79d845234cb27e430d5883045e..35c4fe1274de2ef8e8f0e0701d6a9091f2d9b805 100644 (file)
@@ -1,6 +1,8 @@
 #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"
@@ -58,19 +60,7 @@ XMLNode::xml_nodes (string name)
 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 ();
+       return XMLNode (xml_node (name)).content ();
 }
 
 string
@@ -100,10 +90,31 @@ XMLNode::fraction_node (string name)
        return Fraction (string_node (name));
 }
 
-int
-XMLNode::int_node (string name)
+int64_t
+XMLNode::int64_node (string name)
+{
+       return lexical_cast<int64_t> (string_node (name));
+}
+
+int64_t
+XMLNode::optional_int64_node (string name)
+{
+       list<xmlpp::Node*> nodes = xml_nodes (name);
+       if (nodes.size() > 2) {
+               throw XMLError ("duplicate XML tag " + name);
+       }
+
+       if (nodes.empty ()) {
+               return 0;
+       }
+
+       return int64_node (name);
+}
+
+float
+XMLNode::float_node (string name)
 {
-       return lexical_cast<int> (string_node (name));
+       return lexical_cast<float> (string_node (name));
 }
 
 void
@@ -112,6 +123,93 @@ XMLNode::ignore_node (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) {
+               throw XMLError ("missing attribute");
+       }
+       
+       xmlpp::Attribute* a = e->get_attribute (name);
+       if (!a) {
+               throw 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 ()
 {
@@ -123,8 +221,33 @@ XMLNode::done ()
        }
 }
 
+string
+XMLNode::content ()
+{
+        xmlpp::Node::NodeList c = _node->get_children ();
+
+       if (c.size() > 1) {
+               throw XMLError ("unexpected content in XML node");
+       }
+
+       if (c.empty ()) {
+               return "";
+       }
+       
+        xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (c.front());
+       if (!v) {
+               throw XMLError ("missing content in XML node");
+       }
+       
+       return v->get_content ();
+}
+
 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) {