src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 29 Nov 2020 00:30:36 +0100
branchv_0
changeset 30 f686bdaeb9e0
parent 29 06aaad12c207
child 31 c6527b45fbc2
permissions -rw-r--r--
add --parser-option

/**
 * 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 <stdexcept>
#include <libxml++-2.6/libxml++/libxml++.h>

#include "lib/uri.h"
#include "lib/INIReader.h"
#include "lib/BasicUnescapingProcessor.h"
#include "lib/BackspaceUnescapingProcessor.h"
#include "lib/JavaPropertiesUnescapingProcessor.h"
#include "lib/JavaPropertiesDialect.h"
#include "lib/XMLNameCodec.h"

using namespace relpipe::in::ini::lib;

namespace relpipe {
namespace in {
namespace xmltable {

class HierarchicalINIContentHandler : public INIContentHandler {
private:
	xmlpp::DomParser* domParser;
	XMLNameCodec nameCodec;
	xmlpp::Element* currentSection = nullptr;
public:

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

	virtual ~HierarchicalINIContentHandler() {
	}

	void startDocument() override {
		if (currentSection) throw std::out_of_range("Lunatic INI parser send us multiple documents.");
		currentSection = domParser->get_document()->create_root_node("ini");
	};

	void endDocument() override {
	};

	void startSection(const SectionStartEvent& event) override {
		currentSection = currentSection->add_child(nameCodec.encode(event.name));
		currentSection->set_attribute("type", "section");
		currentSection->set_attribute("name", event.name);
		if (event.comment.size()) currentSection->set_attribute("comment", event.comment);
		if (event.lineNumber >= 0) currentSection->set_attribute("line-number", std::to_string(event.lineNumber));
		if (event.eventNumber >= 0) currentSection->set_attribute("event-number", std::to_string(event.eventNumber));
	};

	void endSection() override {
		currentSection = currentSection->get_parent();
		if (currentSection == nullptr) throw std::out_of_range("Lunatic INI parser tried to end a section without starting it before.");
	};

	void entry(const EntryEvent& event) override {
		xmlpp::Element* entry = currentSection->add_child(nameCodec.encode(event.fullKey));
		entry->set_attribute("type", "entry");
		entry->set_attribute("key", event.key);
		entry->set_attribute("full-key", event.fullKey);
		if (event.subKey.size()) entry->set_attribute("sub-key", event.subKey);
		if (event.comment.size()) entry->set_attribute("comment", event.comment);
		if (event.lineNumber >= 0) entry->set_attribute("line-number", std::to_string(event.lineNumber));
		if (event.eventNumber >= 0) entry->set_attribute("event-number", std::to_string(event.eventNumber));
		entry->add_child_text(event.value);
	};

	void comment(const CommentEvent& event) override {
		xmlpp::Element* comment = currentSection->add_child("comment");
		comment->set_attribute("type", "comment");
		if (event.lineNumber >= 0) comment->set_attribute("line-number", std::to_string(event.lineNumber));
		if (event.eventNumber >= 0) comment->set_attribute("event-number", std::to_string(event.eventNumber));
		comment->add_child_text(event.comment);
	}

	void whitespace(const WhitespaceEvent& event) override {
		xmlpp::Element* comment = currentSection->add_child("whitespace");
		comment->set_attribute("type", "whitespace");
		if (event.lineNumber >= 0) comment->set_attribute("line-number", std::to_string(event.lineNumber));
		if (event.eventNumber >= 0) comment->set_attribute("event-number", std::to_string(event.eventNumber));
		comment->add_child_text(event.whitespace);
	}

};

// TODO: support also other styles/mappings e.g. <section/> and <entry/> with INI names only in the XML attributes (and thus without @type="section|entry")
// or map INI comments and whitespace to native XML comments and text nodes (but there will be no metadata like line/event numbers)
// TODO: optional namespaces (xmlns)

class XMLDocumentConstructor {
private:
	std::istream* input = nullptr;
	xmlpp::DomParser* parser = nullptr;
	std::shared_ptr<INIReader> reader;
public:

	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
		reader.reset(INIReader::create(*input));
		reader->addUnescapingProcessor(std::make_shared<BasicUnescapingProcessor>(), unescaping::Basic, true);
		reader->addUnescapingProcessor(std::make_shared<JavaPropertiesUnescapingProcessor>(), unescaping::JavaProperties, false);
		reader->addUnescapingProcessor(std::make_shared<BackspaceUnescapingProcessor>(), unescaping::Backspace, true);
		reader->addDialect(std::make_shared<JavaPropertiesDialect>(), dialect::JavaProperties, false);
	}

	void setOption(const std::string& uri, const std::string& value) {
		reader->setOption(uri, value);
	}

	void process() {
		HierarchicalINIContentHandler handler(parser);
		reader->addHandler(&handler);
		reader->process();
	}
};

}
}
}