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 <iostream>
|
|
23 |
#include <sstream>
|
|
24 |
#include <locale>
|
|
25 |
#include <codecvt>
|
|
26 |
#include <regex>
|
|
27 |
#include <stdexcept>
|
|
28 |
|
|
29 |
#include <relpipe/common/type/typedefs.h>
|
|
30 |
#include <relpipe/reader/TypeId.h>
|
|
31 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
|
|
32 |
#include <relpipe/reader/handlers/AttributeMetadata.h>
|
|
33 |
|
|
34 |
#include "Configuration.h"
|
|
35 |
#include "INIWriter.h"
|
|
36 |
#include "INIStandardHandler.h"
|
|
37 |
#include "INILiteralHandler.h"
|
|
38 |
|
|
39 |
namespace relpipe {
|
|
40 |
namespace out {
|
|
41 |
namespace ini {
|
|
42 |
|
|
43 |
class INIDispatchHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
|
|
44 |
private:
|
|
45 |
std::ostream& output;
|
|
46 |
INIWriter writer;
|
|
47 |
Configuration& configuration;
|
|
48 |
std::shared_ptr<relpipe::reader::handlers::RelationalReaderStringHandler> currentHandler;
|
|
49 |
|
|
50 |
relpipe::reader::handlers::RelationalReaderStringHandler* createHandlerAuto(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) {
|
|
51 |
bool hasKey = false;
|
|
52 |
bool hasValue = false;
|
|
53 |
|
|
54 |
for (auto a : attributes) {
|
|
55 |
if (a.getAttributeName() == L"key") hasKey = true;
|
|
56 |
else if (a.getAttributeName() == L"value") hasValue = true;
|
|
57 |
}
|
|
58 |
|
|
59 |
if (hasKey && hasValue) return new INIStandardHandler(writer);
|
|
60 |
else return new INILiteralHandler(writer);
|
|
61 |
}
|
|
62 |
|
|
63 |
relpipe::reader::handlers::RelationalReaderStringHandler* createHandler(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) {
|
|
64 |
for (auto rc : configuration.relationConfigurations) {
|
|
65 |
if (std::regex_match(name, std::wregex(rc.relation))) {
|
|
66 |
if (rc.style == Style::Standard) return new INIStandardHandler(writer);
|
|
67 |
else if (rc.style == Style::Literal) return new INILiteralHandler(writer);
|
|
68 |
else if (rc.style == Style::LiteralWithSectionAttribute) return new INILiteralHandler(writer, L"section");
|
|
69 |
else if (rc.style == Style::Dropped) return nullptr;
|
|
70 |
else if (rc.style == Style::Automatic) return createHandlerAuto(name, attributes);
|
|
71 |
else throw std::invalid_argument("Unsupported style: " + std::to_string((int) rc.style));
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
if (configuration.relationConfigurations.size()) return nullptr;
|
|
76 |
else return createHandlerAuto(name, attributes);
|
|
77 |
}
|
|
78 |
|
|
79 |
public:
|
|
80 |
|
|
81 |
INIDispatchHandler(std::ostream& output, Configuration& configuration) : output(output), writer(output), configuration(configuration) {
|
|
82 |
for (auto o : configuration.writerOptions) writer.setOption(o.uri, o.value);
|
|
83 |
}
|
|
84 |
|
|
85 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
|
|
86 |
if (currentHandler) currentHandler->endOfPipe();
|
|
87 |
currentHandler.reset(createHandler(name, attributes));
|
|
88 |
if (currentHandler) currentHandler->startRelation(name, attributes);
|
|
89 |
}
|
|
90 |
|
|
91 |
void attribute(const relpipe::common::type::StringX& value) override {
|
|
92 |
if (currentHandler) currentHandler->attribute(value);
|
|
93 |
}
|
|
94 |
|
|
95 |
void endOfPipe() {
|
|
96 |
if (currentHandler) currentHandler->endOfPipe();
|
|
97 |
currentHandler.reset();
|
|
98 |
output.flush();
|
|
99 |
}
|
|
100 |
|
|
101 |
};
|
|
102 |
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|