author | František Kučera <franta-hg@frantovo.cz> |
Sat, 01 Dec 2018 23:22:41 +0100 | |
branch | v_0 |
changeset 4 | 7e0211f00413 |
parent 3 | 878648aa663f |
child 8 | 0fb84b194a8f |
permissions | -rw-r--r-- |
3 | 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 |
*/ |
|
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#pragma once |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <string> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <vector> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <iostream> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sstream> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <locale> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <codecvt> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
#include <regex> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
#include <relpipe/reader/typedefs.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
#include <relpipe/reader/TypeId.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
namespace relpipe { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
namespace out { |
4 | 35 |
namespace xml { |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
using namespace relpipe::reader; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
class XmlHandler : public handlers::RelationalReaderStringHadler { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
private: |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8 |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
const char* INDENT = "\t"; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
std::ostream &output; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
std::vector<TypeId> columnTypes; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
std::vector<string_t> columnTypeCodes; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
std::vector<string_t> columnNames; |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
49 |
integer_t valueCount = 0; |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
integer_t columnCount = 0; |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
integer_t relationCount = 0; |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
const std::string escapeXmlText(const string_t &value) { |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
std::wstringstream result; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
for (auto & ch : value) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
switch (ch) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
case L'&': result << L"&"; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
break; |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
case L'<': result << L"<"; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
61 |
break; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
62 |
case L'>': result << L">"; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
63 |
break; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
64 |
case L'\'': result << L"'"; // TODO: escape ' and " only in attributes |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
65 |
break; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
66 |
case L'"': result << L"""; // TODO: escape ' and " only in attributes |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
67 |
break; |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
default: result << ch; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
|
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
72 |
return convertor.to_bytes(result.str()); |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
public: |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
XmlHandler(std::ostream& output) : output(output) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override { |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
81 |
// TODO: refactor and move common XML functions to relpipe-lib-xml |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
82 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
83 |
valueCount = 0; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
84 |
columnCount = 0; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
86 |
if (relationCount == 0) { |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
87 |
output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
88 |
output << "<pipe>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
89 |
// TODO: xmlns |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
90 |
} else { |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
91 |
output << INDENT << INDENT << "</record>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
92 |
output << INDENT << "</relation>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
93 |
} |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
94 |
relationCount++; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
95 |
output << INDENT << "<relation>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
96 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
97 |
output << INDENT << INDENT << "<name>" << escapeXmlText(name) << "</name>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
98 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
99 |
|
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
columnCount = attributes.size(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
columnTypes.resize(columnCount); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
columnTypeCodes.resize(columnCount); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
columnNames.resize(columnCount); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
for (int i = 0; i < attributes.size(); i++) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
columnNames[i] = attributes[i].getAttributeName(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
columnTypes[i] = attributes[i].getTypeId(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
columnTypeCodes[i] = attributes[i].getTypeName(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
} |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
109 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
110 |
// TODO: print attribute metadata |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
111 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
112 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
void attribute(const string_t& value) override { |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
114 |
integer_t i = valueCount % columnCount; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
115 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
116 |
if (i == 0 && valueCount) output << INDENT << INDENT << "</record>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
117 |
if (i == 0) output << INDENT << INDENT << "<record>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
118 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
119 |
valueCount++; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
120 |
|
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
121 |
// TODO: print attribute metadata (optional) |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
122 |
output << INDENT << INDENT << INDENT << "<attribute>"; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
123 |
output << escapeXmlText(value); |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
124 |
output << "</attribute>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
125 |
|
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
126 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
127 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
128 |
void endOfPipe() { |
2
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
129 |
if (valueCount) output << INDENT << INDENT << "</record>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
130 |
if (relationCount) output << INDENT << "</relation>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
131 |
output << "</pipe>" << std::endl; |
13a41e435ea0
quick and dirty XML output
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
132 |
|
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
133 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
134 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
135 |
}; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
136 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
137 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
138 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
139 |
} |