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

#include <iostream>

#include "library.hxx"

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

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

using std::cerr;
using std::endl;

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


    // Construct the parser.
    //
    xml_schema::id_pimpl id_p;
    xml_schema::idref_pimpl idref_p;
    xml_schema::string_pimpl string_p;
    xml_schema::boolean_pimpl boolean_p;

    isbn_pimpl isbn_p;

    title_pimpl title_p;
    title_p.lang_parser (string_p);

    genre_pimpl genre_p;

    author_pimpl author_p;
    author_p.parsers (string_p,  // name
                      string_p,  // born
                      string_p,  // died
                      idref_p);  // recommends

    book_pimpl book_p;
    book_p.parsers (boolean_p, // available
                    id_p,      // id
                    isbn_p,    // isbn
                    title_p,   // title
                    genre_p,   // genre
                    author_p); // author

    catalog_pimpl catalog_p;
    catalog_p.book_parser (book_p);


    // Parse the XML instance document.
    //
    xml_schema::document_pimpl doc_p (
      catalog_p,
      "http://www.codesynthesis.com/library", // root element namespace
      "catalog");                             // root element name

    catalog_p.pre ();

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

    catalog c (catalog_p.post_catalog ());


    // Let's print what we've got.
    //
    for (catalog::const_iterator bi (c.begin ()); bi != c.end (); ++bi)
    {
      cerr << endl
           << "ID           : " << bi->id () << endl
           << "ISBN         : " << bi->isbn () << endl
           << "Title        : " << bi->title ()  << endl
           << "Genre        : " << bi->genre () << endl;

      for (book::authors::const_iterator ai (bi->author ().begin ());
           ai != bi->author ().end ();
           ++ai)
      {
        cerr << "Author       : " << ai->name () << endl;
        cerr << "  Born       : " << ai->born () << endl;

        if (!ai->died ().empty ())
          cerr << "  Died       : " << ai->died () << endl;

        if (!ai->recommends ().empty ())
        {
          cerr << "  Recommends : " << ai->recommends () << endl;
        }
      }

      cerr  << "Available    : " << std::boolalpha << bi->available () << endl;
    }
  }
  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;
}