src/lib/DOMBuildingXMLContentHandler.h
branchv_0
changeset 9 7a6abdd00ab5
parent 7 afb7f3a4981a
equal deleted inserted replaced
8:d37c1a5d09ce 9:7a6abdd00ab5
    26 namespace lib {
    26 namespace lib {
    27 
    27 
    28 class DOMBuildingXMLContentHandler : public XMLContentHandler {
    28 class DOMBuildingXMLContentHandler : public XMLContentHandler {
    29 private:
    29 private:
    30 	xmlpp::Document* document;
    30 	xmlpp::Document* document;
       
    31 	xmlpp::Element* current = nullptr;
       
    32 	// TODO: check current == nullptr and throw exception
    31 
    33 
    32 public:
    34 public:
    33 
    35 
    34 	DOMBuildingXMLContentHandler(xmlpp::Document* document) : document(document) {
    36 	DOMBuildingXMLContentHandler(xmlpp::Document* document) : document(document) {
    35 		document->create_root_node("DOMBuildingXMLContentHandler"); // FIXME: real implementation
       
    36 	}
    37 	}
    37 
    38 
    38 	void writeStartElement(const std::string& name, const std::vector<std::string>& attributes) override {
    39 	void writeStartElement(const std::string& name, const std::vector<std::string>& attributes) override {
       
    40 		if (current) current = current->add_child(name);
       
    41 		else current = document->create_root_node(name);
       
    42 
       
    43 		for (int i = 0; i < attributes.size(); i = i + 2) {
       
    44 			std::string attributeName = attributes[i];
       
    45 			std::string attributeValue = attributes[i + 1];
       
    46 			current->set_attribute(attributeName, attributeValue);
       
    47 		}
    39 	}
    48 	}
    40 
    49 
    41 	void writeEndElement() override {
    50 	void writeEndElement() override {
       
    51 		current = current->get_parent();
    42 	}
    52 	}
    43 
    53 
    44 	void writeCharacters(const std::string& text) override {
    54 	void writeCharacters(const std::string& value) override {
       
    55 		current->add_child_text(value);
    45 	}
    56 	}
    46 
    57 
    47 	void writeComment(const std::string& text, bool addSpaces) override {
    58 	void writeComment(const std::string& value, bool addSpaces) override {
    48 		document->get_root_node()->add_child_comment(addSpaces ? " " + text + " " : text);
    59 		current->add_child_comment(addSpaces ? " " + value + " " : value);
    49 	}
    60 	}
    50 
    61 
    51 };
    62 };
    52 
    63 
    53 }
    64 }