src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 11 Jun 2021 18:59:03 +0200
branchv_0
changeset 43 6b194b178946
parent 42 c97b6152016a
permissions -rw-r--r--
constants for attribute names
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    21
#include <sstream>
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    22
#include <iomanip>
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    23
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#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
    25
#include <cbor.h>
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    27
#include <relpipe/writer/RelpipeWriterException.h>
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    28
21
053054f9f702 XML name escaping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    29
#include "XMLNameCodec.h"
053054f9f702 XML name escaping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    30
053054f9f702 XML name escaping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    31
namespace relpipe {
053054f9f702 XML name escaping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    32
namespace in {
053054f9f702 XML name escaping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    33
namespace xmltable {
053054f9f702 XML name escaping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    34
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
class XMLDocumentConstructor {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
private:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	std::istream* input = nullptr;
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	xmlpp::DomParser* parser = nullptr;
21
053054f9f702 XML name escaping
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
    39
	XMLNameCodec nameCodec;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    40
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    41
	enum class Mode {
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    42
		ROOT,
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    43
		ARRAY,
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    44
		MAP_KEY,
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    45
		MAP_VALUE,
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    46
		CHAR_STRING,
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    47
		BYTE_STRING
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    48
	};
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    49
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    50
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    51
	Glib::ustring rootName = "cbor";
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    52
	Glib::ustring itemName;
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    53
	xmlpp::Element* current;
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    54
	std::vector<Mode> mode;
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    55
	/**
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    56
	 * number of remainin items in the fixed-size container (map or array) at current tree-level
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    57
	 */
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    58
	std::vector<ssize_t> remainingItems;
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    59
#define INDEFINITE -1
26
84ff7c97bfdc add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
    60
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    61
#define ATTR_VALUE_TYPE "value-type"
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    62
#define ATTR_KEY_TYPE "key-type"
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    63
#define ATTR_TAG "tag"
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    64
#define ATTR_BYTES_READ "bytes-read"
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    65
36
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    66
	std::stringstream currentIndefiniteString;
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    67
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    68
	uint64_t currentTag;
39
77d585a1b98c improved map support
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
    69
	bool currentTagPresent = false;
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    70
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    71
	cbor_callbacks callbacks = cbor_empty_callbacks;
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    72
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    73
	/**
29
9254988f9382 CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    74
	 * 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
    75
	 */
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    76
	static std::string c2x(cbor_data value, uint64_t length) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    77
		return value && length > 0 ? std::string((const char*) value, length) : "";
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    78
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    79
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    80
	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
    81
		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
    82
	}
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    83
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    84
	void checkRemainingItems() {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    85
		if (mode.back() == Mode::ARRAY || mode.back() == Mode::MAP_KEY) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    86
			if (remainingItems.back() != INDEFINITE) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    87
				remainingItems.back()--;
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    88
				if (remainingItems.back() < 1) containerEnd();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    89
			}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    90
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    91
	}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    92
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    93
	void writeCurrentTag(xmlpp::Element* element) {
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    94
		if (currentTagPresent) {
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
    95
			element->set_attribute(ATTR_TAG, std::to_string(currentTag));
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    96
			currentTagPresent = false;
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    97
		}
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    98
	}
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    99
37
cae9eedf4180 null values: cborType=null, no additional flag needed
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   100
	void appendScalarValue(Glib::ustring value, Glib::ustring cborType) {
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   101
		if (mode.back() == Mode::ARRAY) {
35
5bfd1d389d5f encode XML element names properly
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   102
			xmlpp::Element* element = current->add_child(nameCodec.encode(itemName));
33
57cb51fb5212 add value-type and key-type attributes
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
   103
			element->add_child_text(value);
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   104
			element->set_attribute(ATTR_VALUE_TYPE, cborType);
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   105
			writeCurrentTag(element);
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   106
		} else if (mode.back() == Mode::MAP_KEY) {
35
5bfd1d389d5f encode XML element names properly
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   107
			current = current->add_child(nameCodec.encode(value));
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   108
			current->set_attribute(ATTR_KEY_TYPE, cborType);
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   109
			writeCurrentTag(current);
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   110
			mode.push_back(Mode::MAP_VALUE);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   111
		} else if (mode.back() == Mode::MAP_VALUE) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   112
			current->add_child_text(value);
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   113
			current->set_attribute(ATTR_VALUE_TYPE, cborType);
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   114
			writeCurrentTag(current);
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   115
			current = parentOrSelf(current);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   116
			mode.pop_back();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   117
		} else if (mode.back() == Mode::ROOT) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   118
			current->add_child_text(value);
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   119
			current->set_attribute(ATTR_VALUE_TYPE, cborType);
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   120
			writeCurrentTag(current);
36
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   121
		} else if (mode.back() == Mode::BYTE_STRING || mode.back() == Mode::CHAR_STRING) {
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   122
			currentIndefiniteString << value;
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   123
		} else {
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   124
			// TODO: throw exception?
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   125
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   126
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   127
		checkRemainingItems();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   128
	}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   129
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   130
	void arrayStart(ssize_t size) {
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   131
		if (mode.back() == Mode::ROOT) {
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   132
			current->set_attribute(ATTR_VALUE_TYPE, "array");
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   133
			itemName = "item";
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   134
		} else if (mode.back() == Mode::ARRAY) {
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   135
			current = current->add_child(nameCodec.encode(itemName));
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   136
			itemName = "item";
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   137
		} else if (mode.back() == Mode::MAP_VALUE) {
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   138
			xmlpp::Element* parent = current->get_parent();
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   139
			itemName = current->get_name();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   140
			parent->remove_child(current);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   141
			current = parent;
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   142
			mode.pop_back();
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   143
		} else {
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   144
			// Mode::BYTE_STRING and Mode::CHAR_STRING – make no sense
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   145
			// Mode::MAP_KEY – currently not supported
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   146
			throw relpipe::writer::RelpipeWriterException(L"Unsupported mode in arrayStart(): " + std::to_wstring((int) mode.back()));
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   147
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   148
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   149
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   150
		mode.push_back(Mode::ARRAY);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   151
		remainingItems.push_back(size);
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   152
		writeCurrentTag(current);
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   153
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   154
		if (size == 0) containerEnd();
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   155
	}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   156
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   157
	void mapStart(ssize_t size) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   158
		if (mode.back() == Mode::ROOT) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   159
		} else if (mode.back() == Mode::ARRAY) {
35
5bfd1d389d5f encode XML element names properly
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   160
			current = current->add_child(nameCodec.encode(itemName));
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   161
		} else if (mode.back() == Mode::MAP_VALUE) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   162
			mode.pop_back();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   163
			// TODO: remainingItems
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   164
		} else {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   165
			// TODO: map might be a key of another map → wrap/nest
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   166
			// …probably not
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   167
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   168
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   169
		current->set_attribute(ATTR_VALUE_TYPE, "map");
42
c97b6152016a nested maps
František Kučera <franta-hg@frantovo.cz>
parents: 41
diff changeset
   170
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   171
		mode.push_back(Mode::MAP_KEY);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   172
		remainingItems.push_back(size);
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   173
		writeCurrentTag(current);
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   174
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   175
		if (size == 0) containerEnd();
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   176
	}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   177
