author | František Kučera <franta-hg@frantovo.cz> |
Sat, 29 May 2021 22:12:19 +0200 | |
branch | v_0 |
changeset 29 | 9254988f9382 |
parent 26 | 84ff7c97bfdc |
child 30 | f5ac2d29eeb4 |
permissions | -rw-r--r-- |
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
16
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
19 |
#include <codecvt> |
17
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
20 |
#include <vector> |
16
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
21 |
|
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <libxml++-2.6/libxml++/libxml++.h> |
29
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
23 |
#include <cbor.h> |
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
21 | 25 |
#include "XMLNameCodec.h" |
26 |
||
27 |
namespace relpipe { |
|
28 |
namespace in { |
|
29 |
namespace xmltable { |
|
30 |
||
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
class XMLDocumentConstructor { |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
private: |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
std::istream* input = nullptr; |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
xmlpp::DomParser* parser = nullptr; |
21 | 35 |
XMLNameCodec nameCodec; |
16
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
36 |
|
29
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
37 |
std::string rootName = "cbor"; |
17
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
38 |
xmlpp::Element* current; |
26
84ff7c97bfdc
add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
39 |
|
16
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
40 |
/** |
29
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
41 |
* Both CBOR and XML strings are in UTF-8. |
16
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
42 |
*/ |
29
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
43 |
const char* c2x(char* value) { // FIXME: cbor type |
16
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
44 |
return value ? (const char*) value : ""; |
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
45 |
} |
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
46 |
|
29
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
47 |
const Glib::ustring c2xname(char* value) { // FIXME: cbor type |
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
48 |
return nameCodec.encode(c2x(value)); |
17
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
49 |
} |
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
50 |
|
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
51 |
xmlpp::Element* parentOrSelf(xmlpp::Element* current) { |
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
52 |
return current->get_parent() == nullptr ? current : current->get_parent(); |
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
53 |
} |
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
54 |
|
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
public: |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
|
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) { |
29
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
58 |
// cbor_item_t * root = cbor_new_definite_map(2); |
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
59 |
// FIXME: initialize CBOR parser |
16
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
60 |
} |
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
61 |
|
3b197bf7a231
raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents:
14
diff
changeset
|
62 |
virtual ~XMLDocumentConstructor() { |
29
9254988f9382
CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
63 |
// FIXME: destroy CBOR parser |
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
} |
26
84ff7c97bfdc
add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
65 |
|
25
0e8a58946c48
add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
66 |
void setOption(const std::string& uri, const std::string& value) { |
26
84ff7c97bfdc
add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
67 |
if (uri == "root-name") rootName = value; |
84ff7c97bfdc
add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
68 |
else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“"); |
25
0e8a58946c48
add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents:
24
diff
changeset
|
69 |
} |
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
|
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
void process() { |
26
84ff7c97bfdc
add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents:
25
diff
changeset
|
72 |
current = parser->get_document()->create_root_node(rootName); |
14
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
} |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
}; |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
|
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
} |
5be268bc4c69
separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
} |
17
75c6685cceb9
YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents:
16
diff
changeset
|
78 |
} |