src/XercesStringConvertor.h
author František Kučera <franta-hg@frantovo.cz>
Fri, 11 Jan 2019 17:41:56 +0100
branchv_0
changeset 8 14e14a5db027
parent 6 be83e0f457a8
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 <string>
#include <codecvt>

#include <xercesc/util/XMLString.hpp>

#include <relpipe/writer/typedefs.h>

namespace relpipe {
namespace in {
namespace xml {

using namespace std;
using namespace xercesc;
using namespace relpipe::writer;

class XercesStringConvertor {
private:
	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.

public:

	string_t toString(const XMLCh* const chars) {
		// XMLCh = char16_t
		// „All XML data is handled within Xerces-C++ as strings of XMLCh characters. Regardless of the size of the type chosen, the data stored in variables of type XMLCh will always be utf-16 encoded values.“
		// see https://xerces.apache.org/xerces-c/program-others-3.html
		// other solution (depends on boost): https://flylib.com/books/en/2.131.1/working_with_xerces_strings.html

		// TODO: review this text conversion and test on various platforms
		char* x = XMLString::transcode(chars);
		string s = string(x);
		XMLString::release(&x);
		return convertor.from_bytes(s);
	}

	/**
	 * @param string
	 * @return Xerces string. Must be released manually after use, see XMLString::release().
	 */
	XMLCh* toXercesString(string_t string) {
		return XMLString::transcode(convertor.to_bytes(string).c_str());
	}
};

}
}
}