D:/WorkDir/SugoiProjects/SugoiTools/include/SugoiTools/xml.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 This program is free software; you can redistribute it and/or
00008 modify it under the terms of the GNU General Public License
00009 as published by the Free Software Foundation; either version 2
00010 of the License, or (at your option) any later version.
00011 
00012 This program is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with this program; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 LICENSE_END
00021 */
00022 
00023 /* \file SG_TLS_xml.h
00024  * \author Yannick Allusse
00025 */ 
00026 
00027 #ifndef TOOLS_XML_H
00028 #define TOOLS_XML_H
00029 
00030 #include "SugoiTools\config.h"
00031 //definitions
00032 namespace SGE{
00033           //class CL_XML_OBJ;
00034           //template <typename P> class CL_XML_BASE_OBJ;
00035 
00036 //defintions of default tags...
00037 #define XML_TAG_VAL_ID                  "id"
00038 #define XML_TAG_FCT                     "function"
00039 #define XML_TAG_FCT_MNGR      "FunctionMngr"      
00040 //========================================
00041 
00042 
00043 
00044 };
00045 //===============================
00046 #include "SugoiTools\exceptions.h"
00047 #include "SugoiTools\logger_main.h"
00048 #include "SugoiTools\cl_base_obj.h"
00049 
00050 #define EXIT_ON_ERROR true
00051 
00052 #if _SG_TLS_XML_STL
00053           #define TIXML_USE_STL //Don't know why it doesn't work with this option...??
00054 #endif
00055 
00056 //Include File
00057 #ifdef  TIXML_USE_STL
00058           #include <XML/tinyXML_STL.h>
00059 #else
00060           #include <XML/tinyXML.h>
00061 #endif
00062 
00063 #if _SG_TLS_XML
00064 #if NDEBUG          //release build
00065           #ifdef  TIXML_USE_STL
00066                     #pragma comment(lib ,"tinyXML_STL.lib")
00067           #else
00068                     #pragma comment(lib ,"tinyXML.lib")
00069           #endif
00070 #else               //debug build
00071           #ifdef  TIXML_USE_STL
00072                     #pragma comment(lib ,"tinyXMLd_STL.lib")
00073           #else
00074                     #pragma comment(lib ,"tinyXMLd.lib")
00075           #endif
00076 #endif
00077 #endif//XML
00078 
00079 
00080 namespace SGE{
00081 
00082 #if _SG_TLS_XML
00083 
00084 
00085 //=======================================================================
00086 //=======================================================================
00087 //=======================================================================
00088 //
00089 //        base XML functions
00090 //
00091 //=======================================================================
00092 //=======================================================================
00093 //=======================================================================
00094 
00095 
00104           _SG_TLS_EXPORT 
00105                     TiXmlElement        * 
00106                               XMLOpenFile(std::string _filname, std::string _FirstElement);
00107 
00115           _SG_TLS_EXPORT 
00116                     TiXmlDocument* 
00117                               XMLSaveFile(std::string _filname, std::string _FirstElement);
00118 
00129           _SG_TLS_EXPORT 
00130                     TiXmlElement        * 
00131                               XMLGetElement                 (TiXmlElement* _XML_Src,      std::string  _ElemName,  std::string _ErrorMsg="", bool _ExitOnError = false);
00132 
00140           _SG_TLS_EXPORT 
00141                     TiXmlElement        * 
00142                               XMLAddNewElement    (TiXmlElement* _XML_ROOT,     std::string  _ElemName);
00143 
00144 
00145 /* to remove !!???
00146 #define XML_LOADFromFile(FILE_NAME, XML_TAG_NAME){\
00147           TiXmlElement* XML_ROOT = XMLOpenFile(FILE_NAME, XML_TAG_NAME);\
00148           if (XML_ROOT) return this->XMLLoad(XML_ROOT);\
00149           else return NULL;\
00150           }
00151 
00152 #define XML_SAVEToFile(FILE_NAME, XML_TAG_NAME){\
00153           TiXmlDocument  * XML_File = XMLSaveFile(FILE_NAME, XML_TAG_NAME);\
00154           if (XML_File)\
00155           {\
00156                     TiXmlElement * XML_ROOT = XML_File->FirstChild(XML_TAG_NAME)->ToElement();\
00157                     SG_Assert(XML_ROOT, "No XML Root Tag Found!");\
00158                     SG_Assert(this->XMLSave(XML_ROOT),  "XMLSave Failed!");\
00159                     SG_Assert(XML_File->SaveFile(), "XML_File->SaveFile() Failed!");\
00160                     return XML_File;\
00161           }\
00162           else return NULL;\
00163 }
00164 */
00165 
00166 
00179           /*template <typename ValType>
00180           bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, ValType& _Val, ValType Def=0);
00181           */
00182           _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, int& _Val);
00183           _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, unsigned int& _Val);
00184           _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, bool& _Val);
00185           _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, double& _Val);
00186           _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, float& _Val);
00187           _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, std::string& _Val);
00188           _SG_TLS_EXPORT bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, long& _Val);
00189 
00190 /*        template <typename Type>
00191           bool XMLReadVal (TiXmlElement* _XML_ROOT, std::string _ValName, Type& _Dest)
00192           {
00193                     XMLCheckRoot(_XML_ROOT);
00194 
00195                     if(typeid(Type) == typeid(int)|| typeid(unsigned int))
00196                     {//int / unsigned int
00197                               int TempVal;
00198                               if (_XML_ROOT->QueryIntAttribute(_ValName.data(), &TempVal) != TIXML_NO_ATTRIBUTE)
00199                               {
00200                                         _Dest = TempVal;
00201                                         return true;
00202                               }
00203                     }
00204                     else if(typeid(Type) == typeid(float))
00205                     {//float 
00206                               if (_XML_ROOT->QueryFloatAttribute(_ValName.data(), &_Dest) != TIXML_NO_ATTRIBUTE)
00207                                         return true;
00208                     }
00209                     else if(typeid(Type) == typeid(double))
00210                     {//double
00211                               if (_XML_ROOT->QueryDoubleAttribute(_ValName.data(), &_Dest) != TIXML_NO_ATTRIBUTE)
00212                                         return true;
00213                     }
00214                     else if(typeid(Type) == typeid(long))
00215                     {//long
00216                               double TempVal;
00217                               if (_XML_ROOT->QueryDoubleAttribute(_ValName.data(), &TempVal) != TIXML_NO_ATTRIBUTE)
00218                               {
00219                                         _Dest = TempVal;
00220                                         return true;
00221                               }
00222                     }
00223                     else if(typeid(Type) == typeid(bool))
00224                     {//bool
00225                               int TempVal;
00226                               if (_XML_ROOT->QueryIntAttribute(_ValName.data(), &TempVal) != TIXML_NO_ATTRIBUTE)
00227                               {
00228                                         _Dest= (TempVal)?1:0;
00229                                         return true;
00230                               }
00231                     }
00232                     else if(typeid(Type) == typeid(std::string))
00233                     {//std::string
00234                               const char * Temp = _XML_ROOT->Attribute(_ValName.data());
00235                               if (Temp)
00236                               {
00237                                         _Dest = (Type)Temp;
00238                                         return true;
00239                               }
00240                     }
00241                     return false;
00242           }
00243 */        
00244 
00254           _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const int& _Val);
00255           _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const unsigned int& _Val);
00256           _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const bool& _Val);
00257           _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const double& _Val);
00258           _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const float& _Val);
00259           _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const std::string& _Val);
00260           _SG_TLS_EXPORT void XMLWriteVal (TiXmlElement* _XML_ROOT, std::string _ValName, const long& _Val);
00261 
00262 
00263 
00264 template <class ObjType>
00265           TiXmlElement* 
00266                     XMLLoadVector(TiXmlElement* _XML_ROOT, std::vector<ObjType*> &_Vector, std::string _TagName, std::string _SubTagName)
00267                     {
00268                               if (!_XML_ROOT)
00269                                         return NULL;
00270 
00271                               TiXmlElement*XML_MAIN = XMLGetElement(_XML_ROOT, _TagName.data());
00272                               if (!XML_MAIN)
00273                                         return NULL;
00274                               //<..>< T type name><obj>===============================
00275                               
00276                               ObjType * NewObj;
00277                               
00278                               while(XML_MAIN)
00279                               {
00280                                         try
00281                                         {
00282                                                   //Init values
00283                                                   NewObj              = new ObjType();
00284                                                   //NewObj->SetMainTag(_SubTagName);
00285                     if (NewObj->XMLLoad(XML_MAIN, _SubTagName))
00286                                                             _Vector.push_back(NewObj);
00287                                                   else
00288                                                             delete NewObj;
00289 
00290                                         /*
00291                                                   if(!XMLReadVal(XML_MAIN, SubTagID.data(), ID_Val))
00292                                                   {
00293                                                             //SG_Assert(, );
00294                                                             SG_WARNING_LOG("Parsing XML file in <SubTagID ID_Val='?'>, ID field is not specified!");
00295                                                             osg::notify() << std::endl << "MainTag" << GetMainTag       () ;
00296                                                             osg::notify() << std::endl << "SubTag"  << GetSubTagName() ;
00297                                                             osg::notify() << std::endl << "SubTagID"<< GetSubTagID      () << std::endl ;
00298                                                   }
00299                                                   else
00300                                                   {
00301                                                             XMLReadVal(XML_MAIN, "Type", Type);
00302                                                             XMLLoadObject(XML_MAIN, ID_Val, SubTagName, Type);
00303                                                   }
00304                                         */        
00305 
00306                                                   //get next element
00307                                                   XML_MAIN=XML_MAIN->NextSiblingElement();
00308                                         }
00309 
00310                                         catch(CException &e)
00311                                         {
00312                                                   //if (ID_Val != 0)
00313                                                   {
00314                                                             SG_ERROR_LOG ("Exception catched while reading XMLtag '" << _TagName << "'.\n");
00315                                                   }
00316                               /*else{
00317                                                             SG_ERROR_LOG ("Exception catched while reading XMLtag <" << ID_Val << "> in <" << GetMainTag() << ">.\n");
00318                                                   }*/
00319                                                   std::cerr << "Exception catched : " << e.what() << endl;
00320 
00321                                                   if (NewObj)
00322                                                   {
00323                                                             //we delete it, cause they where some errors
00324                                                             delete NewObj;
00325                                                   }
00326                                                   //we continue and get next element
00327                                                   XML_MAIN=XML_MAIN->NextSiblingElement();
00328                                         }
00329                               }
00330                               
00331                               _SG_DEBUG_XML(std::cout << "Mngr <" << _TagName << "> " << (unsigned int)_Vector.size() << " obj loaded!" << endl);
00332                               return XML_MAIN;
00333                     }
00334 
00335 
00336 
00337 template <class ObjType>
00338           TiXmlElement* 
00339                     XMLSaveVector(TiXmlElement* _XML_ROOT, std::vector<ObjType*> &_Vector, std::string _TagName, std::string _SubTagName)
00340                     {
00341                               TiXmlElement* _XML_VECTOR = new TiXmlElement(_TagName.data() );
00342                               TiXmlElement* XML_ELM = NULL;
00343 
00344                               //<..>< obj >===============================
00345                               //std::vector<ObjType>::iterator itObj;//to manipulate the vector object
00346 
00347                               //Look into the vector for the following name
00348                               for (unsigned int i = 0; i<_Vector.size() ;i++ )
00349                               {
00350                                         try
00351                                         {
00352                                                   XML_ELM = new TiXmlElement( _SubTagName.data() );
00353                                                   //_Vector[i]->SetMainTag(_SubTagName);
00354                                                   _Vector[i]->XMLSave(XML_ELM, _SubTagName);
00355                                                   _XML_VECTOR->InsertEndChild(*XML_ELM);
00356                                         }
00357 
00358                                         catch(CException &e)
00359                                         {
00360                                                   SG_ERROR_LOG ("Error while saving XMLtag '<" << 
00361                                                                       _TagName << 
00362                                                                       "><" <<
00363                                                                       _SubTagName <<
00364                                                                       ">");
00365                                                   std::cerr << "Exception catched : " << e.what() << endl;
00366 
00367                                                   if (XML_ELM)//we delete it, cause they where some errors
00368                                                             delete XML_ELM;
00369                                                   
00370                                                   continue;//we continue and get next element
00371                                         }
00372                               }
00373                               _XML_ROOT->InsertEndChild(*_XML_VECTOR);
00374                               _SG_DEBUG_XML(std::cout << "Mngr <" << _TagName << "> " << (unsigned int)_Vector.size() << " obj saved!" << endl);
00375                               return _XML_VECTOR; 
00376                     }
00377 #endif
00378 
00379 //=======================================================================
00380 //=======================================================================
00381 //=======================================================================
00382 //
00383 //        CL_XML_OBJ
00384 //
00385 //=======================================================================
00386 //=======================================================================
00387 //=======================================================================
00388 
00401 class _SG_TLS_EXPORT CL_XML_OBJ
00402 {
00403           public:
00409                               CL_XML_OBJ(const std::string _TagName);
00410 
00414                               ~CL_XML_OBJ();
00415 
00419                     const std::string &           GetMainTag()const;
00420                     
00425                     void                                              SetMainTag(const std::string &          _newTag);
00426 
00427           #if _SG_TLS_XML
00428           #if 0
00429 
00436                               _SG_TLS_INLINE static virtual 
00437                                         TiXmlElement*       
00438                                         XMLLoadFromMngr     (TiXmlElement* _XML_Root, std::string _TagName);
00439 
00440                               _SG_TLS_INLINE static virtual 
00441                                         TiXmlElement*       
00442                                         XMLSaveFromMngr     (TiXmlElement* _XML_Root, std::string _TagName);
00443           #endif
00444 
00450                               _SG_TLS_INLINE virtual 
00451                                         TiXmlElement*       
00452                                         XMLLoad                       (TiXmlElement* _XML_Root);
00453                     
00459                     _SG_TLS_INLINE virtual        
00460                                         TiXmlElement*       
00461                                         XMLSave                       (TiXmlElement* _XML_Root);
00462 
00471                               _SG_TLS_INLINE virtual
00472                                         TiXmlElement*       
00473                                         XMLLoadFromFile     (std::string _fileName);
00474 
00483                               _SG_TLS_INLINE virtual
00484                                         TiXmlDocument*      
00485                                         XMLSaveToFile       (std::string _fileName);
00486           #endif
00487 
00488           private:
00489                     std::string XMLTagName;
00490 };
00491 
00492 
00493 
00494 //=======================================================================
00495 //=======================================================================
00496 //=======================================================================
00497 //
00498 //        CL_XML_BASE_OBJ
00499 //
00500 //=======================================================================
00501 //=======================================================================
00502 //=======================================================================
00503 
00504 
00505 
00511 template <typename P=std::string> class CL_XML_BASE_OBJ
00512           : public CL_XML_OBJ , public CL_BASE_OBJ<P>
00513           {
00514           public:
00515 
00521                     _SG_TLS_INLINE 
00522                               CL_XML_BASE_OBJ(P _ID, const std::string _TagName = "obj")
00523                               : CL_XML_OBJ(_TagName), 
00524                               CL_BASE_OBJ<P>(_ID)
00525                               {}
00526 
00527                     _SG_TLS_INLINE
00528                               CL_XML_BASE_OBJ(const CL_XML_BASE_OBJ<P> &_copy)
00529                               : CL_XML_OBJ(_copy.GetMainTag()), 
00530                               CL_BASE_OBJ<P>(_copy.GetID())
00531                               {
00532                               
00533                               }
00534 /*
00535                     _SG_TLS_INLINE
00536                               CL_XML_BASE_OBJ(const CL_XML_BASE_OBJ<P> _copy)
00537                               : CL_XML_OBJ(_copy.GetMainTag()), 
00538                               CL_BASE_OBJ<P>(_copy.GetID())
00539                               {
00540                               
00541                               }
00542 */
00543 
00547                     /*_SG_TLS_INLINE              
00548                               ~CL_XML_BASE_OBJ()
00549                               {}
00550 */
00551 
00552                     _SG_TLS_INLINE
00553                               CL_XML_BASE_OBJ & operator = (const CL_XML_BASE_OBJ & _obj)
00554                               {
00555                                         return *this;
00556                               }
00557 
00558           #if _SG_TLS_XML
00559 
00566                               _SG_TLS_INLINE virtual 
00567                                         TiXmlElement*       
00568                                         XMLLoad (TiXmlElement* _XML_Root)
00569                                         {
00570                                                   TiXmlElement* XMLElem = CL_XML_OBJ::XMLLoad(_XML_Root);
00571                                                   XMLReadVal(XMLElem, XML_TAG_VAL_ID, ID);
00572                                                   return _XML_Root;
00573                                         };
00574 
00582                     /*        _SG_TLS_INLINE virtual
00583                                         TiXmlElement*
00584                                         XMLSave (TiXmlElement* _XML_Root) = 0;
00585                     *//*                {
00586                                                   TiXmlElement* XMLElem = CL_XML_OBJ::XMLSave(_XML_Root);
00587                                                   XMLWriteVal(XMLElem, XML_TAG_VAL_ID, ID);
00588                                                   return XMLElem;
00589                                         }
00590                                         */
00591           #endif
00592 };
00593 
00594 
00595 
00596 
00597 //=======================================================================
00598 //=======================================================================
00599 //=======================================================================
00600 //
00601 //        CL_XML_BASE_OBJ_FILE
00602 //
00603 //=======================================================================
00604 //=======================================================================
00605 //=======================================================================
00606 
00607 
00613 class _SG_TLS_EXPORT CL_XML_BASE_OBJ_FILE
00614           : public CL_XML_OBJ , public CL_BASE_OBJ_FILE, virtual public CL_BASE_OBJ<std::string>
00615           {
00616           public:
00617 
00623                     CL_XML_BASE_OBJ_FILE(const std::string & _ID, const std::string _TagName = "obj")
00624                               : CL_XML_OBJ(_TagName), 
00625                               CL_BASE_OBJ_FILE(_ID), 
00626                               CL_BASE_OBJ<std::string>(_ID)
00627                               {};
00628 
00632                               ~CL_XML_BASE_OBJ_FILE()
00633                               {};
00634 
00635 #if _SG_TLS_XML
00636 
00643                               _SG_TLS_INLINE virtual 
00644                                         TiXmlElement*       
00645                                         XMLLoad (TiXmlElement* _XML_Root)
00646                                         {
00647                                                   TiXmlElement* XMLElem = CL_XML_OBJ::XMLLoad(_XML_Root);
00648                                                   XMLReadVal(XMLElem, XML_TAG_VAL_ID, ID);
00649                                                   return _XML_Root;
00650                                         };
00651 
00659                               _SG_TLS_INLINE virtual
00660                                         TiXmlElement*
00661                                         XMLSave (TiXmlElement* _XML_Root)
00662                                         {
00663                                                   TiXmlElement* XMLElem = new TiXmlElement(GetMainTag().c_str());// CL_XML_OBJ::XMLSave(_XML_Root);
00664                                                   XMLWriteVal(XMLElem, XML_TAG_VAL_ID, ID);
00665                                                   return XMLElem;
00666                                         };
00667 #endif
00668 };
00669 
00670 };//namespace SGE
00671 #endif

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