src/XercesStringConvertor.h
branchv_0
changeset 6 be83e0f457a8
child 8 14e14a5db027
equal deleted inserted replaced
5:e5cf88ce91ac 6:be83e0f457a8
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <string>
       
    21 #include <codecvt>
       
    22 
       
    23 #include <xercesc/util/XMLString.hpp>
       
    24 
       
    25 #include <relpipe/writer/typedefs.h>
       
    26 
       
    27 namespace relpipe {
       
    28 namespace in {
       
    29 namespace xml {
       
    30 
       
    31 using namespace std;
       
    32 using namespace xercesc;
       
    33 using namespace relpipe::writer;
       
    34 
       
    35 class XercesStringConvertor {
       
    36 private:
       
    37 	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
       
    38 
       
    39 public:
       
    40 
       
    41 	string_t toString(const XMLCh * const chars) {
       
    42 		// XMLCh = char16_t
       
    43 		// „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.“
       
    44 		// see https://xerces.apache.org/xerces-c/program-others-3.html
       
    45 		// other solution (depends on boost): https://flylib.com/books/en/2.131.1/working_with_xerces_strings.html
       
    46 
       
    47 		// TODO: review this text conversion and test on various platforms
       
    48 		char* x = XMLString::transcode(chars);
       
    49 		string s = string(x);
       
    50 		XMLString::release(&x);
       
    51 		return convertor.from_bytes(s);
       
    52 	}
       
    53 };
       
    54 
       
    55 }
       
    56 }
       
    57 }