src/XMLCommand.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 11 Jan 2019 17:41:56 +0100
branchv_0
changeset 8 14e14a5db027
parent 7 85741b08db48
child 15 177321664baf
permissions -rw-r--r--
lossless bidirectional XML conversion of relational data Both generate same data: relpipe-in-cli demo | relpipe-out-xml | relpipe-in-xml | sha512sum relpipe-in-cli demo | sha512sum

/**
 * Relational pipes
 * Copyright © 2019 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 <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax2/Attributes.hpp>
#include <xercesc/util/XMLString.hpp>

#include <relpipe/writer/typedefs.h>

#include "StreamInputSource.h"
#include "XercesStringConvertor.h"

namespace relpipe {
namespace in {
namespace xml {

using namespace relpipe::writer;
using namespace xercesc;

class XMLCommand {
private:

	class RelpipeSaxHandler : public xercesc::DefaultHandler {
	private:
		const wstring XMLNS = L"tag:globalcode.info,2018:relpipe";
		unique_ptr<RelationalWriter> writer;
		XercesStringConvertor xConvertor;
		wstring currentRelationName;
		vector<AttributeMetadata> currentAttributes;
		wstringstream currentValue;

		void resetCurrentValue() {
			currentValue.str(L"");
			currentValue.clear();
		}

		string_t getAttributeName(const Attributes& attrs, string_t uri, string_t localname) {
			// TODO: less string conversions, better performance
			XMLCh* xUri = xConvertor.toXercesString(uri);
			XMLCh* xLocalName = xConvertor.toXercesString(localname);
			string_t value = xConvertor.toString(attrs.getValue(xUri, xLocalName));
			XMLString::release(&xUri);
			XMLString::release(&xLocalName);
			return value;
		}

		void startElement(const string_t uri, const string_t localname, const string_t qname, const Attributes& attrs) {
			if (uri == XMLNS) {
				if (localname == L"name") {
					resetCurrentValue();
				} else if (localname == L"attributes-metadata") {
					currentAttributes.clear();
				} else if (localname == L"attribute-metadata") {
					AttributeMetadata am;
					am.attributeName = getAttributeName(attrs, L"", L"name");
					am.typeId = writer->toTypeId(getAttributeName(attrs, L"", L"type"));
					currentAttributes.push_back(am);
				} else if (localname == L"attribute") {
					resetCurrentValue();
				}
			}
		}

		void endElement(const string_t uri, const string_t localname, const string_t qname) {
			if (uri == XMLNS) {
				if (localname == L"name") {
					currentRelationName = currentValue.str();
				} else if (localname == L"attributes-metadata") {
					writer->startRelation(currentRelationName, currentAttributes, true);
				} else if (localname == L"attribute") {
					writer->writeAttribute(currentValue.str());
				}
			}
		}

		void characters(const string_t chars) {
			currentValue << chars.c_str();
		}

	public:

		RelpipeSaxHandler(std::ostream& output) : DefaultHandler(), writer(Factory::create(output)) {
		}

		virtual ~RelpipeSaxHandler() {

		}

		void startElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname, const Attributes& attrs) override {
			startElement(xConvertor.toString(uri), xConvertor.toString(localname), xConvertor.toString(qname), attrs);
		}

		void endElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname) override {
			endElement(xConvertor.toString(uri), xConvertor.toString(localname), xConvertor.toString(qname));
		}

		void characters(const XMLCh * const chars, const XMLSize_t length) override {
			characters(xConvertor.toString(chars));
		}

	};

public:

	void process(std::istream& input, std::ostream& output) {
		XMLPlatformUtils::Initialize();
		unique_ptr<SAX2XMLReader> parser(XMLReaderFactory::createXMLReader());
		parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
		parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
		// TODO: optional unbuffered mode for more fluent output?
		// http://xerces.apache.org/xerces-c/program-sax2-3.html#SAX2Features
		// parser->setProperty(XMLUni::fgXercesLowWaterMark, ...);
		// parser->setInputBufferSize(...);

		RelpipeSaxHandler saxHandler(output);
		parser->setContentHandler(&saxHandler);
		parser->setErrorHandler(&saxHandler);

		StreamInputSource inputSource(input);

		parser->parse(inputSource);
	}
};

}
}
}