compiles on openSUSE 15.4 part 2
[engrid-github.git] / src / libengrid / xmlhandler.cpp
blobb06a7e0574e02c78a467521a62be20f812ea2a60
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "xmlhandler.h"
23 #include <QFile>
24 #include <QtDebug>
25 #include <QDir>
26 #include <QString>
27 #include <QMessageBox>
29 #include "engrid.h"
31 #include <iostream>
32 using namespace std;
34 XmlHandler::XmlHandler(QString tagName, QObject *parent)
35 : QObject(parent) {
36 m_parent = parent;
38 // initialise XML document
39 m_TagName = tagName;
40 QDomElement root = m_XmlDoc.createElement(m_TagName);
41 m_XmlDoc.appendChild(root);
44 bool XmlHandler::openXml(QString file_name) {
45 QFile xml_file(file_name);
46 if (!xml_file.open(QIODevice::ReadOnly)) {
47 QString error_message = "Failed to open xml_file " + xml_file.fileName();
48 error_message += QString("\n") + "QDir::current()=" + QDir::current().absolutePath();
49 error_message += QString("\n") + "QDir::currentPath()=" + QDir::currentPath();
51 qWarning() << error_message;
52 return(false);
54 if (!m_XmlDoc.setContent(&xml_file)) {
55 qWarning() << tr("Error reading XML file:\n").arg(file_name) << "\n setContent failed.";
56 return(false);
58 xml_file.close();
60 // initialize m_DomNode
61 resetToTopNode();
63 return(true);
67 QString XmlHandler::nodeToString(QDomNode node, int level)
69 QString indent_txt = "";
70 for (int i = 0; i < level; ++i) {
71 indent_txt += " ";
73 QString txt = "";
74 if (node.isElement()) {
75 QDomElement elem = node.toElement();
76 txt += indent_txt + "<" + elem.tagName() + ">\n";
77 QDomNodeList children = node.childNodes();
78 for (int i = 0; i < children.count(); ++i) {
79 txt += nodeToString(children.at(i), level + 1);
81 txt += indent_txt + "</" + elem.tagName() + ">\n";
83 if (node.isText()) {
84 QString raw = node.toText().data().trimmed();
85 QStringList lines = raw.split('\n');
86 foreach (QString line, lines) {
87 txt += indent_txt + line.trimmed() + "\n";
90 return txt;
93 bool XmlHandler::saveXml(QString file_name)
95 QDomNodeList cases = m_XmlDoc.elementsByTagName("engridcase");
96 if (cases.count() == 1) {
97 QString buffer = nodeToString(cases.at(0), 0);
99 QFile xml_file(file_name);
100 xml_file.open(QIODevice::WriteOnly | QIODevice::Text);
101 QTextStream f(&xml_file);
102 f << buffer << endl;
103 xml_file.close();
104 return(true);
106 return false;
109 QString XmlHandler::getXmlSection(QString name) {
110 QStringList tags = name.toLower().split(tr("/"), QString::SkipEmptyParts);
111 QDomElement element = m_XmlDoc.documentElement();
112 bool found = true;
113 QString section_text = tr("");
114 try {
115 foreach(QString tag, tags) {
116 QDomNodeList nodes = element.elementsByTagName(tag);
117 if (nodes.size() > 1) {
118 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
120 if (nodes.size() == 0) {
121 found = false;
122 break;
124 if (!nodes.at(0).isElement()) {
125 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
127 element = nodes.at(0).toElement();
129 } catch (Error err) {
130 err.display();
132 if (found) {
133 section_text = element.text();
135 return section_text;
138 void XmlHandler::setXmlSection(QString name, QString contents) {
139 QStringList tags = name.toLower().split(tr("/"), QString::SkipEmptyParts);
140 QDomElement element = m_XmlDoc.documentElement();
141 try {
142 foreach(QString tag, tags) {
143 QDomNodeList nodes = element.elementsByTagName(tag);
144 if (nodes.size() > 1) {
145 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
147 if (nodes.size() == 0) {
148 QDomElement new_element = m_XmlDoc.createElement(tag);
149 element.appendChild(new_element);
150 element = new_element;
151 } else if (!nodes.at(0).isElement()) {
152 EG_ERR_RETURN(tr("error retrieving XML section '") + name + tr("'"));
153 } else {
154 element = nodes.at(0).toElement();
157 while (element.hasChildNodes()) {
158 element.removeChild(element.firstChild());
160 QDomText text_node = m_XmlDoc.createTextNode(contents);
161 element.appendChild(text_node);
162 } catch (Error err) {
163 err.display();
167 QStringList XmlHandler::allKeys() {
168 QStringList ret;
169 QDomNode dom_node;
170 dom_node = m_XmlDoc.firstChild();
171 parseNode(dom_node, ret, dom_node.nodeName());
172 return(ret);
175 QStringList XmlHandler::childGroups() {
176 QStringList ret;
178 for (QDomNode sub_node = m_DomNode.firstChild(); !sub_node.isNull(); sub_node = sub_node.nextSibling()) {
179 if (sub_node.nodeType() == QDomNode::ElementNode) {
180 ret << sub_node.nodeName();
184 return(ret);
187 QStringList XmlHandler::childKeys() {
188 QStringList ret;
189 qDebug() << "m_DomNode.nodeName()=" << m_DomNode.nodeName();
190 QString output = parseNode(m_DomNode, ret, m_DomNode.nodeName());
191 qDebug() << "output=" << output;
192 qDebug() << "ret=" << ret;
193 return(ret);
196 QString XmlHandler::group(bool absolute) {
197 if (!absolute) {
198 return m_DomNode.nodeName();
199 } else {
200 QString section;
201 QString stop_node = m_XmlDoc.firstChild().nodeName();
202 QDomNode parent_node = m_DomNode;
203 while (!parent_node.isNull() && parent_node.nodeName() != stop_node) {
204 section = parent_node.nodeName() + tr("/") + section;
205 parent_node = parent_node.parentNode();
207 return section;
211 void XmlHandler::setGroup(const QString & prefix) {
212 resetToTopNode();
213 beginGroup(prefix);
216 void XmlHandler::beginGroup(const QString & prefix) {
217 QStringList tag_list = prefix.split(tr("/"));
218 foreach(QString tag, tag_list) {
219 if (!tag.isEmpty()) {
220 QDomElement dom_element = m_DomNode.toElement();
221 QDomNodeList dom_node_list = dom_element.elementsByTagName(tag);
222 m_DomNode = dom_node_list.at(0);
227 void XmlHandler::endGroup() {
228 if (!m_DomNode.parentNode().isNull()) m_DomNode = m_DomNode.parentNode();
231 QString XmlHandler::parseNode(const QDomNode& dom_node, QStringList& string_list, QString stop_node) {
232 QString section;
234 if (dom_node.nodeType() == QDomNode::TextNode) {
235 QDomNode parent_node = dom_node.parentNode();
236 while (!parent_node.isNull() && parent_node.nodeName() != stop_node) {
237 section = parent_node.nodeName() + tr("/") + section;
238 parent_node = parent_node.parentNode();
240 string_list << section;
243 for (QDomNode sub_node = dom_node.firstChild(); !sub_node.isNull(); sub_node = sub_node.nextSibling()) {
244 parseNode(sub_node, string_list, stop_node);
247 // qDebug() << "section=" << section;
248 return(section);
251 void XmlHandler::resetToTopNode() {
252 m_DomNode = m_XmlDoc.firstChild();
255 void XmlHandler::resetXmlDoc() {
256 m_XmlDoc.clear();
257 QDomElement root = m_XmlDoc.createElement(m_TagName);
258 m_XmlDoc.appendChild(root);