8#include "Arp/System/Commons/Diagnostics/Logging/Loggable.hxx"
9#include "Arp/System/Commons/Xml/XmlReader.hpp"
10#include "Arp/System/Commons/Exceptions/XmlException.hpp"
11#include "Arp/System/Commons/Configuration/TicNodeType.hpp"
12#include "Arp/System/Commons/Configuration/TicSerializationContext.hpp"
16namespace Arp {
namespace System {
namespace Commons {
namespace Configuration
27 typedef std::map<TicNodeType, String> TicNodeTypeNames;
28 typedef std::map<String, TicNodeType> TicNameNodeTypes;
39 static SectionReadCallback<T> Null;
40 static SectionReadCallback<T> Empty;
65 void ReadStartTicNode(TicNodeType& nodeType);
66 bool TryReadStartTicNode(TicNodeType& nodeType);
67 void ReadEndTicNode(TicNodeType nodeType);
68 bool TryReadEndTicNode(TicNodeType nodeType);
71 void ReadStartTicElementList(
void);
72 bool TryReadStartTicElementList(
void);
73 void ReadEndTicElementList(
void);
74 bool TryReadEndTicElementList(
void);
77 void ReadStartTicAttributeList(
void);
78 bool TryReadStartTicAttributeList(
void);
79 void ReadEndTicAttributeList(
void);
80 bool TryReadEndTicAttributeList(
void);
83 void ReadStartTicElement(
String& readELementName);
84 bool TryReadStartTicElement(
String& readELementName);
85 bool TryReadStartTicElement(
const String& elementName,
String& readELementName);
86 void ReadEndTicElement(
void);
87 bool TryReadEndTicElement(
void);
90 void ReadStartTicAttribute(
String& readAttributeName);
91 bool TryReadStartTicAttribute(
String& readAttributeName);
92 bool TryReadStartTicAttribute(
const String& attributeName,
String& readAttributeName);
93 void ReadEndTicAttribute(
void);
94 bool TryReadEndTicAttribute(
void);
97 void SkipTicAttributeValue(
void);
99 T ReadTicAttributeValue(
void);
101 bool TryReadTicAttributeValue(T& result);
105 void ReadTicElementContent(
108 SectionReadCallback<T> attributesReadMethod,
109 SectionReadCallback<T> childElementsReadMethod =
nullptr,
110 bool attributesAreOptional =
false,
111 bool elementsAreOptional =
false);
114 bool TryReadStartTicElement(
const String& elementName,
String& readELementName,
String& errorMessage);
115 bool TryReadStartTicAttribute(
const String& attributeName,
String& readAttributeName,
String& errorMessage);
116 bool TryReadTicAttributeContent(
String& result,
String& errorMessage);
118 bool TryReadTicAttributeValue(T& result,
String& errorMessage);
119 void SkipTicAttributeList(
void);
120 void SkipTicElementList(
void);
126 static TicNodeTypeNames ticNodeTypeNames;
127 static TicNameNodeTypes ticNameNodeTypes;
130 static const char*
const ticElementNameAttributeName;
131 static const char*
const ticAttributeNameAttributeName;
142inline XmlReader& TicReader::GetXmlReader()
144 return this->xmlReader;
147inline bool TicReader::TryReadStartTicElementList()
149 TicNodeType nodeType = TicNodeType::None;
150 return this->TryReadStartTicNode(nodeType) && nodeType == TicNodeType::ElementList;
153inline void TicReader::ReadStartTicElementList()
155 if (!this->TryReadStartTicElementList())
157 throw XmlException(
"Invalid Tic file format: missing StartTicElementList tag '<{0}>'.", ticNodeTypeNames[TicNodeType::ElementList]);
161inline bool TicReader::TryReadEndTicElementList()
163 return this->TryReadEndTicNode(TicNodeType::ElementList);
166inline void TicReader::ReadEndTicElementList()
168 if (!this->TryReadEndTicElementList())
170 throw XmlException(
"Invalid Tic file format: missing EndTicElementList tag '</{0}>'.", ticNodeTypeNames[TicNodeType::ElementList]);
174inline bool TicReader::TryReadStartTicAttributeList()
176 TicNodeType nodeType = TicNodeType::None;
177 return this->TryReadStartTicNode(nodeType) && nodeType == TicNodeType::AttributeList;
180inline void TicReader::ReadStartTicAttributeList()
182 if (!this->TryReadStartTicAttributeList())
184 throw XmlException(
"Invalid Tic file format: missing StartTicAttributeList tag '<{0}>'.", ticNodeTypeNames[TicNodeType::AttributeList]);
188inline bool TicReader::TryReadEndTicAttributeList()
190 return this->TryReadEndTicNode(TicNodeType::AttributeList);
193inline void TicReader::ReadEndTicAttributeList()
195 if (!this->TryReadEndTicAttributeList())
197 throw XmlException(
"Invalid Tic file format: missing EndTicAttributeList tag '</{0}>'.", ticNodeTypeNames[TicNodeType::AttributeList]);
201inline void TicReader::ReadStartTicElement(
String& readELementName)
204 if (!this->TryReadStartTicElement(
String::Empty, readELementName, errorMessage))
210inline bool TicReader::TryReadStartTicElement(
String& readELementName)
213 return this->TryReadStartTicElement(
String::Empty, readELementName, errorMessage);
216inline bool TicReader::TryReadStartTicElement(
const String& elementName,
String& readELementName)
219 return this->TryReadStartTicElement(elementName, readELementName, errorMessage);
222inline void TicReader::ReadEndTicElement()
224 if (!this->TryReadEndTicElement())
226 throw XmlException(
"Invalid Tic file format: missing EndTicElement tag '</{0}>'.", ticNodeTypeNames[TicNodeType::Element]);
230inline bool TicReader::TryReadEndTicElement()
232 return this->TryReadEndTicNode(TicNodeType::Element);
235inline void TicReader::ReadStartTicAttribute(
String& readAttributeName)
238 if (!this->TryReadStartTicAttribute(
String::Empty, readAttributeName, errorMessage))
244inline bool TicReader::TryReadStartTicAttribute(
String& readAttributeName)
247 return this->TryReadStartTicAttribute(
String::Empty, readAttributeName, errorMessage);
250inline bool TicReader::TryReadStartTicAttribute(
const String& attributeName,
String& readAttributeName)
253 return this->TryReadStartTicAttribute(attributeName, readAttributeName, errorMessage);
256inline void TicReader::ReadEndTicAttribute(
void)
258 if (!this->TryReadEndTicAttribute())
260 throw XmlException(
"Invalid Tic file format: missing EndTicAttribute tag '</{0}>'.", ticNodeTypeNames[TicNodeType::Attribute]);
264inline bool TicReader::TryReadEndTicAttribute()
266 return this->TryReadEndTicNode(TicNodeType::Attribute);
269inline void TicReader::SkipTicAttributeValue()
271 (void) this->ReadTicAttributeValue<String>();
275inline T TicReader::ReadTicAttributeValue()
279 if (!this->TryReadTicAttributeValue<T>(result, errorMessage))
287inline bool TicReader::TryReadTicAttributeValue(T& result)
290 return this->TryReadTicAttributeValue<T>(result, errorMessage);
294inline bool TicReader::TryReadTicAttributeValue(T& result,
String& errorMessage)
297 if (!this->TryReadTicAttributeContent(attributeValue, errorMessage))
302 std::stringstream iss(attributeValue);
303 iss >> std::boolalpha >> result;
307inline bool TicReader::TryReadTicAttributeValue(
String& result,
String& errorMessage)
310 if (!this->TryReadTicAttributeContent(result, errorMessage))
322inline void TicReader::ReadTicElementContent(
325 SectionReadCallback<T> attributesReader,
326 SectionReadCallback<T> childElementsReader,
327 bool attributesAreOptional,
328 bool elementsAreOptional)
330 bool hasAttributesRead =
false;
331 bool hasChildElementsRead =
false;
333 while (!(hasAttributesRead && hasChildElementsRead))
335 TicNodeType nodeType = TicNodeType::None;
336 if (this->TryReadStartTicNode(nodeType))
338 bool mustReadEndTicNode =
true;
341 case TicNodeType::AttributeList:
342 if (attributesReader ==
nullptr)
344 this->SkipTicAttributeList();
348 (configuration.*attributesReader)(*
this, context);
350 hasAttributesRead =
true;
352 case TicNodeType::ElementList:
353 if (childElementsReader ==
nullptr)
355 this->SkipTicElementList();
356 mustReadEndTicNode =
false;
360 (configuration.*childElementsReader)(*
this, context);
362 hasChildElementsRead =
true;
365 throw XmlException(
"Invalid TIC file format: expecting attribute or element list");
368 if(mustReadEndTicNode)
370 this->ReadEndTicNode(nodeType);
375 if (!hasAttributesRead && !attributesAreOptional)
377 throw XmlException(
"Invalid TIC file format: expecting attribute list");
379 else if (attributesReader ==
nullptr && attributesAreOptional)
381 hasAttributesRead =
true;
384 if (!hasChildElementsRead && !elementsAreOptional)
386 throw XmlException(
"Invalid TIC file format: expecting element list");
388 else if (elementsAreOptional)
390 hasChildElementsRead =
true;
Definition: TicReader.hpp:23
~TicReader(void)=default
Destructs this instance and frees all resources.
TicReader(TicReader &&arg)
Move contructor.
Definition: TicSerializationContext.hpp:19
Definition: Loggable.hxx:20
This exception is used for xml parsing errors.
Definition: XmlException.hpp:15
Class to read an XML File. Non buffered reader, can only read forward
Definition: XmlReader.hpp:25
static const SelfType Empty
An emtpy static string instance.
Definition: BasicString.hxx:224
@ System
System components used by the System, Device, Plc or Io domains.
Namespace for logging classes
Definition: LogAdapter.hpp:20
Namespace for classes to read XML files
Definition: ConfigHeader.hpp:12
Root namespace for the PLCnext API
Definition: TicReader.hpp:37