00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00045 #include "../tinyxml/tinyxml.h"
00046
00047 #include "svx.h"
00048 #include "attribute.h"
00049 #include "Mnetwork.h"
00050
00051 #include "product.h"
00052
00053 #include "document.h"
00054
00055 using namespace libsvx;
00056
00057 static std::string NullString = "";
00058
00062 Document::Document():
00063 m_SvxInfo(NULL)
00064 ,m_Network(NULL)
00065 {
00066 }
00067
00071 Document::~Document()
00072 {
00073 if (m_SvxInfo != NULL)
00074 {
00075 delete m_SvxInfo;
00076 }
00077 if (m_Network != NULL)
00078 {
00079 delete m_Network;
00080 }
00081 }
00082
00091 bool Document::Parse(const std::string & SVX_buffer, const bool bAllowURLRetrieval)
00092 {
00093 if (m_SvxInfo != NULL)
00094 {
00095 delete m_SvxInfo;
00096 }
00097
00098 ::TiXmlDocument * m_XML_Document = new ::TiXmlDocument;
00099
00100 if (m_XML_Document != NULL)
00101 {
00102 m_XML_Document->Parse(SVX_buffer.c_str());
00103
00104 if (m_XML_Document->FirstChildElement("svx") != NULL)
00105 {
00106 m_SvxInfo = new Svx(*m_XML_Document->FirstChildElement("svx"));
00107 }
00108
00109 delete m_XML_Document;
00110 }
00111
00112
00113
00114
00115 return true;
00116 }
00117
00124 bool Document::ParseAdd(const std::string & SVX_buffer, const bool bAllowURLRetrieval)
00125 {
00126 bool result = false;
00127
00128 if (m_SvxInfo == NULL)
00129 {
00130 result = ParseAdd(SVX_buffer, bAllowURLRetrieval);
00131 }
00132 else
00133 {
00134 ::TiXmlDocument * m_XML_Document = new ::TiXmlDocument;
00135
00136 if (m_XML_Document != NULL)
00137 {
00138 m_XML_Document->Parse(SVX_buffer.c_str());
00139
00140 if (m_XML_Document->FirstChildElement("svx") != NULL)
00141 {
00142 result = m_SvxInfo->Add(*m_XML_Document->FirstChildElement("svx"));
00143 }
00144
00145 delete m_XML_Document;
00146 }
00147 }
00148
00149 return result;
00150 }
00151
00157 bool Document::LoadFile(const std::string & filename, const bool bAllowURLRetrieval)
00158 {
00159 FILE* fp = fopen( filename.c_str(), "r" );
00160 if ( fp )
00161 {
00162 unsigned size;
00163 fseek( fp, 0, SEEK_END );
00164 size = ftell( fp );
00165 fseek( fp, 0, SEEK_SET );
00166
00167 char* buf = new char[size+1];
00168 char* p = buf;
00169 while( fgets( p, size, fp ) )
00170 {
00171 p = strchr( p, 0 );
00172 }
00173 fclose( fp );
00174
00175 Parse( buf, bAllowURLRetrieval );
00176 delete [] buf;
00177 return true;
00178 }
00179 return false;
00180 }
00181
00185 bool Document::AddFile(const std::string & filename, const bool AllowURLRetrieval)
00186 {
00187 FILE* fp = fopen( filename.c_str(), "r" );
00188 if ( fp )
00189 {
00190 unsigned size;
00191 fseek( fp, 0, SEEK_END );
00192 size = ftell( fp );
00193 fseek( fp, 0, SEEK_SET );
00194
00195 char* buf = new char[size+1];
00196 char* p = buf;
00197 while( fgets( p, size, fp ) )
00198 {
00199 p = strchr( p, 0 );
00200 }
00201 fclose( fp );
00202
00203 ParseAdd( buf, AllowURLRetrieval );
00204 delete [] buf;
00205 return true;
00206 }
00207 return false;
00208 }
00209
00216 bool Document::LoadURL(const std::string & Url, const bool bAllowURLRetrieval)
00217 {
00218 return false;
00219 }
00220
00224 bool Document::SaveFile(const std::string & filename)
00225 {
00226 bool bResult = false;
00227
00228
00229 ::TiXmlDocument * m_XML_Document = new ::TiXmlDocument;
00230
00231 if (m_XML_Document != NULL)
00232 {
00233
00234 if (ParseBack(*m_XML_Document))
00235 {
00236
00237 m_XML_Document->SaveFile(filename);
00238 }
00239 delete m_XML_Document;
00240 }
00241
00242 return bResult;
00243 }
00244
00249 bool Document::ParseBack(::TiXmlDocument & m_XML_Document)
00250 {
00251 bool bResult = false;
00252
00253 if (m_SvxInfo != NULL)
00254 {
00255 m_XML_Document.InsertEndChild(m_SvxInfo->XmlElement());
00256 bResult = true;
00257 }
00258
00259 return bResult;
00260 }
00261
00267 void Document::UpdateProduct(const std::string & the_product_name, const bool AllowDependencyRetrieval)
00268 {
00269
00270 const Product * the_prod = FindProduct(the_product_name);
00271
00272 if (the_prod != NULL)
00273 {
00274
00275 const Attribute * update_url = the_prod->HREF();
00276
00277 if (update_url != NULL)
00278 {
00279 if (m_Network == NULL)
00280 {
00281 m_Network = new MNetwork;
00282 }
00283
00284
00285 m_Network->RetrieveURL(update_url->String(),NULL,0);
00286
00287
00288 }
00289 }
00290 }
00291
00295 Product * Document::FindProduct(const std::string & the_product_uid) const
00296 {
00297 if (m_SvxInfo == NULL)
00298 return NULL;
00299
00300 return m_SvxInfo->FindProduct(the_product_uid);
00301 }
00302
00306 void Document::Iterate_Product_Begin()
00307 {
00308 if (m_SvxInfo != NULL)
00309 {
00310 m_SvxInfo->Iterate_Product_Begin();
00311 }
00312 }
00313
00317 bool Document::Iterate_Product_IsEnd()
00318 {
00319 if (m_SvxInfo == NULL)
00320 return false;
00321 else return m_SvxInfo->Iterate_Product_IsEnd();
00322 }
00323
00327 const std::string & Document::Iterate_Product_Next()
00328 {
00329 if (m_SvxInfo == NULL)
00330 return NullString;
00331 else return m_SvxInfo->Iterate_Product_Next();
00332 }
00333
00337 const std::string & Document::Iterate_Product_Prev()
00338 {
00339 if (m_SvxInfo == NULL)
00340 return NullString;
00341 else return m_SvxInfo->Iterate_Product_Prev();
00342 }
00343
00347 bool Document::IsOldProductVersion(const std::string & the_product_uid, const std::string & the_version_to_check) const
00348 {
00349 const Product * the_product = FindProduct(the_product_uid);
00350
00351 if (the_product == NULL)
00352 return false;
00353 else return the_product->IsOldVersion(the_version_to_check);
00354 }
00355
00359 const Version *Document::GetLatestProductVersion(const std::string & the_product_uid)
00360 {
00361 Product * the_product = FindProduct(the_product_uid);
00362
00363 if (the_product == NULL)
00364 return NULL;
00365 else return the_product->GetLatestVersion();
00366 }
00367
00371 const Version * Document::FindVersion(const std::string & the_product_uid, const std::string & the_version_name) const
00372 {
00373 if (m_SvxInfo == NULL)
00374 return NULL;
00375
00376 return m_SvxInfo->FindVersion(the_product_uid, the_version_name);
00377 }
00378
00382 void Document::Iterate_Version_Begin(const std::string & the_product_uid)
00383 {
00384 if (m_SvxInfo != NULL)
00385 {
00386 m_SvxInfo->Iterate_Version_Begin(the_product_uid);
00387 }
00388 }
00389
00393 bool Document::Iterate_Version_IsBegin(const std::string & the_product_uid)
00394 {
00395 if (m_SvxInfo == NULL)
00396 return false;
00397 else return m_SvxInfo->Iterate_Version_IsBegin(the_product_uid);
00398 }
00399
00403 bool Document::Iterate_Version_IsEnd(const std::string & the_product_uid)
00404 {
00405 if (m_SvxInfo == NULL)
00406 return false;
00407 else return m_SvxInfo->Iterate_Version_IsEnd(the_product_uid);
00408 }
00409
00413 const std::string & Document::Iterate_Version_Next(const std::string & the_product_uid)
00414 {
00415 if (m_SvxInfo == NULL)
00416 return NullString;
00417 else return m_SvxInfo->Iterate_Version_Next(the_product_uid);
00418 }
00419
00423 const std::string & Document::Iterate_Version_Prev(const std::string & the_product_uid)
00424 {
00425 if (m_SvxInfo == NULL)
00426 return NullString;
00427 else return m_SvxInfo->Iterate_Version_Prev(the_product_uid);
00428 }
00429