summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/messaging/driver.cxx
blob: 095e2cd0486af2f28c8807712eda3664b997ea6e (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
// file      : examples/cxx/tree/messaging/driver.cxx
// copyright : not copyrighted - public domain

#include <memory>   // std::auto_ptr
#include <string>
#include <fstream>
#include <typeinfo>
#include <iostream>

#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/PlatformUtils.hpp>

#include <xsd/cxx/xml/string.hxx>

#include "dom-parse.hxx"
#include "dom-serialize.hxx"

#include "protocol.hxx"

using namespace std;
using namespace protocol;
using namespace xercesc;

int
main (int argc, char* argv[])
{
  if (argc != 2)
  {
    cerr << "usage: " << argv[0] << " request.xml" << endl;
    return 1;
  }

  int r (0);

  // We need to initialize the Xerces-C++ runtime because we
  // are doing the XML-to-DOM parsing ourselves.
  //
  XMLPlatformUtils::Initialize ();

  try
  {
    ifstream ifs;
    ifs.exceptions (ifstream::badbit | ifstream::failbit);
    ifs.open (argv[1]);

    auto_ptr<xml_schema::element_type> req, res;

    // Parse the XML request to a DOM document using the parse()
    // function from dom-parse.hxx.
    //
    {
      xml_schema::dom::auto_ptr<DOMDocument> doc (parse (ifs, argv[1], true));
      DOMElement& root (*doc->getDocumentElement ());

      req = xml_schema::element_map::parse (root);
    }

    // We can test which request we've got either using RTTI or by
    // comparing the element names, as shown below.
    //
    if (balance* b = dynamic_cast<balance*> (req.get ()))
    {
      account_t& a (b->value ());

      cerr << "balance request for acc# " << a.account () << endl;

      res.reset (new success (balance_t (a.account (), 1000)));
    }
    else if (req->_name () == withdraw::name ())
    {
      withdraw& w (static_cast<withdraw&> (*req));
      change_t& c (w.value ());

      wcerr << "withdrawal request for acc# " << c.account () << ", "
            << "amount: " << c.amount () << endl;

      if (c.amount () > 1000)
        res.reset (new insufficient_funds (balance_t (c.account (), 1000)));
      else
        res.reset (new success (balance_t (c.account (), 1000 - c.amount ())));

    }
    else if (req->_name () == deposit::name ())
    {
      deposit& d (static_cast<deposit&> (*req));
      change_t& c (d.value ());

      wcerr << "deposit request for acc# " << c.account () << ", "
            << "amount: " << c.amount () << endl;

      res.reset (new success (balance_t (c.account (), 1000 + c.amount ())));
    }

    // Serialize the response to a DOM document.
    //
    namespace xml = xsd::cxx::xml;

    const XMLCh ls_id [] = {chLatin_L, chLatin_S, chNull};

    DOMImplementation* impl (
      DOMImplementationRegistry::getDOMImplementation (ls_id));

    const string& name (res->_name ());
    const string& ns (res->_namespace ());

    xml_schema::dom::auto_ptr<DOMDocument> doc (
      impl->createDocument (
        xml::string (ns).c_str (),
        xml::string ("p:" + name).c_str (),
        0));

    xml_schema::element_map::serialize (*doc->getDocumentElement (), *res);

    // Serialize the DOM document to XML using the serialize() function
    // from dom-serialize.hxx.
    //
    cout << "response:" << endl
         << endl;

    serialize (cout, *doc);
  }
  catch (const xml_schema::no_element_info& e)
  {
    // This exception indicates that we tried to parse or serialize
    // an unknown element.
    //
    cerr << "unknown request: " << e.element_namespace () << "#" <<
      e.element_name () << endl;
    r = 1;
  }
  catch (const xml_schema::exception& e)
  {
    cerr << e << endl;
    r = 1;
  }
  catch (const std::ios_base::failure&)
  {
    cerr << argv[1] << ": unable to open or read failure" << endl;
    r = 1;
  }

  XMLPlatformUtils::Terminate ();
  return r;
}