summaryrefslogtreecommitdiff
path: root/libxsd-tests/cxx/parser/expat/basic/driver.cxx
blob: 5c0325a23c68ca0612433acd274d61e261f25382 (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
// file      : cxx/parser/expat/basic/driver.cxx
// copyright : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <iostream>

// Define XSD_CXX11 since we include libxsd headers directly.
//
#ifdef _MSC_VER
#  if _MSC_VER >= 1600 // VC++10 and later have C++11 always enabled.
#    define XSD_CXX11
#  endif
#else
#  if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
#    define XSD_CXX11
#  endif
#endif

#include <xsd/cxx/config.hxx>    // XSD_UNUSED
#include <xsd/cxx/ro-string.hxx>

#include <xsd/cxx/parser/expat/elements.hxx>

#include <xsd/cxx/parser/non-validating/parser.hxx>
#include <xsd/cxx/parser/non-validating/xml-schema-pskel.hxx>
#include <xsd/cxx/parser/non-validating/xml-schema-pimpl.hxx>

#undef NDEBUG
#include <cassert>

using namespace std;
using namespace xsd::cxx;

typedef parser::non_validating::string_pskel<char> string_pskel;

class hello_pimpl: public parser::non_validating::complex_content<char>
{
public:
  hello_pimpl (string_pskel& greeting, string_pskel& name)
      : greeting_parser_ (&greeting),
        name_parser_ (&name) {}

private:
  virtual bool
  _start_element_impl (const ro_string<char>&,
                       const ro_string<char>&,
                       const ro_string<char>*);

  virtual bool
  _end_element_impl (const ro_string<char>&, const ro_string<char>&);

  string_pskel* greeting_parser_;
  string_pskel* name_parser_;
};

bool hello_pimpl::
_start_element_impl (const ro_string<char>& ns,
                     const ro_string<char>& n,
                     const ro_string<char>* t)
{
  XSD_UNUSED (t);

  if (complex_content::_start_element_impl (ns, n, t))
    return true;

  if (n == "greeting" && ns.empty ())
  {
    context_.top ().parser_ = greeting_parser_;

    if (greeting_parser_)
      greeting_parser_->pre ();

    return true;
  }

  if (n == "name" && ns.empty ())
  {
    context_.top ().parser_ = name_parser_;

    if (name_parser_)
      name_parser_->pre ();

    return true;
  }

  return false;
}

bool hello_pimpl::
_end_element_impl (const ro_string<char>& ns, const ro_string<char>& n)
{
  if (complex_content::_end_element_impl (ns, n))
    return true;

  if (n == "greeting" && ns.empty ())
  {
    cout << n << ' ' << greeting_parser_->post_string () << endl;
    return true;
  }

  if (n == "name" && ns.empty ())
  {
    cout << n << ' ' << name_parser_->post_string () << endl;
    return true;
  }

  return false;
}


// Usage: argv[0] <xml-file>
//
// Parse the specified XML file using the event-driven skeleton-based parser
// and print the element names and values to stdout.
//
int
main (int argc, char* argv[])
{
  assert (argc == 2);

  parser::non_validating::string_pimpl<char> string_p;
  hello_pimpl hello_p (string_p, string_p);

  parser::expat::document<char>  doc_p (hello_p, "hello");

  doc_p.parse (argv[1]);
}