aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/serializer/genx/document.cxx
blob: b107f06259a846109e04f68a6b01d1892191e6c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
// file      : xsde/cxx/serializer/genx/document.cxx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/config.hxx>

#include <string.h> // strlen, strcmp

#ifdef XSDE_EXCEPTIONS
#  include <new> // std::bad_alloc
#endif

#ifdef XSDE_IOSTREAM
#  include <iostream>
#endif

#ifdef XSDE_EXCEPTIONS
#  include <xsde/cxx/serializer/exceptions.hxx>
#else
#  include <xsde/cxx/serializer/error.hxx>
#endif

#include <xsde/cxx/serializer/context.hxx>

#ifdef XSDE_POLYMORPHIC
#  include <xsde/cxx/serializer/substitution-map.hxx>
#endif

#include <xsde/cxx/serializer/genx/document.hxx>

namespace xsde
{
  namespace cxx
  {
    namespace serializer
    {
      namespace genx
      {
#ifdef XSDE_POLYMORPHIC
        static substitution_map_init substitution_map_init_;
#endif

        // writer
        //
        writer::
        ~writer ()
        {
        }

#ifdef XSDE_EXCEPTIONS
        void writer::
        write (const char* s)
        {
          write (s, strlen (s));
        }
#else
        bool writer::
        write (const char* s)
        {
          return write (s, strlen (s));
        }
#endif

        // document_simpl
        //
        const document_simpl::flags document_simpl::pretty_print = 0x01;

	document_simpl::
	~document_simpl ()
	{
	  // Prevents auto-generation of this dtor.
        }

        document_simpl::
#ifdef XSDE_POLYMORPHIC
        document_simpl (serializer_base& serializer,
                        const char* name,
                        bool,
                        const char* st)
#else
        document_simpl (serializer_base& serializer, const char* name)
#endif
            : serializer_ (serializer)
        {
#ifdef XSDE_POLYMORPHIC
          root_static_type_ = st;
#endif
          init_root_name (0, name);
        }

        document_simpl::
#ifdef XSDE_POLYMORPHIC
        document_simpl (serializer_base& serializer,
                        const char* ns,
                        const char* name,
                        bool,
                        const char* st)
#else
        document_simpl (serializer_base& serializer,
                        const char* ns,
                        const char* name)
#endif
            : serializer_ (serializer)
        {
#ifdef XSDE_POLYMORPHIC
          root_static_type_ = st;
#endif
          init_root_name (ns, name);
        }

#ifdef XSDE_STL
        document_simpl::
#ifdef XSDE_POLYMORPHIC
        document_simpl (serializer_base& serializer,
                        const std::string& name,
                        bool,
                        const char* st)
#else
        document_simpl (serializer_base& serializer, const std::string& name)
#endif
            : serializer_ (serializer)
        {
#ifdef XSDE_POLYMORPHIC
          root_static_type_ = st;
#endif
          init_root_name (0, name.c_str ());
        }

        document_simpl::
#ifdef XSDE_POLYMORPHIC
        document_simpl (serializer_base& serializer,
                        const std::string& ns,
                        const std::string& name,
                        bool,
                        const char* st)
#else
        document_simpl (serializer_base& serializer,
                        const std::string& ns,
                        const std::string& name)
#endif
            : serializer_ (serializer)
        {
#ifdef XSDE_POLYMORPHIC
          root_static_type_ = st;
#endif
          init_root_name (ns.c_str (), name.c_str ());
        }
#endif // XSDE_STL


        void document_simpl::
        init_root_name (const char* ns, const char* name)
        {
          // Leave root_ns_ uninitialized if ns is not specified.
          //
          if (ns != 0 && *ns != '\0')
          {
#ifdef XSDE_EXCEPTIONS
            root_ns_.assign (ns);
#else
            if (root_ns_.assign (ns))
            {
              error_ = error (sys_error::no_memory);
              return;
            }
#endif
          }

#ifdef XSDE_EXCEPTIONS
          root_name_.assign (name);
#else
          if (root_name_.assign (name))
            error_ = error (sys_error::no_memory);
#endif
        }

