1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
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. +
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. +
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/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "xmlhandler.h"
27 #include <QMessageBox>
34 XmlHandler::XmlHandler(QString tagName
, QObject
*parent
)
38 // initialise XML document
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
;
54 if (!m_XmlDoc
.setContent(&xml_file
)) {
55 qWarning() << tr("Error reading XML file:\n").arg(file_name
) << "\n setContent failed.";
60 // initialize m_DomNode
66 bool XmlHandler::saveXml(QString file_name
) {
67 QString buffer
= m_XmlDoc
.toString(2);
68 // QString buffer = m_XmlDoc.toString(0);
69 QFile
xml_file(file_name
);
70 xml_file
.open(QIODevice::WriteOnly
| QIODevice::Text
);
71 QTextStream
f(&xml_file
);
77 QString
XmlHandler::getXmlSection(QString name
) {
78 QStringList tags
= name
.toLower().split(tr("/"), QString::SkipEmptyParts
);
79 QDomElement element
= m_XmlDoc
.documentElement();
81 QString section_text
= tr("");
83 foreach(QString tag
, tags
) {
84 QDomNodeList nodes
= element
.elementsByTagName(tag
);
85 if (nodes
.size() > 1) {
86 EG_ERR_RETURN(tr("error retrieving XML section '") + name
+ tr("'"));
88 if (nodes
.size() == 0) {
92 if (!nodes
.at(0).isElement()) {
93 EG_ERR_RETURN(tr("error retrieving XML section '") + name
+ tr("'"));
95 element
= nodes
.at(0).toElement();
101 section_text
= element
.text();
106 void XmlHandler::setXmlSection(QString name
, QString contents
) {
107 QStringList tags
= name
.toLower().split(tr("/"), QString::SkipEmptyParts
);
108 QDomElement element
= m_XmlDoc
.documentElement();
110 foreach(QString tag
, tags
) {
111 QDomNodeList nodes
= element
.elementsByTagName(tag
);
112 if (nodes
.size() > 1) {
113 EG_ERR_RETURN(tr("error retrieving XML section '") + name
+ tr("'"));
115 if (nodes
.size() == 0) {
116 QDomElement new_element
= m_XmlDoc
.createElement(tag
);
117 element
.appendChild(new_element
);
118 element
= new_element
;
119 } else if (!nodes
.at(0).isElement()) {
120 EG_ERR_RETURN(tr("error retrieving XML section '") + name
+ tr("'"));
122 element
= nodes
.at(0).toElement();
125 while (element
.hasChildNodes()) {
126 element
.removeChild(element
.firstChild());
128 QDomText text_node
= m_XmlDoc
.createTextNode(contents
);
129 element
.appendChild(text_node
);
130 } catch (Error err
) {
135 QStringList
XmlHandler::allKeys() {
138 dom_node
= m_XmlDoc
.firstChild();
139 parseNode(dom_node
, ret
, dom_node
.nodeName());
143 QStringList
XmlHandler::childGroups() {
146 for (QDomNode sub_node
= m_DomNode
.firstChild(); !sub_node
.isNull(); sub_node
= sub_node
.nextSibling()) {
147 if (sub_node
.nodeType() == QDomNode::ElementNode
) {
148 ret
<< sub_node
.nodeName();
155 QStringList
XmlHandler::childKeys() {
157 qDebug() << "m_DomNode.nodeName()=" << m_DomNode
.nodeName();
158 QString output
= parseNode(m_DomNode
, ret
, m_DomNode
.nodeName());
159 qDebug() << "output=" << output
;
160 qDebug() << "ret=" << ret
;
164 QString
XmlHandler::group(bool absolute
) {
166 return m_DomNode
.nodeName();
169 QString stop_node
= m_XmlDoc
.firstChild().nodeName();
170 QDomNode parent_node
= m_DomNode
;
171 while (!parent_node
.isNull() && parent_node
.nodeName() != stop_node
) {
172 section
= parent_node
.nodeName() + tr("/") + section
;
173 parent_node
= parent_node
.parentNode();
179 void XmlHandler::setGroup(const QString
& prefix
) {
184 void XmlHandler::beginGroup(const QString
& prefix
) {
185 QStringList tag_list
= prefix
.split(tr("/"));
186 foreach(QString tag
, tag_list
) {
187 if (!tag
.isEmpty()) {
188 QDomElement dom_element
= m_DomNode
.toElement();
189 QDomNodeList dom_node_list
= dom_element
.elementsByTagName(tag
);
190 m_DomNode
= dom_node_list
.at(0);
195 void XmlHandler::endGroup() {
196 if (!m_DomNode
.parentNode().isNull()) m_DomNode
= m_DomNode
.parentNode();
199 QString
XmlHandler::parseNode(const QDomNode
& dom_node
, QStringList
& string_list
, QString stop_node
) {
202 if (dom_node
.nodeType() == QDomNode::TextNode
) {
203 QDomNode parent_node
= dom_node
.parentNode();
204 while (!parent_node
.isNull() && parent_node
.nodeName() != stop_node
) {
205 section
= parent_node
.nodeName() + tr("/") + section
;
206 parent_node
= parent_node
.parentNode();
208 string_list
<< section
;
211 for (QDomNode sub_node
= dom_node
.firstChild(); !sub_node
.isNull(); sub_node
= sub_node
.nextSibling()) {
212 parseNode(sub_node
, string_list
, stop_node
);
215 // qDebug() << "section=" << section;
219 void XmlHandler::resetToTopNode() {
220 m_DomNode
= m_XmlDoc
.firstChild();
223 void XmlHandler::resetXmlDoc() {
225 QDomElement root
= m_XmlDoc
.createElement(m_TagName
);
226 m_XmlDoc
.appendChild(root
);