Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
GlXMLTools.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef DOXYGEN_NOTFOR_DEVEL
22 
23 #ifndef Tulip_GLXMLTOOLS_H
24 #define Tulip_GLXMLTOOLS_H
25 
26 #include <sstream>
27 #include <vector>
28 #include <map>
29 
30 #include <tulip/tulipconf.h>
31 #include <tulip/Color.h>
32 #include <tulip/Coord.h>
33 
34 namespace tlp {
35 
36 /**
37  * static tools class use to store/load xml data
38  */
39 class GlSimpleEntity;
40 
41 class TLP_GL_SCOPE GlXMLTools {
42 
43 public :
44 
45  /**
46  * Create and enter into a new data node
47  */
48  static void beginDataNode(std::string &outString);
49 
50  /**
51  * End last data node
52  */
53  static void endDataNode(std::string &outString);
54 
55  /**
56  * Enter into a data node and set the currentPosition integer to the position next the data tag
57  */
58  static void enterDataNode(const std::string &inString, unsigned int &currentPosition);
59 
60  /**
61  * Leave a data node and set the currentPosition integer to the position next the data end tag
62  */
63  static void leaveDataNode(const std::string &inString, unsigned int &currentPosition);
64 
65  /**
66  * Create and enter into a new children node with name : name
67  */
68  static void beginChildNode(std::string &outString, const std::string &name="children");
69 
70  /**
71  * End last children node with name : name
72  */
73  static void endChildNode(std::string &outString, const std::string &name="children");
74 
75  /**
76  * Enter into a child node and set the currentPosition integer to the position next the child tag
77  * \return child name
78  */
79  static std::string enterChildNode(const std::string &inString, unsigned int &currentPosition);
80 
81  /**
82  * Leave into a child node and set the currentPosition integer to the position next the child end tag
83  * \return child name
84  */
85  static void leaveChildNode(const std::string &inString, unsigned int &currentPosition, const std::string &childName);
86 
87  /**
88  * Create a property with name and value in rootNode
89  */
90  static void createProperty(std::string &outString, const std::string &name, const std::string &value, const std::string &parent="");
91 
92  /**
93  * Get properties associated with the last opened child
94  * These properties is returned into a map of string/string
95  */
96  static std::map<std::string,std::string> getProperties(const std::string &inString, unsigned int &currentPosition);
97 
98  /**
99  * Create a GlEntity with the given name
100  */
101  static GlSimpleEntity *createEntity(const std::string &name);
102 
103  /**
104  * Get the XML output for a vector of Object
105  */
106  template <typename Obj>
107  static void getXML(std::string &outString,const std::string &name,const typename std::vector<Obj> &vect) {
108 
109  std::stringstream str;
110  str << "(" ;
111  typename std::vector<Obj>::const_iterator it=vect.begin();
112  assert(it != vect.end());
113  str << *it ;
114  ++it;
115 
116  for(; it!=vect.end(); ++it) {
117  str << "," << *it ;
118  }
119 
120  str << ")" ;
121  outString.append("<"+name+">"+str.str()+"</"+name+">\n");
122  }
123 
124  /**
125  * Set vector of Object with the given XML
126  */
127  template <typename Obj>
128  static void setWithXML(const std::string &inString, unsigned int &currentPosition,const std::string &name,std::vector<Obj> &vect) {
129 
130  goToNextCaracter(inString,currentPosition);
131 
132  std::string nameTag=inString.substr(currentPosition,name.size()+2);
133  assert(nameTag=="<"+name+">");
134  currentPosition+=name.size()+2;
135 
136  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
137  assert(endValuePosition!=std::string::npos);
138 
139  std::istringstream is(inString.substr(currentPosition,endValuePosition-currentPosition));
140  Obj data;
141  char c=is.get();
142 
143  while(c!=')') {
144  is >> data ;
145  vect.push_back(data);
146  c=is.get();
147  }
148 
149  currentPosition=endValuePosition+name.size()+3;
150 
151  }
152 
153  /**
154  * Get the XML output for an Object
155  */
156  template <typename Obj>
157  static void getXML(std::string &outString, const std::string &name, const Obj &value) {
158  std::stringstream str;
159  str << value ;
160  applyIndentation(outString);
161  outString.append("<"+name+">"+str.str()+"</"+name+">\n");
162  }
163 
164 
165  static bool checkNextXMLtag(const std::string &inString, unsigned int &currentPosition, const std::string &name){
166  unsigned int tmp=currentPosition;
167  goToNextCaracter(inString,tmp);
168  std::string nameTag=inString.substr(tmp,name.size()+2);
169  return (nameTag=="<"+name+">");
170  }
171 
172  /**
173  * Set an Object with the given XML
174  */
175  template <typename Obj>
176  static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value) {
177 
178  goToNextCaracter(inString,currentPosition);
179 
180  std::string nameTag=inString.substr(currentPosition,name.size()+2);
181  assert(nameTag=="<"+name+">");
182  currentPosition+=name.size()+2;
183 
184  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
185  assert(endValuePosition!=std::string::npos);
186 
187  std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
188  str >> value;
189  currentPosition=endValuePosition+name.size()+3;
190  }
191 
192  /**
193  * Set an Object with the given XML and default value
194  */
195  template <typename Obj>
196  static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value,const Obj &defValue) {
197  goToNextCaracter(inString,currentPosition);
198 
199  std::string nameTag=inString.substr(currentPosition,name.size()+2);
200 
201  if(nameTag=="<"+name+">") {
202  currentPosition+=name.size()+2;
203 
204  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
205  assert(endValuePosition!=std::string::npos);
206 
207  std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
208  str >> value;
209  currentPosition=endValuePosition+name.size()+3;
210  }
211  else {
212  value=defValue;
213  }
214  }
215 
216 private :
217 
218  static void applyIndentation(std::string &outString);
219  static void goToNextCaracter(const std::string &inString, unsigned int &currentPosition);
220 
221  static unsigned int indentationNumber;
222 
223 };
224 
225 }
226 #endif
227 
228 #endif //DOXYGEN_NOTFOR_DEVEL
229 ///@endcond