|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <string> |
|
20 #include <stdexcept> |
|
21 |
|
22 #include "ASN1ContentHandler.h" |
|
23 |
|
24 namespace relpipe { |
|
25 namespace in { |
|
26 namespace asn1 { |
|
27 namespace lib { |
|
28 |
|
29 /** |
|
30 * Does not generate any output but enforces certain rules (related to stream and collection start/end). |
|
31 * When invalid behavior occurs, an exception is thrown. |
|
32 * |
|
33 */ |
|
34 class ValidatingASN1ContentHandler : public ASN1ContentHandler { |
|
35 private: |
|
36 bool streamOpened = false; |
|
37 bool streamClosed = false; |
|
38 size_t collectionLevel = 0; |
|
39 public: |
|
40 |
|
41 virtual void writeStreamStart() { |
|
42 if (streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: tried to open the stream twice"); // TODO: better exception |
|
43 if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to open the stream that was already closed"); // TODO: better exception |
|
44 streamOpened = true; |
|
45 } |
|
46 |
|
47 virtual void writeStreamEnd() { |
|
48 if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to close the stream twice"); // TODO: better exception |
|
49 if (!streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: tried to close a stream that was already closed or never opened"); // TODO: better exception |
|
50 if (collectionLevel != 0) throw std::logic_error("ValidatingASN1ContentHandler: not all opened collections was closed – remaining: " + std::to_string(collectionLevel)); // TODO: better exception |
|
51 streamClosed = true; |
|
52 } |
|
53 |
|
54 virtual void writeCollectionStart(const Header& header) { |
|
55 if (!streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: tried to open a collection while the stream was not opened"); // TODO: better exception |
|
56 if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to open a collection while the stream was already closed"); // TODO: better exception |
|
57 collectionLevel++; |
|
58 } |
|
59 |
|
60 virtual void writeCollectionEnd() { |
|
61 if (streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: tried to close a collection while the stream was already closed"); // TODO: better exception |
|
62 if (collectionLevel == 0) throw std::logic_error("ValidatingASN1ContentHandler: tried to close a collection while none was opened"); // TODO: better exception |
|
63 collectionLevel--; |
|
64 } |
|
65 |
|
66 virtual void finalCheck() { |
|
67 if (!streamOpened) throw std::logic_error("ValidatingASN1ContentHandler: the stream was not opened at all"); // TODO: better exception |
|
68 if (!streamClosed) throw std::logic_error("ValidatingASN1ContentHandler: the stream was opened but not closed"); // TODO: better exception |
|
69 } |
|
70 |
|
71 // These events are intentionally ignored: |
|
72 |
|
73 void writeBitString(const Header& header, std::vector<bool> value) override { |
|
74 } |
|
75 |
|
76 void writeBoolean(const Header& header, bool value) override { |
|
77 } |
|
78 |
|
79 void writeDateTime(const Header& header, DateTime value) override { |
|
80 } |
|
81 |
|
82 void writeInteger(const Header& header, Integer value) override { |
|
83 } |
|
84 |
|
85 void writeNull(const Header& header) override { |
|
86 } |
|
87 |
|
88 void writeOID(const Header& header, ObjectIdentifier value) override { |
|
89 } |
|
90 |
|
91 void writeOctetString(const Header& header, std::string value) override { |
|
92 } |
|
93 |
|
94 void writeTextString(const Header& header, std::string value) override { |
|
95 } |
|
96 |
|
97 void writeSpecific(const Header& header, std::string value) override { |
|
98 } |
|
99 |
|
100 }; |
|
101 |
|
102 } |
|
103 } |
|
104 } |
|
105 } |