src/XMLTableCommand.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 20 Feb 2021 20:32:56 +0100
branchv_0
changeset 0 2f783f0573fa
child 3 b30965489f42
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
2f783f0573fa 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
2f783f0573fa 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
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
2f783f0573fa 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,
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
2f783f0573fa 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
2f783f0573fa 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/>.
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <cstdlib>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <string>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <sstream>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <vector>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <algorithm>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <exception>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <libxml++-2.6/libxml++/libxml++.h>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/typedefs.h>
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include "Configuration.h"
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "XMLDocumentConstructor.h"
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace in {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace xmltable {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe::writer;
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
class XMLCommand {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
private:
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	string_t formatRawXML(string_t rawXML) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		std::wregex pattern(L"^<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>\n|\n$");
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		return std::regex_replace(rawXML, pattern, L"");
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	void importNode(xmlpp::Node* parent, xmlpp::Node* child, AttributeRecipe attributeRecipe) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		if (dynamic_cast<xmlpp::AttributeNode*> (child)) parent->add_child_with_new_ns(
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
				convertor.to_bytes(attributeRecipe.rawXmlAttributeWrapper.name),
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
				convertor.to_bytes(attributeRecipe.rawXmlAttributeWrapper.uri),
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
				convertor.to_bytes(attributeRecipe.rawXmlAttributeWrapper.prefix))->import_node(child);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		else parent->import_node(child, true);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	void importNode(xmlpp::Document* document, xmlpp::Node* child, AttributeRecipe attributeRecipe) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		if (dynamic_cast<xmlpp::AttributeNode*> (child)) document->create_root_node(
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
				convertor.to_bytes(attributeRecipe.rawXmlAttributeWrapper.name),
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
				convertor.to_bytes(attributeRecipe.rawXmlAttributeWrapper.uri),
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
				convertor.to_bytes(attributeRecipe.rawXmlAttributeWrapper.prefix))->import_node(child);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		else document->create_root_node_by_import(child, true);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	string_t toRawXML(xmlpp::Node* parent, AttributeRecipe attributeRecipe, xmlpp::Node::PrefixNsMap ns) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		xmlpp::Document d;
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		xmlpp::NodeSet nodes = parent->find(convertor.to_bytes(attributeRecipe.xpath), ns);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		if (attributeRecipe.rawXmlNodeListWrapper.name.size()) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			d.create_root_node(
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
					convertor.to_bytes(attributeRecipe.rawXmlNodeListWrapper.name),
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
					convertor.to_bytes(attributeRecipe.rawXmlNodeListWrapper.uri),
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
					convertor.to_bytes(attributeRecipe.rawXmlNodeListWrapper.prefix));
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
			for (xmlpp::Node* node : nodes) importNode(d.get_root_node(), node, attributeRecipe);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		} else {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			if (nodes.size() == 1) importNode(&d, nodes[0], attributeRecipe);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			else if (nodes.size() > 1) throw std::invalid_argument("Multiple nodes found where only one was expected. Use nodelist wrapper."); // TODO: better relpipe exception
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			else return L""; // TODO: null
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		return formatRawXML(convertor.from_bytes(d.write_to_string()));
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
	}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
public:
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	void process(std::istream& input, std::ostream& output, Configuration& configuration) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		xmlpp::DomParser parser;
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		XMLDocumentConstructor documentConstructor(&input, &parser);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		for (ParserOptionRecipe o : configuration.parserOptions) documentConstructor.setOption(convertor.to_bytes(o.uri), convertor.to_bytes(o.value));
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		documentConstructor.process();
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		if (configuration.xinclude) parser.get_document()->process_xinclude(true);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		xmlpp::Element* root = parser.get_document()->get_root_node();
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
		xmlpp::Node::PrefixNsMap ns;
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		for (int i = 0; i < configuration.namespaceMappings.size(); i++) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
			std::string prefix = convertor.to_bytes(configuration.namespaceMappings[i]);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
			std::string uri = convertor.to_bytes(configuration.namespaceMappings[++i]);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
			ns[prefix] = uri;
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		for (const RelationConfiguration& r : configuration.relationConfigurations) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
			std::vector<relpipe::writer::AttributeMetadata> attributesMetadata;
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
			for (AttributeRecipe a : r.attributes) attributesMetadata.push_back(AttributeMetadata{a.name, a.type});
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
			relpipe::writer::string_t name = r.nameIsXPath ? convertor.from_bytes(root->eval_to_string(convertor.to_bytes(r.relation), ns)) : r.relation;
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
			writer->startRelation(name, attributesMetadata, true);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
			for (xmlpp::Node* n : root->find(convertor.to_bytes(r.xpath), ns)) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
				for (AttributeRecipe a : r.attributes) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
					// TODO: convert to bytes only once
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
					std::string attributeXpath = convertor.to_bytes(a.xpath);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
					if (a.mode == Mode::STRING) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
						writer->writeAttribute(convertor.from_bytes(n->eval_to_string(attributeXpath, ns)));
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
					} else if (a.mode == Mode::BOOLEAN) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
						writer->writeAttribute(n->eval_to_boolean(attributeXpath, ns) ? L"true" : L"false");
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
					} else if (a.mode == Mode::LINE_NUMBER) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
						xmlpp::NodeSet attributeNodes = n->find(attributeXpath, ns);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
						string_t line = attributeNodes.size() ? std::to_wstring(attributeNodes[0]->get_line()) : L""; // TODO: null
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
						writer->writeAttribute(line);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
					} else if (a.mode == Mode::XPATH) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
						xmlpp::NodeSet attributeNodes = n->find(attributeXpath, ns);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
						string_t line = attributeNodes.size() ? convertor.from_bytes(attributeNodes[0]->get_path()) : L""; // TODO: null
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
						writer->writeAttribute(line);
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
					} else if (a.mode == Mode::RAW_XML) {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
						writer->writeAttribute(toRawXML(n, a, ns));
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
					} else {
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
						throw logic_error("Unsupported mode."); // should never happer, TODO: better relpipe exception
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
					}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
				}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
			}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
		}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
	}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
};
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
}
2f783f0573fa project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
}