PLCnext API Documentation 23.6.0.37
XmlWriter.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/TypeName.hxx"
9#include "Arp/System/Commons/Logging.h"
10#include "Arp/System/Commons/Exceptions/InvalidFormatException.hpp"
11
12namespace Arp { namespace System { namespace Commons { namespace Xml
13{
14
15class XmlBuffer;
16
19{
20public: // typedefs
21
22public: // construction/destruction
27
28private: // construction/destruction
29 XmlWriter(const String& filename, bool indent, const char* encoding = "UTF-8");
30 XmlWriter(XmlBuffer& buffer, bool indent, const char* encoding);
31 XmlWriter(const XmlWriter& arg) = delete;
32 XmlWriter& operator=(const XmlWriter& arg) = delete;
33
34public: // static operations
48 static XmlWriter Create(const String& filename, bool indent, const char* encoding = "UTF-8");
49
63 static XmlWriter Create(XmlBuffer& buffer, bool indent, const char* encoding = "UTF-8");
64
65public: // setter/getter operations
72 bool GetIndent(void)const;
79 const char* GetEncoding(void)const;
80
81public: // operations
91 void WriteEndDocument(void);
92
100 void WriteStartElement(const char* elementName);
107 void WriteEndElement(void);
116 void WriteElementContent(const String& value);
126
127 //see http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html
128 //for a speed comparison of integer to string conversion operations
129
142 template<class T>
143 void SetAttributeValue(const char* attributeName, const T& value)
144 {
145 try
146 {
147 SetAttributeValueInternal(attributeName, String::Format("{}", value));
148 }
149 catch(std::exception& e)
150 {
151 throw InvalidFormatException("Could not format Xml attribute '{}': {}", attributeName, e.what());
152 }
153 }
154 //overload for const char* to improve performance in this case
155 void SetAttributeValue(const char* attributeName, const char* value);
156 //fmt::MemoryWriter does not support String, so overload for String
157 void SetAttributeValue(const char* attributeName, const String& value);
158 //fmt::MemoryWriter writes true as 1 so overload for bool
159 void SetAttributeValue(const char* attributeName, boolean value);
160
161protected: // operations
162
163private: // static methods
164
165private: // methods
166 void SetAttributeValueInternal(const char* attributeName, const char *value);
167private: // fields
168
169 void *writer;
170 String encoding;
171
172private: // static fields
173
174};
175
177// inline methods of class XmlWriter
178
179}}}} // end of namesapce Arp::System::Commons::Xml
This exception is thrown when an invalid format operation occurs.
Definition: InvalidFormatException.hpp:15
Implements a buffer which can be used to write xml data to memory
Definition: XmlBuffer.hpp:18
Class to write an XML File
Definition: XmlWriter.hpp:19
void WriteElementContent(const String &value)
writes the content inside the currently open element characters not allowed in xml are encoded
void WriteStartElement(const char *elementName)
writes an start element with the given name <elementName>
void WriteEndElement(void)
ends the currently open start element if no content was written inside this element,...
XmlWriter(XmlWriter &&arg)
Move contructor.
void WriteStartDocument(void)
writes the start of an xml document e.g. xml tag
void WriteEndDocument(void)
completes the xml document, must be called after the document data was written
static XmlWriter Create(XmlBuffer &buffer, bool indent, const char *encoding="UTF-8")
Creates a new Instance of the XmlWriter writing to an XmlBuffer in memory
static XmlWriter Create(const String &filename, bool indent, const char *encoding="UTF-8")
Creates a new Instance of the XmlWriter
bool GetIndent(void) const
Reads the indent status of this XmlWriter
const char * GetEncoding(void) const
Gets the encoding used by this XmlWriter
void WriteElementContentCData(const String &text)
writes the content inside the currently open element enclosed in a CDATA section characters not allow...
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:143
~XmlWriter(void)
Destructs this instance and frees all resources.
static SelfType Format(const SelfType &format, const Args &... args)
Formats the format string using the .NET/Python syntax with the given variadic arguments.
Definition: BasicString.hxx:1493
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API