author | František Kučera <franta-hg@frantovo.cz> |
Wed, 30 Dec 2020 01:28:45 +0100 | |
branch | v_0 |
changeset 2 | 426054465916 |
parent 1 | d6dbd5d50d43 |
child 3 | 709abeb5f6d1 |
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; |
|
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
|
52 |
|
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 |
|
1
d6dbd5d50d43
link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
xmlpp::DomParser 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
|
55 |
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
|
56 |
relpipe::in::xmltable::XMLNameCodec xmlNameCodec; // TODO: move to a common library |
0 | 57 |
|
58 |
void copyInputAttributesToOutput() { |
|
59 |
for (auto rm : currentReaderMetadata) currentWriterMetadata.push_back({rm.getAttributeName(), relationalWriter->toTypeId(rm.getTypeName())}); |
|
60 |
} |
|
61 |
||
62 |
bool isPrependingInputAttributes() { |
|
63 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend |
|
64 |
|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0); |
|
65 |
} |
|
66 |
||
67 |
bool isAppendingInputAttributes() { |
|
68 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append; |
|
69 |
} |
|
70 |
||
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
|
71 |
void resetRecordElement() { |
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
|
72 |
if (recordElement) dom.get_document()->get_root_node()->remove_child(recordElement); |
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 |
recordElement = dom.get_document()->get_root_node()->add_child("record"); |
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
|
74 |
} |
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
|
75 |
|
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 |
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
|
77 |
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
|
78 |
} |
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 |
|
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 |
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
|
81 |
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
|
82 |
} |
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 |
|
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 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
} |
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 |
} |
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 |
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
|
94 |
for (auto oa : currentRelationConfiguration->outputAttributes) { |
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 |
auto value = recordElement->eval_to_string(s2x(oa.xpath)); |
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 |
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
|
97 |
} |
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 |
} |
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 |
|
0 | 100 |
public: |
101 |
||
102 |
XPathHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
103 |
} |
|
104 |
||
105 |
virtual ~XPathHandler() { |
|
106 |
} |
|
107 |
||
108 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
109 |
currentRelationConfiguration = nullptr; |
|
110 |
||
111 |
for (int i = 0; i < configuration.relationConfigurations.size(); i++) { |
|
112 |
if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) { |
|
113 |
currentRelationConfiguration = &configuration.relationConfigurations[i]; |
|
114 |
break; |
|
115 |
} |
|
116 |
} |
|
117 |
||
118 |
currentReaderMetadata = attributes; |
|
119 |
currentWriterMetadata.clear(); |
|
120 |
||
121 |
if (currentRelationConfiguration == nullptr) { |
|
122 |
copyInputAttributesToOutput(); |
|
123 |
} else { |
|
124 |
if (isPrependingInputAttributes()) copyInputAttributesToOutput(); |
|
125 |
for (auto oa : currentRelationConfiguration->outputAttributes) currentWriterMetadata.push_back({oa.name, oa.type}); |
|
126 |
if (isAppendingInputAttributes()) copyInputAttributesToOutput(); |
|
127 |
||
128 |
// TODO: prepare 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
|
129 |
dom.get_document()->create_root_node("relpipe-tr-xpath"); |
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
|
130 |
resetRecordElement(); |
0 | 131 |
} |
132 |
||
133 |
relationalWriter->startRelation(name, currentWriterMetadata, true); |
|
134 |
} |
|
135 |
||
136 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
137 |
if (currentRelationConfiguration) { |
|
138 |
relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex]; |
|
139 |
||
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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
attributeElement->add_child_text(s2x(value)); |
0 | 144 |
|
145 |
currentAttributeIndex++; |
|
146 |
||
147 |
if (currentAttributeIndex == currentReaderMetadata.size()) { |
|
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
|
148 |
if (currentRelationConfiguration->where.empty() || recordElement->eval_to_boolean(s2x(currentRelationConfiguration->where))) { |
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 |
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
|
150 |
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
|
151 |
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
|
152 |
} |
0 | 153 |
|
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
|
154 |
resetRecordElement(); |
0 | 155 |
currentAttributeIndex = 0; |
156 |
} |
|
157 |
} else { |
|
158 |
relationalWriter->writeAttribute(value); |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
void endOfPipe() { |
|
163 |
||
164 |
} |
|
165 |
||
166 |
}; |
|
167 |
||
168 |
} |
|
169 |
} |
|
170 |
} |