36
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   178
	void flushCurrentIndefiniteString(Glib::ustring cborType) {
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   179
		appendScalarValue(currentIndefiniteString.str(), cborType);
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   180
		currentIndefiniteString = std::stringstream();
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   181
	}
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   182
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   183
	void containerEnd() {
39
77d585a1b98c improved map support
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   184
		Mode m = mode.back();
77d585a1b98c improved map support
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   185
		mode.pop_back();
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   186
		remainingItems.pop_back();
40
503b77beed57 improved arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   187
41
b11458587f20 nested arrays
František Kučera <franta-hg@frantovo.cz>
parents: 40
diff changeset
   188
		if (m == Mode::MAP_KEY || m == Mode::MAP_VALUE || m == Mode::ARRAY) current = parentOrSelf(current);
39
77d585a1b98c improved map support
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   189
		else if (m == Mode::BYTE_STRING) flushCurrentIndefiniteString("byte-string");
77d585a1b98c improved map support
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   190
		else if (m == Mode::CHAR_STRING) flushCurrentIndefiniteString("string");
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   191
	}
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   192
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   193
public:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   194
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   196
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   197
#define CBOR_CALLBACK_START if (XMLDocumentConstructor* instance = static_cast<XMLDocumentConstructor*> (context)) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   198
#define CBOR_CALLBACK_END } else throw relpipe::writer::RelpipeWriterException(L"Invalid context in the CBOR callback.");
34
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   199
#define NUMERIC_CALLBACK(type, name) [](void* context, type value) { CBOR_CALLBACK_START instance->appendScalarValue(std::to_string(value), name); CBOR_CALLBACK_END }
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   200
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   201
		callbacks.array_start = [](void* context, size_t size) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   202
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   203
			instance->arrayStart(size);
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   204
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   205
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   206
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   207
		callbacks.boolean = [](void* context, bool value) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   208
			CBOR_CALLBACK_START
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   209
			instance->appendScalarValue(value ? "true" : "false", "boolean");
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   210
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   211
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   212
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   213
		callbacks.byte_string = [](void* context, cbor_data value, uint64_t size) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   214
			CBOR_CALLBACK_START
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   215
			std::stringstream hex;
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   216
			hex << std::hex << std::setfill('0') << std::setw(2);
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   217
			for (uint64_t i = 0; i < size; i++) hex << (int) value[i];
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   218
			instance->appendScalarValue(hex.str(), "byte-string");
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   219
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   220
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   221
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   222
		callbacks.byte_string_start = [](void* context) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   223
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   224
			instance->mode.push_back(Mode::BYTE_STRING);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   225
			instance->remainingItems.push_back(INDEFINITE);
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   226
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   227
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   228
34
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   229
		callbacks.float2 = NUMERIC_CALLBACK(float, "float2");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   230
		callbacks.float4 = NUMERIC_CALLBACK(float, "float4");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   231
		callbacks.float8 = NUMERIC_CALLBACK(double, "float8");
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   232
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   233
		callbacks.indef_array_start = [](void* context) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   234
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   235
			instance->arrayStart(INDEFINITE);
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   236
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   237
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   238
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   239
		callbacks.indef_map_start = [](void* context) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   240
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   241
			instance->mapStart(INDEFINITE);
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   242
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   243
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   244
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   245
		callbacks.indef_break = [](void* context) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   246
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   247
			instance->containerEnd();
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   248
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   249
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   250
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   251
		callbacks.map_start = [](void* context, size_t size) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   252
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   253
			instance->mapStart(size);
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   254
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   255
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   256
34
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   257
		callbacks.negint8 = NUMERIC_CALLBACK(uint8_t, "negative-int8");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   258
		callbacks.negint16 = NUMERIC_CALLBACK(uint16_t, "negative-int16");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   259
		callbacks.negint32 = NUMERIC_CALLBACK(uint32_t, "negative-int32");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   260
		callbacks.negint64 = NUMERIC_CALLBACK(uint64_t, "negative-int64");
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   261
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   262
		callbacks.null = [](void* context) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   263
			CBOR_CALLBACK_START
