do not quote nor escape integers and booleans,
escape line ends,
escape backslash
/**
* Relational pipes
* Copyright © 2018 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 <memory>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <relpipe/common/type/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>
namespace relpipe {
namespace out {
namespace yaml {
class YAMLHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
private:
const char ESC = '\\';
const char Q = '"';
std::ostream& output;
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // we generate YAML always in UTF-8 like XML?
std::vector<relpipe::reader::handlers::AttributeMetadata> currentAttributes;
size_t currentAttributeIndex = 0;
bool firstRecord;
std::string escape(const relpipe::common::type::StringX& value) {
std::stringstream result;
result.put(Q);
for (char ch : convertor.to_bytes(value)) {
if (ch == ESC) result.put(ESC).put(ch);
else if (ch == Q) result.put(ESC).put(ch);
else if (ch == '\n') result.put(ESC).put('n');
else if (ch == '\r') result.put(ESC).put('r');
else result.put(ch);
}
result.put(Q);
return result.str();
}
std::string indent(int level) {
std::stringstream result;
for (int i = 0; i < level; i++) result << " ";
return result.str();
}
public:
YAMLHandler(std::ostream& output) : output(output) {
}
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
currentAttributes = attributes;
currentAttributeIndex = 0;
output << escape(name);
output << ":" << std::endl;
output << indent(1) << R"("attribute-metadata":)" << std::endl;
for (auto am : attributes) output << indent(2) << R"(- "name": )" << escape(am.getAttributeName()) << std::endl << indent(2) << R"( "type": )" << escape(am.getTypeName()) << std::endl;
firstRecord = true;
}
void attribute(const relpipe::common::type::StringX& value) override {
if (firstRecord) {
output << indent(1) << R"("record":)" << std::endl;
firstRecord = false;
}
if (currentAttributeIndex % currentAttributes.size() == 0) {
currentAttributeIndex = 0;
output << indent(2) << "- ";
} else {
output << indent(2) << " ";
}
output << escape(currentAttributes[currentAttributeIndex].getAttributeName()) << ": ";
if (currentAttributes[currentAttributeIndex].getTypeId() == relpipe::reader::TypeId::INTEGER) output << convertor.to_bytes(value) << std::endl;
else if (currentAttributes[currentAttributeIndex].getTypeId() == relpipe::reader::TypeId::BOOLEAN) output << convertor.to_bytes(value) << std::endl;
else output << escape(value) << std::endl;
currentAttributeIndex++;
}
void endOfPipe() {
output.flush();
}
};
}
}
}