src/XPathHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 30 Dec 2020 17:09:15 +0100
branchv_0
changeset 7 7f2d09c3b1de
parent 6 e498b3466342
child 8 9f95cfd68f25
permissions -rw-r--r--
add --xml-attribute support (both input and output attributes)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
73e60c77be23 project skeleton
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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <codecvt>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <regex>
7
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    24
#include <stdexcept>
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
1
d6dbd5d50d43 link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
#include <libxml++-2.6/libxml++/libxml++.h>
d6dbd5d50d43 link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    27
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/common/type/typedefs.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/writer/Factory.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include <relpipe/cli/RelpipeCLIException.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include "Configuration.h"
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    38
#include "XMLNameCodec.h"
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace relpipe {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
namespace tr {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
namespace xpath {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
private:
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    46
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // XML is in UTF-8
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	Configuration configuration;
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    49
	RelationConfiguration* currentRelationConfiguration = nullptr;
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	size_t currentAttributeIndex = 0;
3
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    53
	size_t currentRecordNumber = 1;
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    54
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
5
682c378bc4a2 add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    56
	xmlpp::Node::PrefixNsMap xmlns;
6
e498b3466342 use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    57
	xmlpp::Document dom;
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    58
	xmlpp::Element* recordElement = nullptr;
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    59
	relpipe::in::xmltable::XMLNameCodec xmlNameCodec; // TODO: move to a common library
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	void copyInputAttributesToOutput() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		for (auto rm : currentReaderMetadata) currentWriterMetadata.push_back({rm.getAttributeName(), relationalWriter->toTypeId(rm.getTypeName())});
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	bool isPrependingInputAttributes() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
				|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	bool isAppendingInputAttributes() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    74
	void resetRecordElement() {
6
e498b3466342 use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    75
		if (recordElement) dom.get_root_node()->remove_child(recordElement);
e498b3466342 use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    76
		recordElement = dom.get_root_node()->add_child("record");
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    77
	}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    78
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    79
	const Glib::ustring s2x(relpipe::common::type::StringX value) {
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    80
		return Glib::ustring(convertor.to_bytes(value));
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
	}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    82
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    83
	const relpipe::common::type::StringX x2s(const Glib::ustring& value) {
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    84
		return convertor.from_bytes(value);
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    85
	}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    86
7
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    87
	bool isXmlAttribute(const relpipe::common::type::StringX& attributeName) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    88
		for (auto pattern : currentRelationConfiguration->xmlAttributes) if (std::regex_match(attributeName, std::wregex(pattern))) return true;
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    89
		return false;
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    90
	}
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    91
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    92
	const relpipe::common::type::StringX formatRawXML(const relpipe::common::type::StringX& rawXML) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    93
		// TODO: move to a common library (used also in relpipe-in-xmltable)
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    94
		std::wregex pattern(L"^<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>\n|\n$");
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    95
		return std::regex_replace(rawXML, pattern, L"");
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    96
	}
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    97
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    98
	const relpipe::common::type::StringX serialize(xmlpp::Element* element, bool asXml) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    99
		if (element) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   100
			if (asXml) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   101
				xmlpp::Document d;
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   102
				d.create_root_node_by_import(element, true);
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   103
				return formatRawXML(x2s(d.write_to_string()));
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   104
			} else {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   105
				return element->get_child_text() ? x2s(element->get_child_text()->get_content()) : L"";
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   106
			}
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   107
		} else {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   108
			return L"";
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   109
		}
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   110
	}
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   111
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   112
	xmlpp::Element* findSingleElement(const xmlpp::NodeSet& nodeset) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   113
		if (nodeset.empty()) return nullptr;
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   114
		else if (nodeset.size() > 1) throw std::invalid_argument("XPath should find one or zero elements.");
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   115
		else if (xmlpp::Element * element = dynamic_cast<xmlpp::Element*> (nodeset[0])) return element;
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   116
		else if (nodeset[0]->get_path() == "/") return findSingleElement(nodeset[0]->find("*")); // support also "/" not only "/*" expressions (return root element in both cases)
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   117
		else throw std::invalid_argument("XPath should find an element, not other kinds of nodes.");
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   118
	}
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   119
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   120
	void writeInputAttributes() {
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   121
		for (xmlpp::Node* attributeNode : recordElement->get_children()) {
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   122
			if (xmlpp::Element * attributeElement = dynamic_cast<xmlpp::Element*> (attributeNode)) {
7
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   123
				bool asXml = isXmlAttribute(x2s(attributeElement->get_attribute("name")->get_value()));
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   124
				relationalWriter->writeAttribute(serialize(asXml ? dynamic_cast<xmlpp::Element*> (attributeElement->get_first_child()) : attributeElement, asXml));
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   125
			}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   126
		}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   127
	}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   128
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   129
	void writeOutputAttributes() {
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   130
		for (auto oa : currentRelationConfiguration->outputAttributes) {
7
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   131
			relpipe::common::type::StringX value;
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   132
			if (isXmlAttribute(oa.name)) value = serialize(findSingleElement(recordElement->find(s2x(oa.xpath), xmlns)), true);
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   133
			else value = x2s(recordElement->eval_to_string(s2x(oa.xpath), xmlns));
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   134
			relationalWriter->writeAttribute(value);
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   135
		}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   136
	}
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   137
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
public:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   139
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
	XPathHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
