src/XmlHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 16 Sep 2018 14:39:10 +0200
branchv_0
changeset 1 82ba555a97d1
child 2 13a41e435ea0
permissions -rw-r--r--
relpipe-out-xml skeleton

#pragma once

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <regex>

#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

namespace relpipe {
namespace out {
namespace tabular {

using namespace relpipe::reader;

class XmlHandler : public handlers::RelationalReaderStringHadler {
private:
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8
	const char* INDENT = "\t";

	std::ostream &output;

	std::vector<TypeId> columnTypes;
	std::vector<string_t> columnTypeCodes;
	std::vector<string_t> columnNames;
	std::vector<integer_t> columnWidths;
	std::vector<string_t> values; // all values are saved here and processed at the end of the relation
	integer_t columnCount = 0;

	

	const string_t escapeXmlText(const string_t &value, const char* color) {
		std::wstringstream result;

		for (auto & ch : value) {
			switch (ch) {
				// FIXME: xml escaping
				case L'&': result << L"&amp;";
					break;
				default: result << ch;
			}
		}

		return result.str();
	}

public:

	XmlHandler(std::ostream& output) : output(output) {
	}

	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
		output << convertor.to_bytes(name) << ": (XML)" << endl;
		columnCount = attributes.size();
		columnTypes.resize(columnCount);
		columnTypeCodes.resize(columnCount);
		columnNames.resize(columnCount);
		columnWidths.resize(columnCount, 0);
		for (int i = 0; i < attributes.size(); i++) {
			columnNames[i] = attributes[i].getAttributeName();
			columnTypes[i] = attributes[i].getTypeId();
			columnTypeCodes[i] = attributes[i].getTypeName();
		}
	}

	void attribute(const string_t& value) override {
		integer_t i = values.size() % columnCount;
		values.push_back(value);
		columnWidths[i] = max(columnWidths[i], value.length());
	}

	void endOfPipe() {
		
	}

};

}
}
}