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 |
|
|
25 |
#include <relpipe/common/type/typedefs.h>
|
|
26 |
#include <relpipe/reader/TypeId.h>
|
|
27 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
|
|
28 |
#include <relpipe/reader/handlers/AttributeMetadata.h>
|
|
29 |
|
|
30 |
#include <relpipe/writer/Factory.h>
|
|
31 |
|
|
32 |
#include <relpipe/cli/RelpipeCLIException.h>
|
|
33 |
|
|
34 |
#include "Configuration.h"
|
|
35 |
|
|
36 |
namespace relpipe {
|
|
37 |
namespace tr {
|
|
38 |
namespace xpath {
|
|
39 |
|
|
40 |
class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
|
|
41 |
private:
|
|
42 |
shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
|
|
43 |
Configuration configuration;
|
|
44 |
RelationConfiguration* currentRelationConfiguration;
|
|
45 |
std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
|
|
46 |
std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
|
|
47 |
size_t currentAttributeIndex = 0;
|
|
48 |
|
|
49 |
void copyInputAttributesToOutput() {
|
|
50 |
for (auto rm : currentReaderMetadata) currentWriterMetadata.push_back({rm.getAttributeName(), relationalWriter->toTypeId(rm.getTypeName())});
|
|
51 |
}
|
|
52 |
|
|
53 |
bool isPrependingInputAttributes() {
|
|
54 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend
|
|
55 |
|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0);
|
|
56 |
}
|
|
57 |
|
|
58 |
bool isAppendingInputAttributes() {
|
|
59 |
return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append;
|
|
60 |
}
|
|
61 |
|
|
62 |
public:
|
|
63 |
|
|
64 |
XPathHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
|
|
65 |
}
|
|
66 |
|
|
67 |
virtual ~XPathHandler() {
|
|
68 |
}
|
|
69 |
|
|
70 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
|
|
71 |
currentRelationConfiguration = nullptr;
|
|
72 |
|
|
73 |
for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
|
|
74 |
if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) {
|
|
75 |
currentRelationConfiguration = &configuration.relationConfigurations[i];
|
|
76 |
break;
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
currentReaderMetadata = attributes;
|
|
81 |
currentWriterMetadata.clear();
|
|
82 |
|
|
83 |
if (currentRelationConfiguration == nullptr) {
|
|
84 |
copyInputAttributesToOutput();
|
|
85 |
} else {
|
|
86 |
if (isPrependingInputAttributes()) copyInputAttributesToOutput();
|
|
87 |
for (auto oa : currentRelationConfiguration->outputAttributes) currentWriterMetadata.push_back({oa.name, oa.type});
|
|
88 |
if (isAppendingInputAttributes()) copyInputAttributesToOutput();
|
|
89 |
|
|
90 |
// TODO: prepare DOM
|
|
91 |
}
|
|
92 |
|
|
93 |
relationalWriter->startRelation(name, currentWriterMetadata, true);
|
|
94 |
}
|
|
95 |
|
|
96 |
void attribute(const relpipe::common::type::StringX& value) override {
|
|
97 |
if (currentRelationConfiguration) {
|
|
98 |
relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex];
|
|
99 |
|
|
100 |
// TODO: add attribute to DOM
|
|
101 |
|
|
102 |
currentAttributeIndex++;
|
|
103 |
|
|
104 |
if (currentAttributeIndex == currentReaderMetadata.size()) {
|
|
105 |
|
|
106 |
// TODO: evaluate XPath expression
|
|
107 |
// TODO: write record to output, if the XPath condition was met
|
|
108 |
// TODO: clean record node in DOM
|
|
109 |
currentAttributeIndex = 0;
|
|
110 |
}
|
|
111 |
} else {
|
|
112 |
relationalWriter->writeAttribute(value);
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
void endOfPipe() {
|
|
117 |
|
|
118 |
}
|
|
119 |
|
|
120 |
};
|
|
121 |
|
|
122 |
}
|
|
123 |
}
|
|
124 |
}
|