XMLTag.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Friedrich Klank <klank@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 
00019 /************************************************************************
00020                         XMLTag.h - Copyright klank
00021 
00022 
00023 **************************************************************************/
00024 
00025 
00026 #ifndef XMLTAG_H
00027 #define XMLTAG_H
00028 
00029 //#include "RelPoseFactory.h"
00030 #include "Signature.h"
00031 #include "AlgorithmEval.h"
00032 #include "Sensor.h"
00033 #include <map>
00034 
00035 typedef struct _xmlTextWriter xmlTextWriter;
00036 typedef struct _xmlTextReader xmlTextReader;
00037 typedef struct _xmlDoc xmlDoc;
00038 typedef unsigned char xmlChar;
00039 typedef struct _xmlNode xmlNode;
00040 
00041 #define XML_NODE_STD_VECTOR "std_vector"
00042 #define XML_NODE_STD_MAP "std_map"
00043 #define XML_NODE_STD_PAIR "std_pair"
00044 #define XML_NODE_STD_MAPENTRY "mapentry"
00045 #define XML_NODE_INT "int"
00046 #define XML_NODE_DOUBLE "double"
00047 #define XML_NODE_ULONG "ulong"
00048 #define XML_NODE_STRING "std_string"
00049 #define XML_NODE_MATRIX "newmat_Matrix"
00050 
00051 #define XML_ATTRIBUTE_ROWS "rows"
00052 #define XML_ATTRIBUTE_COLS "cols"
00053 
00054 
00055 namespace cop
00056 {
00057 
00058   LocateAlgorithm* LocAlgFactory(XMLTag* tag);
00059 
00060   template<typename T> class AlgorithmEval;
00061 
00062   /**********************************************************************************************/
00071   class XMLTag
00072   {
00073   public:
00074 
00075     // Constructors/Destructors
00076     //
00077 
00078 
00083     XMLTag ( std::string name);
00084 
00088     virtual ~XMLTag ( );
00089 
00090 
00094   /****************************************************************************/
00112     void WriteToFile(const std::string& stFile, XMLTag** fileReference = NULL) const;
00113   /****************************************************************************/
00124    char* WriteToString();
00125   /****************************************************************************/
00135    void FreeAfterWriteToString();
00136 
00137   /****************************************************************************/
00154     static XMLTag* ReadFromFile(const std::string &stFile);
00155   /****************************************************************************/
00165     XMLTag* Clone() const;
00166 
00167   /****************************************************************************/
00180     virtual void Write(xmlTextWriter* pWriter) const;
00181   /****************************************************************************/
00195     static XMLTag* Read(xmlTextReader* pReader);
00196   /****************************************************************************/
00206     template<typename T> static XMLTag* Tag(std::vector<T> vector, std::string name = "")
00207     {
00208       XMLTag* tag = new XMLTag(name.compare("") == 0 ? XML_NODE_STD_VECTOR : name);
00209   #ifdef WIN32
00210       std::vector<T>::const_iterator iter;
00211       for(iter = vector.begin(); iter != vector.end(); iter++)
00212       {
00213         tag->AddChild(XMLTag::Tag((*iter)));
00214       }
00215   #else
00216       for(unsigned int i = 0; i < vector.size(); i++)
00217       {
00218         tag->AddChild(XMLTag::Tag(vector[i]));
00219       }
00220   #endif
00221       return tag;
00222     }
00223 
00224  template<typename T1, typename T2> static XMLTag* Tag(std::map< T1, T2 > map, std::string name = "")
00225     {
00226       XMLTag* tag = new XMLTag(name.compare("") == 0 ? XML_NODE_STD_MAP : name);
00227       typename std::map< T1, T2 >::iterator iter;
00228       for(iter = map.begin(); iter != map.end(); iter++)
00229       {
00230         XMLTag* childTag = new XMLTag(XML_NODE_STD_MAPENTRY);
00231         childTag->AddChild(XMLTag::Tag((*iter).first));
00232         childTag->AddChild(XMLTag::Tag((*iter).second));
00233         tag->AddChild(childTag);
00234       }
00235       return tag;
00236     }
00237 
00238     template<typename T1, typename T2> static XMLTag* Tag(std::pair<T1, T2> p, std::string name = "")
00239     {
00240       XMLTag* tag = new XMLTag(name.compare("") == 0 ? XML_NODE_STD_PAIR : name);
00241       tag->AddChild(XMLTag::Tag(p.first));
00242       tag->AddChild(XMLTag::Tag(p.second));
00243       return tag;
00244     }
00245 
00246     template<typename T3> static XMLTag* Tag(AlgorithmEval<T3> eval, std::string name = "")
00247     {
00248       return eval.Save();
00249     }
00250 
00251 
00252     static XMLTag* Tag(int n, std::string name = "");
00253     static XMLTag* Tag(long n, std::string name = "");
00254     static XMLTag* Tag(unsigned long n, std::string name = "");
00255     static XMLTag* Tag(double d, std::string name = "");
00256     static XMLTag* Tag(std::string value, std::string name = "");
00257     static XMLTag* Tag(Elem* elem, std::string name = "");
00258     static XMLTag* Tag(Algorithm<ImprovedPose>* alg, std::string name = "");
00259     static XMLTag* Tag(Algorithm<std::vector<Signature*> >* alg, std::string name = "");
00260     static XMLTag* Tag(Algorithm<std::vector<RelPose* > >* alg, std::string name = "");
00261     static XMLTag* Tag(Algorithm<Descriptor*>* alg, std::string name = "");
00262     static XMLTag* Tag(Sensor* sensor, std::string name = "");
00263     static XMLTag* Tag(const Matrix& alg, std::string name = "");
00264 
00265 
00266   /****************************************************************************/
00281     template<typename T>
00282     static T Load(XMLTag* tag, T* t )
00283     {
00284       throw "Load Failed: Expected type has no loading function implemented";
00285     };
00286 
00287     template<typename T>
00288      static std::vector<T> Load(XMLTag* tag, std::vector<T>*  )
00289     {
00290       std::vector<T> vectorTemp;
00291       for(unsigned int i = 0; i < tag->CountChildren(); i++)
00292       {
00293         try
00294         {
00295           if(tag->GetChild(i)->GetName().compare("comment") != 0)
00296             vectorTemp.push_back(Load(tag->GetChild(i), (T*)NULL));
00297           else
00298           {
00300             printf("Comment in vector\n");
00301           }
00302         }
00303         catch(char const* text)
00304         {
00305           printf("Error reading a vector: %s\n", text);
00306           /* Do nothing, try to get the others*/
00307         }
00308         catch(...)
00309         {
00310           printf("Error reading a vector.\n");
00311           /* Do nothing, try to get the others*/
00312         }
00313       }
00314       return vectorTemp;
00315     };
00316 
00317     template<typename T1, typename T2>
00318      static std::map<T1, T2> Load(XMLTag* tag, std::map<T1, T2>* map)
00319     {
00320       std::map<T1, T2> mapTemp;
00321       if(tag != NULL)
00322       {
00323         for(unsigned int i = 0; i < tag->CountChildren(); i++)
00324         {
00325           XMLTag* mapentry =  tag->GetChild(i);
00326           if(mapentry == NULL)
00327             continue;
00328           try
00329           {
00330             T1 key;
00331             key = Load(mapentry->GetChild(0), (T1*)NULL);
00332             mapTemp[key] = Load(mapentry->GetChild(1), (T2*)NULL);
00333           }
00334           catch(char const* text)
00335           {
00336             printf("Error reading a map: %s\n", text);
00337             /* Do nothing, try to get the others*/
00338           }
00339           catch(...)
00340           {
00341             printf("Error reading a map.\n");
00342             /* Do nothing, try to get the others*/
00343           }
00344         }
00345       }
00346       return mapTemp;
00347     };
00348 
00349 
00350     template<typename T1, typename T2>
00351     static std::pair<T1, T2> Load(XMLTag* tag, std::pair<T1, T2>*  )
00352     {
00353       std::pair<T1, T2> p;
00354       int childCount = tag->CountChildren();
00355       if(childCount > 0)
00356         p.first = Load(tag->GetChild(0), (T1*)NULL);
00357       if(childCount > 1)
00358         p.second = Load(tag->GetChild(1), (T2*)NULL);
00359 
00360       return p;
00361     };
00362 
00363     template<typename T3> static AlgorithmEval<T3> Load(XMLTag* tag, AlgorithmEval<T3>*)
00364     {
00365       return AlgorithmEval<T3>(tag);
00366     }
00367 
00368     static int Load(XMLTag* tag, int* )
00369     {
00370       if(tag == NULL)
00371         return 0;
00372       return  tag->GetCDataInt();
00373     };
00374 
00375     static int Load(XMLTag* tag, unsigned long* )
00376     {
00377       if(tag == NULL)
00378         return 0;
00379       return  tag->GetCDataUlong();
00380     };
00381 
00382     static double Load(XMLTag* tag,double *)
00383     {
00384       if(tag == NULL)
00385         return 0.0;
00386       return tag->GetCDataDouble();
00387     };
00388 
00389     static std::string Load(XMLTag* tag, std::string *)
00390     {
00391       if(tag == NULL)
00392         return "";
00393       return tag->GetCDataST();
00394     };
00395 
00396     static Elem* Load(XMLTag* tag, Elem**  )
00397     {
00398       if(tag == NULL)
00399         return NULL;
00400       return Elem::ElemFactory(tag);
00401     };
00402 
00403     static Algorithm<std::vector<RelPose*> >* Load(XMLTag* tag, Algorithm<std::vector<RelPose*> >** )
00404     {
00405       if(tag == NULL)
00406         return NULL;
00407       return (Algorithm<std::vector<RelPose*> >*)LocateAlgorithm::LocAlgFactory(tag);
00408     };
00409 
00410     static Algorithm<ImprovedPose>* Load(XMLTag* tag, Algorithm<ImprovedPose>** )
00411     {
00412 
00413       if(tag == NULL)
00414         return NULL;
00415       return (Algorithm<ImprovedPose>*)ProveAlgorithm::ProveAlgFactory(tag);
00416     };
00417 
00418     static Algorithm<Descriptor*>* Load(XMLTag* tag, Algorithm<Descriptor*>** )
00419     {
00420       if(tag == NULL)
00421         return NULL;
00422       return (Algorithm<Descriptor*>*)RefineAlgorithm::RefineAlgFactory(tag);
00423     };
00424 
00425 
00426     static Algorithm<std::vector<Signature*> >* Load(XMLTag* tag, Algorithm<std::vector<Signature*> >** )
00427     {
00428       if(tag == NULL)
00429         return NULL;
00430       return ( Algorithm<std::vector<Signature*> >*)AttentionAlgorithm::AttentionAlgFactory(tag);
00431     };
00432 
00433     static Sensor* Load(XMLTag* tag, Sensor** )
00434     {
00435       if(tag == NULL)
00436       {
00437         printf("Tag for laoding a sensor equals NULL\n");
00438         return NULL;
00439       }
00440       return Sensor::SensorFactory(tag);
00441     };
00442 
00443     /*static SemMapElement* Load(XMLTag* tag, SemMapElement** )
00444     {
00445       if(tag == NULL)
00446         return NULL;
00447       return SemMapElement::SemMapElementFactory(tag);
00448     };*/
00449 
00450   /****************************************************************************/
00465     static Matrix Load(XMLTag* tag, Matrix* m);
00466 
00467   /****************************************************************************/
00482     void AddProperty(const std::string& name, const std::string& value);
00483   /****************************************************************************/
00498     void AddProperty(const std::string &name, const unsigned long &value);
00499     void AddProperty(const std::string &name, const long &value);
00500   /****************************************************************************/
00515     void AddProperty(const std::string& name, const int &value);
00516   /****************************************************************************/
00531     void AddProperty(const std::string& name, const double &value);
00532 
00533   /****************************************************************************/
00546     void SetCData(const int &value);
00547     void SetCData(const long &value);
00548     void SetCData(const unsigned long &value);
00549   /****************************************************************************/
00562     void SetCData(const double &value);
00563   /****************************************************************************/
00576     void SetCData(const std::string &value);
00577 
00578   /****************************************************************************/
00589     int                 GetCDataInt() const;
00590     unsigned long GetCDataUlong() const;
00591     double              GetCDataDouble() const;
00592     std::string GetCDataST() const;
00593 
00594 
00595   /****************************************************************************/
00612     std::string GetProperty(const std::string& name, std::string defaultValue = "");
00613     double GetPropertyDouble(const std::string& name, double defaultValue = 0.0);
00614     int GetPropertyInt(const std::string& name, int defaultValue = 0);
00615 
00616 
00617   /****************************************************************************/
00637     int AddChild(XMLTag* child, int position = -1);
00638      /****************************************************************************/
00656     void ReplaceChild(XMLTag* child, int position);
00657   /****************************************************************************/
00671     unsigned int CountChildren() const;
00672   /****************************************************************************/
00687     void RemoveChild(const unsigned int &position);
00688   /****************************************************************************/
00703    void RemoveChild(const std::string &name);
00704 
00705   /****************************************************************************/
00720     XMLTag* GetChild(const unsigned int &position);
00721     XMLTag* GetChild(const std::string &name);
00722     XMLTag* GetChild(const std::string& name, int innerindex);
00723 
00724   /****************************************************************************/
00736     std::string& GetName(){return m_name;}
00737     void SetName(std::string name){m_name = name;}
00738 
00739     //static public Methods
00740     static unsigned int OldTag()
00741     {
00742       return 0;
00743     }
00744 
00745     unsigned long date();
00746     //
00747     //Methods
00748     //
00749     int getQueryType();
00750 
00751     /****************************************************************************/
00762     std::vector<std::string> GetSubFilenames();
00763     void ReplaceSubFilenames(std::string global_path);
00764     protected:
00765 
00766 
00767     private:
00768 
00769      /****************************************************************************/
00781       void Touch();
00782 
00783 
00784      /****************************************************************************/
00795      void GetSubFilenames(std::vector<std::string> &strings);
00796     /****************************************************************************/
00808       void DocWrite(xmlNode* n) const;
00809 
00810     // Private attributes
00811     //
00812     unsigned long                                                                  m_lastChanged;
00813     std::vector<XMLTag*>                                         m_children;
00814     std::string                                                                    m_cData;
00815     std::string                                                                    m_name;
00816     std::map<std::string, std::string>   m_properties;
00817 
00818     xmlDoc                               *m_doc;
00819     xmlChar                              *m_xmlbuff;
00820   };
00821 
00822 }
00823 
00824 
00825 #endif // XMLTAG_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


cognitive_perception
Author(s): Ulrich F Klank
autogenerated on Thu May 23 2013 07:38:35