// file      : xsd/examples/cxx/parser/library/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <iostream>

#include "library.hxx"
#include "library-parser.hxx"

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

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

  using namespace library;


  // Construct the parser.
  //
  xml_schema::parser::string string_p;
  xml_schema::parser::boolean boolean_p;

  parser::isbn isbn_p;

  parser::title title_p;
  title_p (string_p);

  parser::genre genre_p;

  parser::author author_p;
  author_p (string_p,  // name
            string_p,  // born
            string_p,  // died
            string_p); // recommends

  parser::book book_p;
  book_p (isbn_p,    // isbn
          title_p,   // title
          genre_p,   // genre
          author_p,  // author
          boolean_p, // available
          string_p); // id

  parser::catalog catalog_p;
  catalog_p (book_p);


  // Parse the XML instance document.
  //
  catalog l (catalog_p._parse (argv[1],
                               "catalog",
                               "http://www.codesynthesis.com/library"));


  // Let's print what we've got.
  //
  for (catalog::const_iterator bi (l.begin ()); bi != l.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;
  }
}
