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 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 #include <QDomDocument>
26 #include <QMessageBox>
29 /** A class to simplify the handling of XML files. */
30 class XmlHandler
: public QObject
{
36 QDomDocument m_XmlDoc
; ///< XML document describing the templates to use
37 QObject
*m_parent
;///< Parent widget (for message boxes)
38 QDomNode m_DomNode
;///< current node in the DOM tree
44 QDomNode
getDomNode() { return m_DomNode
; }///< Returns m_DomNode
46 QString
nodeToString(QDomNode node
, int level
);
51 XmlHandler(QString tagName
, QObject
*parent
= 0);///< Constructor
52 bool openXml(QString file_name
);///< Open XML file
53 bool saveXml(QString file_name
);///< Save XML file
54 QString
getXmlSection(QString name
);///< get contents of XML section
55 void setXmlSection(QString name
, QString contents
);///< set contents of XML section
56 void resetXmlDoc();///< Initialize or reset m_XmlDoc
57 QString
getBuffer(int indent
= 1) { return m_XmlDoc
.toString(indent
); }
58 QDomDocument
* getXmlDoc() {return &m_XmlDoc
;}
60 /// Returns a list of all keys, including subkeys, that can be read using the XmlHandler object.
61 QStringList
allKeys();
63 /// Returns a list of all key top-level groups that contain keys that can be read using the XmlHandler object.
64 QStringList
childGroups();
66 /// Returns a list of all top-level keys that can be read using the XmlHandler object.
67 QStringList
childKeys();
69 /// Returns the current group.
70 QString
group(bool absolute
= true);
72 ///Appends prefix to the current group.
73 void beginGroup(const QString
& prefix
);
75 /// Resets the group to what it was before the corresponding beginGroup() call.
78 void resetToTopNode();///< Reset to top node
79 void setGroup(const QString
& prefix
);///< Set current group
81 /** Parses a QDomNode for text nodes.
82 * @param dom_node QDomNode to parse
83 * @param string_list QStringList in which to store the paths containing text
84 * @param stop_node node at which to stop working up the path. ex: if the full path of a located text node is "#document/A/B/C/D/" and stop_node="B" then "C/D/" will be put into string_list (and eventually returned if dom_node is a text node)
85 * @return If dom_node is a text node, returns the path to it, otherwise returns an empty string.
87 QString
parseNode(const QDomNode
& dom_node
, QStringList
& string_list
, QString stop_node
);