author | František Kučera <franta-hg@frantovo.cz> |
Wed, 30 Dec 2020 10:33:24 +0100 | |
branch | v_0 |
changeset 6 | e498b3466342 |
parent 5 | 682c378bc4a2 |
child 7 | 7f2d09c3b1de |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
#pragma once |
|
18 |
||
19 |
#include <memory> |
|
20 |
#include <string> |
|
21 |
#include <vector> |
|
22 |
#include <codecvt> |
|
23 |
#include <regex> |
|
24 |
||
1
d6dbd5d50d43
link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
25 |
#include <libxml++-2.6/libxml++/libxml++.h> |
d6dbd5d50d43
link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
|
0 | 27 |
#include <relpipe/common/type/typedefs.h> |
28 |
#include <relpipe/reader/TypeId.h> |
|
29 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
30 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
31 |
||
32 |
#include <relpipe/writer/Factory.h> |
|
33 |
||
34 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
35 |
||
36 |
#include "Configuration.h" |
|
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
37 |
#include "XMLNameCodec.h" |
0 | 38 |
|
39 |
namespace relpipe { |
|
40 |
namespace tr { |
|
41 |
namespace xpath { |
|
42 |
||
43 |
class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
44 |
private: |
|
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
45 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML is in UTF-8 |
0 | 46 |
shared_ptr<relpipe::writer::RelationalWriter> relationalWriter; |
47 |
Configuration configuration; |
|
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
48 |
RelationConfiguration* currentRelationConfiguration = nullptr; |
0 | 49 |
std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata; |
50 |
std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata; |
|
51 |
size_t currentAttributeIndex = 0; |
|
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
52 |
size_t currentRecordNumber = 1; |
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
|
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
54 |
|
5
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
55 |
xmlpp::Node::PrefixNsMap xmlns; |
6
e498b3466342
use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
56 |
xmlpp::Document dom; |
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
57 |
xmlpp::Element* recordElement = nullptr; |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
58 |
relpipe::in::xmltable::XMLNameCodec xmlNameCodec; // TODO: move to a common library |
0 | 59 |
|
60 |
void copyInputAttributesToOutput() { |
|
61 |
for (auto rm : currentReaderMetadata) currentWriterMetadata.push_back({rm.getAttributeName(), relationalWriter->toTypeId(rm.getTypeName())}); |
|
62 |
} |
|
63 |
||
64 |
bool isPrependingInputAttributes() { |
|
65 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend |
|
66 |
|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0); |
|
67 |
} |
|
68 |
||
69 |
bool isAppendingInputAttributes() { |
|
70 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append; |
|
71 |
} |
|
72 |
||
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
73 |
void resetRecordElement() { |
6
e498b3466342
use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
74 |
if (recordElement) dom.get_root_node()->remove_child(recordElement); |
e498b3466342
use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
75 |
recordElement = dom.get_root_node()->add_child("record"); |
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
76 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
77 |
|
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
78 |
const Glib::ustring s2x(relpipe::common::type::StringX value) { |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
return Glib::ustring(convertor.to_bytes(value)); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
80 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
81 |
|
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
82 |
const relpipe::common::type::StringX x2s(const Glib::ustring& value) { |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
83 |
return convertor.from_bytes(value); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
84 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
|
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
86 |
void writeInputAttributes() { |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
87 |
for (xmlpp::Node* attributeNode : recordElement->get_children()) { |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
88 |
if (xmlpp::Element * attributeElement = dynamic_cast<xmlpp::Element*> (attributeNode)) { |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
89 |
auto value = attributeElement->get_child_text()->get_content(); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
90 |
relationalWriter->writeAttribute(x2s(value)); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
91 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
93 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
94 |
|
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
95 |
void writeOutputAttributes() { |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
96 |
for (auto oa : currentRelationConfiguration->outputAttributes) { |
5
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
97 |
auto value = recordElement->eval_to_string(s2x(oa.xpath), xmlns); |
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
98 |
relationalWriter->writeAttribute(x2s(value)); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
99 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
100 |
} |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
101 |
|
0 | 102 |
public: |
103 |
||
104 |
XPathHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
5
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
105 |
for (int i = 0; i < configuration.namespaceMappings.size(); i++) { |
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
106 |
std::string prefix = convertor.to_bytes(configuration.namespaceMappings[i]); |
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
107 |
std::string uri = convertor.to_bytes(configuration.namespaceMappings[++i]); |
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
108 |
xmlns[prefix] = uri; |
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
109 |
} |
0 | 110 |
} |
111 |
||
112 |
virtual ~XPathHandler() { |
|
113 |
} |
|
114 |
||
115 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
116 |
currentRelationConfiguration = nullptr; |
|
117 |
||
118 |
for (int i = 0; i < configuration.relationConfigurations.size(); i++) { |
|
119 |
if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) { |
|
120 |
currentRelationConfiguration = &configuration.relationConfigurations[i]; |
|
121 |
break; |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
currentReaderMetadata = attributes; |
|
126 |
currentWriterMetadata.clear(); |
|
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
127 |
currentRecordNumber = 1; |
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
128 |
recordElement = nullptr; |
0 | 129 |
|
130 |
if (currentRelationConfiguration == nullptr) { |
|
131 |
copyInputAttributesToOutput(); |
|
132 |
} else { |
|
133 |
if (isPrependingInputAttributes()) copyInputAttributesToOutput(); |
|
134 |
for (auto oa : currentRelationConfiguration->outputAttributes) currentWriterMetadata.push_back({oa.name, oa.type}); |
|
135 |
if (isAppendingInputAttributes()) copyInputAttributesToOutput(); |
|
136 |
||
6
e498b3466342
use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
137 |
dom.create_root_node("relpipe-tr-xpath"); |
e498b3466342
use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
138 |
dom.get_root_node()->add_child("relation-name")->add_child_text(s2x(name)); |
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
139 |
resetRecordElement(); |
0 | 140 |
} |
141 |
||
142 |
relationalWriter->startRelation(name, currentWriterMetadata, true); |
|
143 |
} |
|
144 |
||
145 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
146 |
if (currentRelationConfiguration) { |
|
147 |
relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex]; |
|
148 |
||
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
149 |
xmlpp::Element* attributeElement = recordElement->add_child(xmlNameCodec.encode(s2x(attributeMetadata.getAttributeName()))); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
150 |
attributeElement->set_attribute("name", s2x(attributeMetadata.getAttributeName())); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
151 |
attributeElement->set_attribute("type", s2x(attributeMetadata.getTypeName())); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
152 |
attributeElement->add_child_text(s2x(value)); |
0 | 153 |
|
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
154 |
if (currentAttributeIndex == 0) { |
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
155 |
recordElement->set_attribute("number", std::to_string(currentRecordNumber)); |
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
156 |
} |
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
157 |
|
0 | 158 |
currentAttributeIndex++; |
159 |
||
160 |
if (currentAttributeIndex == currentReaderMetadata.size()) { |
|
5
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
161 |
if (currentRelationConfiguration->where.empty() || recordElement->eval_to_boolean(s2x(currentRelationConfiguration->where), xmlns)) { |
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
162 |
if (isPrependingInputAttributes()) writeInputAttributes(); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
163 |
writeOutputAttributes(); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
164 |
if (isAppendingInputAttributes()) writeInputAttributes(); |
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
165 |
} |
0 | 166 |
|
2
426054465916
build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
167 |
resetRecordElement(); |
0 | 168 |
currentAttributeIndex = 0; |
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
169 |
currentRecordNumber++; |
0 | 170 |
} |
171 |
} else { |
|
172 |
relationalWriter->writeAttribute(value); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
void endOfPipe() { |
|
177 |
||
178 |
} |
|
179 |
||
180 |
}; |
|
181 |
||
182 |
} |
|
183 |
} |
|
184 |
} |