/**
* Relational pipes
* Copyright © 2021 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 <relpipe/writer/RelationalWriter.h>
#include <relpipe/writer/RelpipeWriterException.h>
#include "lib/ASN1ContentHandler.h"
#include "Configuration.h"
namespace relpipe {
namespace in {
namespace asn1 {
class RelpipeASN1ContentHandler : public lib::ASN1ContentHandler {
private:
wstring_convert < codecvt_utf8<wchar_t>> convertor; // ASN.1 parser works with UTF-8
std::shared_ptr<relpipe::writer::RelationalWriter> writer;
Configuration configuration;
relpipe::common::type::StringX currentRelationName;
std::vector<relpipe::writer::AttributeMetadata> currentAttributes;
enum class State {
Start,
Relation,
RelationName,
Header,
HeaderItem,
AttributeName,
AttributeType,
AttributeNext,
Records,
Record,
End,
};
State state = State::Start;
void assertTag(const Header& header, uint64_t expectedTag, TagClass expectedClass = TagClass::Universal) {
if (header.tagClass != expectedClass || header.tag != expectedTag) throw relpipe::writer::RelpipeWriterException(L"Unexpected tag."); // TODO: add actual vs. expected values
}
State startRelation() {
writer->startRelation(currentRelationName, currentAttributes, true);
currentRelationName = L"";
currentAttributes.clear();
return State::Records;
}
State setRelationName(std::string value) {
currentRelationName = convertor.from_bytes(value);
return State::Header;
}
State setAttributeName(relpipe::common::type::StringX value) {
currentAttributes.push_back({value, relpipe::writer::TypeId::STRING});
return State::AttributeType;
}
State setAttributeType(relpipe::common::type::StringX value) {
currentAttributes.back().typeId = writer->toTypeId(value);
return State::AttributeNext;
}
public:
RelpipeASN1ContentHandler(std::shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) {
}
bool setOption(const std::string& uri, const std::string& value) override {
return false;
}
void writeStreamStart() override {
}
void writeStreamEnd() override {
}
void writeCollectionStart(const Header& header) override {
assertTag(header, lib::UniversalType::Sequence);
if (state == State::Start) state = State::Relation;
else if (state == State::Relation) state = State::RelationName;
else if (state == State::Header) state = State::HeaderItem;
else if (state == State::HeaderItem) state = State::AttributeName;
else if (state == State::Records) state = State::Record;
else throw std::logic_error("Illegal state in writeCollectionStart(): " + std::to_string((int) state));
}
void writeCollectionEnd() override {
if (state == State::AttributeNext) state = State::HeaderItem;
else if (state == State::HeaderItem) state = startRelation();
else if (state == State::Records) state = State::Relation;
else if (state == State::Record) state = State::Records;
else if (state == State::Relation) state = State::End;
else throw std::logic_error("Illegal state in writeCollectionEnd(): " + std::to_string((int) state));
}
void writeBitString(const Header& header, std::vector<bool> value) override {
throw std::logic_error("Unsupported data type: BitString");
}
void writeBoolean(const Header& header, bool value) override {
if (state == State::Record) writer->writeAttribute(&value, typeid (value));
else throw std::logic_error("Illegal state in writeBoolean(): " + std::to_string((int) state));
}
void writeDateTime(const Header& header, DateTime value) override {
throw std::logic_error("Unsupported data type: DateTime");
}
void writeInteger(const Header& header, Integer value) override {
relpipe::common::type::Integer integer = value.toInt64();
if (state == State::Record) writer->writeAttribute(&integer, typeid (integer));
else throw std::logic_error("Illegal state in writeInteger(): " + std::to_string((int) state));
}
void writeNull(const Header& header) override {
throw std::logic_error("Unsupported data type: Null");
}
void writeOID(const Header& header, ObjectIdentifier value) override {
throw std::logic_error("Unsupported data type: OID");
}
void writeOctetString(const Header& header, std::string value) override {
throw std::logic_error("Unsupported data type: OctetString");
}
void writeTextString(const Header& header, std::string value) override {
assertTag(header, lib::UniversalType::UTF8String);
relpipe::common::type::StringX str = convertor.from_bytes(value);
if (state == State::RelationName) state = setRelationName(value);
else if (state == State::AttributeName) state = setAttributeName(str);
else if (state == State::AttributeType) state = setAttributeType(str);
else if (state == State::Record) writer->writeAttribute(&str, typeid (str));
else throw std::logic_error("Illegal state in writeTextString(): " + std::to_string((int) state));
}
void writeSpecific(const Header& header, std::string value) override {
throw std::logic_error("Unsupported data type: Specific");
}
};
}
}
}