src/ODSCommand.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 14 May 2023 02:13:46 +0200
branchv_0
changeset 1 e82aaf24b0fe
parent 0 src/XMLTableCommand.h@71b902e1c5ee
child 2 d4e0472e8e5d
permissions -rw-r--r--
first version, prototype, works sometimes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
     3
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <cstdlib>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <string>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <sstream>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <vector>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <algorithm>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <exception>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <libxml++-2.6/libxml++/libxml++.h>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/typedefs.h>
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
namespace relpipe {
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace in {
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    34
namespace ods {
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
using namespace relpipe::writer;
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    38
class ODSCommand {
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
private:
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    40
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor;
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
	xmlpp::Node::PrefixNsMap ns;
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
	std::shared_ptr<RelationalWriter> writer;
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    45
	string_t xpath(xmlpp::Node* node, std::string xpath) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
		return convertor.from_bytes(node->eval_to_string(xpath, ns));
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    49
	void processCell(xmlpp::Node* c, AttributeMetadata& am) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
		string_t value = xpath(c, "@o:value");
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
		if (value.size()) writer->writeAttribute(value);
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
		else writer->writeAttribute(xpath(c, "tx:p"));
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    53
		// TODO: be aware of the current data types
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
	void processRow(xmlpp::Node* r, std::vector<AttributeMetadata>& am) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    57
		for (int i = 0; i < am.size(); i++) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    58
			auto xpe = std::string("t:table-cell[")
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    59
					+ std::to_string(i + 1)
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
					+ "]";
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    61
			auto cells = r->find(xpe, ns);
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    62
			if (cells.size() == 1) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
				processCell(cells[0], am[i]);
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
			} else {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
				writer->writeAttribute(L"");
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
				// TODO: support also other data types
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
			}
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
		}
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
		// FIXME: support sparse data / missing values:
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
		// <table:table-row  table:number-rows-repeated="2">
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
		// <table:table-cell table:number-columns-repeated="3"/>
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
	void processTable(xmlpp::Node* t) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
		auto relation = xpath(t, "@t:name");
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
		std::vector<AttributeMetadata> metadata;
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
		for (xmlpp::Node* c : t->find("t:table-row[1]/t:table-cell", ns)) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
			auto name = xpath(c, "tx:p");
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
			if (name.size()) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
				metadata.push_back({name, TypeId::STRING});
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
				// TODO: detect and support other data types
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
			}
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		}
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
		writer->startRelation(relation, metadata, true);
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    89
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
		int i = 0;
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
		for (xmlpp::Node* r : t->find("t:table-row", ns)) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
			i++;
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    93
			if (i == 1) continue; // skip header row
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
			processRow(r, metadata);
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
		}
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
public:
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   100
	ODSCommand(std::shared_ptr<RelationalWriter> writer) : writer(writer) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   101
		ns["o"] = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   102
		ns["t"] = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   103
		ns["tx"] = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
	}
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   106
	void process(std::istream & input) {
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
		xmlpp::DomParser parser;
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
		parser.parse_stream(input);
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		xmlpp::Element* root = parser.get_document()->get_root_node();
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   111
		auto spreadsheets = root->find("/o:document/o:body/o:spreadsheet", ns);
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   113
		if (spreadsheets.size() == 1) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
			for (xmlpp::Node* table : spreadsheets[0]->find("t:table", ns)) {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
				processTable(table);
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   116
			}
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   117
		} else {
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   118
			throw RelpipeWriterException(L"Invalid XML structure. "
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   119
					"Expecting OpenDocument spreadsheet.");
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
		}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
1
e82aaf24b0fe first version, prototype, works sometimes
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   122
0
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
	}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
};
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
}
71b902e1c5ee project skeleton (taken from relpipe-in-xmltable)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}