/**
* 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/INIReader.h"
#include "lib/BasicUnescapingINIHandler.h"
#include "lib/JavaPropertiesUnescapingINIHandler.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;
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));
BasicUnescapingINIContentHandler unescapingHandler(handler, false);
JavaPropertiesUnescapingINIContentHandler javaHandler(handler, true);
reader->addHandler(&javaHandler);
reader->process();
}
};
}
}
}