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