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