src/FreeformASN1ContentHandler.h
branchv_0
changeset 9 7d309accc639
parent 8 3192dc8772de
equal deleted inserted replaced
8:3192dc8772de 9:7d309accc639
    23 #include "Configuration.h"
    23 #include "Configuration.h"
    24 
    24 
    25 namespace relpipe {
    25 namespace relpipe {
    26 namespace in {
    26 namespace in {
    27 namespace asn1 {
    27 namespace asn1 {
       
    28 
       
    29 namespace freeformOption {
       
    30 static const char* BitStringSymbol0 = "bit-string-symbol-0";
       
    31 static const char* BitStringSymbol1 = "bit-string-symbol-1";
       
    32 }
    28 
    33 
    29 /**
    34 /**
    30  * Converts arbitrary ASN.1 stream of events to a relation
    35  * Converts arbitrary ASN.1 stream of events to a relation
    31  * i.e. does not require any specific structures/schema.
    36  * i.e. does not require any specific structures/schema.
    32  *
    37  *
    38 	wstring_convert < codecvt_utf8<wchar_t>> convertor; // ASN.1 parser works with UTF-8
    43 	wstring_convert < codecvt_utf8<wchar_t>> convertor; // ASN.1 parser works with UTF-8
    39 	std::shared_ptr<relpipe::writer::RelationalWriter> writer;
    44 	std::shared_ptr<relpipe::writer::RelationalWriter> writer;
    40 	Configuration configuration;
    45 	Configuration configuration;
    41 	std::vector<relpipe::common::type::Integer> position;
    46 	std::vector<relpipe::common::type::Integer> position;
    42 
    47 
       
    48 	std::string bitStringSymbol0 = "0";
       
    49 	std::string bitStringSymbol1 = "1";
       
    50 
    43 	class Record {
    51 	class Record {
    44 	public:
    52 	public:
    45 		const Header* header;
    53 		const Header* header;
    46 		const std::vector<relpipe::common::type::Integer>& position;
    54 		const std::vector<relpipe::common::type::Integer>& position;
    47 		relpipe::common::type::Integer level = 0;
    55 		relpipe::common::type::Integer level = 0;
   103 
   111 
   104 	FreeformASN1ContentHandler(std::shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) {
   112 	FreeformASN1ContentHandler(std::shared_ptr<relpipe::writer::RelationalWriter> writer, Configuration configuration) : writer(writer), configuration(configuration) {
   105 	}
   113 	}
   106 
   114 
   107 	bool setOption(const std::string& uri, const std::string& value) override {
   115 	bool setOption(const std::string& uri, const std::string& value) override {
   108 		return false;
   116 		if (uri == freeformOption::BitStringSymbol0) bitStringSymbol0 = value;
       
   117 		else if (uri == freeformOption::BitStringSymbol1) bitStringSymbol1 = value;
       
   118 		else return false;
       
   119 
       
   120 		return true;
   109 	}
   121 	}
   110 
   122 
   111 	void writeStreamStart() override {
   123 	void writeStreamStart() override {
   112 		writer->startRelation(configuration.relation,{
   124 		writer->startRelation(configuration.relation,{
   113 			{L"id", relpipe::writer::TypeId::INTEGER},
   125 			{L"id", relpipe::writer::TypeId::INTEGER},
   129 	}
   141 	}
   130 
   142 
   131 	void writeStreamEnd() override {
   143 	void writeStreamEnd() override {
   132 		Record r(nullptr, position, L"stream-end");
   144 		Record r(nullptr, position, L"stream-end");
   133 		write(r);
   145 		write(r);
   134 		
   146 
   135 		auto id = position.back() + 1;
   147 		auto id = position.back() + 1;
   136 		position.pop_back();
   148 		position.pop_back();
   137 		position.back() = id;
   149 		position.back() = id;
   138 	}
   150 	}
   139 
   151 
   145 	}
   157 	}
   146 
   158 
   147 	void writeCollectionEnd() override {
   159 	void writeCollectionEnd() override {
   148 		Record r(nullptr, position, L"collection-end");
   160 		Record r(nullptr, position, L"collection-end");
   149 		write(r);
   161 		write(r);
   150 		
   162 
   151 		auto id = position.back() + 1;
   163 		auto id = position.back() + 1;
   152 		position.pop_back();
   164 		position.pop_back();
   153 		position.back() = id;
   165 		position.back() = id;
   154 	}
   166 	}
   155 
   167 
   156 	void writeBitString(const Header& header, std::vector<bool> value) override {
   168 	void writeBitString(const Header& header, std::vector<bool> value) override {
   157 		std::stringstream bits;
   169 		std::stringstream bits;
   158 		for (bool b : value) bits << (int) b;
   170 		for (bool b : value) bits << (b ? bitStringSymbol1 : bitStringSymbol0);
   159 		// for (bool b : value) bits << (b ? ':' : '.'); // TODO: configurable true/false symbols?
       
   160 
   171 
   161 		position.back()++;
   172 		position.back()++;
   162 		Record r(&header, position, L"bit-string");
   173 		Record r(&header, position, L"bit-string");
   163 		r.valueText = convertor.from_bytes(bits.str());
   174 		r.valueText = convertor.from_bytes(bits.str());
   164 		write(r);
   175 		write(r);