37
cae9eedf4180 null values: cborType=null, no additional flag needed
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   264
			instance->appendScalarValue("", "null");
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   265
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   266
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   267
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   268
		callbacks.string = [](void* context, cbor_data value, uint64_t size) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   269
			CBOR_CALLBACK_START
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   270
			instance->appendScalarValue(c2x(value, size), "string");
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   271
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   272
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   273
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   274
		callbacks.string_start = [](void* context) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   275
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   276
			instance->mode.push_back(Mode::CHAR_STRING);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   277
			instance->remainingItems.push_back(INDEFINITE);
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   278
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   279
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   280
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   281
		callbacks.tag = [](void* context, uint64_t value) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   282
			CBOR_CALLBACK_START
38
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   283
			instance->currentTag = value;
ba850b20fe0d basic tag support
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
   284
			instance->currentTagPresent = true;
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   285
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   286
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   287
34
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   288
		callbacks.uint8 = NUMERIC_CALLBACK(uint8_t, "uint8");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   289
		callbacks.uint16 = NUMERIC_CALLBACK(uint16_t, "uint16");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   290
		callbacks.uint32 = NUMERIC_CALLBACK(uint32_t, "uint32");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   291
		callbacks.uint64 = NUMERIC_CALLBACK(uint64_t, "uint64");
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   292
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   293
		callbacks.undefined = [](void* context) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   294
			CBOR_CALLBACK_START
37
cae9eedf4180 null values: cborType=null, no additional flag needed
František Kučera <franta-hg@frantovo.cz>
parents: 36
diff changeset
   295
			instance->appendScalarValue("", "undefined"); // TODO: throw exception?
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   296
			CBOR_CALLBACK_END
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   297
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   298
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   299
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   300
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   301
	virtual ~XMLDocumentConstructor() {
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
	}
26
84ff7c97bfdc add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   303
25
0e8a58946c48 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   304
	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
   305
		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
   306
		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
   307
	}
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   308
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   309
	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
   310
		current = parser->get_document()->create_root_node(rootName);
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   311
		mode.push_back(Mode::ROOT);
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   312
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   313
		// TODO: better streaming/buffering? (however, we still have to hold whole document in memory and „infinite“ stream processing is impossible)
30
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   314
		std::stringstream bufferStream;
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   315
		for (char ch = input->get(); input->good(); ch = input->get()) bufferStream.put(ch);
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   316
		std::string buffer = bufferStream.str();
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   317
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   318
		size_t bytesRead = 0;
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   319
		size_t length = buffer.size();
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   320
		for (cbor_decoder_result result; bytesRead < length; bytesRead += result.read) {
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   321
			result = cbor_stream_decode((cbor_data) buffer.c_str() + bytesRead, length - bytesRead, &callbacks, this);
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   322
			if (result.status != cbor_decoder_status::CBOR_DECODER_FINISHED) throw relpipe::writer::RelpipeWriterException(L"CBOR parsing failed: status = " + std::to_wstring(result.status));
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   323
		}
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   324
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   325
		checkRemainingItems();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   326
43
6b194b178946 constants for attribute names
František Kučera <franta-hg@frantovo.cz>
parents: 42
diff changeset
   327
		parser->get_document()->get_root_node()->set_attribute(ATTR_BYTES_READ, std::to_string(bytesRead));
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   328
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   329
};
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   330
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   331
}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   332
}
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   333
}