Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OXMLTreeManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #define FALSE false
21 #define TRUE true
22 
23 #include "Tympan/core/defines.h"
24 
25 #include <qtextstream.h>
26 #include <qfile.h>
27 #include <qstring.h>
28 #include "OXMLTreeManager.h"
29 
30 #ifndef SAFE_DELETE
34  #define SAFE_DELETE(_p) \
35  { \
36  if ((_p) != NULL) \
37  { \
38  delete (_p); \
39  (_p) = NULL; \
40  } \
41  }
42 #endif
43 
44 //----------------------------------------------------------------------------
45 // public methods
46 //----------------------------------------------------------------------------
47 
49 {
50  m_pDomDocument = NULL;
51  m_CurrentDomNode.clear(); // NULL node
52 }
53 
55 {
56  if (m_pDomDocument != NULL)
57  {
58  delete m_pDomDocument;
59  m_pDomDocument = NULL;
60  m_CurrentDomNode.clear(); // NULL node
61  }
62 }
63 
64 bool OXMLTreeManager::create(const QString& docname)
65 {
66  if (m_pDomDocument != NULL)
67  {
68  delete m_pDomDocument;
69  m_pDomDocument = NULL;
70  m_CurrentDomNode.clear(); // NULL node
71  }
72 
73  m_pDomDocument = new QDomDocument(docname);
74  if (m_pDomDocument == NULL)
75  {
76  return FALSE;
77  }
78 
79  QDomElement elem = m_pDomDocument->createElement(docname);
80  QDomNode resultnode;
81 
82  resultnode = m_pDomDocument->appendChild(elem);
83 
85  return (!resultnode.isNull());
86 }
87 
88 bool OXMLTreeManager::load(const QString& filename)
89 {
90  if (m_pDomDocument != NULL)
91  {
93  m_CurrentDomNode.clear(); // NULL node
94  }
95 
96  m_pDomDocument = new QDomDocument(filename);
97  if (m_pDomDocument == NULL)
98  {
99  return FALSE;
100  }
101 
102  QFile filehandle(filename);
103 
104  if (filehandle.open(QIODevice::ReadOnly) == FALSE)
105  {
107  return FALSE;
108  }
109 
110  if (m_pDomDocument->setContent(&filehandle) == FALSE)
111  {
113  filehandle.close();
114  return FALSE;
115  }
116 
117  filehandle.close();
118 
120 
121  return TRUE;
122 }
123 
124 bool OXMLTreeManager::load(const QByteArray& bytearray)
125 {
126  if (m_pDomDocument != NULL)
127  {
129  m_CurrentDomNode.clear(); // NULL node
130  }
131 
132  m_pDomDocument = new QDomDocument("");
133  if (m_pDomDocument == NULL)
134  {
135  return FALSE;
136  }
137 
138  if (m_pDomDocument->setContent(bytearray) == FALSE)
139  {
141  return FALSE;
142  }
143 
145  return TRUE;
146 }
147 
148 bool OXMLTreeManager::save(const QString& filename)
149 {
150  if (m_pDomDocument == NULL)
151  {
152  return FALSE;
153  }
154 
155  QDomNode* rootnode = (QDomNode*)m_pDomDocument;
156  if (rootnode->isNull() == TRUE)
157  {
158  return FALSE;
159  }
160 
161  QFile filehandle(filename);
162 
163  if (filehandle.open(QIODevice::WriteOnly) == FALSE)
164  {
165 
166  return FALSE;
167  }
168 
169  QTextStream textstream(&filehandle);
170 
171  rootnode->save(textstream, 0);
172 
173  filehandle.close();
174 
175  return TRUE;
176 }
177 
178 bool OXMLTreeManager::isFieldPresent(const QString& field)
179 {
180  QDomAttr resultattribute;
181 
182  return findField(field, getCurrentDomNode(), resultattribute);
183 }
184 
185 bool OXMLTreeManager::getFieldType(const QString& field, FIELDTYPE& type)
186 {
187  bool bConv = false;
188  bool bFound = false;
189  QDomAttr resultattribute;
190  QString value;
191 
192  bFound = findField(field, getCurrentDomNode(), resultattribute);
193  value = resultattribute.value();
194  value.toFloat(&bConv);
195  if (bConv == TRUE)
196  {
197  type = FLOAT;
198  }
199  else
200  {
201  value.toLong(&bConv);
202  if (bConv == TRUE)
203  {
204  type = INTEGER;
205  }
206  else
207  {
208  type = STRING;
209  }
210  }
211  return bFound;
212 }
213 
214 bool OXMLTreeManager::addField(const QString& field, long value)
215 {
216  bool bFound = TRUE;
217 
218  if (m_pDomDocument == NULL)
219  {
220  return FALSE;
221  }
222 
223  getCurrentDomNode().toElement().setAttribute(field, (qlonglong)value);
224 
225  return bFound;
226 }
227 
228 bool OXMLTreeManager::addField(const QString& field, float value)
229 {
230  bool bFound = TRUE;
231 
232  if (m_pDomDocument == NULL)
233  {
234  return FALSE;
235  }
236 
237  getCurrentDomNode().toElement().setAttribute(field, value);
238 
239  return bFound;
240 }
241 
242 bool OXMLTreeManager::addField(const QString& field, const QString& value)
243 {
244  bool bFound = TRUE;
245 
246  if (m_pDomDocument == NULL)
247  {
248  return FALSE;
249  }
250 
251  getCurrentDomNode().toElement().setAttribute(field, value);
252 
253  return bFound;
254 }
255 
256 bool OXMLTreeManager::getFieldLongValue(const QString& field, long& value)
257 {
258  bool bFound = false;
259  QDomAttr resultattribute;
260 
261  bFound = findField(field, getCurrentDomNode(), resultattribute);
262  if (bFound == TRUE)
263  {
264  value = resultattribute.value().toLong();
265  }
266 
267  return bFound;
268 }
269 
270 bool OXMLTreeManager::getFieldFloatValue(const QString& field, float& value)
271 {
272  bool bFound = false;
273  QDomAttr resultattribute;
274 
275  bFound = findField(field, getCurrentDomNode(), resultattribute);
276  if (bFound == TRUE)
277  {
278  value = resultattribute.value().toFloat();
279  }
280 
281  return bFound;
282 }
283 
284 bool OXMLTreeManager::getFieldStringValue(const QString& field, QString& value)
285 {
286  bool bFound = false;
287  QDomAttr resultattribute;
288 
289  bFound = findField(field, getCurrentDomNode(), resultattribute);
290  if (bFound == TRUE)
291  {
292  value = resultattribute.value();
293  }
294 
295  return bFound;
296 }
297 
298 bool OXMLTreeManager::setFieldLongValue(const QString& field, long value)
299 {
300  bool bFound = TRUE;
301 
302  getCurrentDomNode().toElement().setAttribute(field, (qlonglong)value);
303 
304  return bFound;
305 }
306 
307 bool OXMLTreeManager::setFieldFloatValue(const QString& field, float value)
308 {
309  bool bFound = TRUE;
310 
311  getCurrentDomNode().toElement().setAttribute(field, value);
312 
313  return bFound;
314 }
315 
316 bool OXMLTreeManager::setFieldStringValue(const QString& field, const QString& value)
317 {
318  bool bFound = TRUE;
319 
320  getCurrentDomNode().toElement().setAttribute(field, value);
321 
322  return bFound;
323 }
324 
325 bool OXMLTreeManager::isDirectoryPresent(const QString& directory, bool absolute, int index)
326 {
327  QDomNode startnode;
328 
329  return findDirectory(directory, startnode, absolute, index);
330 }
331 
332 bool OXMLTreeManager::setCurrentDirectory(const QString& directory, bool absolute, int index)
333 {
334  bool bSuccess = false;
335  QDomNode startnode;
336 
337  bSuccess = findDirectory(directory, startnode, absolute, index);
338  if (bSuccess == TRUE)
339  {
340  setCurrentDomNode(startnode);
341  }
342  return bSuccess;
343 }
344 
346 {
347  int i = 0;
348  signed long currentindex = index;
349  QDomNode foundnode;
350  QDomNodeList childnodes = getCurrentDomNode().childNodes();
351 
352  i = 0;
353  while ((i < childnodes.count()) && (currentindex >= 0))
354  {
355  QDomNode readnode = childnodes.item(i);
356  QDomElement e = readnode.toElement(); // try to convert the node to an element.
357  if (e.isNull() == FALSE)
358  {
359  if (currentindex == 0)
360  {
361  foundnode = readnode;
362  }
363  currentindex--;
364  }
365  i++;
366  }
367 
368  if (currentindex == -1)
369  {
370  setCurrentDomNode(foundnode);
371  return TRUE;
372  }
373  else
374  {
375  return FALSE;
376  }
377 }
378 
380 {
381  bool bSuccess = FALSE;
382  QDomNode startnode = getCurrentDomNode();
383 
384  if ((startnode.isNull() == FALSE) && (isRoot(startnode) == FALSE))
385  {
386  setCurrentDomNode(startnode.parentNode());
387  bSuccess = TRUE;
388  }
389  return bSuccess;
390 }
391 
393 {
394  QString path = "";
395  QDomNode node = getCurrentDomNode();
396 
397  while ((node.isNull() == FALSE) && (isRoot(node) == FALSE))
398  {
399  QDomElement e = node.toElement(); // try to convert the node to an element.
400  if (!e.isNull())
401  {
402  path = e.tagName() + "." + path;
403  }
404  node = node.parentNode();
405  }
406  return path;
407 }
408 
410 {
411  QString path = "";
412  QDomNode node = getCurrentDomNode();
413 
414  if ((node.isNull() == FALSE) && (isRoot(node) == FALSE))
415  {
416  QDomElement e = node.toElement(); // try to convert the node to an element.
417  if (!e.isNull())
418  {
419  path = e.tagName();
420  }
421  }
422  return path;
423 }
424 
425 unsigned long OXMLTreeManager::getSubDirectoryCount(const QString& directory)
426 {
427  int count = 0;
428  QDomNode node = getCurrentDomNode();
429  QDomNodeList childnodes;
430 
431  childnodes = node.childNodes();
432 
433  count = 0;
434 
435  for (int i = 0; i < childnodes.count(); i++)
436  {
437  QDomNode readnode = childnodes.item(i);
438  QDomElement e = readnode.toElement(); // try to convert the node to an element.
439  if (e.isNull() == FALSE)
440  {
441  if ((directory.isNull()) || (e.tagName() == directory))
442  {
443  count++;
444  }
445  }
446  }
447 
448  return count;
449 }
450 
452 {
453  QDomNode node = getCurrentDomNode();
454  QDomNamedNodeMap namednodemap = node.toElement().attributes();
455 
456  return namednodemap.length();
457 }
458 
459 bool OXMLTreeManager::addDirectory(const QString& directory)
460 {
461  QDomNode foundnode;
462  QDomNode resultnode;
463 
464  if (m_pDomDocument == NULL)
465  {
466  return FALSE;
467  }
468 
469  if (findDirectory(directory, foundnode, FALSE) == FALSE)
470  {
471  // code to add directory here
472  QDomElement elem = m_pDomDocument->createElement(directory);
473  resultnode = getCurrentDomNode().appendChild(elem);
474  }
475 
476  return (!resultnode.isNull());
477 }
478 
479 bool OXMLTreeManager::remDirectory(const QString& directory)
480 {
481  QDomNode foundnode;
482  QDomNode resultnode;
483 
484  if (findDirectory(directory, foundnode, FALSE))
485  {
486  // code to delete directory here
487  resultnode = getCurrentDomNode().removeChild(foundnode);
488  }
489 
490  return (resultnode.isNull());
491 }
492 
493 //----------------------------------------------------------------------------
494 // private methods
495 //----------------------------------------------------------------------------
496 
497 bool OXMLTreeManager::findDirectory(const QString& directory, QDomNode& foundnode, bool absolute, int index)
498 {
499  bool bError = FALSE;
500  QDomNode startnode;
501  QString workname = directory;
502 
503  if (absolute == TRUE)
504  {
505  startnode = getRootDomNode();
506  }
507  else
508  {
509  startnode = getCurrentDomNode();
510  }
511 
512  while ((workname != "") && (bError == FALSE))
513  {
514  // Firstly, we extract the name of the first node to be searched for
515  signed long dotindex = 0;
516  QString currentsearch;
517 
518  dotindex = workname.indexOf(".");
519  if (dotindex == -1)
520  {
521  currentsearch = workname;
522  workname = "";
523  }
524  else
525  {
526  currentsearch = workname;
527  currentsearch.truncate(dotindex);
528  workname.remove(0, dotindex + 1);
529  }
530  // Now we parse the elements to find if one is matching the current name
531  QDomNode node = startnode.firstChild();
532 
533  bError = TRUE;
534  while ((node.isNull() == FALSE) && (bError == TRUE))
535  {
536  QDomElement e = node.toElement(); // try to convert the node to an element.
537  if (e.isNull() == FALSE)
538  {
539  if (e.tagName() == currentsearch)
540  {
541  startnode = node;
542  if ((dotindex == -1) && (index > 0))
543  {
544  index--; // Don't stop the research by keeping bError to TRUE
545  }
546  else
547  {
548  bError = FALSE;
549  }
550  }
551  }
552  node = node.nextSibling();
553  }
554  }
555 
556  if (bError == FALSE)
557  {
558  foundnode = startnode;
559  }
560  else
561  {
562  foundnode.clear();
563  }
564  return (!bError);
565 }
566 
567 bool OXMLTreeManager::findField(const QString& field, const QDomNode& currentnode, QDomAttr& attribute)
568 {
569  QDomNode readnode = currentnode;
570  QDomElement currentelement = readnode.toElement();
571  bool bFound = currentelement.hasAttribute(field);
572 
573  attribute = currentelement.attributeNode(field);
574 
575  return bFound;
576 }
#define TRUE
#define FALSE
#define SAFE_DELETE(_p)
bool findField(const QString &field, const QDomNode &currentnode, QDomAttr &attribute)
unsigned long getFieldCount()
bool getFieldFloatValue(const QString &field, float &value)
QString getCurrentSubDirectory()
QDomDocument * m_pDomDocument
static bool isRoot(const QDomNode &node)
bool setFieldStringValue(const QString &field, const QString &value)
void setCurrentDomNode(const QDomNode &node)
unsigned long getSubDirectoryCount(const QString &directory=QString())
bool setCurrentDirectory(const QString &directory, bool absolute=false, int index=0)
bool setFieldFloatValue(const QString &field, float value)
QString getCurrentDirectory()
bool findDirectory(const QString &directory, QDomNode &foundnode, bool absolute, int index=0)
bool isFieldPresent(const QString &field)
bool setCurrentSubDirectory(int index)
bool getFieldType(const QString &field, FIELDTYPE &type)
virtual bool load(const QString &filename)
bool getFieldStringValue(const QString &field, QString &value)
bool isDirectoryPresent(const QString &directory, bool absolute=false, int index=0)
QDomNode getCurrentDomNode()
virtual bool save(const QString &filename)
bool addField(const QString &field, long value)
bool create(const QString &docname)
bool getFieldLongValue(const QString &field, long &value)
bool addDirectory(const QString &directory)
QDomNode m_CurrentDomNode
bool remDirectory(const QString &directory)
virtual ~OXMLTreeManager()
bool setFieldLongValue(const QString &field, long value)
QDomNode getRootDomNode()