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 #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:
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);
00147 }
00148 else
00149 NewObj = Add(_ID);
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
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
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
00185
00186
00187 NewObj = NULL;
00188
00189 Type = "";
00190
00191
00192
00193
00194 SubTagName = XML_MAIN->Value();
00195
00196 if(!XMLReadVal(XML_MAIN, SubTagID.data(), ID_Val))
00197 {
00198
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
00212 XML_MAIN=XML_MAIN->NextSiblingElement();
00213 }
00214
00215 catch(CException &e)
00216 {
00217
00218 {
00219 SG_ERROR_LOG ("Exception catched while reading XMLtag '" << GetMainTag() << "'.\n");
00220 }
00221
00222
00223
00224 std::cerr << "Exception catched : " << e.what() << endl;
00225
00226 if (NewObj)
00227 {
00228
00229 this->Delete(NewObj->GetID());
00230 }
00231
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);
00259
00260 if (!XML_MAIN)
00261 return NULL;
00262
00263
00264 return XML_MAIN;
00265 }
00266
00267 public:
00274 virtual
00275 TiXmlElement*
00276 XMLLoad(TiXmlElement* _XML_Root)
00277 {
00278
00279 TiXmlElement* XML_MAIN = XMLLoadHead (_XML_Root);
00280
00281 if (!XML_MAIN)
00282 return NULL;
00283 else
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
00318 iterator i;
00319
00320
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
00341
00342
00343
00344 std::cerr << "Exception catched : " << e.what() << endl;
00345
00346 if (XML_ELM)
00347 delete XML_ELM;
00348
00349 continue;
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
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
00390 TiXmlElement* XML_MAIN = 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"
00408 #endif
00409
00410 };
00411
00412 #endif //SG_TLS_CL_XML_MNGR_H