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

/**
 * Relational pipes
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

namespace relpipe {
namespace in {
namespace xmltable {

#include <codecvt>

#include <libxml++-2.6/libxml++/libxml++.h>
#include <yaml.h>

class XMLDocumentConstructor {
private:
	std::istream* input = nullptr;
	xmlpp::DomParser* parser = nullptr;
	yaml_parser_t yamlParser;

	static int readFromInput(void* instance, unsigned char* buffer, size_t size, size_t* length) {
		std::istream* input = ((XMLDocumentConstructor*) instance)->input;
		input->read((char*) buffer, size);
		*length = input->gcount();
		return (input->good() || input->eof()) ? 1 : 0;
	}
	
	/**
	 * Both YAML and XML strings are in UTF-8.
	 */
	const char* y2x(yaml_char_t* value) {
		return value ? (const char*) value : "";
	}

public:

	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
		yaml_parser_initialize(&yamlParser);
		yaml_parser_set_input(&yamlParser, readFromInput, (void*) this);
	}

	virtual ~XMLDocumentConstructor() {
		yaml_parser_delete(&yamlParser);
	}

	void process() {
		xmlpp::Element* root = parser->get_document()->create_root_node("yaml");

		while (true) {
			yaml_event_t event;

			if (!yaml_parser_parse(&yamlParser, &event)) {
				// FIXME: throw exception
				std::wcerr << L"YAML error" << std::endl;
				return;
			}


			if (event.type == YAML_STREAM_END_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_STREAM_END_EVENT");
				yaml_event_delete(&event);
				break;
			} else if (event.type == YAML_STREAM_START_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_STREAM_START_EVENT");
			} else if (event.type == YAML_NO_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_NO_EVENT");
			} else if (event.type == YAML_DOCUMENT_START_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_DOCUMENT_START_EVENT");
			} else if (event.type == YAML_DOCUMENT_END_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_DOCUMENT_END_EVENT");
			} else if (event.type == YAML_ALIAS_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_ALIAS_EVENT");
			} else if (event.type == YAML_SCALAR_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_SCALAR_EVENT");
				e->set_attribute("value", y2x(event.data.scalar.value));
				e->set_attribute("anchor", y2x(event.data.scalar.anchor));
				e->set_attribute("tag", y2x(event.data.scalar.tag));
				e->set_attribute("style", std::to_string(event.data.scalar.style));
			} else if (event.type == YAML_SEQUENCE_START_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_SEQUENCE_START_EVENT");
				e->set_attribute("style", std::to_string(event.data.sequence_start.style));
			} else if (event.type == YAML_SEQUENCE_END_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_SEQUENCE_END_EVENT");
			} else if (event.type == YAML_MAPPING_START_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_MAPPING_START_EVENT");
			} else if (event.type == YAML_MAPPING_END_EVENT) {
				xmlpp::Element* e = root->add_child("YAML_MAPPING_END_EVENT");
			}

			yaml_event_delete(&event);
		}


	}
};

}
}
}