        void document_simpl::
        add_prefix (const char* p, const char* ns)
        {
#if defined (XSDE_STL)
          prefixes_.push_back (p);
          prefix_namespaces_.push_back (ns);
#else
#if defined (XSDE_EXCEPTIONS)
          prefixes_.push_back_copy (p);
          prefix_namespaces_.push_back_copy (ns);
#else
          if (prefixes_.push_back_copy (p) ||
              prefix_namespaces_.push_back_copy (ns))
            error_ = error (sys_error::no_memory);
#endif
#endif
        }

        void document_simpl::
        add_default_prefix (const char* ns)
        {
#if defined (XSDE_STL)
          prefixes_.push_back ("");
          prefix_namespaces_.push_back (ns);
#else
#if defined (XSDE_EXCEPTIONS)
          prefixes_.push_back_copy ("");
          prefix_namespaces_.push_back_copy (ns);
#else
          if (prefixes_.push_back_copy ("") ||
              prefix_namespaces_.push_back_copy (ns))
            error_ = error (sys_error::no_memory);
#endif
#endif
        }

        void document_simpl::
        add_schema (const char* ns, const char* l)
        {
#if defined (XSDE_STL)
          schemas_.push_back (l);
          schema_namespaces_.push_back (ns);
#else
#if defined (XSDE_EXCEPTIONS)
          schemas_.push_back_copy (l);
          schema_namespaces_.push_back_copy (ns);
#else
          if (schemas_.push_back_copy (l) ||
              schema_namespaces_.push_back_copy (ns))
            error_ = error (sys_error::no_memory);
#endif
#endif
        }

        void document_simpl::
        add_no_namespace_schema (const char* l)
        {
#if defined (XSDE_STL)
          schemas_.push_back (l);
          schema_namespaces_.push_back ("");
#else
#if defined (XSDE_EXCEPTIONS)
          schemas_.push_back_copy (l);
          schema_namespaces_.push_back_copy ("");
#else
          if (schemas_.push_back_copy (l) ||
              schema_namespaces_.push_back_copy (""))
            error_ = error (sys_error::no_memory);
#endif
#endif
        }

        //
        //
        void document_simpl::
        reset ()
        {
#ifndef XSDE_EXCEPTIONS
          error_ = error ();
#endif
          if (xml_serializer_)
            genxReset (xml_serializer_);

          serializer_._reset ();
        }

        // istream
        //
#ifdef XSDE_IOSTREAM

#ifdef XSDE_EXCEPTIONS
        static void
        ostream_write (void* p, const char* s, size_t n)
        {
          std::ostream* os = reinterpret_cast<std::ostream*> (p);
          os->write (s, static_cast<std::streamsize> (n));

          if (os->bad ())
            throw std::ios_base::failure ("write failed");
        }

        static void
        ostream_flush (void* p)
        {
          std::ostream* os = reinterpret_cast<std::ostream*> (p);
          os->flush ();

          if (os->bad ())
            throw std::ios_base::failure ("flush failed");
        }
#else
        static bool
        ostream_write (void* p, const char* s, size_t n)
        {
          std::ostream* os = reinterpret_cast<std::ostream*> (p);
          os->write (s, static_cast<std::streamsize> (n));
          return !os->bad ();
        }

        static bool
        ostream_flush (void* p)
        {
          std::ostream* os = reinterpret_cast<std::ostream*> (p);
          os->flush ();
          return !os->bad ();
        }
#endif

        void document_simpl::
        serialize (std::ostream& os, flags f)
        {
          serialize (&ostream_write, &ostream_flush, &os, f);
        }
#endif // XSDE_IOSTREAM

        //
        //
#ifdef XSDE_EXCEPTIONS
        static void
        write_thunk (void* p, const char* s)
        {
          reinterpret_cast<writer*> (p)->write (s);
        }

        static void
        write_bound_thunk (void* p, const char* s, size_t n)
        {
          reinterpret_cast<writer*> (p)->write (s, n);
        }

        static void
        flush_thunk (void* p)
        {
          reinterpret_cast<writer*> (p)->flush ();
        }
#else
        static bool
        write_thunk (void* p, const char* s)
        {
          return reinterpret_cast<writer*> (p)->write (s);
        }

