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"
16#include "boost/lexical_cast.hpp"
18namespace Arp {
namespace System {
namespace Commons {
namespace Xml
21class LibXmlErrorHandler;
286 return boost::lexical_cast<T>(elementString);
288 catch (std::exception& e)
293 "Cannot convert '{}' to {}: {}", elementString,
TypeName<T>().GetFullName(), e.what());
312 if (TryGetAttributeValue<T>(attributeName, returnValue))
319 "GetAttributeValue failed for mandatory attribute: {0} elementName: {1}",
340 if (TryGetAttributeValueEnum<T>(attributeName, returnValue))
347 "GetAttributeValueEnum failed for mandatory attribute: {0} elementName: {1}",
367 if (!TryGetAttributeValueEnum<T>(attributeName, result))
370 "GetAttributeValueOfEnum failed for mandatory attribute: {0} elementName: {1}", attributeName,
GetElementLocalName());
392 char* charValue = GetAttributeValueInternal(attributeName);
393 if (charValue ==
nullptr)
397 std::istringstream iss(charValue);
399 FreeInternalAttributeValue(charValue);
420 char* charValue = GetAttributeValueInternal(attributeName);
421 if (charValue ==
nullptr)
427 FreeInternalAttributeValue(charValue);
451 value = defaultValue;
452 return TryGetAttributeValueEnum<T>(attributeName, value);
473 if (TryGetAttributeValueDuration<T>(attributeName, returnValue))
480 "GetAttributeValueDuration failed for mandatory attribute: {0} elementName: {1}",
500 char* charValue = GetAttributeValueInternal(attributeName);
503 if (charValue ==
nullptr)
508 bool result = Chrono::Duration::TryParse(charValue, tempDurationValue);
512 "TryGetAttributeValueDuration failed for attribute '{0}' in element '{1}': invalid duration format in value '{2}'",
515 tempDurationValue.GetDuration(value);
517 FreeInternalAttributeValue(charValue);
541 value = defaultValue;
542 return TryGetAttributeValueDuration<T>(attributeName, value);
562 char* charValue =
nullptr;
565 charValue = GetAttributeValueInternal(attributeName);
566 if (charValue ==
nullptr)
575 value = boost::lexical_cast<T>(charValue);
576 FreeInternalAttributeValue(charValue);
579 catch (boost::bad_lexical_cast&)
582 String strValue(charValue);
583 FreeInternalAttributeValue(charValue);
584 throw XmlException(
"Cast of attribute {0} to {1} failed value: {2}", attributeName,
611 value = defaultValue;
612 return TryGetAttributeValue<T>(attributeName, value);
615 void EnableValidationException();
616 void DisableValidationException();
627 template<
typename... Args>
634 char* GetAttributeValueInternal(
const char* attributeName);
635 void FreeInternalAttributeValue(
char* attributeValue);
639 XmlNodeType MoveToContent();
640 void EnableValidation(
const String& xsdFile);
641 void CheckForValidationException();
647 bool validationEnabled;
649 bool exceptionOnValidationError;
652 bool startElementRead;
676template<
typename... Args>
684 return this->filename;
Adapter class for enums to make them loggable and parsable from e.g. XML files.
Definition: Enum.hxx:23
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.hpp:79
internal class to handle xml parsing errors in XmlReader
Definition: LibXmlErrorHandler.hpp:16
Class to read an XML File. Non buffered reader, can only read forward
Definition: XmlReader.hpp:25
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:390
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:677
T ReadElementContent()
reads the content of the current element if a CDATA section is found, the CDATA enclosure is removed
Definition: XmlReader.hpp:281
static XmlReader CreateForString(const String &buffer)
Creates a new Instance of the XmlReader reading from an in memory buffer
T GetAttributeValue(const char *attributeName)
reads an attribute value from the current element
Definition: XmlReader.hpp:309
int GetCurrentDepth()
gets the current depth of the current element inside the xml document
bool Read()
Reads to the next element inside the xml file
static bool TryCreate(const String &filename, XmlReader &reader)
Tries to creates a new Instance of the XmlReader
Enum< T > GetAttributeValueEnum(const char *attributeName)
reads an attribute value from the current element and converts the String to Enum<T>
Definition: XmlReader.hpp:336
static XmlReader Create(const String &filename, const String &xsdFile)
Creates a new Instance of the XmlReader and enables Schema validation with the XSD schema at xsdFile
XmlNodeType GetCurrentNodeType()
gets the type of the current xml element
bool ReadToNextSibling(const String &name)
reads to the next element with name which is a sibling of the current element
bool IsEmptyElement()
test if the current element is an empty element
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:608
bool SkipElement()
skips the current element with its whole subtree
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:539
XmlReader(void)
Default contructor.
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:418
void ReadStartElement(const char *elementName=nullptr)
reads to the next element and test if this is a start element with the given name
XmlReader(XmlReader &&arg)
Move contructor.
static XmlReader Create(const String &filename)
Creates a new Instance of the XmlReader
bool TryReadStartElement(const char *elementName=nullptr)
trys to read the next element and test if this is a start element with the given name
bool MoveToElement()
Moves the position of the current instance to the node that contains the current Attribute node.
T GetAttributeValueOfEnum(const char *attributeName)
reads an attribute value from the current element and converts the String to Enum<T>
Definition: XmlReader.hpp:364
const String & GetDocumentFileName()
Gets the current document filename of this XML reader
Definition: XmlReader.hpp:682
void ReadEndElement(void)
reads until the next end element is reached skips over an empty element
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:469
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...
bool Skip()
skips the current element with its whole subtree (use SkipElement() because naming is more clear)
bool SkipElementContent()
skips the content of current element with its whole subtree but does not read end element tag
String ReadElementContent(void)
reads the content of the current element if a CDATA section is found, the CDATA enclosure is removed
~XmlReader(void)
Destructs this instance and frees all resources.
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:449
bool TryReadStartElement(String &elementName, bool conditioned=true)
trys to read the next element and returns the read element name as out argument
long GetLineNumber()
Gets the actual line number of this XML reader
String GetElementLocalName(void)
Reads the local name of the current active element
bool TryReadEndElement(void)
tries to read until the next end element is reached skips over an empty element
bool ReadToFollowing(const String &name)
reads the xml file until an element with name is reached or end of file is reached
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:498
bool TryGetAttributeValue(const char *attributeName, T &value)
tries to read an attribute value from the current element
Definition: XmlReader.hpp:560
This (meta programming) class provides the C++ typename of the as template argument passed type.
Definition: TypeName.hxx:67
static Enum Parse(const String &input)
Parses the given input string.
Definition: Enum.hxx:228
std::int8_t int8
The Arp integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:30
std::uint8_t uint8
The Arp unsigned integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:28
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API