675 lines
19 KiB
C++
675 lines
19 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/XmlParser.h
|
|
*
|
|
* @brief Header file for XmlParser.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_XML_PARSER_H)
|
|
#define ACTIV_BASE_XML_PARSER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/MessageValidator.h"
|
|
#include "ActivMiddleware/CxxYZ/functional.h"
|
|
#include "ActivMiddleware/CxxYZ/memory.h"
|
|
#include "ActivMiddleware/CxxYZ/string_view.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(exception/all.hpp)
|
|
|
|
namespace Activ
|
|
{
|
|
|
|
namespace TinyXml
|
|
{
|
|
struct pImplDoc;
|
|
struct pImplAttr;
|
|
struct pImplNode;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! XML parser class.
|
|
class XmlParser
|
|
{
|
|
public:
|
|
typedef TinyXml::pImplAttr BaseXmlAttr;
|
|
typedef TinyXml::pImplNode BaseXmlNode;
|
|
typedef TinyXml::pImplDoc BaseXmlDoc;
|
|
typedef char BaseXmlChar;
|
|
|
|
#if (defined ACTIV_COMPILER_VISUAL_C)
|
|
typedef LengthToIntegralType<sizeof(size_t), true>::Type ssize_t;
|
|
#endif
|
|
|
|
//! Exception thrown on parsing and validation errors.
|
|
class Exception : public virtual Boost::exception, public virtual std::exception
|
|
{
|
|
public:
|
|
typedef Boost::error_info<class NameTag, String> NameInfo;
|
|
typedef Boost::error_info<class NodeTag, String> NodeInfo;
|
|
typedef Boost::error_info<class AttributeTag, String> AttributeInfo;
|
|
typedef Boost::error_info<class ValueTag, String> ValueInfo;
|
|
typedef Boost::error_info<class ErrorTag, String> ErrorInfo;
|
|
typedef Boost::error_info<class LineTag, size_t> LineInfo;
|
|
|
|
// std::exception overrides.
|
|
ACTIV_BASE_API virtual const char *what() const throw() ACTIV_OVERRIDE;
|
|
};
|
|
|
|
class Children;
|
|
class Attributes;
|
|
|
|
//! Node.
|
|
class Node
|
|
{
|
|
public:
|
|
//! Get the name of the node.
|
|
ACTIV_BASE_API String GetName() const;
|
|
|
|
//! Get children of the node.
|
|
ACTIV_BASE_API Children GetChildren() const;
|
|
|
|
// Is a node NODE_TYPE_ELEMENT?
|
|
ACTIV_BASE_API bool IsElement() const;
|
|
|
|
//! Get the value of the node.
|
|
ACTIV_BASE_API String GetValue() const;
|
|
|
|
//! Get text.
|
|
ACTIV_BASE_API String GetText() const;
|
|
|
|
//! Get a range of attributes for the node.
|
|
ACTIV_BASE_API Attributes GetAttributes() const;
|
|
|
|
/**
|
|
* @brief Get an attribute string by name.
|
|
*
|
|
* @tparam MANDATORY whether to throw if the attribute is not found.
|
|
* @param attributeName
|
|
* @param value to receive the attribute's value.
|
|
*
|
|
* @retval true if attribute was found.
|
|
* @retval false if MANDATORY was false and the attribute was not found.
|
|
* @throw Exception MANDATORY was true and the attribute was not found.
|
|
*/
|
|
template<bool MANDATORY>
|
|
bool GetAttribute(const String &attributeName, String &value) const;
|
|
|
|
/**
|
|
* @brief Get a boolean attribute by name.
|
|
*
|
|
* @tparam MANDATORY whether to throw if the attribute is not found.
|
|
* @param attributeName
|
|
* @param value to receive the attribute's value.
|
|
*
|
|
* @retval true if attribute was found.
|
|
* @retval false if MANDATORY was false and the attribute was not found.
|
|
* @throw Exception MANDATORY was true and the attribute was not found.
|
|
*/
|
|
template<bool MANDATORY>
|
|
bool GetAttribute(const String &attributeName, bool &value) const;
|
|
|
|
/**
|
|
* @brief Get an unsigned integral attribute by name.
|
|
*
|
|
* @tparam MANDATORY whether to throw if the attribute is not found.
|
|
* @param attributeName
|
|
* @param value to receive the attribute's value.
|
|
*
|
|
* @retval true if attribute was found.
|
|
* @retval false if MANDATORY was false and the attribute was not found.
|
|
* @throw Exception MANDATORY was true and the attribute was not found, or the attributes contents were not
|
|
* an unsigned integral.
|
|
*/
|
|
template <bool MANDATORY, typename T>
|
|
typename Boost::enable_if_c<Boost::is_integral<T>::value && Boost::is_unsigned<T>::value, bool>::type
|
|
GetAttribute(const String &attributeName, T &value) const
|
|
{
|
|
// vc100 gets upset if this is out-of-line.
|
|
String str;
|
|
if (!GetAttribute<MANDATORY>(attributeName, str))
|
|
return false;
|
|
|
|
MessageValidator messageValidator(str.c_str(), str.length());
|
|
|
|
if (STATUS_CODE_SUCCESS != messageValidator.ValidateUnsignedAsciiIntegral(messageValidator.GetLength(), value))
|
|
{
|
|
Exception exception(MakeException(attributeName, "invalid format"));
|
|
exception << Exception::ValueInfo(str);
|
|
ACTIV_BOOST_THROW_EXCEPTION(exception);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief Get a mandatory attribute by name.
|
|
*
|
|
* @param attributeName
|
|
*
|
|
* @retval true if attribute was found.
|
|
* @retval false if MANDATORY was false and the attribute was not found.
|
|
* @throw Exception MANDATORY was true and the attribute was not found, or the attributes contents were not
|
|
* an unsigned integral.
|
|
*/
|
|
template <typename T>
|
|
T GetAttribute(const String &attributeName) const
|
|
{
|
|
T value;
|
|
(void) GetAttribute<true>(attributeName, value);
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* @brief Get an attribute's value by name and apply a conversion function.
|
|
*
|
|
* @tparam MANDATORY whether to throw if the attribute is not found.
|
|
* @param attributeName
|
|
* @param convertFunction the function to apply. It must take const String & and return T.
|
|
* @param value to receive the attribute's value.
|
|
*
|
|
* @retval true if attribute was found.
|
|
* @retval false if MANDATORY was false and the attribute was not found.
|
|
* @throw Exception MANDATORY was true and the attribute was not found. The conversion function may throw other types.
|
|
*/
|
|
template<bool MANDATORY, typename T, typename F>
|
|
bool GetAttribute(const String &attributeName, const F &convertFunction, T &value) const;
|
|
|
|
private:
|
|
ACTIV_BASE_API bool GetMandatoryStringAttribute(const String &attributeName, String &value) const;
|
|
ACTIV_BASE_API bool GetOptionalStringAttribute(const String &attributeName, String &value) const;
|
|
|
|
template<bool MANDATORY>
|
|
bool GetBooleanAttribute(const String &attributeName, bool &value) const;
|
|
|
|
ACTIV_BASE_API Exception MakeException(const String &attributeName, const String &reason) const;
|
|
|
|
ACTIV_BASE_API Node(BaseXmlNode * pNode);
|
|
|
|
BaseXmlNode * m_pNode;
|
|
|
|
friend class XmlParser;
|
|
};
|
|
|
|
//! Attribute node.
|
|
class Attribute
|
|
{
|
|
public:
|
|
//! Get the name of the node.
|
|
ACTIV_BASE_API String GetName() const;
|
|
|
|
//! Get the attribute's value.
|
|
ACTIV_BASE_API String GetValue() const;
|
|
|
|
private:
|
|
ACTIV_BASE_API Attribute(BaseXmlAttr * pAttr);
|
|
|
|
BaseXmlAttr * m_pAttr;
|
|
|
|
friend class XmlParser;
|
|
};
|
|
|
|
//! Simple helper to provide begin/end range for child nodes.
|
|
class Children
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Iterator class.
|
|
*/
|
|
class Iterator : public std::iterator<std::bidirectional_iterator_tag, Node, ssize_t>
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
Iterator();
|
|
|
|
//! Pre-increment operator.
|
|
ACTIV_BASE_API Iterator &operator++();
|
|
|
|
//! Post-increment operator.
|
|
Iterator operator++(int);
|
|
|
|
//! Pre-decrement operator.
|
|
ACTIV_BASE_API Iterator &operator--();
|
|
|
|
//! Post-decrement operator.
|
|
Iterator operator--(int);
|
|
|
|
//! Equality operator.
|
|
bool operator==(const Iterator &rhs) const;
|
|
|
|
//! Inequality operator.
|
|
bool operator!=(const Iterator &rhs) const;
|
|
|
|
//! Deference operator.
|
|
const Node &operator*() const;
|
|
|
|
//! Deference operator.
|
|
Node &operator*();
|
|
|
|
//! Indirection operator.
|
|
const Node *operator->() const;
|
|
|
|
private:
|
|
Iterator(BaseXmlNode * const m_pParentNode, BaseXmlNode * const m_pNode);
|
|
|
|
BaseXmlNode * m_pParentNode;
|
|
Node m_node;
|
|
|
|
friend class XmlParser;
|
|
};
|
|
|
|
typedef Iterator const_iterator;
|
|
|
|
//! begin() const_iterator.
|
|
ACTIV_BASE_API const_iterator begin() const;
|
|
|
|
//! end() const_iterator.
|
|
ACTIV_BASE_API const_iterator end() const;
|
|
|
|
//! Is the range empty?
|
|
bool empty() const;
|
|
|
|
private:
|
|
Children(BaseXmlNode * pNode);
|
|
|
|
BaseXmlNode * m_pNode;
|
|
|
|
friend class Node;
|
|
};
|
|
|
|
// Simple helper to provide begin/end range for attribute nodes.
|
|
class Attributes
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Iterator class.
|
|
*/
|
|
class Iterator : public std::iterator<std::forward_iterator_tag, Attribute, ssize_t>
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
Iterator();
|
|
|
|
//! Pre-increment operator.
|
|
ACTIV_BASE_API Iterator &operator++();
|
|
|
|
//! Post-increment operator.
|
|
Iterator operator++(int);
|
|
|
|
//! Equality operator.
|
|
bool operator==(const Iterator &rhs) const;
|
|
|
|
//! Inequality operator.
|
|
bool operator!=(const Iterator &rhs) const;
|
|
|
|
//! Deference operator.
|
|
const Attribute &operator*() const;
|
|
|
|
//! Deference operator.
|
|
Attribute &operator*();
|
|
|
|
//! Indirection operator.
|
|
const Attribute *operator->() const;
|
|
|
|
private:
|
|
Iterator(BaseXmlAttr * pAttr);
|
|
|
|
Attribute m_attribute;
|
|
|
|
friend class XmlParser;
|
|
};
|
|
|
|
//! Attribute node const_iterator type.
|
|
typedef Iterator const_iterator;
|
|
|
|
//! Destructor.
|
|
ACTIV_BASE_API ~Attributes();
|
|
|
|
//! begin() const_iterator.
|
|
const_iterator begin() const;
|
|
|
|
//! end() const_iterator.
|
|
const_iterator end() const;
|
|
|
|
//! Is the range empty?
|
|
bool empty() const;
|
|
|
|
private:
|
|
Attributes(BaseXmlAttr * pAttrs);
|
|
|
|
BaseXmlAttr * m_pAttrs;
|
|
|
|
friend class Node;
|
|
};
|
|
|
|
/**
|
|
* @brief Constructor to parse an XML file.
|
|
*
|
|
* @param fileName
|
|
*
|
|
* @throw Exception
|
|
*/
|
|
ACTIV_BASE_API XmlParser(const String &fileName);
|
|
|
|
/**
|
|
* @brief Constructor to parse an XML string.
|
|
*
|
|
* @param name
|
|
* @param xmlString
|
|
*
|
|
* @throw Exception
|
|
*/
|
|
ACTIV_BASE_API XmlParser(const String &name, const CxxYZ::string_view &xmlString);
|
|
|
|
//! Get the root node.
|
|
ACTIV_BASE_API Node GetRoot() const;
|
|
|
|
private:
|
|
class Document
|
|
{
|
|
public:
|
|
ACTIV_BASE_API Document(const String &fileName);
|
|
ACTIV_BASE_API Document(const String &name, const CxxYZ::string_view &xmlString);
|
|
ACTIV_BASE_API ~Document();
|
|
Exception MakeException() const;
|
|
|
|
CxxYZ::unique_ptr<BaseXmlDoc> m_pDocument;
|
|
};
|
|
|
|
class XmlString
|
|
{
|
|
public:
|
|
XmlString();
|
|
XmlString(const BaseXmlChar * const pStr);
|
|
~XmlString();
|
|
|
|
const BaseXmlChar * const m_pStr;
|
|
};
|
|
|
|
Document m_document;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool MANDATORY>
|
|
inline bool XmlParser::Node::GetAttribute(const String &attributeName, String &value) const
|
|
{
|
|
if (MANDATORY)
|
|
return GetMandatoryStringAttribute(attributeName, value);
|
|
else
|
|
return GetOptionalStringAttribute(attributeName, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool MANDATORY>
|
|
inline bool XmlParser::Node::GetAttribute(const String &attributeName, bool &value) const
|
|
{
|
|
return GetBooleanAttribute<MANDATORY>(attributeName, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool MANDATORY, typename T, typename F>
|
|
bool XmlParser::Node::GetAttribute(const String &attributeName, const F &convertFunction, T &value) const
|
|
{
|
|
String str;
|
|
if (!GetAttribute<MANDATORY>(attributeName, str))
|
|
return false;
|
|
|
|
value = convertFunction(str);
|
|
|
|
return true;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool MANDATORY>
|
|
inline bool XmlParser::Node::GetBooleanAttribute(const String &attributeName, bool &value) const
|
|
{
|
|
String str;
|
|
if (!GetAttribute<MANDATORY>(attributeName, str))
|
|
return false;
|
|
|
|
value = (0 == ::stricmp(str.c_str(), "true"));
|
|
|
|
return true;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool XmlParser::Children::empty() const
|
|
{
|
|
return (begin() == end());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Children::Children(BaseXmlNode * pNode) :
|
|
m_pNode(pNode)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Children::Iterator::Iterator() :
|
|
m_pParentNode(ACTIV_NULLPTR),
|
|
m_node(ACTIV_NULLPTR)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Children::Iterator XmlParser::Children::Iterator::operator++(int)
|
|
{
|
|
Iterator initialValue(*this);
|
|
this->operator++();
|
|
|
|
return initialValue;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Children::Iterator XmlParser::Children::Iterator::operator--(int)
|
|
{
|
|
Iterator initialValue(*this);
|
|
this->operator--();
|
|
|
|
return initialValue;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool XmlParser::Children::Iterator::operator==(const XmlParser::Children::Iterator &rhs) const
|
|
{
|
|
return (m_node.m_pNode == rhs.m_node.m_pNode);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool XmlParser::Children::Iterator::operator!=(const XmlParser::Children::Iterator &rhs) const
|
|
{
|
|
return !(*this == rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const XmlParser::Node &XmlParser::Children::Iterator::operator*() const
|
|
{
|
|
return m_node;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Node &XmlParser::Children::Iterator::operator*()
|
|
{
|
|
return m_node;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const XmlParser::Node *XmlParser::Children::Iterator::operator->() const
|
|
{
|
|
return &m_node;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Children::Iterator::Iterator(BaseXmlNode * const m_pParentNode, XmlParser::BaseXmlNode * const pNode) :
|
|
m_pParentNode(m_pParentNode),
|
|
m_node(pNode)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Attributes::Iterator::Iterator() :
|
|
m_attribute(ACTIV_NULLPTR)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Attributes::Iterator XmlParser::Attributes::Iterator::operator++(int)
|
|
{
|
|
Iterator initialValue(*this);
|
|
this->operator++();
|
|
|
|
return initialValue;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool XmlParser::Attributes::Iterator::operator==(const XmlParser::Attributes::Iterator &rhs) const
|
|
{
|
|
return (m_attribute.m_pAttr == rhs.m_attribute.m_pAttr);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool XmlParser::Attributes::Iterator::operator!=(const XmlParser::Attributes::Iterator &rhs) const
|
|
{
|
|
return !(*this == rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const XmlParser::Attribute &XmlParser::Attributes::Iterator::operator*() const
|
|
{
|
|
return m_attribute;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Attribute &XmlParser::Attributes::Iterator::operator*()
|
|
{
|
|
return m_attribute;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const XmlParser::Attribute *XmlParser::Attributes::Iterator::operator->() const
|
|
{
|
|
return &m_attribute;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Attributes::Iterator::Iterator(XmlParser::BaseXmlAttr * pAttr) :
|
|
m_attribute(pAttr)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Attributes::const_iterator XmlParser::Attributes::begin() const
|
|
{
|
|
return const_iterator(m_pAttrs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Attributes::const_iterator XmlParser::Attributes::end() const
|
|
{
|
|
return const_iterator(ACTIV_NULLPTR);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool XmlParser::Attributes::empty() const
|
|
{
|
|
return (begin() == end());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline XmlParser::Attributes::Attributes(BaseXmlAttr * pAttrs) :
|
|
m_pAttrs(pAttrs)
|
|
{
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ACTIV_BOOST_NAMESPACE
|
|
{
|
|
|
|
// For BOOST_FOREACH support, mainly.
|
|
template<>
|
|
struct range_const_iterator<Activ::XmlParser::Children>
|
|
{
|
|
typedef Activ::XmlParser::Children::const_iterator type;
|
|
};
|
|
|
|
template<>
|
|
struct range_mutable_iterator<Activ::XmlParser::Children>
|
|
{
|
|
// There is no mutable iterator; use const.
|
|
typedef Activ::XmlParser::Children::const_iterator type;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<>
|
|
struct range_const_iterator<Activ::XmlParser::Attributes>
|
|
{
|
|
typedef Activ::XmlParser::Attributes::const_iterator type;
|
|
};
|
|
|
|
template<>
|
|
struct range_mutable_iterator<Activ::XmlParser::Attributes>
|
|
{
|
|
// There is no mutable iterator; use const.
|
|
typedef Activ::XmlParser::Attributes::const_iterator type;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace iterators
|
|
{
|
|
|
|
template<>
|
|
struct iterator_reference<Activ::XmlParser::Children::const_iterator>
|
|
{
|
|
typedef const Activ::XmlParser::Node &type;
|
|
};
|
|
|
|
template<>
|
|
struct iterator_reference<Activ::XmlParser::Attributes::const_iterator>
|
|
{
|
|
typedef const Activ::XmlParser::Attribute &type;
|
|
};
|
|
|
|
} // namespace iterators
|
|
|
|
} // namespace ACTIV_BOOST_NAMESPACE
|
|
|
|
#endif // (!defined ACTIV_BASE_XML_PARSER_H)
|