PLCnext API Documentation  21.0.0.35466
XmlWriter.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/TypeName.hxx"
9 #include "Arp/System/Commons/Logging.h"
10 #include "Arp/System/Commons/Exceptions/InvalidFormatException.hpp"
11 
12 #include "cppformat/format.h"
13 
14 namespace Arp { namespace System { namespace Commons { namespace Xml
15 {
16 
17 class XmlBuffer;
18 
20 class XmlWriter
21 {
22 public: // typedefs
23 
24 public: // construction/destruction
26  XmlWriter(XmlWriter&& arg);
28  ~XmlWriter(void);
29 
30 private: // construction/destruction
31  XmlWriter(const String& filename, bool indent, const char* encoding = "UTF-8");
32  XmlWriter(XmlBuffer& buffer, bool indent, const char* encoding);
33  XmlWriter(const XmlWriter& arg) = delete;
34  XmlWriter& operator=(const XmlWriter& arg) = delete;
35 
36 public: // static operations
50  static XmlWriter Create(const String& filename, bool indent, const char* encoding = "UTF-8");
51 
65  static XmlWriter Create(XmlBuffer& buffer, bool indent, const char* encoding = "UTF-8");
66 
67 public: // setter/getter operations
74  bool GetIndent(void)const;
81  const char* GetEncoding(void)const;
82 
83 public: // operations
88  void WriteStartDocument(void);
93  void WriteEndDocument(void);
94 
102  void WriteStartElement(const char* elementName);
109  void WriteEndElement(void);
118  void WriteElementContent(const String& value);
127  void WriteElementContentCData(const String& text);
128 
129  //see http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html
130  //for a speed comparison of integer to string conversion operations
131 
144  template<class T>
145  void SetAttributeValue(const char* attributeName, const T& value)
146  {
147  try
148  {
149  fmt::MemoryWriter tmpWriter;
150  tmpWriter << value;
151  SetAttributeValueInternal(attributeName, tmpWriter.c_str());
152  }
153  catch(std::exception& e)
154  {
155  throw InvalidFormatException("Could not format Xml attribute '{0}': {1}", e.what());
156  }
157  }
158  //overload for const char* to improve performance in this case
159  void SetAttributeValue(const char* attributeName, const char* value);
160  //fmt::MemoryWriter does not support String, so overload for String
161  void SetAttributeValue(const char* attributeName, const String& value);
162  //fmt::MemoryWriter writes true as 1 so overload for bool
163  void SetAttributeValue(const char* attributeName, boolean value);
164 
165 protected: // operations
166 
167 private: // static methods
168 
169 private: // methods
170  void SetAttributeValueInternal(const char* attributeName, const char *value);
171 private: // fields
172 
173  void *writer;
174  String encoding;
175 
176 private: // static fields
177 
178 };
179 
181 // inline methods of class XmlWriter
182 
183 }}}} // end of namesapce Arp::System::Commons::Xml
void WriteElementContent(const String &value)
writes the content inside the currently open element characters not allowed in xml are encoded ...
Class to write an XML File
Definition: XmlWriter.hpp:20
~XmlWriter(void)
Destructs this instance and frees all resources.
This exception is thrown when an invalid format operation occurs.
Definition: InvalidFormatException.hpp:14
void WriteEndElement(void)
ends the currently open start element if no content was written inside this element, an empty element is written if content was written inside this element, an end element is written
void SetAttributeValue(const char *attributeName, const T &value)
writes a new attribute for the currently open element characters not allowed in xml are not encoded ...
Definition: XmlWriter.hpp:145
bool GetIndent(void) const
Reads the indent status of this XmlWriter
void WriteStartDocument(void)
writes the start of an xml document e.g. xml tag
void WriteStartElement(const char *elementName)
writes an start element with the given name <elementName>
static XmlWriter Create(const String &filename, bool indent, const char *encoding="UTF-8")
Creates a new Instance of the XmlWriter
Root namespace for the PLCnext API
void WriteElementContentCData(const String &text)
writes the content inside the currently open element enclosed in a CDATA section characters not allow...
void WriteEndDocument(void)
completes the xml document, must be called after the document data was written
XmlWriter(XmlWriter &&arg)
Move contructor.
System components used by the System, Device, Plc or Io domains.
const char * GetEncoding(void) const
Gets the encoding used by this XmlWriter