8#include "Arp/System/Core/Enum.hxx"
9#include "Arp/System/Core/TypeName.hxx"
10#include "Arp/System/Commons/Chrono/Duration.hpp"
11#include "Arp/System/Commons/Exceptions/Exceptions.h"
12#include "Arp/System/Commons/Xml/XmlErrorHandler.hpp"
13#include "Arp/System/Commons/Xml/XmlNodeType.hpp"
14#include "Arp/Base/Core/Convert.hpp"
17#include "boost/lexical_cast.hpp"
19namespace Arp {
namespace System {
namespace Commons {
namespace Xml
22class LibXmlErrorHandler;
97 return boost::lexical_cast<T>(elementString);
99 catch (std::exception& e)
104 "Cannot convert '{}' to {}: {}", elementString,
TypeName<T>().GetFullName(), e.what());
123 if (TryGetAttributeValue<T>(attributeName, returnValue))
130 "GetAttributeValue failed for mandatory attribute: {0} elementName: {1}",
151 if (TryGetAttributeValueEnum<T>(attributeName, returnValue))
158 "GetAttributeValueEnum failed for mandatory attribute: {0} elementName: {1}",
178 if (!TryGetAttributeValueEnum<T>(attributeName, result))
181 "GetAttributeValueOfEnum failed for mandatory attribute: {0} elementName: {1}", attributeName,
GetElementLocalName());
203 char* charValue = GetAttributeValueInternal(attributeName);
204 if (charValue ==
nullptr)
208 std::istringstream iss(charValue);
210 FreeInternalAttributeValue(charValue);
231 char* charValue = GetAttributeValueInternal(attributeName);
232 if (charValue ==
nullptr)
238 FreeInternalAttributeValue(charValue);
262 value = defaultValue;
263 return TryGetAttributeValueEnum<T>(attributeName, value);
284 if (TryGetAttributeValueDuration<T>(attributeName, returnValue))
291 "GetAttributeValueDuration failed for mandatory attribute: {0} elementName: {1}",
334 value = defaultValue;
335 return TryGetAttributeValueDuration<T>(attributeName, value);
355 char* charValue =
nullptr;
358 charValue = GetAttributeValueInternal(attributeName);
359 if (charValue ==
nullptr)
364 value = boost::lexical_cast<T>(charValue);
365 FreeInternalAttributeValue(charValue);
368 catch (boost::bad_lexical_cast&)
371 String strValue(charValue);
372 FreeInternalAttributeValue(charValue);
373 throw XmlException(
"Cast of attribute {0} to {1} failed value: {2}", attributeName,
399 value = defaultValue;
400 return TryGetAttributeValue<T>(attributeName, value);
403 void EnableValidationException();
404 void DisableValidationException();
415 template<
typename... Args>
422 char* GetAttributeValueInternal(
const char* attributeName);
423 void FreeInternalAttributeValue(
char* attributeValue);
424 XmlNodeType MoveToContent();
425 void EnableValidation(
const String& xsdFile);
426 void CheckForValidationException();
427 bool MoveToNextElement(
void);
428 String GetAndResolveAttributeValueInternal(
const char* attributeName);
434 bool validationEnabled;
435 LibXmlErrorHandler* errorHandler;
436 bool exceptionOnValidationError;
439 bool startElementRead;
463template<
typename... Args>
491 "GetAndResolveAttributeValue failed for mandatory attribute: {0} elementName: {1}",
518 strValue = this->GetAndResolveAttributeValueInternal(attributeName);
520 if (strValue.IsEmpty())
525 value = Convert::ToValue<T>(strValue);
531 throw XmlException(
"Could not convert attribute {0} to {1} failed value: {2}", attributeName,
559 value = defaultValue;
Adapter class for enums to make them loggable and parsable from e.g. XML files.
Definition: Enum.hxx:21
This is the base class of all Arp exception classes.
Definition: Exception.hpp:21
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
This (meta programming) class provides the C++ type-name of the as template argument passed type.
Definition: TypeName.hxx:20
Definition: Duration.hpp:16
This exception is used for xml parsing errors.
Definition: XmlException.hpp:15
static XmlException Create(void)
Creates an XmlException instance using a default message text.
Definition: XmlException.cpp:47
Class to read an XML File. Non buffered reader, can only read forward
Definition: XmlReader.hpp:26
bool TryGetAttributeValueEnum(const char *attributeName, T &value)
trys reads an attribute value from the current element and converts the String to Enum<T>
Definition: XmlReader.hpp:201
XmlException CreateException(const char *message, const Args &... args)
Creates an exception by prefixing the given message with current doc name and line number
Definition: XmlReader.hpp:464
T ReadElementContent()
reads the content of the current element if a CDATA section is found, the CDATA enclosure is removed
Definition: XmlReader.hpp:92
T GetAttributeValue(const char *attributeName)
reads an attribute value from the current element
Definition: XmlReader.hpp:120
int GetCurrentDepth()
gets the current depth of the current element inside the xml document
Definition: XmlReader.cpp:357
bool Read()
Reads to the next element inside the xml file
Definition: XmlReader.cpp:215
Enum< T > GetAttributeValueEnum(const char *attributeName)
reads an attribute value from the current element and converts the String to Enum<T>
Definition: XmlReader.hpp:147
XmlNodeType GetCurrentNodeType()
gets the type of the current xml element
Definition: XmlReader.cpp:245
bool ReadToNextSibling(const String &name)
reads to the next element with name which is a sibling of the current element
Definition: XmlReader.cpp:551
bool IsEmptyElement()
test if the current element is an empty element
Definition: XmlReader.cpp:334
bool TryGetAttributeValue(const char *attributeName, T defaultValue, T &value)
tries to read an attribute value from the current element if the attribute was not found or the conve...
Definition: XmlReader.hpp:397
bool SkipElement()
skips the current element with its whole subtree
Definition: XmlReader.cpp:388
bool TryGetAttributeValueDuration(const char *attributeName, T defaultValue, T &value)
tries to read an attribute value from the current element as std::crono::duration if the attribute wa...
Definition: XmlReader.hpp:332
XmlReader(void)
Default contructor.
Definition: XmlReader.cpp:23
bool TryGetAndResolveAttributeValue(const char *attributeName, T &value)
tries to read and resolve an attribute placeholder from the current element
Definition: XmlReader.hpp:513
bool TryGetAttributeValueEnum(const char *attributeName, Enum< T > &value)
trys reads an attribute value from the current element and converts the String to Enum<T>
Definition: XmlReader.hpp:229
void ReadStartElement(const char *elementName=nullptr)
reads to the next element and test if this is a start element with the given name
Definition: XmlReader.cpp:593
T GetAndResolveAttributeValue(const char *attributeName)
reads and resolve an attribute value from the current element
Definition: XmlReader.hpp:481
static XmlReader CreateForString(const String &buffer)
Creates a new Instance of the XmlReader reading from an in memory buffer
Definition: XmlReader.cpp:149
bool TryReadStartElement(const char *elementName=nullptr)
trys to read the next element and test if this is a start element with the given name
Definition: XmlReader.cpp:614
bool MoveToElement()
Moves the position of the current instance to the node that contains the current Attribute node.
Definition: XmlReader.cpp:268
T GetAttributeValueOfEnum(const char *attributeName)
reads an attribute value from the current element and converts the String to Enum<T>
Definition: XmlReader.hpp:175
const String & GetDocumentFileName()
Gets the current document filename of this XML reader
Definition: XmlReader.cpp:204
void ReadEndElement(void)
reads until the next end element is reached skips over an empty element
Definition: XmlReader.cpp:714
T GetAttributeValueDuration(const char *attributeName)
reads an attribute value from the current element and converts the String to std::chrono::duration i....
Definition: XmlReader.hpp:280
bool ReadToDescendant(const String &name)
reads the xml file until an element with name is reached or the current element is closed reads to th...
Definition: XmlReader.cpp:516
bool Skip()
skips the current element with its whole subtree (use SkipElement() because naming is more clear)
Definition: XmlReader.cpp:377
bool SkipElementContent()
skips the content of current element with its whole subtree but does not read end element tag
Definition: XmlReader.cpp:422
String ReadElementContent(void)
reads the content of the current element if a CDATA section is found, the CDATA enclosure is removed
Definition: XmlReader.cpp:770
~XmlReader(void)
Destructs this instance and frees all resources.
Definition: XmlReader.cpp:49
static XmlReader Create(const String &filename)
Creates a new Instance of the XmlReader
Definition: XmlReader.cpp:82
bool TryGetAttributeValueEnum(const char *attributeName, T defaultValue, Enum< T > &value)
tries to read an attribute value from the current element as enum if the attribute was not found or t...
Definition: XmlReader.hpp:260
static bool TryCreate(const String &filename, XmlReader &reader)
Tries to creates a new Instance of the XmlReader
Definition: XmlReader.cpp:100
long GetLineNumber()
Gets the actual line number of this XML reader
Definition: XmlReader.cpp:192
String GetElementLocalName(void)
Reads the local name of the current active element
Definition: XmlReader.cpp:171
bool TryReadEndElement(void)
tries to read until the next end element is reached skips over an empty element
Definition: XmlReader.cpp:729
bool ReadToFollowing(const String &name)
reads the xml file until an element with name is reached or end of file is reached
Definition: XmlReader.cpp:492
bool TryGetAttributeValueDuration(const char *attributeName, T &value)
trys reads an attribute value from the current element and converts the String to std::chrono::durati...
Definition: XmlReader.hpp:309
bool TryGetAttributeValue(const char *attributeName, T &value)
tries to read an attribute value from the current element
Definition: XmlReader.hpp:353
static Enum Parse(const String &input)
Parses the given input string.
Definition: Enum.ipp:90
std::uint8_t uint8
The Arp unsigned integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:25
std::int8_t int8
The Arp integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:27
Root namespace for the PLCnext API
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
Deprecated! The class implements an adapter for enums to define the string literals of the enum entri...
Definition: EnumStrings.hxx:38