Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxXMLNodeWrapper.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxXMLNodeWrapper.h"
12 
13 #include <QDomNode>
14 #include <QStringList>
15 #include <iostream>
16 
17 namespace cx
18 {
19 
20 XMLNodeAdder::XMLNodeAdder(QDomNode node) : mNode(node)
21 {
22 }
23 
25 {
26  QStringList pathList = path.split("/");
27  QDomElement current = mNode.toElement();
28 
29  if (current.isNull())
30  return XMLNodeAdder(current);
31 
32  for (int i = 0; i < pathList.size(); ++i)
33  {
34  QDomElement next = current.namedItem(pathList[i]).toElement();
35 
36  if (next.isNull())
37  {
38  next = mNode.ownerDocument().createElement(pathList[i]);
39  current.appendChild(next);
40  }
41 
42  current = next;
43  }
44 
45  return XMLNodeAdder(current);
46 }
47 
49 {
50  return mNode;
51 }
52 
53 QDomElement XMLNodeAdder::addTextToElement(QString name, QString text)
54 {
55  QDomElement element = this->addElement(name);
56  element.appendChild(this->document().createTextNode(text));
57  return element;
58 }
59 
60 QDomElement XMLNodeAdder::addElement(QString name)
61 {
62  QDomElement element = this->document().createElement(name);
63  mNode.appendChild(element);
64  return element;
65 }
66 
67 QDomDocument XMLNodeAdder::document()
68 {
69  return mNode.ownerDocument();
70 }
71 
75 
76 XMLNodeParser::XMLNodeParser(QDomNode node) : mNode(node)
77 {
78 }
79 
81 {
82  QStringList pathList = path.split("/");
83  QDomElement current = mNode.toElement();
84 
85  if (current.isNull())
86  return XMLNodeParser(current);
87 
88  for (int i = 0; i < pathList.size(); ++i)
89  {
90  QDomElement next = current.namedItem(pathList[i]).toElement();
91 
92  if (next.isNull())
93  return XMLNodeParser(QDomNode());
94 
95  current = next;
96  }
97 
98  return XMLNodeParser(current);
99 }
100 
102 {
103  return mNode.namedItem(name).toElement().text();
104 }
105 
106 double XMLNodeParser::parseDoubleFromElementWithDefault(QString name, double defaultValue)
107 {
108  QString text = mNode.namedItem(name).toElement().text();
109  bool ok;
110  double val = text.toDouble(&ok);
111  if (ok)
112  return val;
113  return defaultValue;
114 }
115 
117 {
118  QStringList retval;
119  for (QDomElement elem = mNode.firstChildElement(name);
120  !elem.isNull();
121  elem = elem.nextSiblingElement(name))
122  {
123  retval << elem.text();
124  }
125  return retval;
126 }
127 
128 std::vector<QDomElement> XMLNodeParser::getDuplicateElements(QString name)
129 {
130  std::vector<QDomElement> retval;
131  for (QDomElement elem = mNode.firstChildElement(name);
132  !elem.isNull();
133  elem = elem.nextSiblingElement(name))
134  {
135  retval.push_back(elem);
136  }
137  return retval;
138 }
139 
140 
141 QDomElement XMLNodeParser::parseElement(QString name)
142 {
143  return mNode.namedItem(name).toElement();
144 }
145 
147 {
148  return mNode;
149 }
150 
151 
152 } // namespace cx
double parseDoubleFromElementWithDefault(QString name, double defaultValue)
QStringList parseTextFromDuplicateElements(QString name)
XMLNodeAdder descend(QString path)
QDomElement addElement(QString name)
XMLNodeAdder(QDomNode node)
std::vector< QDomElement > getDuplicateElements(QString name)
QDomElement addTextToElement(QString name, QString text)
XMLNodeParser descend(QString path)
QString parseTextFromElement(QString name)
XMLNodeParser(QDomNode node)
QDomElement parseElement(QString name)
Namespace for all CustusX production code.