author | František Kučera <franta-hg@frantovo.cz> |
Sun, 16 Sep 2018 14:39:10 +0200 | |
branch | v_0 |
changeset 1 | 82ba555a97d1 |
child 2 | 13a41e435ea0 |
permissions | -rw-r--r-- |
1
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
#pragma once |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
#include <string> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
#include <vector> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
#include <iostream> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
#include <sstream> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
#include <locale> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
#include <codecvt> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
#include <regex> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
#include <relpipe/reader/typedefs.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
#include <relpipe/reader/TypeId.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
namespace relpipe { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
namespace out { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
namespace tabular { |
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 |
using namespace relpipe::reader; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
class XmlHandler : public handlers::RelationalReaderStringHadler { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
private: |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
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
|
25 |
const char* INDENT = "\t"; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
std::ostream &output; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
std::vector<TypeId> columnTypes; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
std::vector<string_t> columnTypeCodes; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
std::vector<string_t> columnNames; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
std::vector<integer_t> columnWidths; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
std::vector<string_t> values; // all values are saved here and processed at the end of the relation |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
integer_t columnCount = 0; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
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 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
const string_t escapeXmlText(const string_t &value, const char* color) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
std::wstringstream result; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
for (auto & ch : value) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
switch (ch) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
// FIXME: xml escaping |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
case L'&': result << L"&"; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
break; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
default: result << ch; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
return result.str(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
public: |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
XmlHandler(std::ostream& output) : output(output) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
output << convertor.to_bytes(name) << ": (XML)" << endl; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
columnCount = attributes.size(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
columnTypes.resize(columnCount); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
columnTypeCodes.resize(columnCount); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
columnNames.resize(columnCount); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
columnWidths.resize(columnCount, 0); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
for (int i = 0; i < attributes.size(); i++) { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
columnNames[i] = attributes[i].getAttributeName(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
columnTypes[i] = attributes[i].getTypeId(); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
columnTypeCodes[i] = attributes[i].getTypeName(); |
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 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
void attribute(const string_t& value) override { |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
integer_t i = values.size() % columnCount; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
values.push_back(value); |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
columnWidths[i] = max(columnWidths[i], value.length()); |
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 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
void endOfPipe() { |
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 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
}; |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
|
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
} |
82ba555a97d1
relpipe-out-xml skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
} |