-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxercesc.cpp
97 lines (84 loc) · 2.96 KB
/
xercesc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright 2009 Luis Henrique O. Rios
*
* This file is part of YAFS.
*
* YAFS 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.
*
* YAFS 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 YAFS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xercesc.h"
#include <cassert>
#include <string>
/* Xerces includes: */
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/TransService.hpp>
using namespace std;
XERCES_CPP_NAMESPACE_USE
bool Xercesc::xerces_was_initialized = false;
XMLTranscoder *Xercesc::xml_transcoder_utf8 = NULL;
void Xercesc::Initialize(){
if(xerces_was_initialized) return;
try{
XMLTransService::Codes res_value;
XMLPlatformUtils::Initialize();
xml_transcoder_utf8 = XMLPlatformUtils::fgTransService->makeNewTranscoderFor("UTF8" ,
res_value , 64);
xerces_was_initialized = true;
}catch(XMLException &xml_exception){
char *message_buffer = XMLString::transcode(xml_exception.getMessage());
string message(message_buffer);
XMLString::release(&message_buffer);
throw XercescException(message);
}
}
void Xercesc::Terminate(){
assert(xerces_was_initialized);
try{
delete xml_transcoder_utf8;
XMLPlatformUtils::Terminate();
xerces_was_initialized = false;
}catch(XMLException &xml_exception){
char *message_buffer = XMLString::transcode(xml_exception.getMessage());
string message(message_buffer);
XMLString::release(&message_buffer);
throw XercescException(message);
}
}
uint8* Xercesc::TranscodeToUTF8(const XMLCh *string){
const uint32 buffer_length = 4096;
uint8* string_utf8 , buffer[buffer_length];
size_t size, length;
XMLSize_t characters_processed;
length = XMLString::stringLen(string);
size = xml_transcoder_utf8->transcodeTo(string , length , (XMLByte*)buffer ,
buffer_length , characters_processed , XMLTranscoder::UnRep_Throw);
assert(characters_processed <= length);
string_utf8 = new uint8[size + 1];
memcpy(string_utf8 , buffer , size);
string_utf8[size] = '\0';
return string_utf8;
}
XMLCh* Xercesc::TranscodeFromUTF8(const uint8 *string){
const uint32 buffer_length = 4096;
XMLCh *string_xmlch , buffer[buffer_length];
uint8 charSizes[buffer_length];
size_t size, length;
XMLSize_t characters_processed;
length = strlen((const char*)string);
size = xml_transcoder_utf8->transcodeFrom((XMLByte*)string , length , buffer ,
buffer_length , characters_processed , charSizes);
assert(characters_processed <= length);
string_xmlch = new XMLCh[size + 1];
memcpy(string_xmlch , buffer , (size + 1) * sizeof(XMLCh));
return string_xmlch;
}