src/XPathHandler.h
branchv_0
changeset 0 73e60c77be23
child 1 d6dbd5d50d43
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/XPathHandler.h	Sun Dec 27 21:57:52 2020 +0100
@@ -0,0 +1,124 @@
+/**
+ * Relational pipes
+ * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <memory>
+#include <string>
+#include <vector>
+#include <codecvt>
+#include <regex>
+
+#include <relpipe/common/type/typedefs.h>
+#include <relpipe/reader/TypeId.h>
+#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
+#include <relpipe/reader/handlers/AttributeMetadata.h>
+
+#include <relpipe/writer/Factory.h>
+
+#include <relpipe/cli/RelpipeCLIException.h>
+
+#include "Configuration.h"
+
+namespace relpipe {
+namespace tr {
+namespace xpath {
+
+class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
+private:
+	shared_ptr<relpipe::writer::RelationalWriter> relationalWriter;
+	Configuration configuration;
+	RelationConfiguration* currentRelationConfiguration;
+	std::vector<relpipe::reader::handlers::AttributeMetadata> currentReaderMetadata;
+	std::vector<relpipe::writer::AttributeMetadata> currentWriterMetadata;
+	size_t currentAttributeIndex = 0;
+
+	void copyInputAttributesToOutput() {
+		for (auto rm : currentReaderMetadata) currentWriterMetadata.push_back({rm.getAttributeName(), relationalWriter->toTypeId(rm.getTypeName())});
+	}
+
+	bool isPrependingInputAttributes() {
+		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Prepend
+				|| (currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Auto && currentRelationConfiguration->outputAttributes.size() == 0);
+	}
+
+	bool isAppendingInputAttributes() {
+		return currentRelationConfiguration->inputAttributePolicy == InputAttributePolicy::Append;
+	}
+
+public:
+
+	XPathHandler(shared_ptr<relpipe::writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
+	}
+
+	virtual ~XPathHandler() {
+	}
+
+	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
+		currentRelationConfiguration = nullptr;
+
+		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
+			if (std::regex_match(name, std::wregex(configuration.relationConfigurations[i].relation))) {
+				currentRelationConfiguration = &configuration.relationConfigurations[i];
+				break;
+			}
+		}
+
+		currentReaderMetadata = attributes;
+		currentWriterMetadata.clear();
+
+		if (currentRelationConfiguration == nullptr) {
+			copyInputAttributesToOutput();
+		} else {
+			if (isPrependingInputAttributes()) copyInputAttributesToOutput();
+			for (auto oa : currentRelationConfiguration->outputAttributes) currentWriterMetadata.push_back({oa.name, oa.type});
+			if (isAppendingInputAttributes()) copyInputAttributesToOutput();
+
+			// TODO: prepare DOM
+		}
+
+		relationalWriter->startRelation(name, currentWriterMetadata, true);
+	}
+
+	void attribute(const relpipe::common::type::StringX& value) override {
+		if (currentRelationConfiguration) {
+			relpipe::reader::handlers::AttributeMetadata attributeMetadata = currentReaderMetadata[currentAttributeIndex];
+
+			// TODO: add attribute to DOM		
+
+			currentAttributeIndex++;
+
+			if (currentAttributeIndex == currentReaderMetadata.size()) {
+
+				// TODO: evaluate XPath expression
+				// TODO: write record to output, if the XPath condition was met
+				// TODO: clean record node in DOM
+				currentAttributeIndex = 0;
+			}
+		} else {
+			relationalWriter->writeAttribute(value);
+		}
+	}
+
+	void endOfPipe() {
+
+	}
+
+};
+
+}
+}
+}