aboutsummaryrefslogtreecommitdiff
path: root/sxml.cpp
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2011-06-01 10:52:04 +0300
committerOskari Timperi <oskari.timperi@iki.fi>2011-06-01 10:52:04 +0300
commita71564984253ade92fc4d3c9bf185807efc570bf (patch)
tree93e267348605dda8f32262a4d912c33e7720c074 /sxml.cpp
parent5fb5bb1c7cc0ce266f60ab272915a48770d1443f (diff)
downloadsxml-a71564984253ade92fc4d3c9bf185807efc570bf.tar.gz
sxml-a71564984253ade92fc4d3c9bf185807efc570bf.zip
New classes: node and comment.
Move functionality from element to node and derive element from node. This allows nodes to contain different types of nodes, e.g. comments mixed with normal elements.
Diffstat (limited to 'sxml.cpp')
-rw-r--r--sxml.cpp192
1 files changed, 116 insertions, 76 deletions
diff --git a/sxml.cpp b/sxml.cpp
index 53bb31f..957c8c3 100644
--- a/sxml.cpp
+++ b/sxml.cpp
@@ -26,16 +26,13 @@
#include "sxml.h"
#include <sstream>
-//#include <iostream>
using std::string;
using std::ostringstream;
-//using std::cout;
-//using std::endl;
namespace sxml {
-element::element(element *parent)
+node::node(node *parent)
: m_parent(parent),
m_modified(true)
{
@@ -45,41 +42,127 @@ element::element(element *parent)
}
}
-element::element(const element &elem)
- : m_parent(elem.m_parent),
- m_children(elem.clone_children()),
- m_attributes(elem.m_attributes),
- m_name(elem.m_name),
- m_text(elem.m_text),
- m_cached(elem.m_cached),
- m_modified(elem.m_modified)
+node::node(const node &n)
+ : m_parent(NULL),
+ m_children(n.m_children),
+ m_modified(n.m_modified)
{}
-element::element(const string &name, element *parent)
- : m_parent(parent),
- m_name(name),
- m_modified(true)
+node::~node()
{
- if (parent != NULL)
+ node_list::iterator i = m_children.begin();
+ for (; i != m_children.end(); ++i)
{
- parent->add_child(this);
+ delete (*i);
}
}
-element::~element()
+node *node::add_child(node *child)
+{
+ child->m_parent = this;
+ m_children.push_back(child);
+ set_modified(true);
+ return child;
+}
+
+string node::to_string(bool nice , int indent) const
+{
+ nice = nice;
+ indent = indent;
+ return string();
+}
+
+const node_list &node::children() const
+{
+ return m_children;
+}
+
+node *node::clone()
+{
+ node *n = new node;
+ n->m_children = clone_children();
+ n->m_modified = m_modified;
+ return n;
+}
+
+node_list node::clone_children() const
{
- //cout << __FUNCTION__ << " " << m_name << endl;
+ node_list clones;
- element_list::iterator ei = m_children.begin();
- for (; ei != m_children.end(); ++ei)
+ node_list::const_iterator i = m_children.begin();
+ for (; i != m_children.end(); ++i)
{
- delete (*ei);
+ clones.push_back((*i)->clone());
}
+
+ return clones;
+}
+
+bool node::modified() const
+{
+ return m_modified;
+}
+
+void node::set_modified(bool modified) const
+{
+ m_modified = modified;
+
+ if (modified && m_parent != NULL)
+ {
+ m_parent->set_modified(true);
+ }
+}
+
+// *******************************************************************
+
+comment::comment(node *parent)
+ : node(parent)
+{}
+
+comment::comment(const string &text, node *parent)
+ : node(parent)
+{
+ m_text = text;
}
-string element::to_string(bool nice, int indent)
+string comment::to_string(bool nice, int indent) const
{
- if (!m_modified)
+ string result;
+
+ if (nice)
+ result = string(indent, ' ');
+
+ result += string("<!-- ") + m_text + string(" -->");
+
+ return result;
+}
+
+// *******************************************************************
+
+element::element(node *parent)
+ : node(parent)
+{}
+
+element::element(const element &elem)
+ : node(elem),
+ m_attributes(elem.m_attributes),
+ m_name(elem.m_name),
+ m_text(elem.m_text),
+ m_cached(elem.m_cached)
+{}
+
+element::element(const string &name, node *parent)
+ : node(parent),
+ m_name(name)
+{}
+
+element::~element()
+{
+}
+
+string element::to_string(bool nice, int indent) const
+{
+ if (!modified())
return m_cached;
string xmlstr;
@@ -90,32 +173,28 @@ string element::to_string(bool nice, int indent)
xml << "<" << m_name;
- attribute_map::iterator ai = m_attributes.begin();
+ attribute_map::const_iterator ai = m_attributes.begin();
for (; ai != m_attributes.end(); ++ai)
{
xml << " " << ai->first << "=\"" << ai->second << "\"";
}
- if (m_children.empty() && m_text.empty())
+ if (children().empty() && m_text.empty())
{
xml << " />";
return xml.str();
}
- else if (nice)
- {
- xml << ">\n";
- }
else
{
- xml << ">";
+ xml << (nice ? ">\n" : ">");
}
- if (!m_children.empty())
+ if (!children().empty())
{
- element_list::const_iterator ei = m_children.begin();
- for (; ei != m_children.end(); ++ei)
+ node_list::const_iterator ni = children().begin();
+ for (; ni != children().end(); ++ni)
{
- xml << (*ei)->to_string(nice, indent + 2);
+ xml << (*ni)->to_string(nice, indent + 2);
if (nice)
xml << "\n";
@@ -141,21 +220,7 @@ string element::to_string(bool nice, int indent)
return xml.str();
}
-element *element::add_child(element *child)
-{
- child->m_parent = this;
- m_children.push_back(child);
- set_modified(true);
- return child;
-}
-
-element *element::add_child(const element &child)
-{
- element *c = new element(child);
- return add_child(c);
-}
-
-element *element::add_child(const std::string &tag)
+element *element::add_element(const std::string &tag)
{
return new element(tag, this);
}
@@ -175,30 +240,5 @@ template<> element *element::set_attr<>(const string &name,
return this;
}
-element_list element::clone_children() const
-{
- element_list clones;
-
- element_list::const_iterator ei = m_children.begin();
- for (; ei != m_children.end(); ++ei)
- {
- element *orig = *ei;
- element *clone = new element(*orig);
- clones.push_back(clone);
- }
-
- return clones;
-}
-
-void element::set_modified(bool modified)
-{
- m_modified = modified;
-
- if (modified && m_parent != NULL)
- {
- m_parent->set_modified(true);
- }
-}
-
} // namespace sxml