diff -r 000000000000 -r 73e60c77be23 src/XPathHandler.h --- /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 . + */ +#pragma once + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include + +#include "Configuration.h" + +namespace relpipe { +namespace tr { +namespace xpath { + +class XPathHandler : public relpipe::reader::handlers::RelationalReaderStringHandler { +private: + shared_ptr relationalWriter; + Configuration configuration; + RelationConfiguration* currentRelationConfiguration; + std::vector currentReaderMetadata; + std::vector 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 relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { + } + + virtual ~XPathHandler() { + } + + void startRelation(relpipe::common::type::StringX name, std::vector 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() { + + } + +}; + +} +} +}