D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/cl_xml_mngr.h

00001 /* LICENSE_BEGIN
00002 SutoiTools library supply support for debugging, testing and 
00003 setting good bases for you application. It is part of the 
00004 SugoiEngine project.
00005 Copyright (C) 2006 Allusse Yannick (yannick dot allusse at laposte dot net)
00006 
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 LICENSE_END
00022 */
00023 
00024 /* \file SG_TLS_cl_xml_mngr.h
00025  * \brief Template manager to manage all kind of object, with XML loading and saving.
00026  * \author Yannick Allusse
00027 */
00028 #ifndef SG_TLS_CL_XML_MNGR_H
00029 #define SG_TLS_CL_XML_MNGR_H
00030 
00031 #include "SugoiTools\xml.h"
00032 #include "SugoiTools\cl_template_manager.h"
00033 
00034 
00035 #if SG_TLS_MEMORY_MANAGER
00036           #include "SugoiTools\debug_new.h"
00037 #endif 
00038 
00039 namespace SGE{
00040 
00041 
00057 template <class T, typename P=std::string>  
00058           class CL_XML_MNGR
00059                     :public CL_TEMPLATE_OBJECT_MANAGER<T,P>,
00060                     public CL_XML_OBJ 
00061 {
00062 public: //Definitions
00063           typedef  T                                                                                TplObj;
00064           typedef  TplObj                                                       *         TplObjHdle;
00065           typedef    typename CL_XML_MNGR<T, P>             TplManager;
00066           typedef    typename TplManager::iterator          iterator;
00067 private:
00068           std::string                   SubTagName;                   
00069           std::string                   SubTagID;           
00070 public:
00071           
00072 
00080           CL_XML_MNGR(TplManager *_RemoteManager,
00081                                                   const std::string &_TagName,
00082                                                   const std::string &_SubTagName, 
00083                                                   const std::string &_SubTagID)
00084                               :         CL_TEMPLATE_OBJECT_MANAGER<T,P>(_RemoteManager), 
00085                                         CL_XML_OBJ(_TagName),
00086                                         SubTagName(_SubTagName),
00087                                         SubTagID(_SubTagID)
00088                     {}
00089 
00090           
00094           ~CL_XML_MNGR()
00095           {};
00096 
00100           const std::string & 
00101                     GetSubTagName()const
00102                     {return SubTagName;};
00103 
00107           const std::string & 
00108                     GetSubTagID()const
00109                     {return SubTagID;}; 
00110 
00114           void 
00115                     SetSubTagName(const std::string _Name)
00116                     {SubTagName = _Name;};
00117 
00121           void 
00122                     SetSubTagID(const std::string _ID)
00123                     {SubTagID = _ID;};
00124 
00125 
00126 
00127 #if _SG_TLS_XML
00128 protected:
00137           virtual 
00138                     T*        
00139                     XMLLoadObject(TiXmlElement* _XML_Root, IDRefCst _ID, std::string _TagName, std::string Type)
00140                     {
00141                               SG_Assert(_XML_Root, "No XML Root");
00142                               T* NewObj;
00143 
00144                               if (Type!="")
00145                               {         
00146                                         NewObj = Add(_ID);//???take care of type???...not in default manager
00147                               }
00148                               else
00149                                         NewObj = Add(_ID);//add new obj...
00150 
00151                               NewObj->SetMainTag(GetSubTagName());
00152 
00153                               SG_Assert(NewObj, "Error creating new object");
00154                               NewObj->XMLLoad(_XML_Root);
00155                               return NewObj;
00156                     }
00162           virtual 
00163                     TiXmlElement*       
00164                     XMLLoadObjects(TiXmlElement* _XML_Root)
00165           {
00166                     //SG_NOTICE_LOG("XMLLoadObjects()");
00167                     if (!_XML_Root)
00168                               return NULL;
00169 
00170                     TiXmlElement*XML_MAIN = XMLGetElement(_XML_Root, SubTagName.data());
00171                     if (!XML_MAIN)
00172                               return NULL;
00173                     //<..>< T type name><obj>===============================
00174                     
00175                     P ID_Val;
00176                     std::string Type;
00177                     std::string SubTagName;
00178                     CL_TEMPLATE_OBJECT_MANAGER<T,P>::TplHdle NewObj;
00179                     
00180                     while(XML_MAIN)
00181                     {
00182                               try
00183                               {
00184                                         //Init values
00185                                         //XMLInitVal(ID_Val);
00186                                         //Name              = NULL;
00187                                         NewObj              = NULL;
00188                                         //i_Option          = 0;
00189                                         Type                = "";
00190                                         //===============
00191                                         
00192                                         //get Name and ID
00193                                         
00194                                         SubTagName = XML_MAIN->Value();
00195 
00196                                         if(!XMLReadVal(XML_MAIN, SubTagID.data(), ID_Val))
00197                                         {
00198                                                   //SG_Assert(, );
00199                                                   SG_WARNING_LOG("Parsing XML file in <SubTagID ID_Val='?'>, ID field is not specified!");
00200                                                   std::cout << std::endl << "MainTag :" << GetMainTag         () ;
00201                                                   std::cout << std::endl << "SubTag :"    << GetSubTagName() ;
00202                                                   std::cout << std::endl << "SubTagID :"<< GetSubTagID        () << std::endl ;
00203                                         }
00204                                         else
00205                                         {
00206                                                   XMLReadVal(XML_MAIN, "Type", Type);
00207                                                   XMLLoadObject(XML_MAIN, ID_Val, SubTagName, Type);
00208                                         }
00209                                         
00210 
00211                                         //get next element
00212                                         XML_MAIN=XML_MAIN->NextSiblingElement();
00213                               }
00214 
00215                               catch(CException &e)
00216                               {
00217                                         //if (ID_Val != 0)
00218                                         {
00219                                                   SG_ERROR_LOG ("Exception catched while reading XMLtag '" << GetMainTag() << "'.\n");
00220                                         }
00221                     /*else{
00222                                                   SG_ERROR_LOG ("Exception catched while reading XMLtag <" << ID_Val << "> in <" << GetMainTag() << ">.\n");
00223                                         }*/
00224                                         std::cerr << "Exception catched : " << e.what() << endl;
00225 
00226                                         if (NewObj)
00227                                         {
00228                                                   //we delete it, cause they where some errors
00229                                                   this->Delete(NewObj->GetID());
00230                                         }
00231                                         //we continue and get next element
00232                                         XML_MAIN=XML_MAIN->NextSiblingElement();
00233                               }
00234                     }
00235                     
00236                     _SG_DEBUG_MNGR (std::cout << "Mngr <" << GetMainTag() << "> " << (unsigned int)GetCount() << " obj loaded!" << endl);
00237                     
00238                     return _XML_Root;
00239           }
00240 
00241 
00242           
00248           virtual 
00249                     TiXmlElement*       
00250                     XMLLoadHead(TiXmlElement* _XML_Root)
00251           {
00252                     SG_Assert(GetMainTag()!="",             "Error in CL_XML_MNGR<T,P>::XMLLoadHead(), MainTag is empty.");
00253                     SG_Assert(GetSubTagName()!="",          "Error in CL_XML_MNGR<T,P>::XMLLoadHead(), SubTagName is empty.");
00254                     SG_Assert(GetSubTagID()!="",  "Error in CL_XML_MNGR<T,P>::XMLLoadHead(), SubTagID is empty.");
00255                     SG_Assert(_XML_Root, "Error in CL_XML_MNGR<T,P>::XMLLoadHead(), XML entry is empty.");
00256 
00257                     
00258                     TiXmlElement* XML_MAIN        = CL_XML_OBJ::XMLLoad(_XML_Root);//, GetMainTag());
00259 
00260                     if (!XML_MAIN)
00261                               return NULL;//nothing found, but no error
00262                     
00263                     //no parameters to read yet...may be later
00264                     return XML_MAIN;
00265           }
00266 
00267 public:
00274           virtual 
00275                     TiXmlElement*       
00276                     XMLLoad(TiXmlElement* _XML_Root)
00277           {
00278                     //read header tag
00279                     TiXmlElement* XML_MAIN        = XMLLoadHead (_XML_Root);
00280 
00281                     if (!XML_MAIN)
00282                               return NULL;
00283                     else//load objects
00284                               return XMLLoadObjects(XML_MAIN);        
00285           }
00286 
00287 protected:
00288 
00293                     virtual 
00294                     TiXmlElement* 
00295                     XMLSaveObject(TiXmlElement* _XML_Root, T* Obj)
00296                     {
00297                               SG_Assert(_XML_Root, "No XML Root");
00298                               SG_Assert(Obj, "No obj to save");
00299                               return Obj->XMLSave(_XML_Root);
00300                     }
00301 
00306                     virtual 
00307                     TiXmlElement*       
00308                     XMLSaveObjects(TiXmlElement* _XML_Root)
00309                     {
00310                               SG_Assert(_XML_Root, "No XML Root");
00311 
00312                               if (!GetCount())
00313                 return NULL;
00314 
00315                               TiXmlElement* XML_ELM = NULL;
00316 
00317                                         //<..>< obj >===============================
00318                                         iterator i;//to manipulate the vector object
00319 
00320                                         //Look into the vector for the following name
00321                                         for ( i = GetFirstIter( ) ; i != GetLastIter( ) ; i++ )
00322                                         {
00323                                                   try
00324                                                   {
00325                                                             XML_ELM = new TiXmlElement( SubTagName.data() );
00326                                                             XMLWriteVal(XML_ELM, SubTagID.data(), (*i).second->GetID());
00327                                                             XMLSaveObject(XML_ELM,(*i).second);
00328                                                             _XML_Root->InsertEndChild(*XML_ELM);
00329                                                   }
00330 
00331                                                   catch(CException &e)
00332                                                   {
00333                                                             SG_ERROR_LOG ("Error while saving XMLtag '<" << 
00334                                                                                 GetMainTag() << 
00335                                                                                 "><" << 
00336                                                                                 SubTagName << 
00337                                                                                 " " <<  
00338                                                                                 SubTagID  << 
00339                                                                                 "?>'");
00340                                                             /*from object '" <<
00341                                                                                 (*i).second->GetID() <<
00342                                                                                 "'.\n");
00343 */
00344                                                             std::cerr << "Exception catched : " << e.what() << endl;
00345 
00346                                                             if (XML_ELM)//we delete it, cause they where some errors
00347                                                                       delete XML_ELM;
00348                                                             
00349                                                             continue;//we continue and get next element
00350                                                   }
00351                                         }
00352                                         _SG_DEBUG_MNGR(std::cout << "Mngr <" << GetMainTag() << "> " << (unsigned int)GetCount() << " obj saved!" << endl;);
00353                               return _XML_Root;                       
00354                     }
00355 
00361           virtual 
00362                     TiXmlElement*       
00363                               XMLSaveHead(TiXmlElement* _XML_Root)
00364                               {
00365                                         SG_Assert(GetMainTag()!="",             "Error in CL_XML_MNGR<T,P>::XMLSaveHead(), MainTag is empty.");
00366                                         SG_Assert(GetSubTagName()!="",          "Error in CL_XML_MNGR<T,P>::XMLSaveHead(), SubTagName is empty.");
00367                                         SG_Assert(GetSubTagID()!="",  "Error in CL_XML_MNGR<T,P>::XMLSaveHead(), SubTagID is empty.");
00368                                         SG_Assert(_XML_Root,                              "Error in CL_XML_MNGR<T,P>::XMLSaveHead(), XML entry is empty.");
00369                               
00370                                         TiXmlElement* XML_MAIN        = CL_XML_OBJ::XMLSave(_XML_Root);// 
00371                                                   //new TiXmlElement( GetMainTag().data() );
00372                               
00373                                         return XML_MAIN;
00374                               }
00375 public:
00376 
00382           virtual 
00383                     TiXmlElement*       
00384                     XMLSave   (TiXmlElement* _XML_Root)
00385           {
00386                     if (!_XML_Root)
00387                               return NULL;
00388 
00389                     //read header tag
00390                     TiXmlElement* XML_MAIN        = XMLSaveHead(_XML_Root);//new TiXmlElement(GetMainTag().c_str());// XMLSaveHead (_XML_Root);
00391 
00392                     if (XMLSaveObjects(XML_MAIN))
00393                     {
00394                               _XML_Root->InsertEndChild(*XML_MAIN);
00395                               return XML_MAIN;
00396                     }
00397                     else
00398                     {
00399                               delete XML_MAIN;
00400                               return NULL;
00401                     }         
00402           }
00403 #endif              //XML
00404 };
00405 
00406 #if SG_TLS_MEMORY_MANAGER
00407           #include "SugoiTools\debug_new_off.h"                       //Must be the last....
00408 #endif 
00409 
00410 };                            //namespace SGE
00411 
00412 #endif              //SG_TLS_CL_XML_MNGR_H

Generated on Mon Mar 19 23:15:10 2007 for SugoiTools by  doxygen 1.4.6-NO