/**
* Relational pipes
* Copyright © 2023 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 <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <exception>
#include <regex>
#include <libxml++-2.6/libxml++/libxml++.h>
#include <relpipe/writer/typedefs.h>
namespace relpipe {
namespace in {
namespace ods {
using namespace relpipe::writer;
class ODSCommand {
private:
std::wstring_convert<codecvt_utf8<wchar_t>> convertor;
xmlpp::Node::PrefixNsMap ns;
std::shared_ptr<RelationalWriter> writer;
string_t xpath(xmlpp::Node* node, std::string xpath) {
return convertor.from_bytes(node->eval_to_string(xpath, ns));
}
void processCell(xmlpp::Node* c, AttributeMetadata& am) {
string_t value = xpath(c, "@o:value");
if (value.size()) writer->writeAttribute(value);
else writer->writeAttribute(xpath(c, "tx:p"));
// TODO: be aware of the current data types
}
void processRow(xmlpp::Node* r, std::vector<AttributeMetadata>& am) {
for (int i = 0; i < am.size(); i++) {
auto xpe = std::string("t:table-cell[")
+ std::to_string(i + 1)
+ "]";
auto cells = r->find(xpe, ns);
if (cells.size() == 1) {
processCell(cells[0], am[i]);
} else {
writer->writeAttribute(L"");
// TODO: support also other data types
}
}
// FIXME: support sparse data / missing values:
// <table:table-row table:number-rows-repeated="2">
// <table:table-cell table:number-columns-repeated="3"/>
}
void processTable(xmlpp::Node* t) {
auto relation = xpath(t, "@t:name");
std::vector<AttributeMetadata> metadata;
for (xmlpp::Node* c : t->find("t:table-row[1]/t:table-cell", ns)) {
auto name = xpath(c, "tx:p");
if (name.size()) {
metadata.push_back({name, TypeId::STRING});
// TODO: detect and support other data types
}
}
writer->startRelation(relation, metadata, true);
int i = 0;
for (xmlpp::Node* r : t->find("t:table-row", ns)) {
i++;
if (i == 1) continue; // skip header row
processRow(r, metadata);
}
}
public:
ODSCommand(std::shared_ptr<RelationalWriter> writer) : writer(writer) {
ns["o"] = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
ns["t"] = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
ns["tx"] = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
}
void process(std::istream & input) {
xmlpp::DomParser parser;
parser.parse_stream(input);
xmlpp::Element* root = parser.get_document()->get_root_node();
auto spreadsheets = root->find("/o:document/o:body/o:spreadsheet", ns);
if (spreadsheets.size() == 1) {
for (xmlpp::Node* table : spreadsheets[0]->find("t:table", ns)) {
processTable(table);
}
} else {
throw RelpipeWriterException(L"Invalid XML structure. "
"Expecting OpenDocument spreadsheet.");
}
}
};
}
}
}