author | František Kučera <franta-hg@frantovo.cz> |
Wed, 23 Dec 2020 18:22:55 +0100 | |
branch | v_0 |
changeset 1 | af15c47f77ae |
parent 0 | c52ca92aa593 |
child 2 | 6839c85a9621 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* Relational pipes |
|
3 |
* Copyright © 2018 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 |
||
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 |
namespace relpipe { |
|
33 |
namespace out { |
|
34 |
namespace yaml { |
|
35 |
||
36 |
class YAMLHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { |
|
37 |
private: |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
38 |
const char ESC = '\\'; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
const char Q = '"'; |
0 | 40 |
std::ostream& output; |
41 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // we generate YAML always in UTF-8 like XML? |
|
42 |
std::vector<relpipe::reader::handlers::AttributeMetadata> currentAttributes; |
|
43 |
size_t currentAttributeIndex = 0; |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
44 |
bool firstRecord; |
0 | 45 |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
46 |
std::string escape(const relpipe::common::type::StringX& value) { |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
47 |
std::stringstream result; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
48 |
result.put(Q); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
49 |
for (char ch : convertor.to_bytes(value)) { |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
50 |
if (ch == Q) result.put(ESC).put(ch); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
else result.put(ch); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
} |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
53 |
result.put(Q); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
54 |
return result.str(); |
0 | 55 |
} |
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
|
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
57 |
std::string indent(int level) { |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
std::stringstream result; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
for (int i = 0; i < level; i++) result << " "; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
return result.str(); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
} |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
|
0 | 63 |
public: |
64 |
||
65 |
YAMLHandler(std::ostream& output) : output(output) { |
|
66 |
} |
|
67 |
||
68 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
69 |
currentAttributes = attributes; |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
currentAttributeIndex = 0; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
71 |
output << escape(name); |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
output << ":" << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
output << indent(1) << R"("attribute-metadata":)" << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
for (auto am : attributes) output << indent(2) << R"(- "name": )" << escape(am.getAttributeName()) << std::endl << indent(2) << R"( "type": )" << escape(am.getTypeName()) << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
firstRecord = true; |
0 | 76 |
} |
77 |
||
78 |
void attribute(const relpipe::common::type::StringX& value) override { |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
if (firstRecord) { |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
output << indent(1) << R"("record":)" << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
firstRecord = false; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
} |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
|
0 | 84 |
if (currentAttributeIndex % currentAttributes.size() == 0) { |
85 |
currentAttributeIndex = 0; |
|
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
output << indent(2) << "- "; |
0 | 87 |
} else { |
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
output << indent(2) << " "; |
0 | 89 |
} |
90 |
||
1
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
output << escape(currentAttributes[currentAttributeIndex].getAttributeName()) << ": "; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
output << escape(value) << std::endl; |
af15c47f77ae
convert data and metadata
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
|
0 | 94 |
currentAttributeIndex++; |
95 |
} |
|
96 |
||
97 |
void endOfPipe() { |
|
98 |
output.flush(); |
|
99 |
} |
|
100 |
||
101 |
}; |
|
102 |
||
103 |
} |
|
104 |
} |
|
105 |
} |