author | František Kučera <franta-hg@frantovo.cz> |
Wed, 30 Dec 2020 00:39:03 +0100 | |
branch | v_0 |
changeset 1 | d6dbd5d50d43 |
parent 0 | 73e60c77be23 |
child 2 | 426054465916 |
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" |
|
37 |
||
38 |
namespace relpipe { |
|
39 |
namespace tr { |
|
40 |
namespace xpath { |
|
41 |
||
42 |
class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
43 |
private: |
|
44 |
shared_ptr<relpipe::writer::RelationalWriter> relationalWriter; |
|
45 |
Configuration configuration; |
|
46 |
RelationConfiguration* currentRelationConfiguration; |
|
47 |
std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata; |
|
48 |
std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata; |
|
49 |
size_t currentAttributeIndex = 0; |
|
1
d6dbd5d50d43
link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
|
d6dbd5d50d43
link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
xmlpp::DomParser dom; |
0 | 52 |
|
53 |
void copyInputAttributesToOutput() { |
|
54 |
for (auto rm : currentReaderMetadata) currentWriterMetadata.push_back({rm.getAttributeName(), relationalWriter->toTypeId(rm.getTypeName())}); |
|
55 |
} |
|
56 |
||
57 |
bool isPrependingInputAttributes() { |
|
58 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend |
|
59 |
|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0); |
|
60 |
} |
|
61 |
||
62 |
bool isAppendingInputAttributes() { |
|
63 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append; |
|
64 |
} |
|
65 |
||
66 |
public: |
|
67 |
||
68 |
XPathHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
69 |
} |
|
70 |
||
71 |
virtual ~XPathHandler() { |
|
72 |
} |
|
73 |
||
74 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
75 |
currentRelationConfiguration = nullptr; |
|
76 |
||
77 |
for (int i = 0; i < configuration.relationConfigurations.size(); i++) { |
|
78 |
if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) { |
|
79 |
currentRelationConfiguration = &configuration.relationConfigurations[i]; |
|
80 |
break; |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
currentReaderMetadata = attributes; |
|
85 |
currentWriterMetadata.clear(); |
|
86 |
||
87 |
if (currentRelationConfiguration == nullptr) { |
|
88 |
copyInputAttributesToOutput(); |
|
89 |
} else { |
|
90 |
if (isPrependingInputAttributes()) copyInputAttributesToOutput(); |
|
91 |
for (auto oa : currentRelationConfiguration->outputAttributes) currentWriterMetadata.push_back({oa.name, oa.type}); |
|
92 |
if (isAppendingInputAttributes()) copyInputAttributesToOutput(); |
|
93 |
||
94 |
// TODO: prepare DOM |
|
95 |
} |
|
96 |
||
97 |
relationalWriter->startRelation(name, currentWriterMetadata, true); |
|
98 |
} |
|
99 |
||
100 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
101 |
if (currentRelationConfiguration) { |
|
102 |
relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex]; |
|
103 |
||
104 |
// TODO: add attribute to DOM |
|
105 |
||
106 |
currentAttributeIndex++; |
|
107 |
||
108 |
if (currentAttributeIndex == currentReaderMetadata.size()) { |
|
109 |
||
110 |
// TODO: evaluate XPath expression |
|
111 |
// TODO: write record to output, if the XPath condition was met |
|
112 |
// TODO: clean record node in DOM |
|
113 |
currentAttributeIndex = 0; |
|
114 |
} |
|
115 |
} else { |
|
116 |
relationalWriter->writeAttribute(value); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
void endOfPipe() { |
|
121 |
||
122 |
} |
|
123 |
||
124 |
}; |
|
125 |
||
126 |
} |
|
127 |
} |
|
128 |
} |