        static bool
        write_bound_thunk (void* p, const char* s, size_t n)
        {
          return reinterpret_cast<writer*> (p)->write (s, n);
        }

        static bool
        flush_thunk (void* p)
        {
          return reinterpret_cast<writer*> (p)->flush ();
        }
#endif

        void document_simpl::
        serialize (writer& w, flags f)
        {
          serialize (&write_thunk, &write_bound_thunk, &flush_thunk, &w, f);
        }

        // Genx write functions.
        //
        struct writer_info
        {
          void* user_data;
          document_simpl::write_func write;
          document_simpl::write_bound_func write_bound;
          document_simpl::flush_func flush;
        };

        extern "C" genxStatus
        genx_write (void* p, constUtf8 us)
        {
          const char* s = reinterpret_cast<const char*> (us);
          writer_info* wi = reinterpret_cast<writer_info*> (p);

          if (wi->write != 0)
          {
#ifdef XSDE_EXCEPTIONS
            wi->write (wi->user_data, s);
#else
            if (!wi->write (wi->user_data, s))
              return GENX_IO_ERROR;
#endif
          }
          else
          {
#ifdef XSDE_EXCEPTIONS
            wi->write_bound (wi->user_data, s, strlen (s));
#else
            if (!wi->write_bound (wi->user_data, s, strlen (s)))
              return GENX_IO_ERROR;
#endif
          }

          return GENX_SUCCESS;
        }

        extern "C" genxStatus
        genx_write_bound (void* p, constUtf8 start, constUtf8 end)
        {
          size_t size = static_cast<size_t> (end - start);
          const char* s = reinterpret_cast<const char*> (start);
          writer_info* wi = reinterpret_cast<writer_info*> (p);

#ifdef XSDE_EXCEPTIONS
          wi->write_bound (wi->user_data, s, size);
#else
          if (!wi->write_bound (wi->user_data, s, size))
            return GENX_IO_ERROR;
#endif

          return GENX_SUCCESS;
        }

        extern "C" genxStatus
        genx_flush (void* p)
        {
          writer_info* wi = reinterpret_cast<writer_info*> (p);

#ifdef XSDE_EXCEPTIONS
          wi->flush (wi->user_data);
#else
          if (!wi->flush (wi->user_data))
            return GENX_IO_ERROR;
#endif

          return GENX_SUCCESS;
        }

        void document_simpl::
        serialize (write_func wf, write_bound_func wbf, flush_func ff,
                   void* user_data, flags f)
        {
          if (xml_serializer_ == 0)
          {
            xml_serializer_ = genxNew (0, 0, 0);

            if (xml_serializer_ == 0)
            {
#ifdef XSDE_EXCEPTIONS
              throw std::bad_alloc ();
#else
              error_ = error (sys_error::no_memory);
              return;
#endif
            }
          }

          writer_info wi;
          wi.user_data = user_data;
          wi.write = wf;
          wi.write_bound = wbf;
          wi.flush = ff;

          genxSetUserData (xml_serializer_, &wi);

          if (f & pretty_print)
            genxSetPrettyPrint (xml_serializer_, 2);

          genxSender sender;
          sender.send = &genx_write;
          sender.sendBounded = &genx_write_bound;
          sender.flush = &genx_flush;

          genxStatus e = genxStartDocSender (xml_serializer_, &sender);

          if (e != GENX_SUCCESS)
          {
            filter_xml_error (e);
            return;
          }

          serialize (xml_serializer_);

#ifndef XSDE_EXCEPTIONS
          if (error_)
            return;
#endif

          e = genxEndDocument (xml_serializer_);

          if (e != GENX_SUCCESS)
            filter_xml_error (e);
        }

        //
        //
        static const char xsi[] = "http://www.w3.org/2001/XMLSchema-instance";

