generate random relation on SAX events v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 11 Jan 2019 01:13:40 +0100
branchv_0
changeset 3 4b566dee1a57
parent 2 3ab78bf63467
child 4 1363ec0879ca
generate random relation on SAX events
src/XMLCommand.h
--- a/src/XMLCommand.h	Fri Jan 11 00:29:32 2019 +0100
+++ b/src/XMLCommand.h	Fri Jan 11 01:13:40 2019 +0100
@@ -36,42 +36,66 @@
 namespace in {
 namespace xml {
 
+using namespace relpipe::writer;
+using namespace xercesc;
+
 class XMLCommand {
+private:
+
+	class RelpipeSaxHandler : public xercesc::DefaultHandler {
+	private:
+		unique_ptr<RelationalWriter> writer;
+
+	public:
+
+		RelpipeSaxHandler(std::ostream& output) : DefaultHandler(), writer(Factory::create(output)) {
+		}
+
+		void startElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname, const Attributes& attrs) override {
+			//XMLString::
+			// TODO: remove demo
+			writer->startRelation(L"xml_startElement",{
+				{L"s", TypeId::STRING},
+			}, true);
+
+			writer->writeAttribute(L"a");
+		}
+
+		void endElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname) override {
+			// TODO: remove demo
+			writer->startRelation(L"xml_endElement",{
+				{L"s", TypeId::STRING},
+			}, true);
+
+			writer->writeAttribute(L"a");
+		}
+
+		void endDocument() override {
+			// TODO: remove demo
+			writer->startRelation(L"xml_endDocument",{
+				{L"s", TypeId::STRING},
+			}, true);
+
+			writer->writeAttribute(L"a");
+		}
+
+	};
+
 public:
 
 	void process(std::istream& input, std::ostream& output) {
-		using namespace relpipe::writer;
-		using namespace xercesc;
-		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);
+		RelpipeSaxHandler saxHandler(output);
+		parser->setContentHandler(&saxHandler);
+		parser->setErrorHandler(&saxHandler);
 
 		StreamInputSource inputSource(input);
 
 		parser->parse(inputSource);
-
-
-		// TODO: remove demo
-		// Various data types passed as strings
-		writer->startRelation(L"xml",{
-			{L"s", TypeId::STRING},
-			{L"i", TypeId::INTEGER},
-			{L"b", TypeId::BOOLEAN}
-		}, true);
-
-		writer->writeAttribute(L"a");
-		writer->writeAttribute(L"1");
-		writer->writeAttribute(L"true");
-
 	}
 };