src/XercesStringConvertor.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 19:53:12 +0200
branchv_0
changeset 15 177321664baf
parent 8 14e14a5db027
permissions -rw-r--r--
fix license version: GNU GPLv3
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
15
177321664baf fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
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
     8
 *
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
 * 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
    10
 * 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
    11
 * 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
    12
 * 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
    13
 *
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
 * 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
    15
 * 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
    16
 */
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
#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
    18
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
#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
    20
#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
    21
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
#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
    23
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
#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
    25
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
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
    27
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
    28
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
    29
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
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
    31
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
    32
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
    33
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
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
    35
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
    36
	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
    37
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
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
    39
8
14e14a5db027 lossless bidirectional XML conversion of relational data
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    40
	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
    41
		// 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
    42
		// „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
    43
		// 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
    44
		// 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
    45
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
		// 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
    47
		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
    48
		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
    49
		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
    50
		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
    51
	}
8
14e14a5db027 lossless bidirectional XML conversion of relational data
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    52
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
	 * @param string
14e14a5db027 lossless bidirectional XML conversion of relational data
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    55
	 * @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
    56
	 */
14e14a5db027 lossless bidirectional XML conversion of relational data
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    57
	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
    58
		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
    59
	}
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
    60
};
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
}