~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Base/XMLTools.h

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   (c) J�rgen Riegel (juergen.riegel@web.de)                             *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU Library General Public License (LGPL)   *
 
8
 *   as published by the Free Software Foundation; either version 2 of     *
 
9
 *   the License, or (at your option) any later version.                   *
 
10
 *   for detail see the LICENCE text file.                                 *
 
11
 *                                                                         *
 
12
 *   FreeCAD is distributed in the hope that it will be useful,            *
 
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
15
 *   GNU Library General Public License for more details.                  *
 
16
 *                                                                         *
 
17
 *   You should have received a copy of the GNU Library General Public     *
 
18
 *   License along with FreeCAD; if not, write to the Free Software        *
 
19
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
 
20
 *   USA                                                                   *
 
21
 *                                                                         *
 
22
 *   Juergen Riegel 2002                                                   *
 
23
 ***************************************************************************/
 
24
 
 
25
 
 
26
#ifndef __XMLTOOLS_H__
 
27
#define __XMLTOOLS_H__
 
28
 
 
29
// Std. configurations
 
30
 
 
31
 
 
32
// Include files
 
33
#include <memory>
 
34
#include <iostream>
 
35
#include <xercesc/util/XercesDefs.hpp>
 
36
#include <xercesc/util/XercesVersion.hpp>
 
37
#include <xercesc/util/XMLString.hpp>
 
38
#include <xercesc/util/PlatformUtils.hpp>
 
39
#include <xercesc/util/TransService.hpp>
 
40
 
 
41
#include <Base/Exception.h>
 
42
 
 
43
XERCES_CPP_NAMESPACE_BEGIN
 
44
    class DOMNode;
 
45
    class DOMElement;
 
46
    class DOMDocument;
 
47
XERCES_CPP_NAMESPACE_END
 
48
 
 
49
 
 
50
//**************************************************************************
 
51
//**************************************************************************
 
52
// StrXLocal
 
53
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
54
 
 
55
class StrX
 
56
{
 
57
public :
 
58
    StrX(const XMLCh* const toTranscode);
 
59
    ~StrX();
 
60
 
 
61
    /// Getter method
 
62
    const char* c_str() const;
 
63
 
 
64
private :
 
65
    //  This is the local code page form of the string.
 
66
    char*   fLocalForm;
 
67
};
 
68
 
 
69
inline std::ostream& operator<<(std::ostream& target, const StrX& toDump)
 
70
{
 
71
    target << toDump.c_str();
 
72
    return target;
 
73
}
 
74
 
 
75
inline StrX::StrX(const XMLCh* const toTranscode)
 
76
{
 
77
    // Call the private transcoding method
 
78
    fLocalForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
 
79
//#ifdef FC_OS_WIN32
 
80
//    assert(0)
 
81
//    WideCharToMultiByte(CP_UTF8,0,toTranscode,-1,fLocaleForm)
 
82
//#else
 
83
//    fUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
 
84
//#endif 
 
85
}
 
86
 
 
87
inline StrX::~StrX()
 
88
{
 
89
    //delete [] fLocalForm; // dont work on VC7.1
 
90
    XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fLocalForm);
 
91
}
 
92
 
 
93
 
 
94
// -----------------------------------------------------------------------
 
95
//  Getter methods
 
96
// -----------------------------------------------------------------------
 
97
inline const char* StrX::c_str() const
 
98
{
 
99
    return fLocalForm;
 
100
}
 
101
 
 
102
//**************************************************************************
 
103
//**************************************************************************
 
104
// StrXUTF-8
 
105
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
106
 
 
107
class StrXUTF8
 
108
{
 
109
public :
 
110
    StrXUTF8(const XMLCh* const toTranscode);
 
111
 
 
112
    /// Getter method
 
113
    const char* c_str() const;
 
114
    /// string which holds the UTF-8 form
 
115
    std::string  str;
 
116
 
 
117
private :
 
118
    static std::auto_ptr<XERCES_CPP_NAMESPACE::XMLTranscoder> transcoder;
 
119
    //  This is the local code page form of the string.
 
120
};
 
