2014-03-17 15:01:58 +00:00
|
|
|
/*
|
|
|
|
* XmlReader.hpp, part of LatAnalyze 3
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 - 2014 Antonin Portelli
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_XmlReader_hpp_
|
|
|
|
#define Latan_XmlReader_hpp_
|
|
|
|
|
|
|
|
#include <LatAnalyze/Global.hpp>
|
|
|
|
#include <LatAnalyze/XML/tinyxml2.hpp>
|
|
|
|
#include <iostream>
|
2014-04-07 15:55:31 +01:00
|
|
|
#include <vector>
|
2014-03-17 15:01:58 +00:00
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
BEGIN_LATAN_NAMESPACE
|
2014-03-17 15:01:58 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* XML parameter file reader *
|
|
|
|
******************************************************************************/
|
2014-04-08 19:48:03 +01:00
|
|
|
typedef tinyxml2::XMLElement XmlNode;
|
|
|
|
|
2014-03-17 15:01:58 +00:00
|
|
|
class XmlReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// constructor
|
|
|
|
XmlReader(void) = default;
|
|
|
|
explicit XmlReader(const std::string &fileName);
|
|
|
|
// destructor
|
|
|
|
virtual ~XmlReader(void) = default;
|
|
|
|
// IO
|
|
|
|
void open(const std::string &fileName);
|
2014-04-08 19:48:03 +01:00
|
|
|
// XML structure access
|
2014-04-07 15:55:31 +01:00
|
|
|
template <typename... Strs>
|
2014-04-08 19:48:03 +01:00
|
|
|
static const XmlNode * getFirstNode(const XmlNode *startNode,
|
|
|
|
const std::string &nodeName,
|
2014-04-07 15:55:31 +01:00
|
|
|
Strs... nodeNames);
|
2014-04-08 19:48:03 +01:00
|
|
|
template <typename... Strs>
|
|
|
|
const XmlNode * getFirstNode(const std::string &nodeName,
|
|
|
|
Strs... nodeNames) const;
|
|
|
|
static const XmlNode * getNextNode(const XmlNode *node);
|
|
|
|
template <typename T>
|
|
|
|
static T getValue(const XmlNode *node);
|
|
|
|
template <typename T, typename... Strs>
|
|
|
|
static T getFirstValue(const XmlNode *startNode,
|
|
|
|
const std::string &nodeName, Strs... nodeNames);
|
|
|
|
template <typename T, typename... Strs>
|
|
|
|
T getFirstValue(const std::string &nodeName, Strs... nodeNames) const;
|
|
|
|
template <typename T, typename... Strs>
|
|
|
|
static std::vector<T> getAllValues(const XmlNode *startNode,
|
|
|
|
const std::string &nodeName,
|
|
|
|
Strs... nodeNames);
|
|
|
|
template <typename T, typename... Strs>
|
|
|
|
std::vector<T> getAllValues(const std::string &nodeName,
|
|
|
|
Strs... nodeNames) const;
|
|
|
|
private:
|
|
|
|
|
2014-03-17 15:01:58 +00:00
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
tinyxml2::XMLDocument doc_;
|
2014-04-08 19:48:03 +01:00
|
|
|
XmlNode *root_{nullptr};
|
2014-03-17 15:01:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* XmlReader template implementation *
|
|
|
|
******************************************************************************/
|
2014-04-08 19:48:03 +01:00
|
|
|
// XML structure access ////////////////////////////////////////////////////////
|
|
|
|
template <typename... Strs>
|
|
|
|
const XmlNode * XmlReader::getFirstNode(const XmlNode *startNode,
|
|
|
|
const std::string &nodeName,
|
|
|
|
Strs... nodeNames)
|
2014-04-07 15:55:31 +01:00
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
static_assert(static_or<std::is_assignable<std::string, Strs>::value...>::value,
|
|
|
|
"getFirstValue arguments are not compatible with std::string");
|
2014-04-07 15:55:31 +01:00
|
|
|
|
2014-04-08 19:48:03 +01:00
|
|
|
const unsigned int nName = sizeof...(nodeNames) + 1;
|
|
|
|
const std::string name[] = {nodeName, nodeNames...};
|
|
|
|
const XmlNode *node = startNode;
|
|
|
|
|
|
|
|
if (!node)
|
2014-04-07 15:55:31 +01:00
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
LATAN_ERROR(Io, "root node is null, no XML file opened");
|
2014-04-07 15:55:31 +01:00
|
|
|
}
|
2014-04-08 19:48:03 +01:00
|
|
|
for (unsigned int i = 0; i < nName; ++i)
|
2014-04-07 15:55:31 +01:00
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
node = node->FirstChildElement(name[i].c_str());
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Parsing, "XML node " + name[i] + " not found");
|
|
|
|
}
|
2014-04-07 15:55:31 +01:00
|
|
|
}
|
2014-04-08 19:48:03 +01:00
|
|
|
|
|
|
|
return node;
|
2014-04-07 15:55:31 +01:00
|
|
|
}
|
|
|
|
|
2014-04-08 19:48:03 +01:00
|
|
|
template <typename... Strs>
|
|
|
|
const XmlNode * XmlReader::getFirstNode(const std::string &nodeName,
|
|
|
|
Strs... nodeNames) const
|
2014-04-07 15:55:31 +01:00
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
if (!root_)
|
|
|
|
{
|
|
|
|
LATAN_ERROR(Io, "root node is null, no XML file opened");
|
|
|
|
}
|
2014-04-07 15:55:31 +01:00
|
|
|
|
2014-04-08 19:48:03 +01:00
|
|
|
return getFirstNode(root_, nodeName, nodeNames...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T XmlReader::getValue(const XmlNode *node)
|
|
|
|
{
|
|
|
|
if (node)
|
2014-04-07 15:55:31 +01:00
|
|
|
{
|
|
|
|
if (node->GetText())
|
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
return strTo<T>(node->GetText());
|
2014-04-07 15:55:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
return T();
|
2014-04-07 15:55:31 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-08 19:48:03 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return T();
|
|
|
|
}
|
2014-04-07 15:55:31 +01:00
|
|
|
}
|
|
|
|
|
2014-04-08 19:48:03 +01:00
|
|
|
template <typename T, typename... Strs>
|
|
|
|
T XmlReader::getFirstValue(const XmlNode *startNode,
|
|
|
|
const std::string &nodeName, Strs... nodeNames)
|
2014-03-17 15:01:58 +00:00
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
const XmlNode *node = getFirstNode(startNode, nodeName, nodeNames...);
|
2014-03-17 15:01:58 +00:00
|
|
|
|
2014-04-08 19:48:03 +01:00
|
|
|
return getValue<T>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Strs>
|
|
|
|
T XmlReader::getFirstValue(const std::string &nodeName, Strs... nodeNames) const
|
|
|
|
{
|
|
|
|
return getFirstValue<T>(root_, nodeName, nodeNames...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Strs>
|
|
|
|
std::vector<T> XmlReader::getAllValues(const XmlNode *startNode,
|
|
|
|
const std::string &nodeName,
|
|
|
|
Strs... nodeNames)
|
|
|
|
{
|
|
|
|
const XmlNode *node = getFirstNode(startNode, nodeName, nodeNames...);
|
|
|
|
std::vector<T> value;
|
2014-03-17 15:01:58 +00:00
|
|
|
|
2014-04-08 19:48:03 +01:00
|
|
|
while (node)
|
2014-03-17 15:01:58 +00:00
|
|
|
{
|
2014-04-08 19:48:03 +01:00
|
|
|
value.push_back(getValue<T>(node));
|
|
|
|
node = getNextNode(node);
|
2014-03-17 15:01:58 +00:00
|
|
|
}
|
2014-04-07 15:55:31 +01:00
|
|
|
|
2014-04-08 19:48:03 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Strs>
|
|
|
|
std::vector<T> XmlReader::getAllValues(const std::string &nodeName,
|
|
|
|
Strs... nodeNames) const
|
|
|
|
{
|
|
|
|
return getAllValues<T>(root_, nodeName, nodeNames...);
|
2014-03-17 15:01:58 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 17:15:05 +00:00
|
|
|
END_LATAN_NAMESPACE
|
2014-03-17 15:01:58 +00:00
|
|
|
|
|
|
|
#endif // Latan_XmlReader_hpp_
|