src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 27 Oct 2020 00:59:54 +0100
branchv_0
changeset 16 3b197bf7a231
parent 14 5be268bc4c69
child 17 75c6685cceb9
permissions -rw-r--r--
raw YAML events to DOM
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>
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    24
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#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
    26
#include <yaml.h>
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
class XMLDocumentConstructor {
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
private:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
	std::istream* input = nullptr;
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	xmlpp::DomParser* parser = nullptr;
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    32
	yaml_parser_t yamlParser;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    33
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    34
	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
    35
		std::istream* input = ((XMLDocumentConstructor*) instance)->input;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    36
		input->read((char*) buffer, size);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    37
		*length = input->gcount();
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    38
		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
    39
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    40
	
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    41
	/**
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    42
	 * 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
    43
	 */
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    44
	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
    45
		return value ? (const char*) value : "";
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    46
	}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    47
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
public:
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	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
    51
		yaml_parser_initialize(&yamlParser);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    52
		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
    53
	}
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
	virtual ~XMLDocumentConstructor() {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    56
		yaml_parser_delete(&yamlParser);
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	void process() {
16
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    60
		xmlpp::Element* root = parser->get_document()->create_root_node("yaml");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    61
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    62
		while (true) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    63
			yaml_event_t event;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    64
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    65
			if (!yaml_parser_parse(&yamlParser, &event)) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    66
				// FIXME: throw exception
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    67
				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
    68
				return;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    69
			}
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
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    72
			if (event.type == YAML_STREAM_END_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    73
				xmlpp::Element* e = root->add_child("YAML_STREAM_END_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    74
				yaml_event_delete(&event);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    75
				break;
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    76
			} else if (event.type == YAML_STREAM_START_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    77
				xmlpp::Element* e = root->add_child("YAML_STREAM_START_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    78
			} else if (event.type == YAML_NO_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    79
				xmlpp::Element* e = root->add_child("YAML_NO_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    80
			} else if (event.type == YAML_DOCUMENT_START_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    81
				xmlpp::Element* e = root->add_child("YAML_DOCUMENT_START_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    82
			} else if (event.type == YAML_DOCUMENT_END_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    83
				xmlpp::Element* e = root->add_child("YAML_DOCUMENT_END_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    84
			} else if (event.type == YAML_ALIAS_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    85
				xmlpp::Element* e = root->add_child("YAML_ALIAS_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    86
			} else if (event.type == YAML_SCALAR_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    87
				xmlpp::Element* e = root->add_child("YAML_SCALAR_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    88
				e->set_attribute("value", y2x(event.data.scalar.value));
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    89
				e->set_attribute("anchor", y2x(event.data.scalar.anchor));
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    90
				e->set_attribute("tag", y2x(event.data.scalar.tag));
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    91
				e->set_attribute("style", std::to_string(event.data.scalar.style));
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    92
			} else if (event.type == YAML_SEQUENCE_START_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    93
				xmlpp::Element* e = root->add_child("YAML_SEQUENCE_START_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    94
				e->set_attribute("style", std::to_string(event.data.sequence_start.style));
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    95
			} else if (event.type == YAML_SEQUENCE_END_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    96
				xmlpp::Element* e = root->add_child("YAML_SEQUENCE_END_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    97
			} else if (event.type == YAML_MAPPING_START_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    98
				xmlpp::Element* e = root->add_child("YAML_MAPPING_START_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    99
			} else if (event.type == YAML_MAPPING_END_EVENT) {
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   100
				xmlpp::Element* e = root->add_child("YAML_MAPPING_END_EVENT");
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   101
			}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   102
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   103
			yaml_event_delete(&event);
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   104
		}
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   105
3b197bf7a231 raw YAML events to DOM
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   106
14
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
};
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
}
5be268bc4c69 separate the „std::istream to DOM“ phase
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
}