src/XPathHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 27 Dec 2020 21:57:52 +0100
branchv_0
changeset 0 73e60c77be23
child 1 d6dbd5d50d43
permissions -rw-r--r--
project skeleton
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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/common/type/typedefs.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/reader/TypeId.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/handlers/AttributeMetadata.h>
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/writer/Factory.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/cli/RelpipeCLIException.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 "Configuration.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
namespace relpipe {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace tr {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace xpath {
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
class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
private:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	Configuration configuration;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	RelationConfiguration* currentRelationConfiguration;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	size_t currentAttributeIndex = 0;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	void copyInputAttributesToOutput() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		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
    51
	}
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
	bool isPrependingInputAttributes() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
				|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0);
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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	bool isAppendingInputAttributes() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append;
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
public:
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
	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
    65
	}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	virtual ~XPathHandler() {
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
	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
    71
		currentRelationConfiguration = nullptr;
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
		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			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
    75
				currentRelationConfiguration = &configuration.relationConfigurations[i];
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				break;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		currentReaderMetadata = attributes;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		currentWriterMetadata.clear();
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
		if (currentRelationConfiguration == nullptr) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		} else {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			if (isPrependingInputAttributes()) copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
			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
    88
			if (isAppendingInputAttributes()) copyInputAttributesToOutput();
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
			// TODO: prepare DOM
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		relationalWriter->startRelation(name, currentWriterMetadata, true);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
	}
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
	void attribute(const relpipe::common::type::StringX& value) override {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		if (currentRelationConfiguration) {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
			relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex];
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
			// TODO: add attribute to DOM		
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
			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
			if (currentAttributeIndex == currentReaderMetadata.size()) {
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
				// TODO: evaluate XPath expression
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
				// 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
   108
				// TODO: clean record node in DOM
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
				currentAttributeIndex = 0;
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
			}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
		} else {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
			relationalWriter->writeAttribute(value);
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
		}
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
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
	void endOfPipe() {
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
};
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
}