/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                               DEMO PROGRAMS SET
                          xml_test2.cpp  -  description
                             -------------------
    begin                : October 3, 2005
    copyright            : (C) 2005 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef __BORLANDC__
#include <vcl.h>
#pragma hdrstop
#endif

#include <sptk3/sptk.h>
#include <sptk3/CDateTime.h>
#include <sptk3/CException.h>
#include <sptk3/xml/CXml.h>

using namespace std;

void createXMLdocument() {   
   // Creating a new empty XML document
   CXmlDoc *doc = new CXmlDoc("Bookscases Map version 1.00"); 

   // Creating a bookcase element
   CXmlNode *bookcase = new CXmlElement(*doc,"bookcase");
   bookcase->setAttribute("location","next to the window");
   
   // Creating a shelf in the bookcase
   CXmlNode *shelf = new CXmlElement(*bookcase,"shelf");
   shelf->setAttribute("label","English Literature");
   
   // Creating a book on the shelf
   CXmlNode *book = new CXmlElement(*shelf,"book");  
   book->setAttribute("author","William Shakspere");
   book->setAttribute("title","Romeo and Juliete");
   book->setAttribute("issued","Mar 2003");
   book->setAttribute("pages",345);
   
   // Creating another book on the same shelf
   book = new CXmlElement(*shelf,"book");  
   book->setAttribute("author","William Shakspere");
   book->setAttribute("title","King Lear");
   book->setAttribute("issued",CDateTime::Now());
   book->setAttribute("pages",345);
   
   CBuffer buffer;
   doc->save(buffer);
   buffer.saveToFile("MyXML.xml");
   
   delete doc;
}

// Print the bookcase node attributes by the name
void printBookcase(CXmlNode *node) {
  std::string location = node->getAttribute("location");
  printf("Bookcase : %s\n",location.c_str());
}
   
// Print the node attributes by the name
void printShelf(CXmlNode *node) {
   std::string label = node->getAttribute("label");
   printf(" Shelf : %s\n",label.c_str());
}   
   
// Print all the node attributes
void printBook(CXmlNode *node) {
   printf("    Book : ");
   CXmlAttributes& attributes = node->attributes();
   CXmlAttributes::iterator itor = attributes.begin();
   for (; itor != attributes.end(); itor++ ) {
      std::string name = itor->first;
      CXmlAttribute& attribute = itor->second;
      std::string value = attribute;
      printf("%s=%s ",name.c_str(),value.c_str());
   }
   printf("\n");
}   

// Print the node and all the subnodes
void printNode(CXmlNode *node) {
   // Check the node name and print it properly (see below)
   if (node->name() == "bookcase")
      printBookcase(node);
   else if (node->name() == "shelf")
      printShelf(node);
   else if (node->name() == "book")
      printBook(node);
   // Process all the children
   for (unsigned i = 0; i < node->children(); i++)
      printNode( node->child(i) );
}
   
void processXMLdocument() {
   // Creating a new empty XML document
   CXmlDoc *doc = new CXmlDoc(); 
   
   CBuffer buffer;
   buffer.loadFromFile("MyXML.xml");
   doc->load(buffer);

   printNode(doc);
   
   delete doc;
}

int main(int argc,char *argv[]) {
   try {
      createXMLdocument();
      processXMLdocument();
   }
   catch (std::exception& e) {
      puts(e.what());
      return 1;
   }
   return 0;
}
