src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 30 Nov 2020 00:12:16 +0100
branchv_0
changeset 32 e72546725c77
parent 31 c6527b45fbc2
child 33 c9a158da6c32
permissions -rw-r--r--
add --parser-option root-name, enable-comments, enable-whitespace, enable-line-numbers, enable-event-numbers, enable-sub-keys

/**
 * 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;

	enum class TreeStyle {
		Standard,
		Literal,
	};

	TreeStyle parseTreeStyle(const std::string& name) {
		if (name == "standard") return TreeStyle::Standard;
		else if (name == "literal") return TreeStyle::Literal;
		else throw std::invalid_argument(std::string("Unknown tree style: ") + name);
	}

	TreeStyle treeStyle = TreeStyle::Literal;

	std::string rootName = "ini";
	bool enableLineNumbers = true;
	bool enableEventNumbers = true;
	bool enableSubKeys = true;
	bool enableComments = true;
	bool enableWhitespace = true;

	/**
	 * TODO: use a common method
	 */
	bool parseBoolean(const std::string& value) {
		if (value == "true") return true;
		else if (value == "false") return false;
		else throw std::invalid_argument(std::string("Unable to parse boolean value: ") + value + " (expecting true or false)");
	}

	bool treeWithNamespaces = false;

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(rootName, treeWithNamespaces ? xml::XMLNS : "");
	};

	void endDocument() override {
	};

	void startSection(const SectionStartEvent& event) override {
		currentSection = currentSection->add_child(treeStyle == TreeStyle::Literal ? nameCodec.encode(event.name) : "section");
		if (treeStyle == TreeStyle::Literal) 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 && enableLineNumbers) currentSection->set_attribute("line-number", std::to_string(event.lineNumber));
		if (event.eventNumber >= 0 && enableEventNumbers) 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(treeStyle == TreeStyle::Literal ? nameCodec.encode(event.fullKey) : "entry");
		if (treeStyle == TreeStyle::Literal) entry->set_attribute("type", "entry");
		entry->set_attribute("key", enableSubKeys ? event.key : event.fullKey);
		if (enableSubKeys)entry->set_attribute("full-key", event.fullKey);
		if (event.subKey.size() && enableSubKeys) entry->set_attribute("sub-key", event.subKey);
		if (event.comment.size()) entry->set_attribute("comment", event.comment);
		if (event.lineNumber >= 0 && enableLineNumbers) entry->set_attribute("line-number", std::to_string(event.lineNumber));
		if (event.eventNumber >= 0 && enableEventNumbers) 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");
		if (treeStyle == TreeStyle::Literal) comment->set_attribute("type", "comment");
		if (event.lineNumber >= 0 && enableLineNumbers) comment->set_attribute("line-number", std::to_string(event.lineNumber));
		if (event.eventNumber >= 0 && enableEventNumbers) comment->set_attribute("event-number", std::to_string(event.eventNumber));
		comment->add_child_text(event.comment);
	}

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

	bool setOption(const std::string& uri, const std::string& value) {
		if (uri == xml::TreeWithNamespaces) treeWithNamespaces = parseBoolean(value);
		else if (uri == xml::TreeStyle) treeStyle = parseTreeStyle(value);
		else if (uri == xml::RootName) rootName = value;
		else if (uri == xml::EnableWhitespace) enableWhitespace = parseBoolean(value);
		else if (uri == xml::EnableComments) enableComments = parseBoolean(value);
		else if (uri == xml::EnableSubKeys) enableSubKeys = parseBoolean(value);
		else if (uri == xml::EnableLineNumbers) enableLineNumbers = parseBoolean(value);
		else if (uri == xml::EnableEventNumbers) enableEventNumbers = parseBoolean(value);
		else return false;
		return true;
	}

};

// 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;
	std::shared_ptr<HierarchicalINIContentHandler> handler;
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);
		handler = std::make_shared<HierarchicalINIContentHandler>(parser);
	}

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

	void process() {
		reader->addHandler(handler.get());
		reader->process();
	}
};

}
}
}