121
 
 
122
inline std::ostream& operator<<(std::ostream& target, const StrXUTF8& toDump)
 
123
{
 
124
    target << toDump.c_str();
 
125
    return target;
 
126
}
 
127
 
 
128
inline StrXUTF8::StrXUTF8(const XMLCh* const toTranscode)
 
129
{
 
130
    XERCES_CPP_NAMESPACE_USE;
 
131
    if(!transcoder.get()){
 
132
        XMLTransService::Codes  res;
 
133
        transcoder.reset(XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgTransService->makeNewTranscoderFor(XERCES_CPP_NAMESPACE_QUALIFIER XMLRecognizer::UTF_8, res, 4096, XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgMemoryManager));
 
134
        if (res != XMLTransService::Ok)
 
135
            throw Base::Exception("Cant create UTF-8 encoder in StrXUTF8::StrXUTF8()");
 
136
    }
 
137
 
 
138
    //char outBuff[128];
 
139
    static XMLByte outBuff[128];
 
140
#if (XERCES_VERSION_MAJOR == 2)
 
141
    unsigned int outputLength;
 
142
    unsigned int eaten = 0;
 
143
    unsigned int offset = 0;
 
144
    unsigned int inputLength = XMLString::stringLen(toTranscode);
 
145
#else
 
146
    XMLSize_t outputLength;
 
147
    XMLSize_t eaten = 0;
 
148
    XMLSize_t offset = 0;
 
149
    XMLSize_t inputLength = XMLString::stringLen(toTranscode);
 
150
#endif
 
151
 
 
152
    while (inputLength)
 
153
    {
 
154
        outputLength = transcoder->transcodeTo(toTranscode + offset, inputLength, outBuff, 128, eaten, XMLTranscoder::UnRep_RepChar);
 
155
        str.append((const char*)outBuff, outputLength);
 
156
        offset += eaten;
 
157
        inputLength -= eaten;
 
158
    }
 
159
}
 
160
 
 
161
// -----------------------------------------------------------------------
 
162
//  Getter methods
 
163
// -----------------------------------------------------------------------
 
164
inline const char* StrXUTF8::c_str() const
 
165
{
 
166
    return str.c_str();
 
167
}
 
168
 
 
169
 
 
170
//**************************************************************************
 
171
//**************************************************************************
 
172
// XStr
 
173
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
174
 
 
175
 
 
176
class XStr
 
177
{
 
178
public :
 
179
    ///  Constructors and Destructor
 
180
    XStr(const char* const toTranscode);
 
181
    /// 
 
182
    ~XStr();
 
183
 
 
184
 
 
185
    ///  Getter method
 
186
    const XMLCh* unicodeForm() const;
 
187
 
 
188
private :
 
189
    /// This is the Unicode XMLCh format of the string.
 
190
    XMLCh*   fUnicodeForm;
 
191
};
 
192
 
 
193
 
 
194
inline XStr::XStr(const char* const toTranscode)
 
195
{
 
196
    // Call the private transcoding method
 
197
//#ifdef FC_OS_WIN32
 
198
//    assert(0)
 
199
//    WideCharToMultiByte()
 
200
//#else
 
201
    fUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
 
202
//#endif 
 
203
}
 
204
 
 
205
inline XStr::~XStr()
 
206
{
 
207
    //delete [] fUnicodeForm;
 
208
    XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fUnicodeForm);
 
209
}
 
210
 
 
211
 
 
212
// -----------------------------------------------------------------------
 
213
//  Getter methods
 
214
// -----------------------------------------------------------------------
 
215
inline const XMLCh* XStr::unicodeForm() const
 
216
{
 
217
    return fUnicodeForm;
 
218
}
 
219
 
 
220
#endif // __XMLTOOLS_H__