author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Oct 2019 19:53:46 +0200 | |
branch | v_0 |
changeset 4 | a0689654b3c2 |
parent 2 | 0d3eb5129582 |
child 7 | ff69af3c67a3 |
permissions | -rw-r--r-- |
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 |
4
a0689654b3c2
fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
0
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* 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
|
10 |
* 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
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* 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
|
15 |
* 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
|
16 |
*/ |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <cstdlib> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <iostream> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <string> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <sstream> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <vector> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <algorithm> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
#include <libxml++-2.6/libxml++/libxml++.h> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
#include <relpipe/writer/typedefs.h> |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
|
2
0d3eb5129582
convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
#include "Configuration.h" |
0
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
namespace relpipe { |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
namespace in { |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
namespace xmltable { |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
using namespace relpipe::writer; |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
class XMLCommand { |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
private: |
2
0d3eb5129582
convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
40 |
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
|
41 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
public: |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
|
2
0d3eb5129582
convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
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
|
45 |
std::shared_ptr<RelationalWriter> writer(Factory::create(output)); |
0
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
|
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
xmlpp::DomParser parser; |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
|
2
0d3eb5129582
convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
ns[prefix] = uri; |
0
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
} |
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
|
2
0d3eb5129582
convert arbitrary XML to one or more relations
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
// 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
|
66 |
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
|
67 |
} |
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 |
} |
0
a37196931f63
project skeleton, libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
} |
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 |
} |