AbstractParser: AbstractParser, ASN1Reader, ASN1ContentHandler, SAXContentHandler, BasicASN1Reader, DOMBuildingSAXContentHandler, GenericASN1ContentHandler skeletons v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 13 Mar 2021 17:58:09 +0100
branchv_0
changeset 1 2179f13227f4
parent 0 bfee08a31fdc
child 2 8b8175615adb
AbstractParser: AbstractParser, ASN1Reader, ASN1ContentHandler, SAXContentHandler, BasicASN1Reader, DOMBuildingSAXContentHandler, GenericASN1ContentHandler skeletons
nbproject/configurations.xml
src/CMakeLists.txt
src/XMLDocumentConstructor.h
src/lib/ASN1ContentHandler.h
src/lib/ASN1Reader.h
src/lib/AbstractParser.cpp
src/lib/AbstractParser.h
src/lib/BasicASN1Reader.h
src/lib/DOMBuildingSAXContentHandler.h
src/lib/GenericASN1ContentHandler.h
src/lib/SAXContentHandler.h
--- a/nbproject/configurations.xml	Sun Mar 07 17:50:11 2021 +0100
+++ b/nbproject/configurations.xml	Sat Mar 13 17:58:09 2021 +0100
@@ -42,6 +42,16 @@
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df root="." name="0">
       <df name="src">
+        <df name="lib">
+          <in>ASN1ContentHandler.h</in>
+          <in>ASN1Reader.h</in>
+          <in>AbstractParser.cpp</in>
+          <in>AbstractParser.h</in>
+          <in>BasicASN1Reader.h</in>
+          <in>DOMBuildingSAXContentHandler.h</in>
+          <in>GenericASN1ContentHandler.h</in>
+          <in>SAXContentHandler.h</in>
+        </df>
         <in>XMLDocumentConstructor.h</in>
         <in>relpipe-in-xmltable.cpp</in>
       </df>
@@ -102,6 +112,28 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
+      <item path="src/lib/ASN1ContentHandler.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/ASN1Reader.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/AbstractParser.cpp" ex="false" tool="1" flavor2="0">
+      </item>
+      <item path="src/lib/AbstractParser.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/BasicASN1Reader.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/DOMBuildingSAXContentHandler.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
+      <item path="src/lib/GenericASN1ContentHandler.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
+      <item path="src/lib/SAXContentHandler.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="src/relpipe-in-xmltable.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
@@ -143,6 +175,28 @@
       </makefileType>
       <item path="src/XMLDocumentConstructor.h" ex="false" tool="3" flavor2="0">
       </item>
+      <item path="src/lib/ASN1ContentHandler.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/ASN1Reader.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/AbstractParser.cpp" ex="false" tool="1" flavor2="0">
+      </item>
+      <item path="src/lib/AbstractParser.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/BasicASN1Reader.h" ex="false" tool="3" flavor2="0">
+      </item>
+      <item path="src/lib/DOMBuildingSAXContentHandler.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
+      <item path="src/lib/GenericASN1ContentHandler.h"
+            ex="false"
+            tool="3"
+            flavor2="0">
+      </item>
+      <item path="src/lib/SAXContentHandler.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="src/relpipe-in-xmltable.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
--- a/src/CMakeLists.txt	Sun Mar 07 17:50:11 2021 +0100
+++ b/src/CMakeLists.txt	Sat Mar 13 17:58:09 2021 +0100
@@ -30,6 +30,7 @@
 add_executable(
 	${EXECUTABLE_FILE}
 	relpipe-in-xmltable.cpp
+	lib/AbstractParser.cpp
 )
 
 # Link libraries:
--- a/src/XMLDocumentConstructor.h	Sun Mar 07 17:50:11 2021 +0100
+++ b/src/XMLDocumentConstructor.h	Sat Mar 13 17:58:09 2021 +0100
@@ -16,12 +16,16 @@
  */
 #pragma once
 
