PLCnext API Documentation 23.0.2.9
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
12#include "cppformat/format.h"
13
14namespace Arp { namespace System { namespace Commons { namespace Xml
15{
16
17class XmlBuffer;
18
21{
22public: // typedefs
23
24public: // construction/destruction
29
30private: // 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
36public: // 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
67public: // setter/getter operations
74 bool GetIndent(void)const;
81 const char* GetEncoding(void)const;
82
83public: // operations
93 void WriteEndDocument(void);
94
102 void WriteStartElement(const char* elementName);
109 void WriteEndElement(void);
118 void WriteElementContent(const String& value);
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
165protected: // operations
166
167private: // static methods
168
169private: // methods
170 void SetAttributeValueInternal(const char* attributeName, const char *value);
171private: // fields
172
173 void *writer;
174 String encoding;
175
176private: // static fields
177
178};
179
181// inline methods of class XmlWriter
182
183}}}} // end of namesapce Arp::System::Commons::Xml
This exception is thrown when an invalid format operation occurs.
Definition: InvalidFormatException.hpp:15
Class to write an XML File
Definition: XmlWriter.hpp:21
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:145
~XmlWriter(void)
Destructs this instance and frees all resources.
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API