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

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

#include "protocol.hxx"
#include "protocol-pskel.hxx" // Get the configuration macros (XSDE_*).

#if defined(XSDE_REUSE_STYLE_MIXIN)
#  include "protocol-pimpl-mixin.hxx"
#elif defined(XSDE_REUSE_STYLE_TIEIN)
#  include "protocol-pimpl-tiein.hxx"
#else
#  error this example requires mixin or tiein parser reuse support
#endif

using std::cerr;
using std::endl;
using xml_schema::ro_string;

namespace protocol
{
  // We are going to use our own "type ids" for the request
  // types. You could instead use dynamic_cast if your system
  // provides RTTI.
  //
  enum request_type
  {
    unknown_type,
    balance_type,
    withdraw_type
  };

  // Customize the xml_schema::document_pimpl object to handle our protocol
  // vocabulary with multiple root elements.
  //
  class document_pimpl: public xml_schema::document_pimpl
  {
  public:
    document_pimpl (balance_pskel& balance_p, withdraw_pskel& withdraw_p)
        : result_type_ (unknown_type),
          balance_p_ (balance_p),
          withdraw_p_ (withdraw_p)
    {
    }

    request_type
    result_type () const
    {
      return result_type_;
    }

    request*
    result ()
    {
      return result_.release ();
    }

  protected:
    // This function is called to obtain the root element type parser.
    // If the returned pointed is 0 then the whole document content
    // is ignored. Note that the signature of this function varies
    // depending on whether the runtime was built with polymorphism
    // support.
    //
    virtual xml_schema::parser_base*
#ifndef XSDE_POLYMORPHIC
    start_root_element (const ro_string& ns,
                        const ro_string& name)
#else
    start_root_element (const ro_string& ns,
                        const ro_string& name,
                        const char* /* xsi:type */)
#endif
    {
      if (ns == "http://www.codesynthesis.com/protocol")
      {
        if (name == "balance")
        {
          balance_p_.pre ();
          return &balance_p_;
        }
        else if (name == "withdraw")
        {
          balance_p_.pre ();
          return &withdraw_p_;
        }

        cerr << "ignoring unknown request: " << name << endl;
        return 0;
      }
      else
      {
        // This document is from the wrong namespace. If the runtime and
        // the generated code are built with validation enabled then we
        // can set an XML Schema error.
        //
#ifdef XSDE_PARSER_VALIDATION
        context_.schema_error (
          xml_schema::parser_schema_error::unexpected_element);
#endif
        return 0;
      }
    }

    // This function is called to indicate the completion of document
    // parsing. The parser argument contains the pointer returned by
    // start_root_element.
    //
    virtual void
    end_root_element (const ro_string& /* ns */,
                      const ro_string& /* name */,
                      xml_schema::parser_base* parser)
    {
      // We could have handled the result directly in this function
      // instead of storing it in the result_ variable.
      //
      if (parser == &balance_p_)
      {
        result_type_ = balance_type;
        result_.reset (balance_p_.post_balance ());
      }
      else if (parser == &withdraw_p_)
      {
        result_type_ = withdraw_type;
        result_.reset (withdraw_p_.post_withdraw ());
      }
      else
      {
        result_type_ = unknown_type;
        result_.reset (0);
      }
    }


  public:
    // If we need to be able to reset and reuse the parser after
    // an error then we also need to override reset() and reset
    // the root parsers. We can also get smarter here by caching
    // the parser that was used last and only reset that. Note
    // that you always need to call _reset() from the base.
    //
    virtual void
    reset ()
    {
      xml_schema::document_pimpl::reset ();

      balance_p_._reset ();
      withdraw_p_._reset ();
    }

  private:
    request_type result_type_;
    std::auto_ptr<request> result_;

    balance_pskel& balance_p_;
    withdraw_pskel& withdraw_p_;
  };
}


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 protocol;

    // Construct the parser.
    //
    xml_schema::unsigned_int_pimpl unsigned_int_p;

    balance_pimpl balance_p;
    withdraw_pimpl withdraw_p;

    balance_p.parsers (unsigned_int_p); // account

    withdraw_p.parsers (unsigned_int_p,  // account
                        unsigned_int_p); // amount

    // Parse the XML instance document.
    //
    document_pimpl doc_p (balance_p, withdraw_p);

    // pre() and post() will be called as part of the start_root_element()
    // and end_root_element() calls.
    //
    if (argc < 2)
      doc_p.parse (std::cin);
    else
      doc_p.parse (argv[1]);

    std::auto_ptr<request> r (doc_p.result ());
    request_type t = doc_p.result_type ();

    // Let's print what we've got.
    //
    switch (t)
    {
    case balance_type:
      {
        balance* b = static_cast<balance*> (r.get ());
        cerr << "balance request for acc# " << b->account () << endl;
        break;
      }
    case withdraw_type:
      {
        withdraw* w = static_cast<withdraw*> (r.get ());
        cerr << "withdrawal request for acc# " << w->account () << ", "
             << "amount: " << w->amount () << endl;
        break;
      }
    case unknown_type:
      {
        cerr << "unknown request" << endl;
        break;
      }
    }
  }
  catch (const xml_schema::parser_exception& e)
  {
    cerr << input << ":" << e.line () << ":" << e.column () << ": "
         << e.text () << endl;
    return 1;
  }
  catch (const std::ios_base::failure&)
  {
    cerr << input << ": unable to open or read failure" << endl;
    return 1;
  }

  return 0;
}