root/win32/xsd-3.1.0-i686/libxsd/xsd/cxx/tree/serialization.txx @ 111

Revision 111, 15.9 kB (checked in by mido, 16 years ago)

pridana knihovna XSD (a jeji chlebodarkyne XERCES), v ramci Win32 zprovoznen priklad tests/test_xsd_hello.cxx

Line 
1// file      : xsd/cxx/tree/serialization.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 <string>
7#include <sstream>
8
9#include <xercesc/dom/DOMAttr.hpp>
10#include <xercesc/dom/DOMElement.hpp>
11
12#include <xsd/cxx/xml/string.hxx>        // xml::{string, transcode}
13#include <xsd/cxx/xml/dom/serialization-header.hxx>  // dom::{prefix, clear}
14
15#include <xsd/cxx/tree/exceptions.hxx>   // no_namespace_mapping
16#include <xsd/cxx/tree/elements.hxx>
17#include <xsd/cxx/tree/types.hxx>
18#include <xsd/cxx/tree/list.hxx>
19
20// The only way to make the following serialization operators
21// for fundamental types work is to defined them in the xercesc
22// namespace so that they can be found by ADL. Placing them into
23// the global namespace does not work.
24//
25
26namespace XERCES_CPP_NAMESPACE
27{
28  // Serialization of std::basic_string and C string. Used in other
29  // serializers. Also used to serialize enumerators.
30  //
31  template <typename C>
32  void
33  operator<< (xercesc::DOMElement& e, const C* s)
34  {
35    xsd::cxx::xml::dom::clear<char> (e);
36    e.setTextContent (xsd::cxx::xml::string (s).c_str ());
37  }
38
39  template <typename C>
40  void
41  operator<< (xercesc::DOMAttr& a, const C* s)
42  {
43    a.setValue (xsd::cxx::xml::string (s).c_str ());
44  }
45
46  // We duplicate the code above instead of delegating in order to
47  // allow the xml::string type to take advantage of cached string
48  // sizes.
49  //
50  template <typename C>
51  void
52  operator<< (xercesc::DOMElement& e, const std::basic_string<C>& s)
53  {
54    xsd::cxx::xml::dom::clear<char> (e);
55    e.setTextContent (xsd::cxx::xml::string (s).c_str ());
56  }
57
58  template <typename C>
59  void
60  operator<< (xercesc::DOMAttr& a, const std::basic_string<C>& s)
61  {
62    a.setValue (xsd::cxx::xml::string (s).c_str ());
63  }
64}
65
66namespace xsd
67{
68  namespace cxx
69  {
70    namespace tree
71    {
72      // List serialization operators for std::basic_string and C string.
73      //
74
75      template <typename C>
76      void
77      operator<< (list_stream<C>& ls, const C* s)
78      {
79        ls.os_ << s;
80      }
81
82      template <typename C>
83      void
84      operator<< (list_stream<C>& ls, const std::basic_string<C>& s)
85      {
86        ls.os_ << s;
87      }
88
89      // Insertion operators for type.
90      //
91      inline void
92      operator<< (xercesc::DOMElement& e, const type&)
93      {
94        xml::dom::clear<char> (e);
95      }
96
97      inline void
98      operator<< (xercesc::DOMAttr&, const type&)
99      {
100      }
101
102      template <typename C>
103      inline void
104      operator<< (list_stream<C>&, const type&)
105      {
106      }
107
108      // Insertion operators for simple_type.
109      //
110      template <typename B>
111      inline void
112      operator<< (xercesc::DOMElement& e, const simple_type<B>&)
113      {
114        xml::dom::clear<char> (e);
115      }
116
117      template <typename B>
118      inline void
119      operator<< (xercesc::DOMAttr&, const simple_type<B>&)
120      {
121      }
122
123      template <typename C, typename B>
124      inline void
125      operator<< (list_stream<C>&, const simple_type<B>&)
126      {
127      }
128
129      // Insertion operators for list.
130      //
131      template <typename C, typename X, bool fund>
132      void
133      operator<< (xercesc::DOMElement& e, const list<X, C, fund>& v)
134      {
135        xml::dom::clear<char> (e);
136
137        std::basic_ostringstream<C> os;
138        list_stream<C> ls (os, e);
139
140        ls << v;
141
142        e << os.str ();
143      }
144
145      template <typename C, typename X, bool fund>
146      void
147      operator<< (xercesc::DOMAttr& a, const list<X, C, fund>& v)
148      {
149        std::basic_ostringstream<C> os;
150        list_stream<C> ls (os, *a.getOwnerElement ());
151
152        ls << v;
153
154        a << os.str ();
155      }
156
157      template <typename C, typename X, bool fund>
158      void
159      operator<< (list_stream<C>& ls, const list<X, C, fund>& v)
160      {
161        for (typename list<X, C, fund>::const_iterator
162               b (v.begin ()), e (v.end ()), i (b); i != e; ++i)
163        {
164          if (i != b)
165            ls.os_ << C (' ');
166
167          ls << *i;
168        }
169      }
170
171
172      // Insertion operators for fundamental_base.
173      //
174      template <typename X, typename C, typename B>
175      void
176      operator<< (xercesc::DOMElement& e, const fundamental_base<X, C, B>& x)
177      {
178        const X& r (x);
179        e << r;
180      }
181
182      template <typename X, typename C, typename B>
183      void
184      operator<< (xercesc::DOMAttr& a, const fundamental_base<X, C, B>& x)
185      {
186        const X& r (x);
187        a << r;
188      }
189
190      template <typename X, typename C, typename B>
191      void
192      operator<< (list_stream<C>& ls, const fundamental_base<X, C, B>& x)
193      {
194        const X& r (x);
195        ls << r;
196      }
197
198
199      // Insertion operators for built-in types.
200      //
201
202
203      namespace bits
204      {
205        template <typename C, typename X>
206        void
207        insert (xercesc::DOMElement& e, const X& x)
208        {
209          std::basic_ostringstream<C> os;
210          os << x;
211          e << os.str ();
212        }
213
214        template <typename C, typename X>
215        void
216        insert (xercesc::DOMAttr& a, const X& x)
217        {
218          std::basic_ostringstream<C> os;
219          os << x;
220          a << os.str ();
221        }
222      }
223
224
225      // string
226      //
227      template <typename C, typename B>
228      inline void
229      operator<< (xercesc::DOMElement& e, const string<C, B>& x)
230      {
231        bits::insert<C> (e, x);
232      }
233
234      template <typename C, typename B>
235      inline void
236      operator<< (xercesc::DOMAttr& a, const string<C, B>& x)
237      {
238        bits::insert<C> (a, x);
239      }
240
241      template <typename C, typename B>
242      inline void
243      operator<< (list_stream<C>& ls, const string<C, B>& x)
244      {
245        ls.os_ << x;
246      }
247
248
249      // normalized_string
250      //
251      template <typename C, typename B>
252      inline void
253      operator<< (xercesc::DOMElement& e, const normalized_string<C, B>& x)
254      {
255        bits::insert<C> (e, x);
256      }
257
258      template <typename C, typename B>
259      inline void
260      operator<< (xercesc::DOMAttr& a, const normalized_string<C, B>& x)
261      {
262        bits::insert<C> (a, x);
263      }
264
265      template <typename C, typename B>
266      inline void
267      operator<< (list_stream<C>& ls, const normalized_string<C, B>& x)
268      {
269        ls.os_ << x;
270      }
271
272
273      // token
274      //
275      template <typename C, typename B>
276      inline void
277      operator<< (xercesc::DOMElement& e, const token<C, B>& x)
278      {
279        bits::insert<C> (e, x);
280      }
281
282      template <typename C, typename B>
283      inline void
284      operator<< (xercesc::DOMAttr& a, const token<C, B>& x)
285      {
286        bits::insert<C> (a, x);
287      }
288
289      template <typename C, typename B>
290      inline void
291      operator<< (list_stream<C>& ls, const token<C, B>& x)
292      {
293        ls.os_ << x;
294      }
295
296
297      // nmtoken
298      //
299      template <typename C, typename B>
300      inline void
301      operator<< (xercesc::DOMElement& e, const nmtoken<C, B>& x)
302      {
303        bits::insert<C> (e, x);
304      }
305
306      template <typename C, typename B>
307      inline void
308      operator<< (xercesc::DOMAttr& a, const nmtoken<C, B>& x)
309      {
310        bits::insert<C> (a, x);
311      }
312
313      template <typename C, typename B>
314      inline void
315      operator<< (list_stream<C>& ls, const nmtoken<C, B>& x)
316      {
317        ls.os_ << x;
318      }
319
320
321      // nmtokens
322      //
323      template <typename C, typename B, typename nmtoken>
324      inline void
325      operator<< (xercesc::DOMElement& e, const nmtokens<C, B, nmtoken>& v)
326      {
327        const list<nmtoken, C>& r (v);
328        e << r;
329      }
330
331      template <typename C, typename B, typename nmtoken>
332      inline void
333      operator<< (xercesc::DOMAttr& a, const nmtokens<C, B, nmtoken>& v)
334      {
335        const list<nmtoken, C>& r (v);
336        a << r;
337      }
338
339      template <typename C, typename B, typename nmtoken>
340      inline void
341      operator<< (list_stream<C>& ls, const nmtokens<C, B, nmtoken>& v)
342      {
343        const list<nmtoken, C>& r (v);
344        ls << r;
345      }
346
347
348      // name
349      //
350      template <typename C, typename B>
351      inline void
352      operator<< (xercesc::DOMElement& e, const name<C, B>& x)
353      {
354        bits::insert<C> (e, x);
355      }
356
357      template <typename C, typename B>
358      inline void
359      operator<< (xercesc::DOMAttr& a, const name<C, B>& x)
360      {
361        bits::insert<C> (a, x);
362      }
363
364      template <typename C, typename B>
365      inline void
366      operator<< (list_stream<C>& ls, const name<C, B>& x)
367      {
368        ls.os_ << x;
369      }
370
371
372      // ncname
373      //
374      template <typename C, typename B>
375      inline void
376      operator<< (xercesc::DOMElement& e, const ncname<C, B>& x)
377      {
378        bits::insert<C> (e, x);
379      }
380
381      template <typename C, typename B>
382      inline void
383      operator<< (xercesc::DOMAttr& a, const ncname<C, B>& x)
384      {
385        bits::insert<C> (a, x);
386      }
387
388      template <typename C, typename B>
389      inline void
390      operator<< (list_stream<C>& ls, const ncname<C, B>& x)
391      {
392        ls.os_ << x;
393      }
394
395
396      // language
397      //
398      template <typename C, typename B>
399      inline void
400      operator<< (xercesc::DOMElement& e, const language<C, B>& x)
401      {
402        bits::insert<C> (e, x);
403      }
404
405      template <typename C, typename B>
406      inline void
407      operator<< (xercesc::DOMAttr& a, const language<C, B>& x)
408      {
409        bits::insert<C> (a, x);
410      }
411
412      template <typename C, typename B>
413      inline void
414      operator<< (list_stream<C>& ls, const language<C, B>& x)
415      {
416        ls.os_ << x;
417      }
418
419
420      // id
421      //
422      template <typename C, typename B>
423      inline void
424      operator<< (xercesc::DOMElement& e, const id<C, B>& x)
425      {
426        bits::insert<C> (e, x);
427      }
428
429      template <typename C, typename B>
430      inline void
431      operator<< (xercesc::DOMAttr& a, const id<C, B>& x)
432      {
433        bits::insert<C> (a, x);
434      }
435
436      template <typename C, typename B>
437      inline void
438      operator<< (list_stream<C>& ls, const id<C, B>& x)
439      {
440        ls.os_ << x;
441      }
442
443
444      // idref
445      //
446      template <typename X, typename C, typename B>
447      inline void
448      operator<< (xercesc::DOMElement& e, const idref<X, C, B>& x)
449      {
450        bits::insert<C> (e, x);
451      }
452
453      template <typename X, typename C, typename B>
454      inline void
455      operator<< (xercesc::DOMAttr& a, const idref<X, C, B>& x)
456      {
457        bits::insert<C> (a, x);
458      }
459
460      template <typename X, typename C, typename B>
461      inline void
462      operator<< (list_stream<C>& ls, const idref<X, C, B>& x)
463      {
464        ls.os_ << x;
465      }
466
467
468      // idrefs
469      //
470      template <typename C, typename B, typename idref>
471      inline void
472      operator<< (xercesc::DOMElement& e, const idrefs<C, B, idref>& v)
473      {
474        const list<idref, C>& r (v);
475        e << r;
476      }
477
478      template <typename C, typename B, typename idref>
479      inline void
480      operator<< (xercesc::DOMAttr& a, const idrefs<C, B, idref>& v)
481      {
482        const list<idref, C>& r (v);
483        a << r;
484      }
485
486      template <typename C, typename B, typename idref>
487      inline void
488      operator<< (list_stream<C>& ls, const idrefs<C, B, idref>& v)
489      {
490        const list<idref, C>& r (v);
491        ls << r;
492      }
493
494
495      // uri
496      //
497      template <typename C, typename B>
498      inline void
499      operator<< (xercesc::DOMElement& e, const uri<C, B>& x)
500      {
501        bits::insert<C> (e, x);
502      }
503
504      template <typename C, typename B>
505      inline void
506      operator<< (xercesc::DOMAttr& a, const uri<C, B>& x)
507      {
508        bits::insert<C> (a, x);
509      }
510
511      template <typename C, typename B>
512      inline void
513      operator<< (list_stream<C>& ls, const uri<C, B>& x)
514      {
515        ls.os_ << x;
516      }
517
518
519      // qname
520      //
521      template <typename C, typename B, typename uri, typename ncname>
522      void
523      operator<< (xercesc::DOMElement& e, const qname<C, B, uri, ncname>& x)
524      {
525        std::basic_ostringstream<C> os;
526
527        if (x.qualified ())
528        {
529          const std::basic_string<C>& ns (x.namespace_ ());
530
531          try
532          {
533            std::basic_string<C> p (xml::dom::prefix (ns, e));
534
535            if (!p.empty ())
536              os << p << C (':');
537          }
538          catch (const xml::dom::no_prefix&)
539          {
540            throw no_namespace_mapping<C> (ns);
541          }
542        }
543
544        os << x.name ();
545        e << os.str ();
546      }
547
548      template <typename C, typename B, typename uri, typename ncname>
549      void
550      operator<< (xercesc::DOMAttr& a, const qname<C, B, uri, ncname>& x)
551      {
552        std::basic_ostringstream<C> os;
553
554        if (x.qualified ())
555        {
556          const std::basic_string<C>& ns (x.namespace_ ());
557
558          try
559          {
560            std::basic_string<C> p (
561              xml::dom::prefix (ns, *a.getOwnerElement ()));
562
563            if (!p.empty ())
564              os << p << C (':');
565          }
566          catch (const xml::dom::no_prefix&)
567          {
568            throw no_namespace_mapping<C> (ns);
569          }
570        }
571
572        os << x.name ();
573        a << os.str ();
574      }
575
576      template <typename C, typename B, typename uri, typename ncname>
577      void
578      operator<< (list_stream<C>& ls, const qname<C, B, uri, ncname>& x)
579      {
580
581        if (x.qualified ())
582        {
583          const std::basic_string<C>& ns (x.namespace_ ());
584
585          try
586          {
587            std::basic_string<C> p (xml::dom::prefix (ns, ls.parent_));
588
589            if (!p.empty ())
590              ls.os_ << p << C (':');
591          }
592          catch (const xml::dom::no_prefix&)
593          {
594            throw no_namespace_mapping<C> (ns);
595          }
596        }
597
598        ls.os_ << x.name ();
599      }
600
601
602      // base64_binary
603      //
604      template <typename C, typename B>
605      inline void
606      operator<< (xercesc::DOMElement& e, const base64_binary<C, B>& x)
607      {
608        e << x.encode ();
609      }
610
611      template <typename C, typename B>
612      inline void
613      operator<< (xercesc::DOMAttr& a, const base64_binary<C, B>& x)
614      {
615        a << x.encode ();
616      }
617
618      template <typename C, typename B>
619      inline void
620      operator<< (list_stream<C>& ls, const base64_binary<C, B>& x)
621      {
622        ls.os_ << x.encode ();
623      }
624
625
626      // hex_binary
627      //
628      template <typename C, typename B>
629      inline void
630      operator<< (xercesc::DOMElement& e, const hex_binary<C, B>& x)
631      {
632        e << x.encode ();
633      }
634
635      template <typename C, typename B>
636      inline void
637      operator<< (xercesc::DOMAttr& a, const hex_binary<C, B>& x)
638      {
639        a << x.encode ();
640      }
641
642      template <typename C, typename B>
643      inline void
644      operator<< (list_stream<C>& ls, const hex_binary<C, B>& x)
645      {
646        ls.os_ << x.encode ();
647      }
648
649
650      // entity
651      //
652      template <typename C, typename B>
653      inline void
654      operator<< (xercesc::DOMElement& e, const entity<C, B>& x)
655      {
656        bits::insert<C> (e, x);
657      }
658
659      template <typename C, typename B>
660      inline void
661      operator<< (xercesc::DOMAttr& a, const entity<C, B>& x)
662      {
663        bits::insert<C> (a, x);
664      }
665
666      template <typename C, typename B>
667      inline void
668      operator<< (list_stream<C>& ls, const entity<C, B>& x)
669      {
670        ls.os_ << x;
671      }
672
673
674      // entities
675      //
676      template <typename C, typename B, typename entity>
677      inline void
678      operator<< (xercesc::DOMElement& e, const entities<C, B, entity>& v)
679      {
680        const list<entity, C>& r (v);
681        e << r;
682      }
683
684      template <typename C, typename B, typename entity>
685      inline void
686      operator<< (xercesc::DOMAttr& a, const entities<C, B, entity>& v)
687      {
688        const list<entity, C>& r (v);
689        a << r;
690      }
691
692      template <typename C, typename B, typename entity>
693      inline void
694      operator<< (list_stream<C>& ls, const entities<C, B, entity>& v)
695      {
696        const list<entity, C>& r (v);
697        ls << r;
698      }
699    }
700  }
701}
Note: See TracBrowser for help on using the browser.