src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 06 Jun 2021 11:43:18 +0200
branchv_0
changeset 36 c97810ccfdef
parent 35 5bfd1d389d5f
child 37 cae9eedf4180
permissions -rw-r--r--
indefinite byte and char strings
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
36
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    61
	std::stringstream currentIndefiniteString;
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
    62
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
    63
	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
    64
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    65
	/**
29
9254988f9382 CBOR skeleton: remove YAML library, add CBOR one
František Kučera <franta-hg@frantovo.cz>
parents: 26
diff changeset
    66
	 * 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
    67
	 */
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
    68
	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
    69
		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
    70
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    71
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    72
	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
    73
		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
    74
	}
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    75
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    76
	void checkRemainingItems() {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    77
		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
    78
			if (remainingItems.back() != INDEFINITE) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    79
				remainingItems.back()--;
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    80
				if (remainingItems.back() < 1) containerEnd();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    81
			}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    82
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    83
	}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    84
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
    85
	void appendScalarValue(Glib::ustring value, Glib::ustring cborType, bool isNull = false) {
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    86
		// TODO: null
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    87
		if (mode.back() == Mode::ARRAY) {
35
5bfd1d389d5f encode XML element names properly
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
    88
			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
    89
			element->add_child_text(value);
57cb51fb5212 add value-type and key-type attributes
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    90
			element->set_attribute("value-type", cborType);
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    91
		} 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
    92
			current = current->add_child(nameCodec.encode(value));
33
57cb51fb5212 add value-type and key-type attributes
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    93
			current->set_attribute("key-type", cborType);
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    94
			mode.push_back(Mode::MAP_VALUE);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    95
		} else if (mode.back() == Mode::MAP_VALUE) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    96
			current->add_child_text(value);
33
57cb51fb5212 add value-type and key-type attributes
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
    97
			current->set_attribute("value-type", cborType);
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    98
			current = parentOrSelf(current);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
    99
			mode.pop_back();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   100
		} else if (mode.back() == Mode::ROOT) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   101
			current->add_child_text(value);
36
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   102
		} 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
   103
			currentIndefiniteString << value;
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   104
		} else {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   105
			// TODO: process YAML_SCALAR_EVENT
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   106
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   107
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   108
		checkRemainingItems();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   109
	}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   110
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   111
	void arrayStart(ssize_t size) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   112
		xmlpp::Element* parent = current->get_parent();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   113
		if (parent) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   114
			itemName = current->get_name();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   115
			parent->remove_child(current);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   116
			current = parent;
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   117
		} else {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   118
			itemName = "item";
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   119
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   120
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   121
		if (mode.back() == Mode::MAP_KEY) mode.pop_back();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   122
		mode.push_back(Mode::ARRAY);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   123
		remainingItems.push_back(size);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   124
	}
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
	void mapStart(ssize_t size) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   127
		if (mode.back() == Mode::ROOT) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   128
		} 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
   129
			current = 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
   130
			current->set_attribute("value-type", "map");
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   131
		} else if (mode.back() == Mode::MAP_VALUE) {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   132
			mode.pop_back();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   133
			// TODO: remainingItems
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   134
		} else {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   135
			// 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
   136
			// …probably not
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   137
		}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   138
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   139
		mode.push_back(Mode::MAP_KEY);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   140
		remainingItems.push_back(size);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   141
	}
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   142
36
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   143
	void flushCurrentIndefiniteString(Glib::ustring cborType) {
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   144
		mode.pop_back();
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   145
		appendScalarValue(currentIndefiniteString.str(), cborType);
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   146
		currentIndefiniteString = std::stringstream();
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   147
	}
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   148
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   149
	void containerEnd() {
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   150
		remainingItems.pop_back();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   151
		if (mode.back() == Mode::MAP_KEY || mode.back() == Mode::MAP_VALUE) current = parentOrSelf(current);
36
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   152
		else if (mode.back() == Mode::BYTE_STRING) flushCurrentIndefiniteString("byte-string");
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   153
		else if (mode.back() == Mode::CHAR_STRING) flushCurrentIndefiniteString("string");
c97810ccfdef indefinite byte and char strings
František Kučera <franta-hg@frantovo.cz>
parents: 35
diff changeset
   154
		else mode.pop_back(); // TODO: assert map/array
31
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   155
	}
273faff8b848 pass all scalar values through a single method
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   156
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
public:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
	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
   160
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   161
#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
   162
#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
   163
#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
   164
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   165
		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
   166
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   167
			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
   168
			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
   169
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   170
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   171
		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
   172
			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
   173
			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
   174
			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
   175
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   176
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   177
		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
   178
			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
   179
			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
   180
			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
   181
			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
   182
			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
   183
			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
   184
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   185
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   186
		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
   187
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   188
			instance->mode.push_back(Mode::BYTE_STRING);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   189
			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
   190
			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
   191
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   192
34
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   193
		callbacks.float2 = NUMERIC_CALLBACK(float, "float2");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   194
		callbacks.float4 = NUMERIC_CALLBACK(float, "float4");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   195
		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
   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
		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
   198
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   199
			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
   200
			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
   201
		};
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
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   203
		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
   204
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   205
			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
   206
			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
   207
		};
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
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   209
		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
   210
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   211
			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
   212
			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
   213
		};
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
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
		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
   216
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   217
			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
   218
			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
   219
		};
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
34
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   221
		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
   222
		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
   223
		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
   224
		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
   225
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
		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
   227
			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
   228
			instance->appendScalarValue("", "null", 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
   229
			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
   230
		};
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   231
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
		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
   233
			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
   234
			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
   235
			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
   236
		};
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
		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
   239
			CBOR_CALLBACK_START
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   240
			instance->mode.push_back(Mode::CHAR_STRING);
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   241
			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
   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.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
   246
			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
   247
			// TODO: implement
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
			xmlpp::Element* element = instance->current->add_child("tag");
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
			element->add_child_text(std::to_string(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
   250
			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
   251
		};
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
34
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   253
		callbacks.uint8 = NUMERIC_CALLBACK(uint8_t, "uint8");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   254
		callbacks.uint16 = NUMERIC_CALLBACK(uint16_t, "uint16");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   255
		callbacks.uint32 = NUMERIC_CALLBACK(uint32_t, "uint32");
0b0d214c3cb2 add NUMERIC_CALLBACK macro
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   256
		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
   257
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   258
		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
   259
			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
   260
			instance->appendScalarValue("undefined", "undefined", true); // FIXME: 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
   261
			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
   262
		};
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
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   264
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   265
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   266
	virtual ~XMLDocumentConstructor() {
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   267
	}
26
84ff7c97bfdc add --parser-option 'root-name' 'custom-root-element-name'
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   268
25
0e8a58946c48 add --parser-option
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   269
	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
   270
		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
   271
		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
   272
	}
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   273
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   274
	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
   275
		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
   276
		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
   277
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   278
		// 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
   279
		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
   280
		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
   281
		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
   282
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   283
		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
   284
		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
   285
		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
   286
			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
   287
			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
   288
		}
f5ac2d29eeb4 read CBOR and convert it to a flat XML DOM structure
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   289
32
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   290
		checkRemainingItems();
82b7f6e41a65 handle arrays and maps
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   291
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
		parser->get_document()->get_root_node()->set_attribute("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
   293
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   294
};
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   295
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   296
}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   297
}
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   298
}