Basic subtitle test works.
authorCarl Hetherington <cth@carlh.net>
Mon, 13 Aug 2012 00:45:42 +0000 (01:45 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 13 Aug 2012 00:45:42 +0000 (01:45 +0100)
src/subtitle_asset.cc
src/subtitle_asset.h
src/xml.cc
src/xml.h
test/tests.cc

index 19697a7f8864b8221a623095c80053d4b8c45ea7..ca27d6dab08a770f6a67a71fdd80b66f0725792b 100644 (file)
@@ -44,11 +44,14 @@ Font::Font (xmlpp::Node const * node)
 Subtitle::Subtitle (xmlpp::Node const * node)
        : XMLNode (node)
 {
+       _in = time_attribute ("TimeIn");
+       _out = time_attribute ("TimeOut");
        _texts = sub_nodes<Text> ("Text");
 }
 
 Text::Text (xmlpp::Node const * node)
        : XMLNode (node)
 {
-
+       _text = content ();
+       _v_position = float_attribute ("VPosition");
 }
index 392d70b433b8c11c31bf664438356991f9a3688b..02db6815b27a76bc9275d10b03656e98ed345640 100644 (file)
@@ -57,7 +57,7 @@ public:
                return _out;
        }
 
-       std::list<boost::shared_ptr<Text> > texts () const {
+       std::list<boost::shared_ptr<Text> > const & texts () const {
                return _texts;
        }
 
@@ -73,7 +73,7 @@ public:
        Font () {}
        Font (xmlpp::Node const * node);
 
-       std::list<boost::shared_ptr<Subtitle> > subtitles () const {
+       std::list<boost::shared_ptr<Subtitle> > const & subtitles () const {
                return _subtitles;
        }
 
@@ -95,7 +95,7 @@ public:
                return _language;
        }
 
-       std::list<boost::shared_ptr<Font> > fonts () const {
+       std::list<boost::shared_ptr<Font> > const & fonts () const {
                return _fonts;
        }
 
index 79baa01cda1b5a5309c4a1a47de1b74ecc88153e..21912bdf1458e7be70d1eb77642ee06e3482a301 100644 (file)
@@ -2,6 +2,7 @@
 #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"
@@ -59,24 +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");
-       }
-
-       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 ();
+       return XMLNode (xml_node (name)).content ();
 }
 
 string
@@ -142,7 +126,35 @@ XMLNode::ignore_node (string name)
 Time
 XMLNode::time_attribute (string name)
 {
+       string const t = string_attribute (name);
+
+       vector<string> b;
+       boost::split (b, t, is_any_of (":"));
+       assert (b.size() == 4);
+
+       return Time (lexical_cast<int> (b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]));
+}
+
+string
+XMLNode::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));
 }
 
 void
@@ -156,6 +168,27 @@ 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)) {
index 5c0719580e2e1c3af58a97dcee200a06abee3664..85589a0f41d885e52913bd82da40a2d38ffa3d63 100644 (file)
--- a/src/xml.h
+++ b/src/xml.h
@@ -8,6 +8,7 @@
 #include <boost/shared_ptr.hpp>
 #include "types.h"
 #include "exceptions.h"
+#include "dcp_time.h"
 
 namespace xmlpp {
        class Node;
@@ -35,6 +36,9 @@ protected:
 
        Time time_attribute (std::string);
        float float_attribute (std::string);
+       std::string string_attribute (std::string);
+
+       std::string content ();
 
        template <class T>
        boost::shared_ptr<T> sub_node (std::string name) {
index c6d286221227ea3b10f9014987b9fd2ce93cb715..b8d9ef976d40a6597965119492e796c562318246 100644 (file)
@@ -101,6 +101,29 @@ BOOST_AUTO_TEST_CASE (subtitles)
        BOOST_CHECK_EQUAL ((*i)->texts().size(), 1);
        BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 15);
        BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "My jacket was Idi Amin's");
+       ++i;
+
+       BOOST_CHECK_EQUAL ((*i)->in(), libdcp::Time (0, 0, 7, 177));
+       BOOST_CHECK_EQUAL ((*i)->out(), libdcp::Time (0, 0, 11, 31));
+       BOOST_CHECK_EQUAL ((*i)->texts().size(), 2);
+       BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 21);
+       BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "My corset was H.M. The Queen's");
+       BOOST_CHECK_EQUAL ((*i)->texts().back()->v_position(), 15);
+       BOOST_CHECK_EQUAL ((*i)->texts().back()->text(), "My large wonderbra");
+       ++i;
+
+       BOOST_CHECK_EQUAL ((*i)->in(), libdcp::Time (0, 0, 11, 94));
+       BOOST_CHECK_EQUAL ((*i)->out(), libdcp::Time (0, 0, 13, 63));
+       BOOST_CHECK_EQUAL ((*i)->texts().size(), 1);
+       BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 15);
+       BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "Once belonged to the Shah");
+       ++i;
+
+       BOOST_CHECK_EQUAL ((*i)->in(), libdcp::Time (0, 0, 13, 104));
+       BOOST_CHECK_EQUAL ((*i)->out(), libdcp::Time (0, 0, 15, 177));
+       BOOST_CHECK_EQUAL ((*i)->texts().size(), 1);
+       BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 15);
+       BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "And these are Roy Hattersley's jeans");
 }