XMLTools.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   (c) Jürgen Riegel (juergen.riegel@web.de)                             *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU Library General Public License (LGPL)   *
00008  *   as published by the Free Software Foundation; either version 2 of     *
00009  *   the License, or (at your option) any later version.                   *
00010  *   for detail see the LICENCE text file.                                 *
00011  *                                                                         *
00012  *   FreeCAD is distributed in the hope that it will be useful,            *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with FreeCAD; if not, write to the Free Software        *
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020  *   USA                                                                   *
00021  *                                                                         *
00022  *   Juergen Riegel 2002                                                   *
00023  ***************************************************************************/
00024 
00025 
00026 #ifndef BASE_XMLTOOLS_H
00027 #define BASE_XMLTOOLS_H
00028 
00029 // Std. configurations
00030 
00031 
00032 // Include files
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 // StrXLocal
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     //  This is the local code page form of the string.
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     // Call the private transcoding method
00078     fLocalForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
00079 //#ifdef FC_OS_WIN32
00080 //    assert(0)
00081 //    WideCharToMultiByte(CP_UTF8,0,toTranscode,-1,fLocaleForm)
00082 //#else
00083 //    fUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
00084 //#endif 
00085 }
00086 
00087 inline StrX::~StrX()
00088 {
00089     //delete [] fLocalForm; // dont work on VC7.1
00090     XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fLocalForm);
00091 }
00092 
00093 
00094 // -----------------------------------------------------------------------
00095 //  Getter methods
00096 // -----------------------------------------------------------------------
00097 inline const char* StrX::c_str() const
00098 {
00099     return fLocalForm;
00100 }
00101 
00102 //**************************************************************************
00103 //**************************************************************************
00104 // StrXUTF-8
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     //  This is the local code page form of the string.
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     //char outBuff[128];
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 //  Getter methods
00163 // -----------------------------------------------------------------------
00164 inline const char* StrXUTF8::c_str() const
00165 {
00166     return str.c_str();
00167 }
00168 
00169 
00170 //**************************************************************************
00171 //**************************************************************************
00172 // XStr
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     // Call the private transcoding method
00197 //#ifdef FC_OS_WIN32
00198 //    assert(0)
00199 //    WideCharToMultiByte()
00200 //#else
00201     fUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
00202 //#endif 
00203 }
00204 
00205 inline XStr::~XStr()
00206 {
00207     //delete [] fUnicodeForm;
00208     XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fUnicodeForm);
00209 }
00210 
00211 
00212 // -----------------------------------------------------------------------
00213 //  Getter methods
00214 // -----------------------------------------------------------------------
00215 inline const XMLCh* XStr::unicodeForm() const
00216 {
00217     return fUnicodeForm;
00218 }
00219 
00220 //**************************************************************************
00221 //**************************************************************************
00222 // XUTF-8Str
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 //  Getter methods
00285 // -----------------------------------------------------------------------
00286 inline const XMLCh* XUTF8Str::unicodeForm() const
00287 {
00288     return str.c_str();
00289 }
00290 
00291 #endif // BASE_XMLTOOLS_H

Generated on Wed Nov 23 19:01:12 2011 for FreeCAD by  doxygen 1.6.1