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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    19
#include <stdexcept>
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    20
#include <libxml++-2.6/libxml++/libxml++.h>
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    21
29
06aaad12c207 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    22
#include "lib/uri.h"
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    23
#include "lib/INIReader.h"
28
0e7c57d48d1e configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    24
#include "lib/BasicUnescapingProcessor.h"
0e7c57d48d1e configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    25
#include "lib/BackspaceUnescapingProcessor.h"
0e7c57d48d1e configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    26
#include "lib/JavaPropertiesUnescapingProcessor.h"
29
06aaad12c207 configurable dialects: in separate classes
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    27
#include "lib/JavaPropertiesDialect.h"
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    28
#include "lib/XMLNameCodec.h"
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    29
26
80e129ec3408 new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    30
using namespace relpipe::in::ini::lib;
80e129ec3408 new INI parser
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    31
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace relpipe {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace in {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace xmltable {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    36
class HierarchicalINIContentHandler : public INIContentHandler {
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    37
private:
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    38
	xmlpp::DomParser* domParser;
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    39
	XMLNameCodec nameCodec;
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    40
	xmlpp::Element* currentSection = nullptr;
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    41
public:
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    42
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    43
	HierarchicalINIContentHandler(xmlpp::DomParser* domParser) : domParser(domParser) {
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    44
	}
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    45
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    46
	virtual ~HierarchicalINIContentHandler() {
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    47
	}
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    48
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    49
	void startDocument() override {
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    50
		if (currentSection) throw std::out_of_range("Lunatic INI parser send us multiple documents.");
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    51
		currentSection = domParser->get_document()->create_root_node("ini");
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    52
	};
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    53
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    54
	void endDocument() override {
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    55
	};
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    56
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    57
	void startSection(const SectionStartEvent& event) override {
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    58
		currentSection = currentSection->add_child(nameCodec.encode(event.name));
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    59
		currentSection->set_attribute("type", "section");
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    60
		currentSection->set_attribute("name", event.name);
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    61
		if (event.comment.size()) currentSection->set_attribute("comment", event.comment);
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    62
		if (event.lineNumber >= 0) currentSection->set_attribute("line-number", std::to_string(event.lineNumber));
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    63
		if (event.eventNumber >= 0) currentSection->set_attribute("event-number", std::to_string(event.eventNumber));
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    64
	};
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    65
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    66
	void endSection() override {
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    67
		currentSection = currentSection->get_parent();
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    68
		if (currentSection == nullptr) throw std::out_of_range("Lunatic INI parser tried to end a section without starting it before.");
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    69
	};
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    70
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    71
	void entry(const EntryEvent& event) override {
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    72
		xmlpp::Element* entry = currentSection->add_child(nameCodec.encode(event.fullKey));
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    73
		entry->set_attribute("type", "entry");
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    74
		entry->set_attribute("key", event.key);
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    75
		entry->set_attribute("full-key", event.fullKey);
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    76
		if (event.subKey.size()) entry->set_attribute("sub-key", event.subKey);
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    77
		if (event.comment.size()) entry->set_attribute("comment", event.comment);
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    78
		if (event.lineNumber >= 0) entry->set_attribute("line-number", std::to_string(event.lineNumber));
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    79
		if (event.eventNumber >= 0) entry->set_attribute("event-number", std::to_string(event.eventNumber));
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    80
		entry->add_child_text(event.value);
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    81
	};
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    82
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    83
	void comment(const CommentEvent& event) override {
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    84
		xmlpp::Element* comment = currentSection->add_child("comment");
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    85
		comment->set_attribute("type", "comment");
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    86
		if (event.lineNumber >= 0) comment->set_attribute("line-number", std::to_string(event.lineNumber));
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    87
		if (event.eventNumber >= 0) comment->set_attribute("event-number", std::to_string(event.eventNumber));
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    88
		comment->add_child_text(event.comment);
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    89
	}
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    90
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    91
	void whitespace(const WhitespaceEvent& event) override {
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    92
		xmlpp::Element* comment = currentSection->add_child("whitespace");
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    93
		comment->set_attribute("type", "whitespace");
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    94
		if (event.lineNumber >= 0) comment->set_attribute("line-number", std::to_string(event.lineNumber));
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    95
		if (event.eventNumber >= 0) comment->set_attribute("event-number", std::to_string(event.eventNumber));
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    96
		comment->add_child_text(event.whitespace);
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    97
	}
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    98
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    99
};
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   101
// 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")
19
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   102
// or map INI comments and whitespace to native XML comments and text nodes (but there will be no metadata like line/event numbers)
90f2b8ca32bf improved support for comments and whitespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   103
// TODO: optional namespaces (xmlns)
18
45c06bdf9045 produce hierarchical structure of INI sections and entries
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   104
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
class XMLDocumentConstructor {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
private:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	std::istream* input = nullptr;
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
	xmlpp::DomParser* parser = nullptr;
30
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   109
	std::shared_ptr<INIReader> reader;
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
public:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
30
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   113
		reader.reset(INIReader::create(*input));
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   114
		reader->addUnescapingProcessor(std::make_shared<BasicUnescapingProcessor>(), unescaping::Basic, true);
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   115
		reader->addUnescapingProcessor(std::make_shared<JavaPropertiesUnescapingProcessor>(), unescaping::JavaProperties, false);
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   116
		reader->addUnescapingProcessor(std::make_shared<BackspaceUnescapingProcessor>(), unescaping::Backspace, true);
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   117
		reader->addDialect(std::make_shared<JavaPropertiesDialect>(), dialect::JavaProperties, false);
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   118
	}
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   119
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   120
	void setOption(const std::string& uri, const std::string& value) {
f686bdaeb9e0 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   121
		reader->setOption(uri, value);
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
	void process() {
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   125
		HierarchicalINIContentHandler handler(parser);
28
0e7c57d48d1e configurable unescaping processors
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   126
		reader->addHandler(&handler);
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   127
		reader->process();
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
};
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
}
17
786977554fc3 dump raw INI parser events
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   133
}