Branch data Line data Source code
1 : : // file : xsd/cxx/tree/types.txx
2 : : // copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
3 : : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 : :
5 : : #include <xercesc/util/Base64.hpp>
6 : : #include <xercesc/util/XMLString.hpp>
7 : :
8 : : #include <xsd/cxx/config.hxx> // XSD_CXX11
9 : :
10 : : #ifdef XSD_CXX11
11 : : # include <memory> // std::unique_ptr
12 : : #else
13 : : # include <xsd/cxx/auto-array.hxx>
14 : : #endif
15 : :
16 : : #include <xsd/cxx/xml/std-memory-manager.hxx>
17 : :
18 : : namespace xsd
19 : : {
20 : : namespace cxx
21 : : {
22 : : namespace tree
23 : : {
24 : :
25 : : // string
26 : : //
27 : : template <typename C, typename B>
28 : 0 : string<C, B>* string<C, B>::
29 : : _clone (flags f, container* c) const
30 : : {
31 [ # # ]: 0 : return new string (*this, f, c);
32 : : }
33 : :
34 : :
35 : : // normalized_string
36 : : //
37 : : template <typename C, typename B>
38 : : normalized_string<C, B>* normalized_string<C, B>::
39 : : _clone (flags f, container* c) const
40 : : {
41 : : return new normalized_string (*this, f, c);
42 : : }
43 : :
44 : :
45 : : // token
46 : : //
47 : : template <typename C, typename B>
48 : : token<C, B>* token<C, B>::
49 : : _clone (flags f, container* c) const
50 : : {
51 : : return new token (*this, f, c);
52 : : }
53 : :
54 : :
55 : : // nmtoken
56 : : //
57 : : template <typename C, typename B>
58 : : nmtoken<C, B>* nmtoken<C, B>::
59 : : _clone (flags f, container* c) const
60 : : {
61 : : return new nmtoken (*this, f, c);
62 : : }
63 : :
64 : :
65 : : // nmtokens
66 : : //
67 : : template <typename C, typename B, typename nmtoken>
68 : : nmtokens<C, B, nmtoken>* nmtokens<C, B, nmtoken>::
69 : : _clone (flags f, container* c) const
70 : : {
71 : : return new nmtokens (*this, f, c);
72 : : }
73 : :
74 : :
75 : : // name
76 : : //
77 : : template <typename C, typename B>
78 : : name<C, B>* name<C, B>::
79 : : _clone (flags f, container* c) const
80 : : {
81 : : return new name (*this, f, c);
82 : : }
83 : :
84 : :
85 : : // ncname
86 : : //
87 : : template <typename C, typename B>
88 : : ncname<C, B>* ncname<C, B>::
89 : : _clone (flags f, container* c) const
90 : : {
91 : : return new ncname (*this, f, c);
92 : : }
93 : :
94 : :
95 : : // language
96 : : //
97 : : template <typename C, typename B>
98 : : language<C, B>* language<C, B>::
99 : : _clone (flags f, container* c) const
100 : : {
101 : : return new language (*this, f, c);
102 : : }
103 : :
104 : :
105 : : // identity_impl
106 : : //
107 : : template <typename C, typename ncname>
108 : : bool identity_impl<C, ncname>::
109 : : before (const identity& y) const
110 : : {
111 : : return id_ < static_cast<const identity_impl&> (y).id_;
112 : : }
113 : :
114 : : template <typename C, typename ncname>
115 : : void identity_impl<C, ncname>::
116 : : throw_duplicate_id () const
117 : : {
118 : : throw duplicate_id<C> (id_);
119 : : }
120 : :
121 : :
122 : : // id
123 : : //
124 : : template <typename C, typename B>
125 : : id<C, B>* id<C, B>::
126 : : _clone (flags f, container* c) const
127 : : {
128 : : return new id (*this, f, c);
129 : : }
130 : :
131 : : template <typename C, typename B>
132 : : id<C, B>& id<C, B>::
133 : : operator= (C c)
134 : : {
135 : : unregister_id ();
136 : : base () = c;
137 : : register_id ();
138 : :
139 : : return *this;
140 : : }
141 : :
142 : : template <typename C, typename B>
143 : : id<C, B>& id<C, B>::
144 : : operator= (const C* s)
145 : : {
146 : : unregister_id ();
147 : : base () = s;
148 : : register_id ();
149 : :
150 : : return *this;
151 : : }
152 : :
153 : : template <typename C, typename B>
154 : : id<C, B>& id<C, B>::
155 : : operator= (const std::basic_string<C>& s)
156 : : {
157 : : unregister_id ();
158 : : base () = s;
159 : : register_id ();
160 : :
161 : : return *this;
162 : : }
163 : :
164 : : template <typename C, typename B>
165 : : id<C, B>& id<C, B>::
166 : : operator= (const id& x)
167 : : {
168 : : unregister_id ();
169 : : base () = x;
170 : : register_id ();
171 : :
172 : : return *this;
173 : : }
174 : :
175 : : template <typename C, typename B>
176 : : void id<C, B>::
177 : : _container (container* c)
178 : : {
179 : : B::_container (c);
180 : : register_id ();
181 : : }
182 : :
183 : : template <typename C, typename B>
184 : : void id<C, B>::
185 : : register_id ()
186 : : {
187 : : container* r (this->_root ());
188 : :
189 : : if (r != 0 && !this->empty ())
190 : : r->_register_id (identity_, this->_container ());
191 : : }
192 : :
193 : : template <typename C, typename B>
194 : : void id<C, B>::
195 : : unregister_id ()
196 : : {
197 : : container* r (this->_root ());
198 : :
199 : : if (r != 0 && !this->empty ())
200 : : r->_unregister_id (identity_);
201 : : }
202 : :
203 : :
204 : : // idref
205 : : //
206 : : template <typename C, typename B, typename T>
207 : : idref<C, B, T>* idref<C, B, T>::
208 : : _clone (flags f, container* c) const
209 : : {
210 : : return new idref (*this, f, c);
211 : : }
212 : :
213 : : template <typename C, typename B, typename T>
214 : : const _type* idref<C, B, T>::
215 : : get_ () const
216 : : {
217 : : if (!this->empty () && this->_container () != 0)
218 : : {
219 : : return this->_root ()->_lookup_id (identity_);
220 : : }
221 : : else
222 : : return 0;
223 : : }
224 : :
225 : : template <typename C, typename B, typename T>
226 : : _type* idref<C, B, T>::
227 : : get_ ()
228 : : {
229 : : if (!this->empty () && this->_container () != 0)
230 : : {
231 : : return this->_root ()->_lookup_id (identity_);
232 : : }
233 : : else
234 : : return 0;
235 : : }
236 : :
237 : : template <typename C, typename B, typename T>
238 : : void idref<C, B, T>::
239 : : true_ ()
240 : : {
241 : : }
242 : :
243 : :
244 : : // idrefs
245 : : //
246 : : template <typename C, typename B, typename idref>
247 : : idrefs<C, B, idref>* idrefs<C, B, idref>::
248 : : _clone (flags f, container* c) const
249 : : {
250 : : return new idrefs (*this, f, c);
251 : : }
252 : :
253 : :
254 : : // uri
255 : : //
256 : : template <typename C, typename B>
257 : : uri<C, B>* uri<C, B>::
258 : : _clone (flags f, container* c) const
259 : : {
260 : : return new uri (*this, f, c);
261 : : }
262 : :
263 : :
264 : : // qname
265 : : //
266 : : template <typename C, typename B, typename uri, typename ncname>
267 : : qname<C, B, uri, ncname>* qname<C, B, uri, ncname>::
268 : : _clone (flags f, container* c) const
269 : : {
270 : : return new qname (*this, f, c);
271 : : }
272 : :
273 : :
274 : : // base64_binary
275 : : //
276 : : template <typename C, typename B>
277 : : base64_binary<C, B>::
278 : : base64_binary (size_t size)
279 : : : buffer<C> (size)
280 : : {
281 : : }
282 : :
283 : : template <typename C, typename B>
284 : : base64_binary<C, B>::
285 : : base64_binary (size_t size, size_t capacity)
286 : : : buffer<C> (size, capacity)
287 : : {
288 : : }
289 : :
290 : : template <typename C, typename B>
291 : : base64_binary<C, B>::
292 : : base64_binary (const void* data, size_t size)
293 : : : buffer<C> (data, size)
294 : : {
295 : : }
296 : :
297 : : template <typename C, typename B>
298 : : base64_binary<C, B>::
299 : : base64_binary (const void* data, size_t size, size_t capacity)
300 : : : buffer<C> (data, size, capacity)
301 : : {
302 : : }
303 : :
304 : : template <typename C, typename B>
305 : : base64_binary<C, B>::
306 : : base64_binary (void* data, size_t size, size_t capacity, bool own)
307 : : : buffer<C> (data, size, capacity, own)
308 : : {
309 : : }
310 : :
311 : : template <typename C, typename B>
312 : : base64_binary<C, B>* base64_binary<C, B>::
313 : : _clone (flags f, container* c) const
314 : : {
315 : : return new base64_binary (*this, f, c);
316 : : }
317 : :
318 : : template <typename C, typename B>
319 : : std::basic_string<C> base64_binary<C, B>::
320 : : encode () const
321 : : {
322 : : using namespace xercesc;
323 : : std::basic_string<C> str;
324 : :
325 : : XMLSize_t n;
326 : : xml::std_memory_manager mm;
327 : :
328 : : #ifdef XSD_CXX11
329 : : std::unique_ptr<XMLByte[], xml::std_memory_manager&> r (
330 : : #else
331 : : auto_array<XMLByte, xml::std_memory_manager> r (
332 : : #endif
333 : : Base64::encode (
334 : : reinterpret_cast<const XMLByte*> (this->data ()),
335 : : static_cast<XMLSize_t> (this->size ()),
336 : : &n,
337 : : &mm),
338 : : mm);
339 : :
340 : : if (r)
341 : : {
342 : : str.reserve (n + 1);
343 : : str.resize (n);
344 : :
345 : : for (XMLSize_t i (0); i < n; ++i)
346 : : str[i] = C (r[i]);
347 : : }
348 : : else
349 : : {
350 : : //@@ throw
351 : : }
352 : :
353 : : return str;
354 : : }
355 : :
356 : : template <typename C, typename B>
357 : : void base64_binary<C, B>::
358 : : decode (const XMLCh* src)
359 : : {
360 : : using namespace xercesc;
361 : :
362 : : xml::std_memory_manager mm;
363 : : XMLSize_t size;
364 : :
365 : : #ifdef XSD_CXX11
366 : : std::unique_ptr<XMLByte[], xml::std_memory_manager&> data (
367 : : #else
368 : : auto_array<XMLByte, xml::std_memory_manager> data (
369 : : #endif
370 : : Base64::decodeToXMLByte (src, &size, &mm, Base64::Conf_RFC2045),
371 : : mm);
372 : :
373 : : if (data)
374 : : {
375 : : buffer<C> tmp (data.get (), size, size, true);
376 : : data.release ();
377 : : this->swap (tmp); // g++ 4.1 likes it qualified, not sure why.
378 : : }
379 : : else
380 : : {
381 : : //@@ throw
382 : : }
383 : : }
384 : :
385 : :
386 : : // hex_binary
387 : : //
388 : : template <typename C, typename B>
389 : : hex_binary<C, B>::
390 : : hex_binary (size_t size)
391 : : : buffer<C> (size)
392 : : {
393 : : }
394 : :
395 : : template <typename C, typename B>
396 : : hex_binary<C, B>::
397 : : hex_binary (size_t size, size_t capacity)
398 : : : buffer<C> (size, capacity)
399 : : {
400 : : }
401 : :
402 : : template <typename C, typename B>
403 : : hex_binary<C, B>::
404 : : hex_binary (const void* data, size_t size)
405 : : : buffer<C> (data, size)
406 : : {
407 : : }
408 : :
409 : : template <typename C, typename B>
410 : : hex_binary<C, B>::
411 : : hex_binary (const void* data, size_t size, size_t capacity)
412 : : : buffer<C> (data, size, capacity)
413 : : {
414 : : }
415 : :
416 : : template <typename C, typename B>
417 : : hex_binary<C, B>::
418 : : hex_binary (void* data, size_t size, size_t capacity, bool own)
419 : : : buffer<C> (data, size, capacity, own)
420 : : {
421 : : }
422 : :
423 : : template <typename C, typename B>
424 : : hex_binary<C, B>* hex_binary<C, B>::
425 : : _clone (flags f, container* c) const
426 : : {
427 : : return new hex_binary (*this, f, c);
428 : : }
429 : :
430 : : template <typename C, typename B>
431 : : std::basic_string<C> hex_binary<C, B>::
432 : : encode () const
433 : : {
434 : : std::basic_string<C> str;
435 : :
436 : : const char tab[] = "0123456789ABCDEF";
437 : :
438 : : if (size_t n = this->size ())
439 : : {
440 : : str.reserve (2 * n + 1);
441 : : str.resize (2 * n);
442 : :
443 : : for (size_t i (0); i < n; ++i)
444 : : {
445 : : unsigned char byte (
446 : : static_cast<unsigned char> (*(this->data () + i)));
447 : : unsigned char h (byte >> 4);
448 : : unsigned char l (byte & 0x0F);
449 : :
450 : : str[2 * i] = C (tab[h]);
451 : : str[2 * i + 1] = C (tab[l]);
452 : : }
453 : : }
454 : :
455 : : return str;
456 : : }
457 : :
458 : : namespace bits
459 : : {
460 : : inline unsigned char
461 : : hex_decode (XMLCh c)
462 : : {
463 : : unsigned char r (0xFF);
464 : :
465 : : if (c >= '0' && c <= '9')
466 : : r = static_cast<unsigned char> (c - '0');
467 : : else if (c >= 'A' && c <= 'F')
468 : : r = static_cast<unsigned char> (10 + (c - 'A'));
469 : : else if (c >= 'a' && c <= 'f')
470 : : r = static_cast<unsigned char> (10 + (c - 'a'));
471 : :
472 : : return r;
473 : : }
474 : : }
475 : :
476 : : template <typename C, typename B>
477 : : void hex_binary<C, B>::
478 : : decode (const XMLCh* src)
479 : : {
480 : : size_t src_n (xercesc::XMLString::stringLen (src));
481 : :
482 : : if (src_n % 2 != 0)
483 : : return; // @@ throw
484 : :
485 : : size_t n (src_n / 2);
486 : :
487 : : buffer<C> tmp (n);
488 : :
489 : : for (size_t i (0); i < n; ++i)
490 : : {
491 : : unsigned char h (bits::hex_decode (src[2 * i]));
492 : : unsigned char l (bits::hex_decode (src[2 * i + 1]));
493 : :
494 : : if (h == 0xFF || l == 0xFF)
495 : : return; //@@ throw
496 : :
497 : : tmp.data()[i] = (h << 4) | l;
498 : : }
499 : :
500 : : this->swap (tmp); // g++ 4.1 likes it qualified, not sure why.
501 : : }
502 : :
503 : :
504 : : // entity
505 : : //
506 : : template <typename C, typename B>
507 : : entity<C, B>* entity<C, B>::
508 : : _clone (flags f, container* c) const
509 : : {
510 : : return new entity (*this, f, c);
511 : : }
512 : :
513 : :
514 : : // entities
515 : : //
516 : : template <typename C, typename B, typename entity>
517 : : entities<C, B, entity>* entities<C, B, entity>::
518 : : _clone (flags f, container* c) const
519 : : {
520 : : return new entities (*this, f, c);
521 : : }
522 : : }
523 : : }
524 : : }
|