aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/custom/wildcard/driver.cxx
blob: 3168f43463995678c469a7acf30cbdf300d746d3 (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
// file      : examples/cxx/hybrid/custom/wildcard/driver.cxx
// copyright : not copyrighted - public domain

#include <string>   // memcpy
#include <memory>   // std::auto_ptr
#include <iostream>

#include "email.hxx"
#include "email-pimpl.hxx"
#include "email-simpl.hxx"

using namespace std;

// Extended parser and serializer aggregates for the message
// element. They add parser/serializer aggregates for the
// wildcard elements.
//
namespace email
{
  class message_paggr_ex: public message_paggr
  {
  public:
    message_paggr_ex ()
    {
      envelope_p_.text_parser (text_p_.root_parser ());
      envelope_p_.binary_parser (binary_p_.root_parser ());
    }

  public:
    text_paggr text_p_;
    binary_paggr binary_p_;
  };

  class message_saggr_ex: public message_saggr
  {
  public:
    message_saggr_ex ()
    {
      envelope_s_.text_serializer (text_s_.root_serializer ());
      envelope_s_.binary_serializer (binary_s_.root_serializer ());
    }

  public:
    text_saggr text_s_;
    binary_saggr binary_s_;
  };
}

int
main (int argc, char* argv[])
{
  const char* input;

  if (argc < 2)
  {
    input = "STDIN";
    cerr << "XML file not specified, reading from STDIN" << endl;
  }
  else
    input = argv[1];

  try
  {
    using namespace email;

    // Parse.
    //
    message_paggr_ex message_p;

    xml_schema::document_pimpl doc_p (
      message_p.root_parser (),
      message_p.root_namespace (),
      message_p.root_name ());

    message_p.pre ();

    if (argc < 2)
      doc_p.parse (cin);
    else
      doc_p.parse (argv[1]);

    auto_ptr<envelope> msg (message_p.post ());

    // Print what we've got.
    //
    cerr << "To:      " << msg->to () << endl
         << "From:    " << msg->from () << endl
         << "Subject: " << msg->subject () << endl;

    envelope::body_sequence& body_seq (msg->body ());

    for (envelope::body_iterator i = body_seq.begin ();
         i != body_seq.end ();
         ++i)
    {
      body& b = *i;

      switch (b.body_type ())
      {
      case body::type_text:
        {
          cerr << b.text () << endl
               << endl;
          break;
        }
      case body::type_binary:
        {
          const binary& bin = b.binary ();
          cerr << "binary: " << bin.name () << " "
               << "type: " << bin.mime () << endl
               << endl;
          break;
        }
      default:
        {
          cerr << "unexpected body type" << endl;
          break;
        }
      }
    }

    // Create a reply message.
    //
    auto_ptr<envelope> reply (new envelope);
    reply->to (msg->from ());
    reply->from (msg->to ());
    reply->subject ("Re: " + msg->subject ());

    // Add a text body.
    //
    auto_ptr<body> b (new body);
    b->text ("Hi!\n\n"
             "Indeed nice pictures. Check out mine.\n\n"
             "Jane");
    reply->body ().push_back (b.release ());

    // Add a (fake) image.
    //
    auto_ptr<binary> pic (new binary);
    pic->name ("pic.jpg");
    pic->mime ("image/jpeg");
    pic->size (3);
    memcpy (pic->data (), "123", 3);

    b = auto_ptr<body> (new body);
    b->binary (pic.release ());
    reply->body ().push_back (b.release ());

    // Serialize.
    //
    message_saggr_ex message_s;

    xml_schema::document_simpl doc_s (
      message_s.root_serializer (),
      message_s.root_namespace (),
      message_s.root_name ());

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

    message_s.pre (*reply);
    doc_s.serialize (cout, xml_schema::document_simpl::pretty_print);
    message_s.post ();
  }
  catch (const xml_schema::parser_exception& e)
  {
    cerr << input << ":" << e.line () << ":" << e.column () << ": "
         << e.text () << endl;
    return 1;
  }
  catch (const xml_schema::serializer_exception& e)
  {
    cerr << "error: " << e.text () << endl;
    return 1;
  }
  catch (const std::ios_base::failure&)
  {
    cerr << input << ": unable to open or read/write failure" << endl;
    return 1;
  }

  return 0;
}