+#include <libxml++-2.6/libxml++/libxml++.h>
+
+#include "lib/BasicASN1Reader.h"
+#include "lib/GenericASN1ContentHandler.h"
+#include "lib/DOMBuildingSAXContentHandler.h"
+
 namespace relpipe {
 namespace in {
 namespace xmltable {
 
-#include <libxml++-2.6/libxml++/libxml++.h>
-
 class XMLDocumentConstructor {
 private:
 	std::istream* input = nullptr;
@@ -30,11 +34,15 @@
 
 	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
 	}
-	
+
 	void setOption(const std::string& uri, const std::string& value) {
 	}
 
 	void process() {
+
+		relpipe::in::asn1::lib::BasicASN1Reader reader;
+		reader.write(nullptr, 0); // FIXME: transfer data from input to reader + use DOMBuildingSAXContentHandler
+
 		parser->parse_stream(*input);
 	}
 };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/ASN1ContentHandler.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,30 @@
+/**
+ * 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
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class ASN1ContentHandler {
+};
+
+}
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/ASN1Reader.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,39 @@
+/**
+ * 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 <memory>
+
+#include "AbstractParser.h"
+#include "ASN1ContentHandler.h"
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class ASN1Reader : public AbstractParser {
+public:
+	virtual ~ASN1Reader() = default;
+	virtual void addHandler(std::shared_ptr<ASN1ContentHandler> handler) = 0;
+	virtual void process() = 0;
+};
+
+}
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/AbstractParser.cpp	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,95 @@
+/**
+ * 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/>.
+ */
+
+#include "AbstractParser.h"
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class AbstractParserImpl {
+private:
+	AbstractParser* interface;
+public:
+
+	AbstractParserImpl(AbstractParser* interface) : interface(interface) {
+	}
+
+	virtual ~AbstractParserImpl() {
+	}
+
+	void write(const char* buffer, const size_t length) {
+		// TODO: update pointers and positions
+		try {
+			// TODO: call an overridable method to get preferred minimum block size and run cycle only if we have enough data or EOF
+			interface->update();
+			commit();
+		} catch (const AbstractParser::ReadBufferUnderflowException& e) {
+			rollback();
+		} catch (const AbstractParser::ExplicitRollbackException& e) {
+			rollback();
+		}
+	}
+
+	void rollback() {
+		// FIXME: store content of the current buffer + update pointers and positions
+		// TODO: notify rollback listeners? (they can monitor the performance / frequency of rollbacks)
+	}
+
+	void commit() {
+	}
+
+	void read(char* buffer, const size_t length) {
+	}
+
+	void peek(char* buffer, const size_t length) {
+	}
+};
+
+AbstractParser::AbstractParser() {
+	implementation = new AbstractParserImpl(this);
+}
+
+AbstractParser::~AbstractParser() {
+	delete (AbstractParserImpl*) implementation;
+}
+
+void AbstractParser::write(const char* buffer, const size_t length) {
+	((AbstractParserImpl*) implementation)->write(buffer, length);
+}
+
+void AbstractParser::commit() {
+	((AbstractParserImpl*) implementation)->commit();
+}
+
+void AbstractParser::rollback() {
+	((AbstractParserImpl*) implementation)->rollback();
+}
+
+void AbstractParser::read(char* buffer, const size_t length) {
+	((AbstractParserImpl*) implementation)->read(buffer, length);
+}
+
+void AbstractParser::peek(char* buffer, const size_t length) {
+	((AbstractParserImpl*) implementation)->peek(buffer, length);
+}
+
+}
+}
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/AbstractParser.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,91 @@
+/**
+ * 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 <cstring>
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class AbstractParserImpl;
+
+class AbstractParser {
+private:
+	friend AbstractParserImpl;
+	AbstractParserImpl* implementation;
+public:
+	virtual ~AbstractParser();
+	void write(const char* buffer, const size_t length);
+	void rollback();
+protected:
+	AbstractParser();
+
+	/**
+	 * Is thrown from read() and peak() methods if there are not enough data.
+	 * Interrupts current update() cycle and causes rollback.
+	 */
+	class ReadBufferUnderflowException {
+		// TODO: common super-class for exceptions, hidden implementation
+	};
+
+	/**
+	 * May be thrown from the update() method in order to cancel currenty cycle and do explicit rollback.
+	 * Same data will be processed in the next cycle.
+	 */
+	class ExplicitRollbackException {
+		// TODO: common super-class for exceptions, hidden implementation
+	};
+
+	/**
+	 * May be called from the update() method in order to explicitly confirm that read data was successfully processed.
+	 * Such data will not be processed again during the next cycle (even if ReadBufferUnderflowException occurs later in this cycle).
+	 
+	 * Explicit commit() call is useful when we did some demanding work (so we are not willing to do it again in case of ReadBufferUnderflowException);
+	 * and is necessary when we already published some outputs or did other non-idempotent operation or caused some other significant side effects.
+	 
+	 * If there is no commit() called and update() just finishes, commit() is called implicitly.
+	 * 
+	 * Note: There is no accessible rollback() method – throw ExplicitRollbackException instead.
+	 */
+	void commit();
+
+	/**
+	 * 
+	 * @param buffer
+	 * @param length
+	 */
+	void read(char* buffer, const size_t length);
+
+	/**
+	 * 
+	 * @param buffer
+	 * @param length
+	 */
+	void peek(char* buffer, const size_t length);
+
+	/**
+	 *  
+	 */
+	virtual void update() = 0;
+};
+
+}
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/BasicASN1Reader.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,76 @@
+/**
+ * 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 <memory>
+
+#include "ASN1Reader.h"
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+/**
+ * Reads ASN.1 data encoded as BER (DER, CER).
+ */
+class BasicASN1Reader : public ASN1Reader {
+public:
+	virtual ~BasicASN1Reader() = default;
+
+	virtual void addHandler(std::shared_ptr<ASN1ContentHandler> handler) {
+	};
+
+	virtual void process() {
+	};
+
+	static ASN1Reader* create(std::istream& input) {
+		return new BasicASN1Reader();
+	}
+
+private:
+
+	enum class State {
+		A,
+		B,
+		C
+	};
+
+	State state = State::A;
+
+protected:
+
+	void update() override {
+
+		if (state == State::A) {
+			
+		} else if (state == State::B) {
+			
+		} else if (state == State::C) {
+			
+		}
+
+
+
+	}
+
+};
+
+}
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/DOMBuildingSAXContentHandler.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,46 @@
+/**
+ * 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 <libxml++-2.6/libxml++/libxml++.h>
+
+#include "SAXContentHandler.h"
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class DOMBuildingSAXContentHandler : public SAXContentHandler {
+private:
+	xmlpp::Document* document;
+
+	DOMBuildingSAXContentHandler(xmlpp::Document* document) : document(document) {
+	}
+
+public:
+
+	static DOMBuildingSAXContentHandler* create(xmlpp::Document* document) {
+		return new DOMBuildingSAXContentHandler(document);
+	}
+
+};
+
+}
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/GenericASN1ContentHandler.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,30 @@
+/**
+ * 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
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class GenericASN1ContentHandler : public ASN1ContentHandler {
+};
+
+}
+}
+}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/SAXContentHandler.h	Sat Mar 13 17:58:09 2021 +0100
@@ -0,0 +1,34 @@
+/**
+ * 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
+
+namespace relpipe {
+namespace in {
+namespace asn1 {
+namespace lib {
+
+class SAXContentHandler {
+public:
+
+	virtual ~SAXContentHandler() = default;
+
+};
+
+}
+}
+}
+}