/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#include <xml-cxx/xml.hxx>
#include <iostream>
#include <sstream>
#include <fstream>

int main(int, char**) try {
  // Example template in factory instantiation
  xml::Factory addrTpl(xml::Node("address")
                       <<(xml::Node("name").limits(1, 1)
                          <<xml::String("first").limits(1, 1)
                          <<xml::String("middle") // 0..n -> .limit(0, 0)
                          <<xml::String("last").limits(1, 1))
                       <<(xml::Node("location").max(1)
                          <<xml::String("line").min(1))
                       <<xml::String("email")
                       <<xml::String("url")
                       <<xml::String("country").max(1));
  // Example XML file to read
  std::stringstream ss("\
      <address>\
        <name>\
          <first>Marc</first>\
          <middle>Roman</middle>\
          <last>Wäckerlin</last>\
        </name>\
        <location>\
          <line>SwissSign AG</line>\
          <line>Pfingstweidstrasse 60b</line>\
          <line>8005 Zürich</line>\
        </location>\
        <country>Schweiz</country>\
        <email>marc@waeckerlin.org</email>\
        <email>marc.waeckerlin@swisssign.com</email>\
        <url>http://dev.swisssign.com/trac/libxml-cxx</url>\
        <url>http://marc.wäckerlin.ch</url>\
        <url>http://marc.waeckerlin.org</url>\
        <url>http://dev.swisssign.com</url>\
        <url>http://swissign.com</url>\
        <url>http://swissign.ch</url>\
      </address>");
  std::unique_ptr<xml::Node> author(addrTpl.read(ss));
  // write to stdout
  std::cout<<"Successfully read:"<<std::endl
           <<"------------------------------"<<std::endl
           <<*author<<std::endl
           <<"------------------------------"<<std::endl;
  // store in project's AUTHORS file
  std::ofstream ofs("../AUTHORS");
  ofs<<*author<<std::endl;
  return 0;
 } catch (std::exception& e) {
  std::cerr<<"**** Error: "<<e.what()<<std::endl;
 }
