src/XMLDocumentConstructor.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 29 May 2021 22:12:19 +0200
branchv_0
changeset 29 9254988f9382
parent 26 84ff7c97bfdc
child 30 f5ac2d29eeb4
permissions -rw-r--r--
CBOR skeleton: remove YAML library, add CBOR one

/**
 * 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

#include <codecvt>
#include <vector>

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

#include "XMLNameCodec.h"

namespace relpipe {
namespace in {
namespace xmltable {

class XMLDocumentConstructor {
private:
	std::istream* input = nullptr;
	xmlpp::DomParser* parser = nullptr;
	XMLNameCodec nameCodec;

	std::string rootName = "cbor";
	xmlpp::Element* current;

	/**
	 * Both CBOR and XML strings are in UTF-8.
	 */
	const char* c2x(char* value) { // FIXME: cbor type
		return value ? (const char*) value : "";
	}

	const Glib::ustring c2xname(char* value) { // FIXME: cbor type
		return nameCodec.encode(c2x(value));
	}

	xmlpp::Element* parentOrSelf(xmlpp::Element* current) {
		return current->get_parent() == nullptr ? current : current->get_parent();
	}

public:

	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
		// cbor_item_t * root = cbor_new_definite_map(2);
		// FIXME: initialize CBOR parser
	}

	virtual ~XMLDocumentConstructor() {
		// FIXME: destroy CBOR parser
	}

	void setOption(const std::string& uri, const std::string& value) {
		if (uri == "root-name") rootName = value;
		else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“");
	}

	void process() {
		current = parser->get_document()->create_root_node(rootName);
	}
};

}
}
}