Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces 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 #include <cassert>
30 
31 #include <tulip/tulipconf.h>
32 
33 namespace tlp {
34 
35 /**
36  * static tools class use to store/load xml data
37  */
38 class GlSimpleEntity;
39 
40 class TLP_GL_SCOPE GlXMLTools {
41 
42 public :
43 
44  /**
45  * Create and enter into a new data node
46  */
47  static void beginDataNode(std::string &outString);
48 
49  /**
50  * End last data node
51  */
52  static void endDataNode(std::string &outString);
53 
54  /**
55  * Enter into a data node and set the currentPosition integer to the position next the data tag
56  */
57  static void enterDataNode(const std::string &inString, unsigned int &currentPosition);
58 
59  /**
60  * Leave a data node and set the currentPosition integer to the position next the data end tag
61  */
62  static void leaveDataNode(const std::string &inString, unsigned int &currentPosition);
63 
64  /**
65  * Create and enter into a new children node with name : name
66  */
67  static void beginChildNode(std::string &outString, const std::string &name="children");
68 
69  /**
70  * End last children node with name : name
71  */
72  static void endChildNode(std::string &outString, const std::string &name="children");
73 
74  /**
75  * Enter into a child node and set the currentPosition integer to the position next the child tag
76  * \return child name
77  */
78  static std::string enterChildNode(const std::string &inString, unsigned int &currentPosition);
79 
80  /**
81  * Leave into a child node and set the currentPosition integer to the position next the child end tag
82  * \return child name
83  */
84  static void leaveChildNode(const std::string &inString, unsigned int &currentPosition, const std::string &childName);
85 
86  /**
87  * Create a property with name and value in rootNode
88  */
89  static void createProperty(std::string &outString, const std::string &name, const std::string &value, const std::string &parent="");
90 
91  /**
92  * Get properties associated with the last opened child
93  * These properties is returned into a map of string/string
94  */
95  static std::map<std::string,std::string> getProperties(const std::string &inString, unsigned int &currentPosition);
96 
97  /**
98  * Create a GlEntity with the given name
99  */
100  static GlSimpleEntity *createEntity(const std::string &name);
101 
102  /**
103  * Get the XML output for a vector of Object
104  */
105  template <typename Obj>
106  static void getXML(std::string &outString,const std::string &name,const typename std::vector<Obj> &vect) {
107 
108  std::stringstream str;
109  str << "(" ;
110  typename std::vector<Obj>::const_iterator it=vect.begin();
111  assert(it != vect.end());
112  str << *it ;
113  ++it;
114 
115  for(; it!=vect.end(); ++it) {
116  str << "," << *it ;
117  }
118 
119  str << ")" ;
120  outString.append("<"+name+">"+str.str()+"</"+name+">\n");
121  }
122 
123  /**
124  * Set vector of Object with the given XML
125  */
126  template <typename Obj>
127  static void setWithXML(const std::string &inString, unsigned int &currentPosition,const std::string &name,std::vector<Obj> &vect) {
128 
129  goToNextCaracter(inString,currentPosition);
130 
131  std::string nameTag=inString.substr(currentPosition,name.size()+2);
132  assert(nameTag=="<"+name+">");
133  currentPosition+=name.size()+2;
134 
135  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
136  assert(endValuePosition!=std::string::npos);
137 
138  std::istringstream is(inString.substr(currentPosition,endValuePosition-currentPosition));
139  Obj data;
140  char c=is.get();
141 
142  while(c!=')') {
143  is >> data ;
144  vect.push_back(data);
145  c=is.get();
146  }
147 
148  currentPosition=endValuePosition+name.size()+3;
149 
150  }
151 
152  /**
153  * Get the XML output for an Object
154  */
155  template <typename Obj>
156  static void getXML(std::string &outString, const std::string &name, const Obj &value) {
157  std::stringstream str;
158  str << value ;
159  applyIndentation(outString);
160  outString.append("<"+name+">"+str.str()+"</"+name+">\n");
161  }
162 
163 
164  static bool checkNextXMLtag(const std::string &inString, unsigned int &currentPosition, const std::string &name) {
165  unsigned int tmp=currentPosition;
166  goToNextCaracter(inString,tmp);
167  std::string nameTag=inString.substr(tmp,name.size()+2);
168  return (nameTag=="<"+name+">");
169  }
170 
171  /**
172  * Set an Object with the given XML
173  */
174  template <typename Obj>
175  static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value) {
176 
177  goToNextCaracter(inString,currentPosition);
178 
179  std::string nameTag=inString.substr(currentPosition,name.size()+2);
180  assert(nameTag=="<"+name+">");
181  currentPosition+=name.size()+2;
182 
183  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
184  assert(endValuePosition!=std::string::npos);
185 
186  std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
187  str >> value;
188  currentPosition=endValuePosition+name.size()+3;
189  }
190 
191  /**
192  * Set an Object with the given XML and default value
193  */
194  template <typename Obj>
195  static void setWithXML(const std::string &inString, unsigned int &currentPosition, const std::string &name, Obj &value,const Obj &defValue) {
196  goToNextCaracter(inString,currentPosition);
197 
198  std::string nameTag=inString.substr(currentPosition,name.size()+2);
199 
200  if(nameTag=="<"+name+">") {
201  currentPosition+=name.size()+2;
202 
203  size_t endValuePosition=inString.find("</"+name+">",currentPosition);
204  assert(endValuePosition!=std::string::npos);
205 
206  std::stringstream str(inString.substr(currentPosition,endValuePosition-currentPosition));
207  str >> value;
208  currentPosition=endValuePosition+name.size()+3;
209  }
210  else {
211  value=defValue;
212  }
213  }
214 
215 private :
216 
217  static void applyIndentation(std::string &outString);
218  static void goToNextCaracter(const std::string &inString, unsigned int &currentPosition);
219 
220  static unsigned int indentationNumber;
221 
222 };
223 
224 }
225 #endif
226 
227 #endif //DOXYGEN_NOTFOR_DEVEL
228 ///@endcond