Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxXMLNodeWrapper.h"
33 
34 #include <QDomNode>
35 #include <QStringList>
36 #include <iostream>
37 
38 namespace cx
39 {
40 
41 XMLNodeAdder::XMLNodeAdder(QDomNode node) : mNode(node)
42 {
43 }
44 
46 {
47  QStringList pathList = path.split("/");
48  QDomElement current = mNode.toElement();
49 
50  if (current.isNull())
51  return XMLNodeAdder(current);
52 
53  for (int i = 0; i < pathList.size(); ++i)
54  {
55  QDomElement next = current.namedItem(pathList[i]).toElement();
56 
57  if (next.isNull())
58  {
59  next = mNode.ownerDocument().createElement(pathList[i]);
60  current.appendChild(next);
61  }
62 
63  current = next;
64  }
65 
66  return XMLNodeAdder(current);
67 }
68 
70 {
71  return mNode;
72 }
73 
74 QDomElement XMLNodeAdder::addTextToElement(QString name, QString text)
75 {
76  QDomElement element = this->addElement(name);
77  element.appendChild(this->document().createTextNode(text));
78  return element;
79 }
80 
81 QDomElement XMLNodeAdder::addElement(QString name)
82 {
83  QDomElement element = this->document().createElement(name);
84  mNode.appendChild(element);
85  return element;
86 }
87 
88 QDomDocument XMLNodeAdder::document()
89 {
90  return mNode.ownerDocument();
91 }
92 
96 
97 XMLNodeParser::XMLNodeParser(QDomNode node) : mNode(node)
98 {
99 }
100 
102 {
103  QStringList pathList = path.split("/");
104  QDomElement current = mNode.toElement();
105 
106  if (current.isNull())
107  return XMLNodeParser(current);
108 
109  for (int i = 0; i < pathList.size(); ++i)
110  {
111  QDomElement next = current.namedItem(pathList[i]).toElement();
112 
113  if (next.isNull())
114  return XMLNodeParser(QDomNode());
115 
116  current = next;
117  }
118 
119  return XMLNodeParser(current);
120 }
121 
123 {
124  return mNode.namedItem(name).toElement().text();
125 }
126 
127 double XMLNodeParser::parseDoubleFromElementWithDefault(QString name, double defaultValue)
128 {
129  QString text = mNode.namedItem(name).toElement().text();
130  bool ok;
131  double val = text.toDouble(&ok);
132  if (ok)
133  return val;
134  return defaultValue;
135 }
136 
138 {
139  QStringList retval;
140  for (QDomElement elem = mNode.firstChildElement(name);
141  !elem.isNull();
142  elem = elem.nextSiblingElement(name))
143  {
144  retval << elem.text();
145  }
146  return retval;
147 }
148 
149 std::vector<QDomElement> XMLNodeParser::getDuplicateElements(QString name)
150 {
151  std::vector<QDomElement> retval;
152  for (QDomElement elem = mNode.firstChildElement(name);
153  !elem.isNull();
154  elem = elem.nextSiblingElement(name))
155  {
156  retval.push_back(elem);
157  }
158  return retval;
159 }
160 
161 
162 QDomElement XMLNodeParser::parseElement(QString name)
163 {
164  return mNode.namedItem(name).toElement();
165 }
166 
168 {
169  return mNode;
170 }
171 
172 
173 } // 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)