src/ODSCommand.h
author František Kučera <franta-hg@frantovo.cz>
Mon, 15 May 2023 00:55:16 +0200
branchv_0
changeset 2 d4e0472e8e5d
parent 1 e82aaf24b0fe
permissions -rw-r--r--
support @table:number-columns-repeated and @table:number-columns-spanned

/**
 * 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;

/**
 * Reads OpenDocument / LibreOffice Spreadsheet.
 * 
 * Known limitations:
 *  - currently reads only „flat“ uncompressed files (.fods, not .ods)
 *  - while streaming is possible, this implementation reads whole XML document
 *    in memory and then processes it (DOM)
 *  - only string type is supported now → use relpipe-tr-infertypes
 */
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));
	}

	int xpathInt(xmlpp::Node* node, std::string xpath, int defaultValue = 0) {
		double result = node->eval_to_number(xpath, ns);
		return result != result ? defaultValue : result;
	}

	void processRow(xmlpp::Node* r, std::vector<AttributeMetadata>& am) {
		auto cells = r->find("t:table-cell", ns);
		for (int i = 0; i < cells.size(); i++) {
			xmlpp::Node* c = cells[i];
			string_t value = xpath(c, "@o:value");
			if (value.size() == 0) value = xpath(c, "tx:p");

			// value = am[i].attributeName + L"=" + value;

			double repeated = xpathInt(c, "@t:number-columns-repeated", 1);
			double spanned = xpathInt(c, "@t:number-columns-spanned", 1);

			for (int i = 0; i < repeated; i++) writer->writeAttribute(value);
			for (int i = 1; i < spanned; i++) writer->writeAttribute(L"");

			// TODO: support also other data types
		}
	}

	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");
			double repeated = xpathInt(c, "@t:number-columns-repeated", 1);
			double spanned = xpathInt(c, "@t:number-columns-spanned", 1);
			for (int i = 0, limit = repeated * spanned; i < limit; i++) {
				metadata.push_back({name, TypeId::STRING});
			}
			// TODO: detect and support other data types
		}

		if (metadata.size()) {
			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);
			}
		} else {
			// no values on the first row
			// probably empty table
		}
	}

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.");
		}
	}
};

}
}
}