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