Tulip  4.6.0
Better Visualization Through Research
library/tulip-ogl/include/tulip/GlXMLTools.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip 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.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 #ifndef DOXYGEN_NOTFOR_DEVEL
00022 
00023 #ifndef Tulip_GLXMLTOOLS_H
00024 #define Tulip_GLXMLTOOLS_H
00025 
00026 #include <sstream>
00027 #include <vector>
00028 #include <map>
00029 #include <cassert>
00030 
00031 #include <tulip/tulipconf.h>
00032 
00033 namespace tlp {
00034 
00035 /**
00036  * static tools class use to store/load xml data
00037  */
00038 class GlSimpleEntity;
00039 
00040 class TLP_GL_SCOPE GlXMLTools {
00041 
00042 public :
00043 
00044   /**
00045    * Create and enter into a new data node
00046    */
00047   static void beginDataNode(std::string &outString);
00048 
00049   /**
00050    * End last data node
00051    */
00052   static void endDataNode(std::string &outString);
00053 
00054   /**
00055    * Enter into a data node and set the currentPosition integer to the position next the data tag
00056    */
00057   static void enterDataNode(const std::string &inString, unsigned int &currentPosition);
00058 
00059   /**
00060    * Leave a data node and set the currentPosition integer to the position next the data end tag
00061    */
00062   static void leaveDataNode(const std::string &inString, unsigned int &currentPosition);
00063 
00064   /**
00065    * Create and enter into a new children node with name : name
00066    */
00067   static void beginChildNode(std::string &outString, const std::string &name="children");
00068 
00069   /**
00070    * End last children node with name : name
00071    */
00072   static void endChildNode(std::string &outString, const std::string &name="children");
00073 
00074   /**
00075    * Enter into a child node and set the currentPosition integer to the position next the child tag
00076    * \return child name
00077    */
00078   static std::string enterChildNode(const std::string &inString, unsigned int &currentPosition);
00079 
00080   /**
00081    * Leave into a child node and set the currentPosition integer to the position next the child end tag
00082    * \return child name
00083    */
00084   static void leaveChildNode(const std::string &inString, unsigned int &currentPosition, const std::string &childName);
00085 
00086   /**
00087    * Create a property with name and value in rootNode
00088    */
00089   static void createProperty(std::string &outString, const std::string &name, const std::string &value, const std::string &parent="");
00090 
00091   /**
00092    * Get properties associated with the last opened child
00093    * These properties is returned into a map of string/string
00094    */
00095   static std::map<std::string,std::string> getProperties(const std::string &inString, unsigned int &currentPosition);
00096 
00097   /**
00098    * Create a GlEntity with the given name
00099    */
00100   static GlSimpleEntity *createEntity(const std::string &name);
00101 
00102   /**
00103    * Get the XML output for a vector of Object
00104    */
00105   template <typename Obj>
00106   static void getXML(std::string &outString,const std::string &name,const typename std::vector<Obj> &vect) {
00107 
00108     std::stringstream str;
00109     str << "(" ;
00110     typename std::vector<Obj>::const_iterator it=vect.begin();
00111     assert(it != vect.end());
00112     str << *it ;
00113     ++it;
00114 
00115     for(; it!=vect.end(); ++it) {
00116       str << "," << *it ;
00117     }
00118 
00119     str << ")" ;
00120     outString.append("<"+name+">"+str.str()+"</"+name+">\n");
00121   }
00122 
00123   /**
00124    * Set vector of Object with the given XML
00125    */
00126   template <typename Obj>
00127   static void setWithXML(const std::string &inString, unsigned int &currentPosition,const std::string &name,std::vector<Obj> &vect) {
00128 
00129     goToNextCaracter(inString,currentPosition);
00130 
00131     std::string nameTag=inString.substr(currentPosition,name.size()+2);
00132     assert(nameTag=="<"+name+">");
00133     currentPosition+=name.size()+2;
00134 
00135     size_t endValuePosition=inString.find("</"+name+">",currentPosition);
00136     assert(endValuePosition!=std::string::npos);
00137 
00138     std::istringstream is(inString.substr(currentPosition,endValuePosition-currentPosition));
00139     Obj data;
00140     char c=is.get();
00141 
00142     while(c!=')') {
00143       is >> data ;
00144       vect.push_back(data);
00145       c=is.get();
00146     }
00147 
00148     currentPosition=endValuePosition+name.size()+3;
00149 
00150   }
00151 
00152   /**
00153    * Get the XML output for an Object
00154    */
00155   template <typename Obj>
00156   static void getXML(std::string &outString, const std::string &name, const Obj &value) {
00157     std::stringstream str;
00158     str << value ;
00159     applyIndentation(outString);
00160     outString.append("<"+name+">"+str.str()+"</"+name+">\n");
00161   }
00162 
00163 
00164   static bool checkNextXMLtag(const std::string &inString, unsigned int &currentPosition, const std::string &name) {
00165     unsigned int tmp=currentPosition;
00166     goToNextCaracter(inString,tmp);
00167     std::string nameTag=inString.substr(tmp,name.size()+2);
00168     return (nameTag=="<"+name+">");
00169   }
00170 
00171   /**
00172    * Set an Object with the given XML
00173    */
00174   template <typename Obj>
00175   static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value) {
00176 
00177     goToNextCaracter(inString,currentPosition);
00178 
00179     std::string nameTag=inString.substr(currentPosition,name.size()+2);
00180     assert(nameTag=="<"+name+">");
00181     currentPosition+=name.size()+2;
00182 
00183     size_t endValuePosition=inString.find("</"+name+">",currentPosition);
00184     assert(endValuePosition!=std::string::npos);
00185 
00186     std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
00187     str >> value;
00188     currentPosition=endValuePosition+name.size()+3;
00189   }
00190 
00191   /**
00192    * Set an Object with the given XML and default value
00193    */
00194   template <typename Obj>
00195   static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value,const Obj &defValue) {
00196     goToNextCaracter(inString,currentPosition);
00197 
00198     std::string nameTag=inString.substr(currentPosition,name.size()+2);
00199 
00200     if(nameTag=="<"+name+">") {
00201       currentPosition+=name.size()+2;
00202 
00203       size_t endValuePosition=inString.find("</"+name+">",currentPosition);
00204       assert(endValuePosition!=std::string::npos);
00205 
00206       std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
00207       str >> value;
00208       currentPosition=endValuePosition+name.size()+3;
00209     }
00210     else {
00211       value=defValue;
00212     }
00213   }
00214 
00215 private :
00216 
00217   static void applyIndentation(std::string &outString);
00218   static void goToNextCaracter(const std::string &inString, unsigned int &currentPosition);
00219 
00220   static unsigned int indentationNumber;
00221 
00222 };
00223 
00224 }
00225 #endif
00226 
00227 #endif //DOXYGEN_NOTFOR_DEVEL
00228 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties