src/XMLDocumentConstructor.h
branchv_0
changeset 17 786977554fc3
parent 14 5be268bc4c69
child 18 45c06bdf9045
equal deleted inserted replaced
16:b9a3c806468a 17:786977554fc3
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    16  */
    17 #pragma once
    17 #pragma once
    18 
    18 
       
    19 #include <libxml++-2.6/libxml++/libxml++.h>
       
    20 
       
    21 #include "lib/INIReader.h"
       
    22 #include "lib/XMLNameCodec.h"
       
    23 
    19 namespace relpipe {
    24 namespace relpipe {
    20 namespace in {
    25 namespace in {
    21 namespace xmltable {
    26 namespace xmltable {
    22 
    27 
    23 #include <libxml++-2.6/libxml++/libxml++.h>
    28 class HierarchicalINIContentHandler : public INIContentHandler {
       
    29 private:
       
    30 	xmlpp::DomParser* domParser;
       
    31 	wstring_convert < codecvt_utf8<wchar_t>> convertor; // INI parser works with UTF-8
       
    32 public:
       
    33 
       
    34 	HierarchicalINIContentHandler(xmlpp::DomParser* domParser) : domParser(domParser) {
       
    35 	}
       
    36 
       
    37 	virtual ~HierarchicalINIContentHandler() {
       
    38 	}
       
    39 
       
    40 	void startDocument() override {
       
    41 		domParser->get_document()->create_root_node("ini");
       
    42 		domParser->get_document()->get_root_node()->add_child("startDocument");
       
    43 		// there should be only one document
       
    44 	};
       
    45 
       
    46 	void endDocument() override {
       
    47 		domParser->get_document()->get_root_node()->add_child("endDocument");
       
    48 	};
       
    49 
       
    50 	void startSection(const SectionStartEvent& event) override {
       
    51 		xmlpp::Element* node = domParser->get_document()->get_root_node()->add_child("startSection");
       
    52 		node->add_child("name")->add_child_text(event.name);
       
    53 		node->add_child("comment")->add_child_text(event.comment);
       
    54 		node->add_child("line-number")->add_child_text(std::to_string(event.lineNumber));
       
    55 		node->add_child("event-number")->add_child_text(std::to_string(event.eventNumber));
       
    56 	};
       
    57 
       
    58 	void endSection() override {
       
    59 		domParser->get_document()->get_root_node()->add_child("endSection");
       
    60 	};
       
    61 
       
    62 	void entry(const EntryEvent& event) override {
       
    63 		xmlpp::Element* node = domParser->get_document()->get_root_node()->add_child("entry");
       
    64 		node->add_child("key")->add_child_text(event.key);
       
    65 		node->add_child("sub-key")->add_child_text(event.subKey);
       
    66 		node->add_child("full-key")->add_child_text(event.fullKey);
       
    67 		node->add_child("value")->add_child_text(event.value);
       
    68 		node->add_child("comment")->add_child_text(event.comment);
       
    69 		node->add_child("line-number")->add_child_text(std::to_string(event.lineNumber));
       
    70 		node->add_child("event-number")->add_child_text(std::to_string(event.eventNumber));
       
    71 	};
       
    72 
       
    73 };
    24 
    74 
    25 class XMLDocumentConstructor {
    75 class XMLDocumentConstructor {
    26 private:
    76 private:
    27 	std::istream* input = nullptr;
    77 	std::istream* input = nullptr;
    28 	xmlpp::DomParser* parser = nullptr;
    78 	xmlpp::DomParser* parser = nullptr;
    30 
    80 
    31 	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
    81 	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
    32 	}
    82 	}
    33 
    83 
    34 	void process() {
    84 	void process() {
    35 		parser->parse_stream(*input);
    85 		HierarchicalINIContentHandler handler(parser);
       
    86 		std::shared_ptr<INIReader> reader(INIReader::create(*input));
       
    87 		reader->addHandler(&handler);
       
    88 		reader->process();
    36 	}
    89 	}
    37 };
    90 };
    38 
    91 
    39 }
    92 }
    40 }
    93 }