#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"
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
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
_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 ()
{
}
}
+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) {