src/XMLDocumentConstructor.h
branchv_0
changeset 0 ea26b3359fed
child 1 356d0b024abf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/XMLDocumentConstructor.h	Sat Jan 16 16:36:39 2021 +0100
@@ -0,0 +1,84 @@
+/**
+ * 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 <codecvt>
+#include <vector>
+
+#include <libxml++-2.6/libxml++/libxml++.h>
+
+#include <vmime/vmime.hpp>
+
+#include "XMLNameCodec.h"
+
+namespace relpipe {
+namespace in {
+namespace xmltable {
+
+class XMLDocumentConstructor {
+private:
+	std::istream* input = nullptr;
+	xmlpp::DomParser* parser = nullptr;
+	XMLNameCodec nameCodec;
+
+	enum class Mode {
+		ROOT,
+		SEQUENCE,
+		MAPPING,
+		MAP_KEY
+	};
+
+	std::string rootName = "mime";
+	xmlpp::Element* current;
+	std::vector<Mode> mode;
+
+	/**
+	 * Both MIME and XML strings are in UTF-8.
+	const char* y2x(mime_char_t* value) {
+		return value ? (const char*) value : "";
+	}
+
+	const Glib::ustring y2xname(mime_char_t* value) {
+		return nameCodec.encode(y2x(value));
+	}
+	 */
+
+	xmlpp::Element* parentOrSelf(xmlpp::Element* current) {
+		return current->get_parent() == nullptr ? current : current->get_parent();
+	}
+
+public:
+
+	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
+	}
+
+	virtual ~XMLDocumentConstructor() {
+	}
+
+	void setOption(const std::string& uri, const std::string& value) {
+		if (uri == "root-name") rootName = value;
+		else throw std::invalid_argument(std::string("Invalid parser option: „") + uri + "“ with value: „" + value + "“");
+	}
+
+	void process() {
+		current = parser->get_document()->create_root_node(rootName);
+	}
+};
+
+}
+}
+}