diff -r 71b902e1c5ee -r e82aaf24b0fe src/ODSCommand.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ODSCommand.h Sun May 14 02:13:46 2023 +0200 @@ -0,0 +1,128 @@ +/** + * 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 . + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace relpipe { +namespace in { +namespace ods { + +using namespace relpipe::writer; + +class ODSCommand { +private: + std::wstring_convert> convertor; + + xmlpp::Node::PrefixNsMap ns; + std::shared_ptr 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& 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: + // + // + + } + + void processTable(xmlpp::Node* t) { + auto relation = xpath(t, "@t:name"); + std::vector 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 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."); + } + + + } +}; + +} +} +}