Tulip  4.0.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  * Set an Object with the given XML
166  */
167  template <typename Obj>
168  static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value) {
169 
170  goToNextCaracter(inString,currentPosition);
171 
172  std::string nameTag=inString.substr(currentPosition,name.size()+2);
173  assert(nameTag=="<"+name+">");
174  currentPosition+=name.size()+2;
175 
176  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
177  assert(endValuePosition!=std::string::npos);
178 
179  std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
180  str >> value;
181  currentPosition=endValuePosition+name.size()+3;
182  }
183 
184  /**
185  * Set an Object with the given XML and default value
186  */
187  template <typename Obj>
188  static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value,const Obj &defValue) {
189  goToNextCaracter(inString,currentPosition);
190 
191  std::string nameTag=inString.substr(currentPosition,name.size()+2);
192 
193  if(nameTag=="<"+name+">") {
194  currentPosition+=name.size()+2;
195 
196  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
197  assert(endValuePosition!=std::string::npos);
198 
199  std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
200  str >> value;
201  currentPosition=endValuePosition+name.size()+3;
202  }
203  else {
204  value=defValue;
205  }
206  }
207 
208 private :
209 
210  static void applyIndentation(std::string &outString);
211  static void goToNextCaracter(const std::string &inString, unsigned int &currentPosition);
212 
213  static unsigned int indentationNumber;
214 
215 };
216 
217 }
218 #endif
219 
220 #endif //DOXYGEN_NOTFOR_DEVEL
221 ///@endcond