/**
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 <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 ods {
using namespace relpipe::reader;
class OdsHandler : public handlers::RelationalReaderStringHadler {
private:
std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // XML output will be always in UTF-8
// TODO: refactor and move common XML functions to relpipe-lib-xml
const char* INDENT_1 = "\t";
const char* INDENT_2 = "\t\t";
const char* INDENT_3 = "\t\t\t";
const char* INDENT_4 = "\t\t\t\t";
const char* INDENT_5 = "\t\t\t\t\t";
const char* INDENT_6 = "\t\t\t\t\t\t";
std::ostream &output;
std::vector<TypeId> columnTypes;
std::vector<string_t> columnTypeCodes;
std::vector<string_t> columnNames;
integer_t valueCount = 0;
integer_t columnCount = 0;
integer_t relationCount = 0;
// TODO: refactor and move common XML functions to relpipe-lib-xml
const std::string escapeXmlText(const string_t &value) {
std::wstringstream result;
for (auto & ch : value) {
switch (ch) {
case L'&': result << L"&";
break;
case L'<': result << L"<";
break;
case L'>': result << L">";
break;
case L'\'': result << L"'"; // TODO: escape ' and " only in attributes
break;
case L'"': result << L"""; // TODO: escape ' and " only in attributes
break;
default: result << ch;
}
}
return convertor.to_bytes(result.str());
}
public:
OdsHandler(std::ostream& output) : output(output) {
}
void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
// TODO: refactor and move common XML functions to relpipe-lib-xml
valueCount = 0;
columnCount = 0;
if (relationCount == 0) {
output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
output << "<office:document" << std::endl;
output << INDENT_1 << "xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"" << std::endl;
output << INDENT_1 << "xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"" << std::endl;
output << INDENT_1 << "xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"" << std::endl;
output << INDENT_1 << "office:mimetype=\"application/vnd.oasis.opendocument.spreadsheet\"" << std::endl;
output << INDENT_1 << "office:version=\"1.2\">" << std::endl;
output << INDENT_1 << "<office:body>" << std::endl;
output << INDENT_2 << "<office:spreadsheet>" << std::endl;
} else {
output << INDENT_4 << "</table:table-row>" << std::endl;
output << INDENT_3 << "</table:table>" << std::endl;
}
relationCount++;
// TODO: rename relations with same names
output << INDENT_3 << "<table:table table:name=\"" << escapeXmlText(name) << "\">" << std::endl;
columnCount = attributes.size();
columnTypes.resize(columnCount);
columnTypeCodes.resize(columnCount);
columnNames.resize(columnCount);
output << INDENT_4 << "<table:table-row>" << std::endl;
for (int i = 0; i < attributes.size(); i++) {
columnNames[i] = attributes[i].getAttributeName();
columnTypes[i] = attributes[i].getTypeId();
columnTypeCodes[i] = attributes[i].getTypeName();
output << INDENT_5 << "<table:table-cell>" << std::endl;
output << INDENT_6 << "<text:p>";
output << escapeXmlText(columnNames[i]);
output << "</text:p>" << std::endl;
output << INDENT_5 << "</table:table-cell>" << std::endl;
}
output << INDENT_4 << "</table:table-row>" << std::endl;
}
void attribute(const string_t& value) override {
integer_t i = valueCount % columnCount;
if (i == 0 && valueCount) output << INDENT_4 << "</table:table-row>" << std::endl;
if (i == 0) output << INDENT_4 << "<table:table-row>" << std::endl;
valueCount++;
output << INDENT_5 << "<table:table-cell>" << std::endl;
output << INDENT_6 << "<text:p>";
output << escapeXmlText(value);
output << "</text:p>" << std::endl;
output << INDENT_5 << "</table:table-cell>" << std::endl;
}
void endOfPipe() {
if (valueCount) output << INDENT_4 << "</table:table-row>" << std::endl;
if (relationCount) output << INDENT_3 << "</table:table>" << std::endl;
output << INDENT_2 << "</office:spreadsheet>" << std::endl;
output << INDENT_1 << "</office:body>" << std::endl;
output << "</office:document>" << std::endl;
}
};
}
}
}