src/lib/ValidatingASN1ContentHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 27 Jul 2021 18:33:27 +0200
branchv_0
changeset 8 3192dc8772de
parent 1 68a281aefa76
permissions -rw-r--r--
FreeformASN1ContentHandler: put (collection|stream)-end events under (collection|stream)-start parents

/**
 * 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 <string>
#include <stdexcept>

#include "ASN1ContentHandler.h"

namespace relpipe {
namespace in {
namespace asn1 {
namespace lib {

/**
 * Does not generate any output but enforces certain rules (related to stream and collection start/end).
 * When invalid behavior occurs, an exception is thrown.
 * 
 */
class ValidatingASN1ContentHandler : public ASN1ContentHandler {
private:
	bool streamOpened = false;
	bool streamClosed = false;
	size_t collectionLevel = 0;
public:

	virtual void writeStreamStart() {
		if (streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: tried to open the stream twice"); // TODO: better exception
		if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to open the stream that was already closed"); // TODO: better exception
		streamOpened = true;
	}

	virtual void writeStreamEnd() {
		if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to close the stream twice"); // TODO: better exception
		if (!streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: tried to close a stream that was already closed or never opened"); // TODO: better exception
		if (collectionLevel != 0) throw std::logic_error("ValidatingASN1ContentHandler: not all opened collections was closed – remaining: " + std::to_string(collectionLevel)); // TODO: better exception
		streamClosed = true;
	}

	virtual void writeCollectionStart(const Header& header) {
		if (!streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: tried to open a collection while the stream was not opened"); // TODO: better exception
		if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to open a collection while the stream was already closed"); // TODO: better exception
		collectionLevel++;
	}

	virtual void writeCollectionEnd() {
		if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to close a collection while the stream was already closed"); // TODO: better exception
		if (collectionLevel == 0) throw std::logic_error("ValidatingASN1ContentHandler: tried to close a collection while none was opened"); // TODO: better exception
		collectionLevel--;
	}

	virtual void finalCheck() {
		if (!streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: the stream was not opened at all"); // TODO: better exception
		if (!streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: the stream was opened but not closed"); // TODO: better exception
	}

	// These events are intentionally ignored:

	void writeBitString(const Header& header, std::vector<bool> value) override {
	}

	void writeBoolean(const Header& header, bool value) override {
	}

	void writeDateTime(const Header& header, DateTime value) override {
	}

	void writeInteger(const Header& header, Integer value) override {
	}

	void writeNull(const Header& header) override {
	}

	void writeOID(const Header& header, ObjectIdentifier value) override {
	}

	void writeOctetString(const Header& header, std::string value) override {
	}

	void writeTextString(const Header& header, std::string value) override {
	}

	void writeSpecific(const Header& header, std::string value) override {
	}

};

}
}
}
}