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