read XML from istream using Xerces (just parse/validate, no processing yet) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 11 Jan 2019 00:29:32 +0100
branchv_0
changeset 2 3ab78bf63467
parent 1 9aed8c2ea97d
child 3 4b566dee1a57
read XML from istream using Xerces (just parse/validate, no processing yet)
nbproject/configurations.xml
src/StreamInputSource.h
src/XMLCommand.h
src/relpipe-in-xml.cpp
--- a/nbproject/configurations.xml	Thu Jan 10 20:05:13 2019 +0100
+++ b/nbproject/configurations.xml	Fri Jan 11 00:29:32 2019 +0100
@@ -42,6 +42,7 @@
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df root="." name="0">
       <df name="src">
+        <in>StreamInputSource.h</in>
         <in>relpipe-in-xml.cpp</in>
       </df>
     </df>
@@ -91,6 +92,8 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
+      <item path="src/StreamInputSource.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="src/relpipe-in-xml.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
@@ -130,6 +133,8 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
+      <item path="src/StreamInputSource.h" ex="false" tool="3" flavor2="0">
+      </item>
       <item path="src/relpipe-in-xml.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/StreamInputSource.h	Fri Jan 11 00:29:32 2019 +0100
@@ -0,0 +1,72 @@
+/**
+ * Relational pipes
+ * Copyright © 2018 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, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 <istream>
+
+#include <xercesc/sax/InputSource.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+#include <xercesc/util/BinInputStream.hpp>
+
+namespace relpipe {
+namespace in {
+namespace xml {
+
+class StreamInputSource : public xercesc::InputSource {
+private:
+	std::istream& input;
+
+	class StreamBinInputStream : public xercesc::BinInputStream {
+	private:
+		std::istream& input;
+	public:
+
+		StreamBinInputStream(std::istream& input) : BinInputStream(), input(input) {
+		}
+
+		XMLFilePos curPos() const override {
+			return input.tellg();
+		}
+
+		const XMLCh* getContentType() const override {
+			return nullptr;
+		}
+
+		XMLSize_t readBytes(XMLByte * const toFill, const XMLSize_t maxToRead) override {
+			input.read((char*) toFill, maxToRead);
+			return input.gcount();
+		}
+	};
+
+public:
+
+	StreamInputSource(std::istream& input, xercesc::MemoryManager * const manager = xercesc::XMLPlatformUtils::fgMemoryManager) : InputSource(manager), input(input) {
+	}
+
+	virtual ~StreamInputSource() {
+	}
+
+	xercesc::BinInputStream* makeStream() const override {
+		// TODO: avoid multiple calls, input can be used only once 
+		return new StreamBinInputStream(input);
+	}
+};
+
+}
+}
+}
\ No newline at end of file
--- a/src/XMLCommand.h	Thu Jan 10 20:05:13 2019 +0100
+++ b/src/XMLCommand.h	Fri Jan 11 00:29:32 2019 +0100
@@ -30,6 +30,8 @@
 
 #include <relpipe/writer/typedefs.h>
 
+#include "StreamInputSource.h"
+
 namespace relpipe {
 namespace in {
 namespace xml {
@@ -40,7 +42,23 @@
 	void process(std::istream& input, std::ostream& output) {
 		using namespace relpipe::writer;
 		using namespace xercesc;
-		std::shared_ptr<RelationalWriter> writer(Factory::create(output));
+		unique_ptr<RelationalWriter> writer(Factory::create(output));
+		XMLPlatformUtils::Initialize();
+
+
+		unique_ptr<SAX2XMLReader> parser(XMLReaderFactory::createXMLReader());
+		parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
+		parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
+
+		// TODO: custom handler that reads
+		DefaultHandler defaultHandler;
+		parser->setContentHandler(&defaultHandler);
+		parser->setErrorHandler(&defaultHandler);
+
+		StreamInputSource inputSource(input);
+
+		parser->parse(inputSource);
+
 
 		// TODO: remove demo
 		// Various data types passed as strings
@@ -53,7 +71,7 @@
 		writer->writeAttribute(L"a");
 		writer->writeAttribute(L"1");
 		writer->writeAttribute(L"true");
-		
+
 	}
 };
 
--- a/src/relpipe-in-xml.cpp	Thu Jan 10 20:05:13 2019 +0100
+++ b/src/relpipe-in-xml.cpp	Fri Jan 11 00:29:32 2019 +0100
@@ -46,10 +46,16 @@
 		XMLCommand command;
 		command.process(cin, cout);
 		resultCode = CLI::EXIT_CODE_SUCCESS;
-	} catch (RelpipeWriterException e) {
+	} catch (RelpipeWriterException& e) {
 		fwprintf(stderr, L"Caught Writer exception: %ls\n", e.getMessge().c_str());
 		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
 		resultCode = CLI::EXIT_CODE_DATA_ERROR;
+	} catch (const xercesc::XMLException& e) {
+		// TODO: print message
+		// char* message = xercesc::XMLString::transcode(e.getMessage());
+		fwprintf(stderr, L"Caught xercesc::XMLException\n");
+		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
+		resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
 	}
 
 	return resultCode;