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