/**
* 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, 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 <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());
}
};
}
}
}