author | František Kučera <franta-hg@frantovo.cz> |
Sun, 13 Dec 2020 16:47:25 +0100 | |
branch | v_0 |
changeset 7 | bda9d3c83cb7 |
parent 5 | bee7acb57330 |
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 <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" |
|
1
e04e5bbc147b
add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
#include "INISectionFirstHandler.h" |
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
39 |
#include "JavaPropertiesDialect.h" |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
40 |
#include "JavaManifestMFDialect.h" |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
41 |
#include "JavaPropertiesEscapingProcessor.h" |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
42 |
#include "BasicEscapingProcessor.h" |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
43 |
#include "uri.h" |
0 | 44 |
|
45 |
namespace relpipe { |
|
46 |
namespace out { |
|
47 |
namespace ini { |
|
48 |
||
49 |
class INIDispatchHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
50 |
private: |
|
51 |
std::ostream& output; |
|
52 |
INIWriter writer; |
|
53 |
Configuration& configuration; |
|
54 |
std::shared_ptr<relpipe::reader::handlers::RelationalReaderStringHandler> currentHandler; |
|
55 |
||
56 |
relpipe::reader::handlers::RelationalReaderStringHandler* createHandlerAuto(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) { |
|
57 |
bool hasKey = false; |
|
58 |
bool hasValue = false; |
|
59 |
||
60 |
for (auto a : attributes) { |
|
61 |
if (a.getAttributeName() == L"key") hasKey = true; |
|
62 |
else if (a.getAttributeName() == L"value") hasValue = true; |
|
63 |
} |
|
64 |
||
65 |
if (hasKey && hasValue) return new INIStandardHandler(writer); |
|
66 |
else return new INILiteralHandler(writer); |
|
67 |
} |
|
68 |
||
69 |
relpipe::reader::handlers::RelationalReaderStringHandler* createHandler(const relpipe::common::type::StringX& name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) { |
|
70 |
for (auto rc : configuration.relationConfigurations) { |
|
71 |
if (std::regex_match(name, std::wregex(rc.relation))) { |
|
72 |
if (rc.style == Style::Standard) return new INIStandardHandler(writer); |
|
73 |
else if (rc.style == Style::Literal) return new INILiteralHandler(writer); |
|
1
e04e5bbc147b
add --style section-first
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
else if (rc.style == Style::SectionFirst) return new INISectionFirstHandler(writer); |
0 | 75 |
else if (rc.style == Style::Dropped) return nullptr; |
76 |
else if (rc.style == Style::Automatic) return createHandlerAuto(name, attributes); |
|
77 |
else throw std::invalid_argument("Unsupported style: " + std::to_string((int) rc.style)); |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
if (configuration.relationConfigurations.size()) return nullptr; |
|
82 |
else return createHandlerAuto(name, attributes); |
|
83 |
} |
|
84 |
||
85 |
public: |
|
86 |
||
87 |
INIDispatchHandler(std::ostream& output, Configuration& configuration) : output(output), writer(output), configuration(configuration) { |
|
7
bda9d3c83cb7
improved basic escaping
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
88 |
// TODO: dialects might be moved to configuration (after switching to alt2xml writers) |
3
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
89 |
writer.addDialect(std::make_shared<JavaPropertiesDialect>(), dialect::JavaProperties, false); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
90 |
writer.addDialect(std::make_shared<JavaManifestMFDialect>(), dialect::JavaManifestMF, false); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
91 |
writer.addEscapingProcessor(std::make_shared<BasicEscapingProcessor>(), escaping::Basic, true); |
ae8775e0bc7a
configurable/modular dialects and escaping
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
writer.addEscapingProcessor(std::make_shared<JavaPropertiesEscapingProcessor>(), escaping::JavaProperties, false); |
0 | 93 |
for (auto o : configuration.writerOptions) writer.setOption(o.uri, o.value); |
94 |
} |
|
95 |
||
96 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
97 |
if (currentHandler) currentHandler->endOfPipe(); |
|
98 |
currentHandler.reset(createHandler(name, attributes)); |
|
99 |
if (currentHandler) currentHandler->startRelation(name, attributes); |
|
100 |
} |
|
101 |
||
102 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
103 |
if (currentHandler) currentHandler->attribute(value); |
|
104 |
} |
|
105 |
||
106 |
void endOfPipe() { |
|
107 |
if (currentHandler) currentHandler->endOfPipe(); |
|
108 |
currentHandler.reset(); |
|
5
bee7acb57330
implement --writer-option allow-sections false (flat key=value output)
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
109 |
writer.endDocument(); |
0 | 110 |
output.flush(); |
111 |
} |
|
112 |
||
113 |
}; |
|
114 |
||
115 |
} |
|
116 |
} |
|
117 |
} |