Debian Codegraph @

src/Base/XMLTools.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Library General Public License (LGPL) *
10 * as published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * for detail see the LICENCE text file. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Library General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Library General Public *
20 * License along with FreeCAD; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 * USA *
23 * *
24 ***************************************************************************/
25
26
27 #ifndef BASE_XMLTOOLS_H
28 #define BASE_XMLTOOLS_H
29
30 #include <memory>
31 #include <ostream>
32 #include <xercesc/util/TransService.hpp>
33 #include <xercesc/framework/MemoryManager.hpp>
34
35 #include <FCGlobal.h>
36
37 #ifndef XERCES_CPP_NAMESPACE_BEGIN
38 # define XERCES_CPP_NAMESPACE_QUALIFIER
39 namespace XERCES_CPP_NAMESPACE
40 {
41 class DOMNode;
42 class DOMElement;
43 class DOMDocument;
44 } // namespace XERCES_CPP_NAMESPACE
45 #else
46 XERCES_CPP_NAMESPACE_BEGIN
47 class DOMNode;
48 class DOMElement;
49 class DOMDocument;
50 XERCES_CPP_NAMESPACE_END
51 #endif
52
53 // Helper class
54 class BaseExport XMLTools
55 {
56 public:
57 static std::string toStdString(const XMLCh* const toTranscode);
58 static std::basic_string<XMLCh> toXMLString(const char* const fromTranscode);
59 static void initialize();
60 static void terminate();
61
62 private:
63 static std::unique_ptr<XERCES_CPP_NAMESPACE::XMLTranscoder> transcoder; // NOLINT
64 };
65
66 // Helper class for XStrLiteral macro
67 // This implementation is almost same as Xerces default memory manager.
68 class BaseExport XStrMemoryManager final: public XERCES_CPP_NAMESPACE::MemoryManager
69 {
70 public:
71 XStrMemoryManager() = default;
72 ~XStrMemoryManager() = default;
73
74 MemoryManager* getExceptionMemoryManager() override
75 {
76 return this;
77 }
78
79 void* allocate(XMLSize_t size) override;
80 void deallocate(void* p) override;
81 };
82
83 //**************************************************************************
84 //**************************************************************************
85 // StrXLocal
86 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87
88 class StrX
89 {
90 public:
91 explicit StrX(const XMLCh* const toTranscode);
92 ~StrX();
93
94 /// Getter method
95 const char* c_str() const;
96 FC_DISABLE_COPY_MOVE(StrX)
97
98 private:
99 // This is the local code page form of the string.
100 char* fLocalForm;
101 };
102
103 inline std::ostream& operator<<(std::ostream& target, const StrX& toDump)
104 {
105 target << toDump.c_str();
106 return target;
107 }
108
109 inline StrX::StrX(const XMLCh* const toTranscode)
110 : fLocalForm(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode))
111 {}
112
113 inline StrX::~StrX()
114 {
115 XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fLocalForm);
116 }
117
118
119 // -----------------------------------------------------------------------
120 // Getter methods
121 // -----------------------------------------------------------------------
122 inline const char* StrX::c_str() const
123 {
124 return fLocalForm;
125 }
126
127 //**************************************************************************
128 //**************************************************************************
129 // StrXUTF-8
130 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131
132 class StrXUTF8
133 {
134 public:
135 explicit StrXUTF8(const XMLCh* const toTranscode);
136
137 /// Getter method
138 const char* c_str() const;
139 /// string which holds the UTF-8 form
140 std::string str;
141 };
142
143 inline std::ostream& operator<<(std::ostream& target, const StrXUTF8& toDump)
144 {
145 target << toDump.c_str();
146 return target;
147 }
148
149 inline StrXUTF8::StrXUTF8(const XMLCh* const toTranscode)
150 {
151 str = XMLTools::toStdString(toTranscode);
152 }
153
154 // -----------------------------------------------------------------------
155 // Getter methods
156 // -----------------------------------------------------------------------
157 inline const char* StrXUTF8::c_str() const
158 {
159 return str.c_str();
160 }
161
162
163 //**************************************************************************
164 //**************************************************************************
165 // XStr
166 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
167
168
169 class XStr
170 {
171 public:
172 /// Constructors and Destructor
173 explicit XStr(const char* const toTranscode);
174 explicit XStr(const char* const toTranscode, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr);
175 ~XStr();
176
177
178 /// Getter method
179 const XMLCh* unicodeForm() const;
180 FC_DISABLE_COPY_MOVE(XStr)
181
182 private:
183 /// This is the Unicode XMLCh format of the string.
184 XMLCh* fUnicodeForm;
185 XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr;
186 };
187
188
189 inline XStr::XStr(const char* const toTranscode)
190 : XStr(toTranscode, XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::fgMemoryManager)
191 {}
192
193 inline XStr::XStr(const char* const toTranscode, XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager* memMgr)
194 : fUnicodeForm(XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode, memMgr))
195 , memMgr(memMgr)
196 {}
197
198 inline XStr::~XStr()
199 {
200 XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fUnicodeForm, memMgr);
201 }
202
203 // Uses the compiler to create a cache of transcoded string literals so that each subsequent call
204 // can reuse the data from the lambda's initial creation. Permits the same usage as
205 // XStr("literal").unicodeForm()
206 // XStrLiteral macro use local memory manager instance to prevent segfault on releasing cached
207 // string because xerces default memory manager is already deleted when destructing local static
208 // variable.
209 #define XStrLiteral(literal) \
210 ([]() -> const XStr& { \
211 static XStrMemoryManager memMgr; \
212 static const XStr str {literal, &memMgr}; \
213 return str; \
214 }())
215
216
217 // -----------------------------------------------------------------------
218 // Getter methods
219 // -----------------------------------------------------------------------
220 inline const XMLCh* XStr::unicodeForm() const
221 {
222 return fUnicodeForm;
223 }
224
225 //**************************************************************************
226 //**************************************************************************
227 // XUTF-8Str
228 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
229
230 class XUTF8Str
231 {
232 public:
233 explicit XUTF8Str(const char* const fromTranscode);
234 ~XUTF8Str();
235
236 /// Getter method
237 const XMLCh* unicodeForm() const;
238
239 FC_DISABLE_COPY_MOVE(XUTF8Str)
240
241 private:
242 std::basic_string<XMLCh> str;
243 };
244
245 inline XUTF8Str::XUTF8Str(const char* const fromTranscode)
246 {
247 str = XMLTools::toXMLString(fromTranscode);
248 }
249
250 inline XUTF8Str::~XUTF8Str() = default;
251
252 // Uses the compiler to create a cache of transcoded string literals so that each subsequent call
253 // can reuse the data from the lambda's initial creation. Permits the same usage as
254 // XStr("literal").unicodeForm()
255 #define XUTF8StrLiteral(literal) \
256 ([]() -> const XUTF8Str& { \
257 static const XUTF8Str str {literal}; \
258 return str; \
259 }())
260
261 // -----------------------------------------------------------------------
262 // Getter methods
263 // -----------------------------------------------------------------------
264 inline const XMLCh* XUTF8Str::unicodeForm() const
265 {
266 return str.c_str();
267 }
268
269 #endif // BASE_XMLTOOLS_H