|
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, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 #pragma once |
|
19 |
|
20 #include <string> |
|
21 #include <vector> |
|
22 #include <iostream> |
|
23 #include <sstream> |
|
24 #include <locale> |
|
25 #include <codecvt> |
|
26 #include <regex> |
|
27 |
|
28 #include <relpipe/reader/typedefs.h> |
|
29 #include <relpipe/reader/TypeId.h> |
|
30 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
31 #include <relpipe/reader/handlers/AttributeMetadata.h> |
|
32 |
|
33 namespace relpipe { |
|
34 namespace out { |
|
35 namespace ods { |
|
36 |
|
37 using namespace relpipe::reader; |
|
38 |
|
39 class OdsHandler : public handlers::RelationalReaderStringHadler { |
|
40 private: |
|
41 std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8 |
|
42 const char* INDENT = "\t"; |
|
43 |
|
44 std::ostream &output; |
|
45 |
|
46 std::vector<TypeId> columnTypes; |
|
47 std::vector<string_t> columnTypeCodes; |
|
48 std::vector<string_t> columnNames; |
|
49 integer_t valueCount = 0; |
|
50 integer_t columnCount = 0; |
|
51 integer_t relationCount = 0; |
|
52 |
|
53 const std::string escapeXmlText(const string_t &value) { |
|
54 std::wstringstream result; |
|
55 |
|
56 for (auto & ch : value) { |
|
57 switch (ch) { |
|
58 case L'&': result << L"&"; |
|
59 break; |
|
60 case L'<': result << L"<"; |
|
61 break; |
|
62 case L'>': result << L">"; |
|
63 break; |
|
64 case L'\'': result << L"'"; // TODO: escape ' and " only in attributes |
|
65 break; |
|
66 case L'"': result << L"""; // TODO: escape ' and " only in attributes |
|
67 break; |
|
68 default: result << ch; |
|
69 } |
|
70 } |
|
71 |
|
72 return convertor.to_bytes(result.str()); |
|
73 } |
|
74 |
|
75 public: |
|
76 |
|
77 OdsHandler(std::ostream& output) : output(output) { |
|
78 } |
|
79 |
|
80 void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override { |
|
81 // TODO: refactor and move common XML functions to relpipe-lib-xml |
|
82 |
|
83 valueCount = 0; |
|
84 columnCount = 0; |
|
85 |
|
86 if (relationCount == 0) { |
|
87 output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl; |
|
88 output << "<pipe TODO=\"ods\">" << std::endl; |
|
89 // TODO: xmlns |
|
90 } else { |
|
91 output << INDENT << INDENT << "</record>" << std::endl; |
|
92 output << INDENT << "</relation>" << std::endl; |
|
93 } |
|
94 relationCount++; |
|
95 output << INDENT << "<relation>" << std::endl; |
|
96 |
|
97 output << INDENT << INDENT << "<name>" << escapeXmlText(name) << "</name>" << std::endl; |
|
98 |
|
99 |
|
100 columnCount = attributes.size(); |
|
101 columnTypes.resize(columnCount); |
|
102 columnTypeCodes.resize(columnCount); |
|
103 columnNames.resize(columnCount); |
|
104 for (int i = 0; i < attributes.size(); i++) { |
|
105 columnNames[i] = attributes[i].getAttributeName(); |
|
106 columnTypes[i] = attributes[i].getTypeId(); |
|
107 columnTypeCodes[i] = attributes[i].getTypeName(); |
|
108 } |
|
109 |
|
110 // TODO: print attribute metadata |
|
111 } |
|
112 |
|
113 void attribute(const string_t& value) override { |
|
114 integer_t i = valueCount % columnCount; |
|
115 |
|
116 if (i == 0 && valueCount) output << INDENT << INDENT << "</record>" << std::endl; |
|
117 if (i == 0) output << INDENT << INDENT << "<record>" << std::endl; |
|
118 |
|
119 valueCount++; |
|
120 |
|
121 // TODO: print attribute metadata (optional) |
|
122 output << INDENT << INDENT << INDENT << "<attribute>"; |
|
123 output << escapeXmlText(value); |
|
124 output << "</attribute>" << std::endl; |
|
125 |
|
126 } |
|
127 |
|
128 void endOfPipe() { |
|
129 if (valueCount) output << INDENT << INDENT << "</record>" << std::endl; |
|
130 if (relationCount) output << INDENT << "</relation>" << std::endl; |
|
131 output << "</pipe>" << std::endl; |
|
132 |
|
133 } |
|
134 |
|
135 }; |
|
136 |
|
137 } |
|
138 } |
|
139 } |