iLand
xmlhelper.h
Go to the documentation of this file.
1/********************************************************************************************
2** iLand - an individual based forest landscape and disturbance model
3** http://iland-model.org
4** Copyright (C) 2009- Werner Rammer, Rupert Seidl
5**
6** This program is free software: you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation, either version 3 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program. If not, see <http://www.gnu.org/licenses/>.
18********************************************************************************************/
19
20#ifndef XMLHELPER_H
21#define XMLHELPER_H
22
23#include <QtXml>
24
26{
27public:
28 XmlHelper();
29 ~XmlHelper();
30 XmlHelper(const QString &fileName) {loadFromFile(fileName);}
31 XmlHelper(QDomElement topNode);
32 void loadFromFile(const QString &fileName);
33 // relative top nodes
34 QDomElement top() const { return mTopNode;}
35 void setCurrentNode(const QString &path) { mCurrentTop = node(path); }
36 void setCurrentNode(const QDomElement &node) { mCurrentTop = node; }
38 bool isValid() const { return !mCurrentTop.isNull(); }
39 bool hasNode(const QString &path) const;
40 // read access
41 QDomElement node(const QString &path) const;
42 QString value(const QString &path, const QString &defaultValue="") const;
43 bool valueBool(const QString &path, const bool defaultValue=false) const;
44 double valueDouble(const QString &path, const double defaultValue=0.) const;
45 int valueInt(const QString &path, const int defaultValue=0) const;
46 // write access
47 bool setNodeValue(QDomElement &node, const QString &value);
48 bool setNodeValue(const QString &path, const QString &value);
49 // special parameters
50 double paramValue(const QString &paramName, const double defaultValue=0.) const;
51 QString paramValueString(const QString &paramName, const QString &defaultValue="") const;
52 bool paramValueBool(const QString &paramName, const bool &defaultValue=true) const;
53 // helpers
54 QStringList dump(const QString &path, int levels=-1);
55private:
56 void dump_rec(QDomElement c, QStringList &stack, QStringList &out);
57 QDomDocument mDoc;
58 QDomElement mCurrentTop;
59 QDomElement mTopNode;
60 QHash<const QString, QString> mParamCache;
61};
62
63#endif // XMLHELPER_H
XmlHelper wraps a XML file and provides some convenient functions to retrieve values.
Definition: xmlhelper.h:26
XmlHelper()
Definition: xmlhelper.cpp:54
QStringList dump(const QString &path, int levels=-1)
Definition: xmlhelper.cpp:253
bool isValid() const
returns true if the current (relative!) node is valid (i.e. not null).
Definition: xmlhelper.h:38
QString paramValueString(const QString &paramName, const QString &defaultValue="") const
get value of special "parameter" space
Definition: xmlhelper.cpp:107
QDomElement node(const QString &path) const
retrieve node defined by path (see class description)
Definition: xmlhelper.cpp:176
XmlHelper(const QString &fileName)
Definition: xmlhelper.h:30
bool paramValueBool(const QString &paramName, const bool &defaultValue=true) const
get value of special "parameter" space
Definition: xmlhelper.cpp:114
bool valueBool(const QString &path, const bool defaultValue=false) const
retrieve value (as bool) from node path.
Definition: xmlhelper.cpp:142
bool setNodeValue(QDomElement &node, const QString &value)
set value of 'node'. return true on success.
Definition: xmlhelper.cpp:208
QDomElement top() const
Definition: xmlhelper.h:34
~XmlHelper()
Definition: xmlhelper.cpp:57
void setCurrentNode(const QDomElement &node)
sets node as the current (relative) top node.
Definition: xmlhelper.h:36
int valueInt(const QString &path, const int defaultValue=0) const
retrieve value (as int) from node path.
Definition: xmlhelper.cpp:169
double valueDouble(const QString &path, const double defaultValue=0.) const
retrieve value (as double) from node path.
Definition: xmlhelper.cpp:155
double paramValue(const QString &paramName, const double defaultValue=0.) const
get value of special "parameter" space
Definition: xmlhelper.cpp:101
QString value(const QString &path, const QString &defaultValue="") const
retrieve value (as string) from node path.
Definition: xmlhelper.cpp:129
void loadFromFile(const QString &fileName)
Definition: xmlhelper.cpp:70
void setCurrentNode(const QString &path)
sets path as the current (relative) node.
Definition: xmlhelper.h:35
bool hasNode(const QString &path) const
returns true if path exists.
Definition: xmlhelper.cpp:124