5
682c378bc4a2 add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   141
		for (int i = 0; i < configuration.namespaceMappings.size(); i++) {
682c378bc4a2 add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   142
			std::string prefix = convertor.to_bytes(configuration.namespaceMappings[i]);
682c378bc4a2 add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   143
			std::string uri = convertor.to_bytes(configuration.namespaceMappings[++i]);
682c378bc4a2 add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   144
			xmlns[prefix] = uri;
682c378bc4a2 add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   145
		}
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   146
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
	virtual ~XPathHandler() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
		currentRelationConfiguration = nullptr;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   155
			if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
				currentRelationConfiguration = &configuration.relationConfigurations[i];
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
				break;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
			}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
		currentReaderMetadata = attributes;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
		currentWriterMetadata.clear();
3
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   163
		currentRecordNumber = 1;
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   164
		recordElement = nullptr;
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
		if (currentRelationConfiguration == nullptr) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
			copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
		} else {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
			if (isPrependingInputAttributes()) copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
			for (auto oa : currentRelationConfiguration->outputAttributes) currentWriterMetadata.push_back({oa.name, oa.type});
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
			if (isAppendingInputAttributes()) copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   172
6
e498b3466342 use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   173
			dom.create_root_node("relpipe-tr-xpath");
e498b3466342 use xmlpp::Document instead of xmlpp::DomParser (the parser is not needed because we do not parse)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   174
			dom.get_root_node()->add_child("relation-name")->add_child_text(s2x(name));
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   175
			resetRecordElement();
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
		relationalWriter->startRelation(name, currentWriterMetadata, true);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
	void attribute(const relpipe::common::type::StringX& value) override {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
		if (currentRelationConfiguration) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   183
			relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex];
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   184
7
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   185
			// TODO: Parallel processing of records like in relpipe-in-filesystem? Or share common code with the XPath streamlet? (streamlets are parallelized)
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   186
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   187
			xmlpp::Element* attributeElement = recordElement->add_child(xmlNameCodec.encode(s2x(attributeMetadata.getAttributeName())));
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   188
			attributeElement->set_attribute("name", s2x(attributeMetadata.getAttributeName()));
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   189
			attributeElement->set_attribute("type", s2x(attributeMetadata.getTypeName()));
7
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   190
			if (isXmlAttribute(attributeMetadata.getAttributeName())) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   191
				if (value.size()) {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   192
					xmlpp::DomParser attributeParser;
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   193
					attributeParser.parse_memory(s2x(value));
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   194
					attributeElement->import_node(attributeParser.get_document()->get_root_node(), true);
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   195
				}
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   196
			} else {
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   197
				attributeElement->add_child_text(s2x(value));
7f2d09c3b1de add --xml-attribute support (both input and output attributes)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   198
			}
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
3
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   200
			if (currentAttributeIndex == 0) {
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   201
				recordElement->set_attribute("number", std::to_string(currentRecordNumber));
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   202
			}
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   203
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
			currentAttributeIndex++;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
			if (currentAttributeIndex == currentReaderMetadata.size()) {
5
682c378bc4a2 add XML namespace support, xmlns
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   207
				if (currentRelationConfiguration->where.empty() || recordElement->eval_to_boolean(s2x(currentRelationConfiguration->where), xmlns)) {
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   208
					if (isPrependingInputAttributes()) writeInputAttributes();
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   209
					writeOutputAttributes();
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   210
					if (isAppendingInputAttributes()) writeInputAttributes();
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   211
				}
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   212
2
426054465916 build DOM, filter records and evaluate XPath expressions for additional output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   213
				resetRecordElement();
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   214
				currentAttributeIndex = 0;
3
709abeb5f6d1 add current record number: @number
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   215
				currentRecordNumber++;
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   216
			}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
		} else {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
			relationalWriter->writeAttribute(value);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   219
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   220
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   221
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
	void endOfPipe() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   223
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   224
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   225
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   226
};
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   229
}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   230
}