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

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

#include "library.hxx"

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

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


  using namespace lib;


  // Read in the file.
  //
  auto_ptr<class library> l (library (argv[1]));


  // Let's print what we've got.
  //
  typedef library::book::const_iterator BookIterator;

  for (library::book::const_iterator b (l->book ().begin ());
       b != l->book ().end ();
       ++b)
  {
    cerr << endl
         << "ID           : " << b->id () << endl
         << "ISBN         : " << b->isbn () << endl
         << "Title        : " << b->title ()  << endl
         << "Genre        : " << b->genre () << endl;

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

      if (a->died.present ())
        cerr << "  Died       : " << a->died () << endl;

      if (a->recommends.present ())
      {
        book const& r (dynamic_cast<book const&> (*a->recommends ()));

        cerr << "  Recommends : " << r.title () << endl;
      }
    }

    cerr  << "Available    : " << std::boolalpha << b->available () << endl;
  }
}
