XMLTools.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef BASE_XMLTOOLS_H
00027 #define BASE_XMLTOOLS_H
00028
00029
00030
00031
00032
00033 #include <memory>
00034 #include <iostream>
00035 #include <xercesc/util/XercesDefs.hpp>
00036 #include <xercesc/util/XercesVersion.hpp>
00037 #include <xercesc/util/XMLString.hpp>
00038 #include <xercesc/util/PlatformUtils.hpp>
00039 #include <xercesc/util/TransService.hpp>
00040
00041 #include <Base/Exception.h>
00042
00043 XERCES_CPP_NAMESPACE_BEGIN
00044 class DOMNode;
00045 class DOMElement;
00046 class DOMDocument;
00047 XERCES_CPP_NAMESPACE_END
00048
00049
00050
00051
00052
00053
00054
00055 class StrX
00056 {
00057 public :
00058 StrX(const XMLCh* const toTranscode);
00059 ~StrX();
00060
00062 const char* c_str() const;
00063
00064 private :
00065
00066 char* fLocalForm;
00067 };
00068
00069 inline std::ostream& operator<<(std::ostream& target, const StrX& toDump)
00070 {
00071 target << toDump.c_str();
00072 return target;
00073 }
00074
00075 inline StrX::StrX(const XMLCh* const toTranscode)
00076 {
00077
00078 fLocalForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
00079
00080
00081
00082
00083
00084
00085 }
00086
00087 inline StrX::~StrX()
00088 {
00089
00090 XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fLocalForm);
00091 }
00092
00093
00094
00095
00096
00097 inline const char* StrX::c_str() const
00098 {
00099 return fLocalForm;
00100 }
00101
00102
00103
00104
00105
00106
00107 class StrXUTF8
00108 {
00109 public :
00110 StrXUTF8(const XMLCh* const toTranscode);
00111
00113 const char* c_str() const;
00115 std::string str;
00116
00117 private :
00118 static std::auto_ptr<XERCES_CPP_NAMESPACE::XMLTranscoder> transcoder;
00119
00120 };
00121
00122 inline std::ostream& operator<<(std::ostream& target, const StrXUTF8& toDump)
00123 {
00124 target << toDump.c_str();
00125 return target;
00126 }
00127
00128 inline StrXUTF8::StrXUTF8(const XMLCh* const toTranscode)
00129 {
00130 XERCES_CPP_NAMESPACE_USE;
00131 if(!transcoder.get()){
00132 XMLTransService::Codes res;
00133 transcoder.reset(XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgTransService->makeNewTranscoderFor(XERCES_CPP_NAMESPACE_QUALIFIER XMLRecognizer::UTF_8, res, 4096, XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgMemoryManager));
00134 if (res != XMLTransService::Ok)
00135 throw Base::Exception("Cant create UTF-8 encoder in StrXUTF8::StrXUTF8()");
00136 }
00137
00138
00139 static XMLByte outBuff[128];
00140 #if (XERCES_VERSION_MAJOR == 2)
00141 unsigned int outputLength;
00142 unsigned int eaten = 0;
00143 unsigned int offset = 0;
00144 unsigned int inputLength = XMLString::stringLen(toTranscode);
00145 #else
00146 XMLSize_t outputLength;
00147 XMLSize_t eaten = 0;
00148 XMLSize_t offset = 0;
00149 XMLSize_t inputLength = XMLString::stringLen(toTranscode);
00150 #endif
00151
00152 while (inputLength)
00153 {
00154 outputLength = transcoder->transcodeTo(toTranscode + offset, inputLength, outBuff, 128, eaten, XMLTranscoder::UnRep_RepChar);
00155 str.append((const char*)outBuff, outputLength);
00156 offset += eaten;
00157 inputLength -= eaten;
00158 }
00159 }
00160
00161
00162
00163
00164 inline const char* StrXUTF8::c_str() const
00165 {
00166 return str.c_str();
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176 class XStr
00177 {
00178 public :
00180 XStr(const char* const toTranscode);
00182 ~XStr();
00183
00184
00186 const XMLCh* unicodeForm() const;
00187
00188 private :
00190 XMLCh* fUnicodeForm;
00191 };
00192
00193
00194 inline XStr::XStr(const char* const toTranscode)
00195 {
00196
00197
00198
00199
00200
00201 fUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
00202
00203 }
00204
00205 inline XStr::~XStr()
00206 {
00207
00208 XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fUnicodeForm);
00209 }
00210
00211
00212
00213
00214
00215 inline const XMLCh* XStr::unicodeForm() const
00216 {
00217 return fUnicodeForm;
00218 }
00219
00220
00221
00222
00223
00224
00225 class XUTF8Str
00226 {
00227 public :
00228 XUTF8Str(const char* const fromTranscode);
00229 ~XUTF8Str();
00230
00232 const XMLCh* unicodeForm() const;
00233
00234 private :
00235 std::basic_string<XMLCh> str;
00236 static std::auto_ptr<XERCES_CPP_NAMESPACE::XMLTranscoder> transcoder;
00237 };
00238
00239 inline XUTF8Str::XUTF8Str(const char* const fromTranscode)
00240 {
00241 if (!fromTranscode)
00242 return;
00243
00244 XERCES_CPP_NAMESPACE_USE;
00245 if(!transcoder.get()){
00246 XMLTransService::Codes res;
00247 transcoder.reset(XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgTransService->makeNewTranscoderFor(XERCES_CPP_NAMESPACE_QUALIFIER XMLRecognizer::UTF_8, res, 4096, XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgMemoryManager));
00248 if (res != XMLTransService::Ok)
00249 throw Base::Exception("Cant create UTF-8 decoder in XUTF8Str::XUTF8Str()");
00250 }
00251
00252 static XMLCh outBuff[128];
00253 XMLByte* xmlBytes = (XMLByte*)fromTranscode;
00254 #if (XERCES_VERSION_MAJOR == 2)
00255 unsigned int outputLength;
00256 unsigned int eaten = 0;
00257 unsigned int offset = 0;
00258 unsigned int inputLength = std::string(fromTranscode).size();
00259 #else
00260 XMLSize_t outputLength;
00261 XMLSize_t eaten = 0;
00262 XMLSize_t offset = 0;
00263 XMLSize_t inputLength = std::string(fromTranscode).size();
00264 #endif
00265
00266 unsigned char* charSizes = new unsigned char[inputLength];
00267 while (inputLength)
00268 {
00269 outputLength = transcoder->transcodeFrom(xmlBytes + offset, inputLength, outBuff, 128, eaten, charSizes);
00270 str.append(outBuff, outputLength);
00271 offset += eaten;
00272 inputLength -= eaten;
00273 }
00274
00275 delete[] charSizes;
00276 }
00277
00278 inline XUTF8Str::~XUTF8Str()
00279 {
00280 }
00281
00282
00283
00284
00285
00286 inline const XMLCh* XUTF8Str::unicodeForm() const
00287 {
00288 return str.c_str();
00289 }
00290
00291 #endif // BASE_XMLTOOLS_H