src/XMLTableCommand.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 24 Jul 2019 21:29:56 +0200
branchv_0
changeset 2 0d3eb5129582
parent 0 a37196931f63
child 4 a0689654b3c2
permissions -rw-r--r--
convert arbitrary XML to one or more relations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
a37196931f63 project skeleton, libxml2
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
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <cstdlib>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <iostream>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <string>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <vector>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <algorithm>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <libxml++-2.6/libxml++/libxml++.h>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/writer/typedefs.h>
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
2
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    31
#include "Configuration.h"
0
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace relpipe {
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace in {
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace xmltable {
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
using namespace relpipe::writer;
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
class XMLCommand {
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
private:
2
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
0
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
public:
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
2
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
	void process(std::istream& input, std::ostream& output, Configuration& configuration) {
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
0
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		xmlpp::DomParser parser;
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		parser.parse_stream(input);
2
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
		xmlpp::Element* root = parser.get_document()->get_root_node();
0
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
2
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
		xmlpp::Node::PrefixNsMap ns;
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
		for (int i = 0; i < configuration.namespaceMappings.size(); i++) {
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    54
			std::string prefix = convertor.to_bytes(configuration.namespaceMappings[i]);
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    55
			std::string uri = convertor.to_bytes(configuration.namespaceMappings[++i]);
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
			ns[prefix] = uri;
0
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		}
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
2
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
		for (const RelationConfiguration& r : configuration.relationConfigurations) {
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
			std::vector<relpipe::writer::AttributeMetadata> attributesMetadata;
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
			for (AttributeRecipe a : r.attributes) attributesMetadata.push_back(AttributeMetadata{a.name, a.type});
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
			relpipe::writer::string_t name = r.nameIsXPath ? convertor.from_bytes(root->eval_to_string(convertor.to_bytes(r.relation), ns)) : r.relation;
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
			writer->startRelation(name, attributesMetadata, true);
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
			for (xmlpp::Node* n : root->find(convertor.to_bytes(r.xpath), ns)) {
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
				for (AttributeRecipe a : r.attributes) {
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
					// TODO: convert to bytes only once
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
					writer->writeAttribute(convertor.from_bytes(n->eval_to_string(convertor.to_bytes(a.xpath), ns)));
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
				}
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
			}
0d3eb5129582 convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
		}
0
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	}
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
};
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
}
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
}
a37196931f63 project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}