author | František Kučera <franta-hg@frantovo.cz> |
Sat, 04 Dec 2021 21:14:54 +0100 | |
branch | v_0 |
changeset 10 | 27c836487df3 |
parent 8 | 9f95cfd68f25 |
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> |
|
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
24 |
#include <stdexcept> |
0 | 25 |
|
1
d6dbd5d50d43
link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
26 |
#include <libxml++-2.6/libxml++/libxml++.h> |
d6dbd5d50d43
link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
|
0 | 28 |
#include <relpipe/common/type/typedefs.h> |
29 |
#include <relpipe/reader/TypeId.h> |
|
30 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
31 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
32 |
||
33 |
#include <relpipe/writer/Factory.h> |
|
34 |
||
35 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
36 |
||
37 |
#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
|
38 |
#include "XMLNameCodec.h" |
0 | 39 |
|
40 |
namespace relpipe { |
|
41 |
namespace tr { |
|
42 |
namespace xpath { |
|
43 |
||
44 |
class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
45 |
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
|
46 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML is in UTF-8 |
0 | 47 |
shared_ptr<relpipe::writer::RelationalWriter> relationalWriter; |
48 |
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
|
49 |
RelationConfiguration* currentRelationConfiguration = nullptr; |
0 | 50 |
std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata; |
51 |
std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata; |
|
52 |
size_t currentAttributeIndex = 0; |
|
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
53 |
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
|
54 |
|
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 |
|
5
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
relpipe::in::xmltable::XMLNameCodec xmlNameCodec; // TODO: move to a common library |
0 | 60 |
|
61 |
void copyInputAttributesToOutput() { |
|
62 |
for (auto rm : currentReaderMetadata) currentWriterMetadata.push_back({rm.getAttributeName(), relationalWriter->toTypeId(rm.getTypeName())}); |
|
63 |
} |
|
64 |
||
65 |
bool isPrependingInputAttributes() { |
|
66 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend |
|
67 |
|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0); |
|
68 |
} |
|
69 |
||
70 |
bool isAppendingInputAttributes() { |
|
71 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append; |
|
72 |
} |
|
73 |
||
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
|
74 |
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
|
75 |
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
|
76 |
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
|
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 |
|
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 |
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
|
80 |
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
|
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 |
|
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 |
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
|
84 |
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
|
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 |
|
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
87 |
bool isXmlAttribute(const relpipe::common::type::StringX& attributeName) { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
88 |
for (auto pattern : currentRelationConfiguration->xmlAttributes) if (std::regex_match(attributeName, std::wregex(pattern))) return true; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
89 |
return false; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
90 |
} |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
91 |
|
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
92 |
const relpipe::common::type::StringX formatRawXML(const relpipe::common::type::StringX& rawXML) { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
93 |
// TODO: move to a common library (used also in relpipe-in-xmltable) |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
94 |
std::wregex pattern(L"^<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>\n|\n$"); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
95 |
return std::regex_replace(rawXML, pattern, L""); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
96 |
} |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
97 |
|
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
98 |
const relpipe::common::type::StringX serialize(xmlpp::Element* element, bool asXml) { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
99 |
if (element) { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
100 |
if (asXml) { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
101 |
xmlpp::Document d; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
102 |
d.create_root_node_by_import(element, true); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
103 |
return formatRawXML(x2s(d.write_to_string())); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
104 |
} else { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
105 |
return element->get_child_text() ? x2s(element->get_child_text()->get_content()) : L""; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
106 |
} |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
107 |
} else { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
108 |
return L""; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
109 |
} |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
110 |
} |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
111 |
|
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
112 |
xmlpp::Element* findSingleElement(const xmlpp::NodeSet& nodeset) { |
8 | 113 |
// TODO: Allow multiple elements and attributes and wrap them? Like in relpipe-in-xmltable --raw-xml-attribute-wrapper --raw-xml-nodelist-wrapper |
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
114 |
if (nodeset.empty()) return nullptr; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
115 |
else if (nodeset.size() > 1) throw std::invalid_argument("XPath should find one or zero elements."); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
116 |
else if (xmlpp::Element * element = dynamic_cast<xmlpp::Element*> (nodeset[0])) return element; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
117 |
else if (nodeset[0]->get_path() == "/") return findSingleElement(nodeset[0]->find("*")); // support also "/" not only "/*" expressions (return root element in both cases) |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
118 |
else throw std::invalid_argument("XPath should find an element, not other kinds of nodes."); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
119 |
} |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
120 |
|
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
|
121 |
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
|
122 |
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
|
123 |
if (xmlpp::Element * attributeElement = dynamic_cast<xmlpp::Element*> (attributeNode)) { |
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
124 |
bool asXml = isXmlAttribute(x2s(attributeElement->get_attribute("name")->get_value())); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
125 |
relationalWriter->writeAttribute(serialize(asXml ? dynamic_cast<xmlpp::Element*> (attributeElement->get_first_child()) : attributeElement, asXml)); |
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
|
126 |
} |
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
|
127 |
} |
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
|
128 |
} |
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 |
|
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 |
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
|
131 |
for (auto oa : currentRelationConfiguration->outputAttributes) { |
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
132 |
relpipe::common::type::StringX value; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
133 |
if (isXmlAttribute(oa.name)) value = serialize(findSingleElement(recordElement->find(s2x(oa.xpath), xmlns)), true); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
134 |
else value = x2s(recordElement->eval_to_string(s2x(oa.xpath), xmlns)); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
135 |
relationalWriter->writeAttribute(value); |
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
|
136 |
} |
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
|
137 |
} |
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
|
138 |
|
0 | 139 |
public: |
140 |
||
141 |
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
|
142 |
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
|
143 |
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
|
144 |
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
|
145 |
xmlns[prefix] = uri; |
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
146 |
} |
0 | 147 |
} |
148 |
||
149 |
virtual ~XPathHandler() { |
|
150 |
} |
|
151 |
||
152 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
153 |
currentRelationConfiguration = nullptr; |
|
154 |
||
155 |
for (int i = 0; i < configuration.relationConfigurations.size(); i++) { |
|
156 |
if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) { |
|
157 |
currentRelationConfiguration = &configuration.relationConfigurations[i]; |
|
158 |
break; |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
currentReaderMetadata = attributes; |
|
163 |
currentWriterMetadata.clear(); |
|
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
164 |
currentRecordNumber = 1; |
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
165 |
recordElement = nullptr; |
0 | 166 |
|
167 |
if (currentRelationConfiguration == nullptr) { |
|
168 |
copyInputAttributesToOutput(); |
|
169 |
} else { |
|
170 |
if (isPrependingInputAttributes()) copyInputAttributesToOutput(); |
|
171 |
for (auto oa : currentRelationConfiguration->outputAttributes) currentWriterMetadata.push_back({oa.name, oa.type}); |
|
172 |
if (isAppendingInputAttributes()) copyInputAttributesToOutput(); |
|
173 |
||
8 | 174 |
// TODO: better metadata structure |
175 |
// TODO: optional namespaces |
|
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
|
176 |
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
|
177 |
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
|
178 |
resetRecordElement(); |
0 | 179 |
} |
180 |
||
181 |
relationalWriter->startRelation(name, currentWriterMetadata, true); |
|
182 |
} |
|
183 |
||
184 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
185 |
if (currentRelationConfiguration) { |
|
186 |
relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex]; |
|
187 |
||
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
188 |
// TODO: Parallel processing of records like in relpipe-in-filesystem? Or share common code with the XPath streamlet? (streamlets are parallelized) |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
189 |
|
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
|
190 |
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
|
191 |
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
|
192 |
attributeElement->set_attribute("type", s2x(attributeMetadata.getTypeName())); |
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
193 |
if (isXmlAttribute(attributeMetadata.getAttributeName())) { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
194 |
if (value.size()) { |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
195 |
xmlpp::DomParser attributeParser; |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
196 |
attributeParser.parse_memory(s2x(value)); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
197 |
attributeElement->import_node(attributeParser.get_document()->get_root_node(), true); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
198 |
} |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
199 |
} else { |
8 | 200 |
// TODO: better boolean mapping? Missing text node will be evaluated as false(), however the expression still had to be "someAttribute/text()" because "someAttribute" will be evaluated as true() because the "someAttribute" element is present. |
7
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
201 |
attributeElement->add_child_text(s2x(value)); |
7f2d09c3b1de
add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
202 |
} |
0 | 203 |
|
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
204 |
if (currentAttributeIndex == 0) { |
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
205 |
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
|
206 |
} |
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
207 |
|
0 | 208 |
currentAttributeIndex++; |
209 |
||
210 |
if (currentAttributeIndex == currentReaderMetadata.size()) { |
|
5
682c378bc4a2
add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents:
4
diff
changeset
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
} |
0 | 216 |
|
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
|
217 |
resetRecordElement(); |
0 | 218 |
currentAttributeIndex = 0; |
3
709abeb5f6d1
add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
219 |
currentRecordNumber++; |
0 | 220 |
} |
221 |
} else { |
|
222 |
relationalWriter->writeAttribute(value); |
|
223 |
} |
|
224 |
} |
|
225 |
||
226 |
void endOfPipe() { |
|
227 |
||
228 |
} |
|
229 |
||
230 |
}; |
|
231 |
||
232 |
} |
|
233 |
} |
|
234 |
} |