Main Page   Class Hierarchy   Data Structures   File List   Data Fields  

xmlfileparser.cpp

00001 /***************************************************************************
00002                           xmlfileparser.cpp  -  description
00003                              -------------------
00004     begin                : Sun Jan 19 2002
00005     copyright            : (C) 2002 by Michael Otto
00006     email                : Michael.Otto@saskathex.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018  
00019 #include "xmlfileparser.h"
00020 #include "stockmarketvector.h"
00021 #include "stockmarket.h"
00022 #include "stockcontainer.h"
00023 #include "stock.h"
00024 #include "preferences.h"
00025 
00026 #include "config.h"
00027 
00031 XMLFileParser::XMLFileParser() {
00032 
00033 }
00034 
00038 XMLFileParser::~XMLFileParser() {
00039 
00040 }
00041  
00042 
00047 void XMLFileParser::parseDoc(char *docname, StockMarketVector *stockMarketVector) {
00048   xmlDocPtr doc;
00049   xmlNodePtr cur;
00050 
00051   doc = xmlParseFile(docname);
00052   if (doc == NULL ) {
00053     cerr << "Document not parsed successfully." << endl;
00054     xmlFreeDoc(doc);
00055     return;
00056   }
00057 
00058   cur = xmlDocGetRootElement(doc);
00059 
00060   if (cur == NULL) {
00061     cerr << "empty document" << endl;
00062     xmlFreeDoc(doc);
00063     return;
00064   }
00065 
00066   if (xmlStrcmp(cur->name, (const xmlChar *) "stocks")) {
00067     cerr << "document of the wrong type, root node != stocks" << endl;
00068     xmlFreeDoc(doc);
00069     return;
00070   }
00071 
00072   cur = cur->xmlChildrenNode;
00073   StockMarket *market;
00074 
00075   while (cur != NULL) {
00076     if ((!xmlStrcmp(cur->name, (const xmlChar *)"lastSave"))) {
00077       char *lastSave = (char *) xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00078       time_t t1;
00079       (void) time(&t1);
00080       tm *t;
00081       t = localtime(&t1);
00082       char today[5];
00083       sprintf(today,"%d/%d",t->tm_mon,t->tm_mday);
00084       string sT = (string) today;
00085       string sF = (string) lastSave;
00086       if (sT == sF) {
00087         stockMarketVector->setLastSaveYesterday(false);
00088 #ifdef DEBUG
00089         cout << "lastSave == today" << endl;
00090 #endif
00091       } else {
00092         stockMarketVector->setLastSaveYesterday(true);
00093 #ifdef DEBUG
00094         cout << "lastSave != today" << endl;
00095 #endif
00096       }
00097     }
00098     if ((!xmlStrcmp(cur->name, (const xmlChar *)"stockmarket"))) {
00099       market = new StockMarket();
00100       parseStockMarket(market, stockMarketVector->getLastSaveYesterday(), doc, cur);
00101       stockMarketVector->addStockMarket(market);
00102     }
00103     cur = cur->next;
00104   }
00105   xmlFreeDoc(doc);
00106 }
00107 
00114 void XMLFileParser::parseStockMarket(StockMarket *stockMarket, bool lastSaveYesterday, xmlDocPtr doc, xmlNodePtr cur) {
00115 #ifdef DEBUG
00116   printf("\nPARSING THE %s\n", (stockMarket->getMarketName()).c_str());
00117 #endif
00118   cur = cur->xmlChildrenNode;
00119   while (cur != NULL) {
00120     if ((!xmlStrcmp(cur->name, (const xmlChar *)"name"))) {
00121       string name = (char *) xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00122       stockMarket->setMarketName(name);
00123 #ifdef DEBUG
00124       cout << "stockmarket name: " << name << endl;
00125 #endif
00126     }
00127     if ((!xmlStrcmp(cur->name, (const xmlChar *)"stock"))) {
00128       parseStock(stockMarket, lastSaveYesterday, doc, cur);
00129     }
00130     cur = cur->next;
00131   }
00132 }
00133 
00139 vector <float> XMLFileParser::parseValues(xmlDocPtr doc, xmlNodePtr cur) {
00140   vector <float> values;
00141   float f;
00142   cur = cur->xmlChildrenNode;
00143   while (cur != NULL) {
00144     if ((!xmlStrcmp(cur->name, (const xmlChar *)"v"))) {
00145       f = atof((const char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00146 #ifdef DEBUG
00147       cout << "  " << f;
00148 #endif
00149       values.push_back(f);
00150     }
00151     cur = cur->next;
00152   }
00153 #ifdef DEBUG
00154   cout << endl;
00155 #endif
00156   return values;
00157 }
00158 
00159 
00166 void XMLFileParser::parseStock(StockMarket *stockMarket, bool lastSaveYesterday, xmlDocPtr doc, xmlNodePtr cur) {
00167 #ifdef DEBUG
00168   cout << "PARSING THE STOCK" << endl;
00169 #endif
00170   cur = cur->xmlChildrenNode;
00171   char *name = "", *wkn = "", *buyingDate = "";
00172   int number = 0;
00173   float currentMarketPrice = 0.0, buyCost = 0.0, sellValue = 0.0, buyValue = 0.0;
00174   vector <float> values;
00175   while (cur != NULL) {
00176     if ((!xmlStrcmp(cur->name, (const xmlChar *)"name"))) {
00177       name = (char *) xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00178     }
00179     if ((!xmlStrcmp(cur->name, (const xmlChar *)"wkn"))) {
00180       wkn = (char *) xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00181     }
00182     if ((!xmlStrcmp(cur->name, (const xmlChar *)"buyingDate"))) {
00183       buyingDate = (char *) xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00184     }
00185     if ((!xmlStrcmp(cur->name, (const xmlChar *)"amount"))) {
00186       number = atoi((const char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00187     }
00188     if ((!xmlStrcmp(cur->name, (const xmlChar *)"buyingRate"))) {
00189       buyValue = atof((const char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00190     }
00191     if ((!xmlStrcmp(cur->name, (const xmlChar *)"purchaseCost"))) {
00192       buyCost = atof((const char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00193     }
00194     if ((!xmlStrcmp(cur->name, (const xmlChar *)"currentMarketPrice"))) {
00195       currentMarketPrice = atof((const char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00196     }
00197     if ((!xmlStrcmp(cur->name, (const xmlChar *)"values"))) {
00198       values = parseValues(doc, cur);
00199     }
00200     cur = cur->next;
00201   }
00202   if (lastSaveYesterday) {
00203     values.push_back(currentMarketPrice);
00204   }
00205   
00206 
00207   StockContainer *stockContainer = new StockContainer(Stock(name, wkn, buyingDate, number, buyValue,
00208                                                             buyCost, currentMarketPrice, values));
00209   stockMarket->addStockContainer(stockContainer);
00210 }
00211 
00216 void XMLFileParser::parsingTheXmlFile(char *filename,  StockMarketVector *stockMarketVector) {
00217   parseDoc(filename, stockMarketVector);
00218 }
00219 
00220 
00225 void XMLFileParser::parsingTheXmlFile(char *filename, Preferences *preferences) {
00226   xmlDocPtr doc;
00227   xmlNodePtr cur;
00228 
00229   doc = xmlParseFile(filename);
00230   if (doc == NULL ) {
00231     cerr << "Document not parsed successfully." << endl;
00232     xmlFreeDoc(doc);
00233     return;
00234   }
00235   cur = xmlDocGetRootElement(doc);
00236   if (cur == NULL) {
00237     cerr << "empty document" << endl;
00238     xmlFreeDoc(doc);
00239     return;
00240   }
00241   if (xmlStrcmp(cur->name, (const xmlChar *) "preferences")) {
00242     fprintf(stderr,"document of the wrong type, root node != preferences");
00243     xmlFreeDoc(doc);
00244     return;
00245   }
00246   cur = cur->xmlChildrenNode;
00247   int choice;
00248   while (cur != NULL) {
00249     if ((!xmlStrcmp(cur->name, (const xmlChar *)"autoSaveOnExit"))) {
00250       choice = atoi((const char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00251       preferences->setAutoSaveOnExit((bool) choice);
00252     }
00253     if ((!xmlStrcmp(cur->name, (const xmlChar *)"saveDialogOnExit"))) {
00254       choice = atoi((const char*)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1));
00255       preferences->setSaveDialogOnExit((bool) choice);
00256     }
00257     cur = cur->next;
00258   }
00259   xmlFreeDoc(doc);
00260 }

Generated on Tue May 27 21:24:02 2003 for gstockcalc by doxygen1.3-rc3