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
67 QString
XmlHandler::nodeToString(QDomNode node
, int level
)
69 QString indent_txt
= "";
70 for (int i
= 0; i
< level
; ++i
) {
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";
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";
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
);
109 QString
XmlHandler::getXmlSection(QString name
) {
110 QStringList tags
= name
.toLower().split(tr("/"), QString::SkipEmptyParts
);
111 QDomElement element
= m_XmlDoc
.documentElement();
113 QString section_text
= tr("");
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) {
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
) {
133 section_text
= element
.text();
138 void XmlHandler::setXmlSection(QString name
, QString contents
) {
139 QStringList tags
= name
.toLower().split(tr("/"), QString::SkipEmptyParts
);
140 QDomElement element
= m_XmlDoc
.documentElement();
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("'"));
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
) {
167 QStringList
XmlHandler::allKeys() {
170 dom_node
= m_XmlDoc
.firstChild();
171 parseNode(dom_node
, ret
, dom_node
.nodeName());
175 QStringList
XmlHandler::childGroups() {
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();
187 QStringList
XmlHandler::childKeys() {
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
;
196 QString
XmlHandler::group(bool absolute
) {
198 return m_DomNode
.nodeName();
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();
211 void XmlHandler::setGroup(const QString
& 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
) {
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;
251 void XmlHandler::resetToTopNode() {
252 m_DomNode
= m_XmlDoc
.firstChild();
255 void XmlHandler::resetXmlDoc() {
257 QDomElement root
= m_XmlDoc
.createElement(m_TagName
);
258 m_XmlDoc
.appendChild(root
);