src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 16 Jan 2021 16:36:39 +0100
branchv_0
changeset 0 ea26b3359fed
child 1 356d0b024abf
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
ea26b3359fed project skeleton
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
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <codecvt>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <vector>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <libxml++-2.6/libxml++/libxml++.h>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <vmime/vmime.hpp>
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include "XMLNameCodec.h"
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace relpipe {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace in {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
namespace xmltable {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
class XMLDocumentConstructor {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
private:
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	std::istream* input = nullptr;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	xmlpp::DomParser* parser = nullptr;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	XMLNameCodec nameCodec;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	enum class Mode {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
		ROOT,
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		SEQUENCE,
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		MAPPING,
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		MAP_KEY
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	};
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::string rootName = "mime";
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	xmlpp::Element* current;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	std::vector<Mode> mode;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	/**
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	 * Both MIME and XML strings are in UTF-8.
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	const char* y2x(mime_char_t* value) {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		return value ? (const char*) value : "";
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	const Glib::ustring y2xname(mime_char_t* value) {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		return nameCodec.encode(y2x(value));
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	 */
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	xmlpp::Element* parentOrSelf(xmlpp::Element* current) {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		return current->get_parent() == nullptr ? current : current->get_parent();
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
public:
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	virtual ~XMLDocumentConstructor() {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	void setOption(const std::string& uri, const std::string& value) {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		if (uri == "root-name") rootName = value;
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“");
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	void process() {
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		current = parser->get_document()->create_root_node(rootName);
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
};
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
}
ea26b3359fed project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
}