diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-09 11:30:43 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-09 11:30:43 +0100 |
commit | 33caaa0d202d4d6a3abd1462f3d5650e9b6d506f (patch) | |
tree | 4aa7809c30b84afa6623c1cf759d5b7d8883725f | |
parent | 1a05a69ebfaed97a387ec5f5ccdada7e82409743 (diff) |
added a parent reference to XML nodes.
-rw-r--r-- | src/core/XML.hpp | 28 | ||||
-rw-r--r-- | test/core/XMLTest.cpp | 20 |
2 files changed, 32 insertions, 16 deletions
diff --git a/src/core/XML.hpp b/src/core/XML.hpp index 51ef6fd..b4b803d 100644 --- a/src/core/XML.hpp +++ b/src/core/XML.hpp @@ -51,15 +51,20 @@ namespace ousia { namespace xml { +class Element; + /** * Node is the common super-class of actual elements (tag-bounded) and text. * It specifies the pure virtual serialize() function that the subclasses * implement. */ class Node : public Managed { +private: + Owned<Element> parent; public: - Node(Manager &mgr) : Managed(mgr){}; + Node(Manager &mgr, Handle<Element> parent) + : Managed(mgr), parent(acquire(parent)){}; /** * This method writes an XML prolog and the XML representing the current @@ -69,7 +74,7 @@ public: * @param doctype enables you to add a prefix after the XML prolog * specifying the doctype. */ - void serialize(std::ostream &out, const std::string & doctype = ""); + void serialize(std::ostream &out, const std::string &doctype = ""); /** * This method just writes the XML representation of this node to the * output stream, without the XML prolog. @@ -79,6 +84,11 @@ public: * @param tabdepth the current tabdepth for prettier output. */ virtual void doSerialize(std::ostream &out, unsigned int tabdepth) = 0; + + /** + * @return the parent XML element of this node. + */ + Rooted<Element> getParent() const { return parent; } }; /** @@ -96,13 +106,16 @@ public: std::map<std::string, std::string> attributes; ManagedVector<Node> children; - Element(Manager &mgr, std::string name) : Node(mgr), name(std::move(name)) + Element(Manager &mgr, Handle<Element> parent, std::string name) + : Node(mgr, parent), name(std::move(name)) { } - Element(Manager &mgr, std::string name, + Element(Manager &mgr, Handle<Element> parent, std::string name, std::map<std::string, std::string> attributes) - : Node(mgr), name(std::move(name)), attributes(std::move(attributes)) + : Node(mgr, parent), + name(std::move(name)), + attributes(std::move(attributes)) { } @@ -120,7 +133,10 @@ class Text : public Node { public: const std::string text; - Text(Manager &mgr, std::string text) : Node(mgr), text(std::move(text)) {} + Text(Manager &mgr, Handle<Element> parent, std::string text) + : Node(mgr, parent), text(std::move(text)) + { + } /** * This just writes the text to the output. diff --git a/test/core/XMLTest.cpp b/test/core/XMLTest.cpp index 124b58d..53bd28d 100644 --- a/test/core/XMLTest.cpp +++ b/test/core/XMLTest.cpp @@ -29,24 +29,24 @@ TEST(Node, testSerialize) { Manager mgr; - Rooted<Element> html{new Element{mgr, "html"}}; - Rooted<Element> head{new Element{mgr, "head"}}; + Rooted<Element> html{new Element{mgr, {nullptr}, "html"}}; + Rooted<Element> head{new Element{mgr, html, "head"}}; html->children.push_back(head); - Rooted<Element> title{new Element{mgr, "title"}}; + Rooted<Element> title{new Element{mgr, head, "title"}}; head->children.push_back(title); - title->children.push_back(new Text(mgr, "my title")); - Rooted<Element> body{new Element{mgr, "body"}}; + title->children.push_back(new Text(mgr, title, "my title")); + Rooted<Element> body{new Element{mgr, html, "body"}}; html->children.push_back(body); // This div element contains our text. Rooted<Element> div{ - new Element{mgr, "div", {{"class", "content"}, {"id", "1"}}}}; + new Element{mgr, body, "div", {{"class", "content"}, {"id", "1"}}}}; body->children.push_back(div); - Rooted<Element> p{new Element{mgr, "p"}}; + Rooted<Element> p{new Element{mgr, div, "p"}}; div->children.push_back(p); - p->children.push_back(new Text(mgr, "my text")); - Rooted<Element> p2{new Element{mgr, "p"}}; + p->children.push_back(new Text(mgr, p, "my text")); + Rooted<Element> p2{new Element{mgr, div, "p"}}; div->children.push_back(p2); - p2->children.push_back(new Text(mgr, "my text")); + p2->children.push_back(new Text(mgr, p2, "my text")); // Now this is what we expect to see: std::string expected{ |