src/YAMLHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 23 Dec 2020 17:15:34 +0100
branchv_0
changeset 0 c52ca92aa593
child 1 af15c47f77ae
permissions -rw-r--r--
project skeleton
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
c52ca92aa593 project skeleton
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
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace relpipe {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace out {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace yaml {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
class YAMLHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
private:
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	std::ostream& output;
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // we generate YAML always in UTF-8 like XML?
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentAttributes;
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	size_t currentAttributeIndex = 0;
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	void writeYamlValue(const relpipe::common::type::StringX& value) {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		// TODO: escaping
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		output << convertor.to_bytes(value);
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
public:
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	YAMLHandler(std::ostream& output) : output(output) {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		currentAttributes = attributes;
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	void attribute(const relpipe::common::type::StringX& value) override {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
		if (currentAttributeIndex % currentAttributes.size() == 0) {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
			currentAttributeIndex = 0;
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		} else {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		currentAttributeIndex++;
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	void endOfPipe() {
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		output.flush();
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
};
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
}
c52ca92aa593 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
}