        void document_simpl::
        serialize (genxWriter s)
        {
          context ctx (s);

#ifndef XSDE_POLYMORPHIC

#ifdef XSDE_EXCEPTIONS
          ctx.start_element (root_ns_.data (), root_name_.data ());
#else
          if (!ctx.start_element (root_ns_.data (), root_name_.data ()))
          {
            error_ = error (ctx.xml_error ());
            return;
          }
#endif
#else // XSDE_POLYMORPHIC
          const char* ns = root_ns_.data ();
          const char* n = root_name_.data ();
          const char* dt = 0;

          if (root_static_type_ != 0)
          {
            dt = serializer_._dynamic_type ();

            // Call to check sets ns and n if successful.
            //
            if (strcmp (dt, root_static_type_) == 0 ||
                substitution_map_instance ().check (ns, n, dt, 0))
              dt = 0;
          }

#ifdef XSDE_EXCEPTIONS
          ctx.start_element (ns, n);
#else
          if (!ctx.start_element (ns, n))
          {
            error_ = error (ctx.xml_error ());
            return;
          }
#endif
#endif // XSDE_POLYMORPHIC

          // Add namespace prefixes.
          //
          for (size_t i = 0; i < prefixes_.size (); ++i)
          {
#ifdef XSDE_STL
            const char* p = prefixes_[i].c_str ();
            const char* ns = prefix_namespaces_[i].c_str ();
#else
            const char* p = prefixes_[i];
            const char* ns = prefix_namespaces_[i];
#endif

#ifdef XSDE_EXCEPTIONS
            ctx.declare_namespace (ns, p);
#else
            if (!ctx.declare_namespace (ns, p))
              break;
#endif
          }

#ifndef XSDE_EXCEPTIONS
          if (ctx.error_type ())
          {
            error_ = error (ctx.xml_error ());
            return;
          }
#endif

          // Add the schema location attributes.
          //
          if (schemas_.size () != 0)
          {
#ifdef XSDE_EXCEPTIONS
            ctx.declare_namespace (xsi, "xsi");
#else
            if (!ctx.declare_namespace (xsi, "xsi"))
            {
              error_ = error (ctx.xml_error ());
              return;
            }
#endif
            // First add the xsi:schemaLocation attribute.
            //
            bool start = false;

            for (size_t j = 0; j < schemas_.size (); ++j)
            {
#ifdef XSDE_STL
              const char* l = schemas_[j].c_str ();
              const char* ns = schema_namespaces_[j].c_str ();
#else
              const char* l = schemas_[j];
              const char* ns = schema_namespaces_[j];
#endif
              if (ns[0] != '\0')
              {
                if (!start)
                {
#ifdef XSDE_EXCEPTIONS
                  ctx.start_attribute (xsi, "schemaLocation");
#else
                  if (!ctx.start_attribute (xsi, "schemaLocation"))
                    break;
#endif
                  start = true;
                }
                else
                {
#ifdef XSDE_EXCEPTIONS
                  ctx.characters (" ", 1);
#else
                  if (!ctx.characters (" ", 1))
                    break;
#endif
                }

#ifdef XSDE_EXCEPTIONS
                ctx.characters (ns);
                ctx.characters (" ", 1);
                ctx.characters (l);
#else
                if (!ctx.characters (ns) ||
                    !ctx.characters (" ", 1) ||
                    !ctx.characters (l))
                  break;
#endif
              }
            }

#ifndef XSDE_EXCEPTIONS
            if (ctx.error_type ())
            {
              error_ = error (ctx.xml_error ());
              return;
            }
#endif

            if (start)
            {
#ifdef XSDE_EXCEPTIONS
              ctx.end_attribute ();
#else
              if (!ctx.end_attribute ())
              {
                error_ = error (ctx.xml_error ());
                return;
              }
#endif
            }

            // Now add the xsi:noNamespaceSchemaLocation attribute.
            //
            start = false;

            for (size_t k = 0; k < schemas_.size (); ++k)
            {
#ifdef XSDE_STL
              const char* l = schemas_[k].c_str ();
              const char* ns = schema_namespaces_[k].c_str ();
#else
              const char* l = schemas_[k];
              const char* ns = schema_namespaces_[k];
#endif
              if (ns[0] == '\0')
              {
                if (!start)
                {
#ifdef XSDE_EXCEPTIONS
                  ctx.start_attribute (xsi, "noNamespaceSchemaLocation");
#else
                  if (!ctx.start_attribute (xsi, "noNamespaceSchemaLocation"))
                    break;
#endif
                  start = true;
                }
                else
                {
#ifdef XSDE_EXCEPTIONS
                  ctx.characters (" ", 1);
#else
                  if (!ctx.characters (" ", 1))
                    break;
#endif
                }

#ifdef XSDE_EXCEPTIONS
                ctx.characters (l);
#else
                if (!ctx.characters (l))
                  break;
#endif
              }
            }

#ifndef XSDE_EXCEPTIONS
            if (ctx.error_type ())
            {
              error_ = error (ctx.xml_error ());
              return;
            }
#endif

            if (start)
            {
#ifdef XSDE_EXCEPTIONS
              ctx.end_attribute ();
#else
              if (!ctx.end_attribute ())
              {
                error_ = error (ctx.xml_error ());
                return;
              }
#endif
            }
          }

          // Call the root type serializer to serialize the content.
          //

#if !defined (XSDE_SERIALIZER_VALIDATION) && defined (XSDE_EXCEPTIONS)

#ifdef XSDE_POLYMORPHIC
          // Set xsi:type if necessary.
          //
          if (dt != 0)
            ctx.set_type (dt);
#endif
          serializer_._pre_impl (ctx);
          serializer_._serialize_attributes ();
          serializer_._serialize_content ();
          serializer_._post_impl ();

#else // !defined (XSDE_SERIALIZER_VALIDATION) && defined (XSDE_EXCEPTIONS)

#ifdef XSDE_POLYMORPHIC
          // Set xsi:type if necessary.
          //
          if (dt != 0)
            ctx.set_type (dt);

#ifndef XSDE_EXCEPTIONS
          if (!ctx.error_type ())
#endif
#endif // XSDE_POLYMORPHIC

            serializer_._pre_impl (ctx);

          if (!ctx.error_type ())
            serializer_._serialize_attributes ();

          if (!ctx.error_type ())
            serializer_._serialize_content ();

          if (!ctx.error_type ())
            serializer_._post_impl ();

          if (context::error_type_t et = ctx.error_type ())
          {
            switch (et)
            {
#ifndef XSDE_EXCEPTIONS
            case context::error_app:
              {
                error_ = error (ctx.app_error ());
                break;
              }
#endif
#ifdef XSDE_SERIALIZER_VALIDATION
            case context::error_schema:
              {
#ifdef XSDE_EXCEPTIONS
                throw schema (ctx.schema_error ());
#else
                error_ = error (ctx.schema_error ());
                break;
#endif
              }
#endif
#ifndef XSDE_EXCEPTIONS
            case context::error_xml:
              {
                filter_xml_error (ctx.xml_error ());
                break;
              }
            case context::error_sys:
              {
                error_ = error (ctx.sys_error ());
                break;
              }
#endif
            default:
              break;
            }
            return;
          }
#endif // !XSDE_SERIALIZER_VALIDATION && XSDE_EXCEPTIONS


#ifdef XSDE_EXCEPTIONS
          ctx.end_element ();
#else
          if (!ctx.end_element ())
          {
            error_ = error (ctx.xml_error ());
            return;
          }
#endif
        }

        void document_simpl::
        filter_xml_error (genxStatus e)
        {
          switch (e)
          {
          case GENX_ALLOC_FAILED:
            {
#ifdef XSDE_EXCEPTIONS
              throw std::bad_alloc ();
#else
              error_ = error (sys_error::no_memory);
              break;
#endif
            }
          case GENX_IO_ERROR:
            {
#ifdef XSDE_EXCEPTIONS
              // This should never happen with consistent exception
              // handling usage since the write/flush functions
              // throw exceptions to indicate write failures.
              //
              throw xml (e);
#else
              error_ = error (sys_error::write_failed);
              break;
#endif
            }
          default:
            {
#ifdef XSDE_EXCEPTIONS
              throw xml (e);
#else
              error_ = error (e);
              break;
#endif
            }
          }
        }
      }
    }
  }
}