aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/serializer/wildcard/driver.cxx
blob: 876f47beb974bbb5d4c6dc16c1428a0306c71150 (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
// file      : examples/cxx/serializer/wildcard/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <iostream>

#include "email-sskel.hxx"

#ifndef XSDE_REUSE_STYLE_TIEIN
#  error this example requires the tiein serializer reuse support
#endif

using namespace std;

namespace email
{
  struct binary_simpl: binary_sskel
  {
    // Indicate to base64_binary_simpl that we want it to release
    // the buffer passed via pre().
    //
    binary_simpl ()
        : binary_sskel (&base_impl_), base_impl_ (true)
    {
    }

    virtual void
    pre (const binary* bin)
    {
      using namespace xml_schema;

      bin_ = bin;
      buffer* buf = new buffer (bin->data (), bin->size ());
      base_impl_.pre (buf);
    }

    virtual std::string
    name ()
    {
      return bin_->name ();
    }

    virtual std::string
    mime ()
    {
      return bin_->mime ();
    }

  private:
    xml_schema::base64_binary_simpl base_impl_;
    const binary* bin_;
  };

  struct envelope_simpl: envelope_sskel
  {
    envelope_simpl (xml_schema::string_simpl& text_s,
                    binary_sskel& bin_s,
                    xml_schema::unsigned_int_simpl& uint_s)
        : text_s_ (text_s), bin_s_ (bin_s), uint_s_ (uint_s)
    {
    }

    virtual void
    pre (const envelope& env)
    {
      env_ = &env;
      tid_set_ = false;
      i_ = env_->begin_body ();
    }

    // Attributes.
    //
    virtual bool
    any_attribute_next ()
    {
      return !tid_set_;
    }

    virtual void
    any_attribute (std::string& ns, std::string& name)
    {
      ns = "http://www.codesynthesis.com/email";
      name = "thread-id";
    }

    virtual void
    serialize_any_attribute ()
    {
      // This function is where we perform the actual wildcard
      // serialization.
      //
      xml_schema::serializer_context& ctx = _context ();

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

      uint_s_.pre (env_->thread_id ());
      uint_s_._pre_impl (ctx);
      uint_s_._serialize_content ();
      uint_s_._post_impl ();
      uint_s_.post ();
#else
      // If we are not using exceptions or XML Schema validation
      // is enabled then we need to check for the error condition
      // and, if the (user) error was set in pre() or post(),
      // also copy the error code to the context. The _error_type()
      // function returns non-0 value if there an error pending.
      // The _copy_error() functions copies the error state to
      // the context.

      uint_s_.pre (env_->thread_id ());

#ifndef XSDE_EXCEPTIONS
      if (uint_s_._error_type ())
        uint_s_._copy_error (ctx);

      if (ctx.error_type ())
        return;
#endif
      uint_s_._pre_impl (ctx);

      if (ctx.error_type ())
        return;

      uint_s_._serialize_content ();

      if (ctx.error_type ())
        return;

      uint_s_._post_impl ();

      if (ctx.error_type ())
        return;

      uint_s_.post ();

#ifndef XSDE_EXCEPTIONS
      if (uint_s_._error_type ())
        uint_s_._copy_error (ctx);

      if (_error_type ())
        return;
#endif
#endif

      tid_set_ = true;
    }

    // Elements.
    //
    virtual std::string
    to ()
    {
      return env_->to ();
    }

    virtual std::string
    from ()
    {
      return env_->from ();
    }

    virtual std::string
    subject ()
    {
      return env_->subject ();
    }

    virtual bool
    any_next ()
    {
      // See if there is a body that we know how to serialize.
      //
      for (; i_ != env_->end_body (); ++i_)
      {
        body_type t = (*i_)->type ();

        if (t == email::text_body || t == email::binary_body)
          break;
      }

      return i_ != env_->end_body ();
    }

    virtual void
    any (std::string& ns, std::string& name)
    {
      ns = "http://www.codesynthesis.com/email";

      switch ((*i_)->type ())
      {
      case email::text_body:
        {
          name = "text";
          break;
        }
      case email::binary_body:
        {
          name = "binary";
          break;
        }
      }
    }

    virtual void
    serialize_any ()
    {
      const body* b = *i_++;
      xml_schema::serializer_context& ctx = _context ();

      switch (b->type ())
      {
      case email::text_body:
        {
          // For more information on what's going here, see the
          // serialize_any_attribute() function above.
          //
#if defined(XSDE_EXCEPTIONS) && !defined(XSDE_SERIALIZER_VALIDATION)

          text_s_.pre (static_cast<const text*> (b)->content ());
          text_s_._pre_impl (ctx);
          text_s_._serialize_content ();
          text_s_._post_impl ();
          text_s_.post ();
#else

          text_s_.pre (static_cast<const text*> (b)->content ());

#ifndef XSDE_EXCEPTIONS
          if (text_s_._error_type ())
            text_s_._copy_error (ctx);

          if (ctx.error_type ())
            return;
#endif
          text_s_._pre_impl (ctx);

          if (ctx.error_type ())
            return;

          text_s_._serialize_content ();

          if (ctx.error_type ())
            return;

          text_s_._post_impl ();

          if (ctx.error_type ())
            return;

          text_s_.post ();

#ifndef XSDE_EXCEPTIONS
          if (text_s_._error_type ())
            text_s_._copy_error (ctx);

          if (ctx.error_type ())
            return;
#endif
#endif
          break;
        }
      case email::binary_body:
        {
          // For more information on what's going here, see the
          // serialize_any_attribute() function above. Note also
          // that in this case we also call _serialize_attributes
          // since binary is a complex type.
          //

#if defined(XSDE_EXCEPTIONS) && !defined(XSDE_SERIALIZER_VALIDATION)
          bin_s_.pre (static_cast<const binary*> (b));
          bin_s_._pre_impl (ctx);
          bin_s_._serialize_attributes ();
          bin_s_._serialize_content ();
          bin_s_._post_impl ();
          bin_s_.post ();
#else
          bin_s_.pre (static_cast<const binary*> (b));

#ifndef XSDE_EXCEPTIONS
          if (bin_s_._error_type ())
            bin_s_._copy_error (ctx);

          if (ctx.error_type ())
            return;
#endif
          bin_s_._pre_impl (ctx);

          if (ctx.error_type ())
            return;

          bin_s_._serialize_attributes ();

          if (ctx.error_type ())
            return;

          bin_s_._serialize_content ();

          if (ctx.error_type ())
            return;

          bin_s_._post_impl ();

          if (ctx.error_type ())
            return;

          bin_s_.post ();

#ifndef XSDE_EXCEPTIONS
          if (bin_s_._error_type ())
            bin_s_._copy_error (ctx);

          if (ctx.error_type ())
            return;
#endif
#endif
          break;
        }
      }
    }

    // If we need to be able to reset and reuse the serializer
    // after an error then we also need to override _reset() and
    // reset the serializers that are used to handle wildcards.
    // Note that you always need to call _reset() from the base.
    //
    virtual void
    _reset ()
    {
      envelope_sskel::_reset ();

      text_s_._reset ();
      bin_s_._reset ();
      uint_s_._reset ();
    }

  private:
    // Serializers used in wildcard content serialization.
    //
    xml_schema::string_simpl& text_s_;
    binary_sskel& bin_s_;
    xml_schema::unsigned_int_simpl& uint_s_;

    const envelope* env_;
    bool tid_set_;
    envelope::body_iterator i_;
  };
}

int
main ()
{
  try
  {
    using namespace email;

    // Create a sample email with a text body and two (fake) pictures.
    //
    envelope env ("Jane Doe <jane@doe.com>",
                  "John Doe <john@doe.com>",
                  "Surfing pictures",
                  123); // thread id

    env.add_body (new text ("Hi Jane,\n"
                            "Here are cool pictures of me surfing.\n\n"
                            "Cheers,\n"
                            "John"));

    env.add_body (new binary ("pic1.jpg", "image/jpeg", "abc123", 6));
    env.add_body (new binary ("pic2.jpg", "image/jpeg", "123abc", 6));


    // Construct the serializer.
    //
    xml_schema::string_simpl string_s;
    xml_schema::unsigned_int_simpl uint_s;

    binary_simpl binary_s;
    envelope_simpl envelope_s (string_s, binary_s, uint_s);

    binary_s.serializers (string_s, string_s);
    envelope_s.serializers (string_s, string_s, string_s);

    // Serialize the sample email to XML document.
    //
    xml_schema::document_simpl doc_s (
      envelope_s,
      "http://www.codesynthesis.com/email",
      "message");

    doc_s.add_prefix ("eml", "http://www.codesynthesis.com/email");
    doc_s.add_schema ("http://www.codesynthesis.com/email", "email.xsd");

    envelope_s.pre (env);
    doc_s.serialize (cout, xml_schema::document_simpl::pretty_print);
    envelope_s.post ();
  }
  catch (const xml_schema::serializer_exception& e)
  {
    cerr << "error: " << e.text () << endl;
    return 1;
  }
  catch (const std::ios_base::failure&)
  {
    cerr << "error: write failure" << endl;
    return 1;
  }

  return 0;
}