src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 28 Oct 2020 16:30:01 +0100
branchv_0
changeset 19 aabdab3e05a0
parent 18 737c507a3e32
child 21 053054f9f702
permissions -rw-r--r--
allow parsing a single scalar value (plain text) directly at the root, without any sequences or maps
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
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
namespace relpipe {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
namespace in {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
namespace xmltable {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    23
#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
    24
#include <vector>
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    25
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <libxml++-2.6/libxml++/libxml++.h>
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    27
#include <yaml.h>
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
class XMLDocumentConstructor {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
private:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	std::istream* input = nullptr;
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	xmlpp::DomParser* parser = nullptr;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    33
	yaml_parser_t yamlParser;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    34
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    35
	enum class Mode {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    36
		ROOT,
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    37
		SEQUENCE,
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    38
		MAPPING,
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    39
		MAP_KEY
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    40
	};
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    41
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    42
	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
    43
	std::vector<Mode> mode;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    44
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    45
	static int readFromInput(void* instance, unsigned char* buffer, size_t size, size_t* length) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    46
		std::istream* input = ((XMLDocumentConstructor*) instance)->input;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    47
		input->read((char*) buffer, size);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    48
		*length = input->gcount();
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    49
		return (input->good() || input->eof()) ? 1 : 0;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    50
	}
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    51
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    52
	/**
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    53
	 * Both YAML and XML strings are in UTF-8.
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    54
	 */
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    55
	const char* y2x(yaml_char_t* value) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    56
		return value ? (const char*) value : "";
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    57
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    58
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    59
	const std::string y2xname(yaml_char_t* value) {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    60
		// FIXME: escaping, assure valid XML names
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    61
		//return std::string("name_") + y2x(value);
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    62
		return y2x(value);
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    63
	}
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    64
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    65
	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
    66
		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
    67
	}
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    68
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
public:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    72
		yaml_parser_initialize(&yamlParser);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    73
		yaml_parser_set_input(&yamlParser, readFromInput, (void*) this);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    74
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    75
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    76
	virtual ~XMLDocumentConstructor() {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    77
		yaml_parser_delete(&yamlParser);
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
	void process() {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    81
		current = parser->get_document()->create_root_node("yaml");
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    82
		mode.push_back(Mode::ROOT);
18
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    83
		std::string itemName;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    84
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    85
		while (true) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    86
			yaml_event_t event;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    87
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    88
			if (!yaml_parser_parse(&yamlParser, &event)) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    89
				// FIXME: throw exception
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    90
				std::wcerr << L"YAML error" << std::endl;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    91
				return;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    92
			}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    93
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    94
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    95
			if (event.type == YAML_STREAM_END_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    96
				std::cerr << "YAML_STREAM_END_EVENT" << std::endl;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    97
				yaml_event_delete(&event);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    98
				break;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    99
			} else if (event.type == YAML_STREAM_START_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   100
				std::cerr << "YAML_STREAM_START_EVENT" << std::endl;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   101
			} else if (event.type == YAML_NO_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   102
				std::cerr << "YAML_NO_EVENT" << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   103
				current->add_child("null"); // TODO: null?
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   104
			} else if (event.type == YAML_DOCUMENT_START_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   105
				std::cerr << "YAML_DOCUMENT_START_EVENT" << std::endl;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   106
			} else if (event.type == YAML_DOCUMENT_END_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   107
				std::cerr << "YAML_DOCUMENT_END_EVENT" << std::endl;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   108
			} else if (event.type == YAML_ALIAS_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   109
				std::cerr << "YAML_ALIAS_EVENT" << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   110
				// TODO: alias?
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   111
			} else if (event.type == YAML_SCALAR_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   112
				if (mode.back() == Mode::SEQUENCE) {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   113
					std::cerr << "YAML_SCALAR_EVENT: Mode::SEQUENCE: " << event.data.scalar.value << std::endl;
18
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   114
					current->add_child(itemName)->add_child_text(y2x(event.data.scalar.value));
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   115
				} else if (mode.back() == Mode::MAPPING) {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   116
					std::cerr << "YAML_SCALAR_EVENT: Mode::MAPPING: " << event.data.scalar.value << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   117
					current = current->add_child(y2xname(event.data.scalar.value));
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   118
					mode.push_back(Mode::MAP_KEY);
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   119
				} else if (mode.back() == Mode::MAP_KEY) {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   120
					std::cerr << "YAML_SCALAR_EVENT: Mode::MAP_KEY: " << event.data.scalar.value << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   121
					current->add_child_text(y2x(event.data.scalar.value));
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   122
					current = parentOrSelf(current);
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   123
					mode.pop_back();
19
aabdab3e05a0 allow parsing a single scalar value (plain text)
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   124
				} else if (mode.back() == Mode::ROOT) {
aabdab3e05a0 allow parsing a single scalar value (plain text)
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   125
					current->add_child_text(y2x(event.data.scalar.value));
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   126
				} else {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   127
					std::cerr << "YAML_SCALAR_EVENT: ???" << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   128
				}
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   129
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   130
			} else if (event.type == YAML_SEQUENCE_START_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   131
				std::cerr << "YAML_SEQUENCE_START_EVENT" << std::endl;
18
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   132
				xmlpp::Element* parent = current->get_parent();
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   133
				if (parent) {
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   134
					itemName = current->get_name();
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   135
					parent->remove_child(current);
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   136
					current = parent;
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   137
				} else {
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   138
					itemName = "item";
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   139
				}
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   140
				if (mode.back() == Mode::MAP_KEY) mode.pop_back();
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   141
				mode.push_back(Mode::SEQUENCE);
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   142
			} else if (event.type == YAML_SEQUENCE_END_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   143
				std::cerr << "YAML_SEQUENCE_END_EVENT" << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   144
				mode.pop_back(); // TODO: assert sequence?
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   145
			} else if (event.type == YAML_MAPPING_START_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   146
18
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   147
				if (mode.back() == Mode::ROOT) {
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   148
					std::cerr << "YAML_MAPPING_START_EVENT: Mode:ROOT" << std::endl;
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   149
				} else if (mode.back() == Mode::SEQUENCE) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   150
					std::cerr << "YAML_MAPPING_START_EVENT: Mode::SEQUENCE" << std::endl;
18
737c507a3e32 YAML data to DOM: use key as item element name + remove wrapper
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   151
					current = current->add_child(itemName);
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   152
				} else if (mode.back() == Mode::MAP_KEY) {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   153
					std::cerr << "YAML_MAPPING_START_EVENT: Mode::MAP_KEY" << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   154
					mode.pop_back();
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   155
				} else {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   156
					std::cerr << "YAML_MAPPING_START_EVENT: Mode::?" << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   157
					// TODO: map might be a key of another map → wrap/nest
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   158
				}
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   159
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   160
				mode.push_back(Mode::MAPPING);
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   161
			} else if (event.type == YAML_MAPPING_END_EVENT) {
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   162
				std::cerr << "YAML_MAPPING_END_EVENT" << std::endl;
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   163
				current = parentOrSelf(current);
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   164
				mode.pop_back(); // TODO: assert map?
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   165
			} else {
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   166
				std::cerr << "???" << std::endl;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   167
			}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   168
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   169
			yaml_event_delete(&event);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   170
		}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   171
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   172
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
};
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
}
17
75c6685cceb9 YAML data to DOM: first dirty version with debug
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   178
}