src/lib/DOMBuildingXMLContentHandler.h
branchv_0
changeset 9 7a6abdd00ab5
parent 7 afb7f3a4981a
--- a/src/lib/DOMBuildingXMLContentHandler.h	Sat Jun 12 21:29:18 2021 +0200
+++ b/src/lib/DOMBuildingXMLContentHandler.h	Sat Jun 12 22:37:44 2021 +0200
@@ -28,24 +28,35 @@
 class DOMBuildingXMLContentHandler : public XMLContentHandler {
 private:
 	xmlpp::Document* document;
+	xmlpp::Element* current = nullptr;
+	// TODO: check current == nullptr and throw exception
 
 public:
 
 	DOMBuildingXMLContentHandler(xmlpp::Document* document) : document(document) {
-		document->create_root_node("DOMBuildingXMLContentHandler"); // FIXME: real implementation
 	}
 
 	void writeStartElement(const std::string& name, const std::vector<std::string>& attributes) override {
+		if (current) current = current->add_child(name);
+		else current = document->create_root_node(name);
+
+		for (int i = 0; i < attributes.size(); i = i + 2) {
+			std::string attributeName = attributes[i];
+			std::string attributeValue = attributes[i + 1];
+			current->set_attribute(attributeName, attributeValue);
+		}
 	}
 
 	void writeEndElement() override {
+		current = current->get_parent();
 	}
 
-	void writeCharacters(const std::string& text) override {
+	void writeCharacters(const std::string& value) override {
+		current->add_child_text(value);
 	}
 
-	void writeComment(const std::string& text, bool addSpaces) override {
-		document->get_root_node()->add_child_comment(addSpaces ? " " + text + " " : text);
+	void writeComment(const std::string& value, bool addSpaces) override {
+		current->add_child_comment(addSpaces ? " " + value + " " : value);
 	}
 
 };