src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 22 Nov 2020 17:39:10 +0100
branchv_0
changeset 17 786977554fc3
parent 14 5be268bc4c69
child 18 45c06bdf9045
permissions -rw-r--r--
dump raw INI parser events

/**
 * Relational pipes
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <libxml++-2.6/libxml++/libxml++.h>

#include "lib/INIReader.h"
#include "lib/XMLNameCodec.h"

namespace relpipe {
namespace in {
namespace xmltable {

class HierarchicalINIContentHandler : public INIContentHandler {
private:
	xmlpp::DomParser* domParser;
	wstring_convert < codecvt_utf8<wchar_t>> convertor; // INI parser works with UTF-8
public:

	HierarchicalINIContentHandler(xmlpp::DomParser* domParser) : domParser(domParser) {
	}

	virtual ~HierarchicalINIContentHandler() {
	}

	void startDocument() override {
		domParser->get_document()->create_root_node("ini");
		domParser->get_document()->get_root_node()->add_child("startDocument");
		// there should be only one document
	};

	void endDocument() override {
		domParser->get_document()->get_root_node()->add_child("endDocument");
	};

	void startSection(const SectionStartEvent& event) override {
		xmlpp::Element* node = domParser->get_document()->get_root_node()->add_child("startSection");
		node->add_child("name")->add_child_text(event.name);
		node->add_child("comment")->add_child_text(event.comment);
		node->add_child("line-number")->add_child_text(std::to_string(event.lineNumber));
		node->add_child("event-number")->add_child_text(std::to_string(event.eventNumber));
	};

	void endSection() override {
		domParser->get_document()->get_root_node()->add_child("endSection");
	};

	void entry(const EntryEvent& event) override {
		xmlpp::Element* node = domParser->get_document()->get_root_node()->add_child("entry");
		node->add_child("key")->add_child_text(event.key);
		node->add_child("sub-key")->add_child_text(event.subKey);
		node->add_child("full-key")->add_child_text(event.fullKey);
		node->add_child("value")->add_child_text(event.value);
		node->add_child("comment")->add_child_text(event.comment);
		node->add_child("line-number")->add_child_text(std::to_string(event.lineNumber));
		node->add_child("event-number")->add_child_text(std::to_string(event.eventNumber));
	};

};

class XMLDocumentConstructor {
private:
	std::istream* input = nullptr;
	xmlpp::DomParser* parser = nullptr;
public:

	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
	}

	void process() {
		HierarchicalINIContentHandler handler(parser);
		std::shared_ptr<INIReader> reader(INIReader::create(*input));
		reader->addHandler(&handler);
		reader->process();
	}
};

}
}
}