src/XPathHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 30 Dec 2020 00:39:03 +0100
branchv_0
changeset 1 d6dbd5d50d43
parent 0 73e60c77be23
child 2 426054465916
permissions -rw-r--r--
link to libxml++ / libxml2
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>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
1
d6dbd5d50d43 link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    25
#include <libxml++-2.6/libxml++/libxml++.h>
d6dbd5d50d43 link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    26
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/common/type/typedefs.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/writer/Factory.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/cli/RelpipeCLIException.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "Configuration.h"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace relpipe {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace tr {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace xpath {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
private:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	Configuration configuration;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	RelationConfiguration* currentRelationConfiguration;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	size_t currentAttributeIndex = 0;
1
d6dbd5d50d43 link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
	
d6dbd5d50d43 link to libxml++ / libxml2
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	xmlpp::DomParser dom;
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	void copyInputAttributesToOutput() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		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
    55
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	bool isPrependingInputAttributes() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
				|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	bool isAppendingInputAttributes() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append;
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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
public:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	XPathHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	virtual ~XPathHandler() {
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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	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
    75
		currentRelationConfiguration = nullptr;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			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
    79
				currentRelationConfiguration = &configuration.relationConfigurations[i];
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
				break;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		currentReaderMetadata = attributes;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		currentWriterMetadata.clear();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		if (currentRelationConfiguration == nullptr) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
			copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		} else {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
			if (isPrependingInputAttributes()) copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
			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
    92
			if (isAppendingInputAttributes()) copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
			// TODO: prepare DOM
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		relationalWriter->startRelation(name, currentWriterMetadata, true);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	void attribute(const relpipe::common::type::StringX& value) override {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		if (currentRelationConfiguration) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
			relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex];
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
			// TODO: add attribute to DOM		
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
			currentAttributeIndex++;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
			if (currentAttributeIndex == currentReaderMetadata.size()) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
				// TODO: evaluate XPath expression
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
				// TODO: write record to output, if the XPath condition was met
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
				// TODO: clean record node in DOM
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
				currentAttributeIndex = 0;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
			}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
		} else {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
			relationalWriter->writeAttribute(value);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
	void endOfPipe() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
};
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
}