PLCnext API Documentation 25.0.2.69
XmlWriter.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
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
23 XmlWriter(XmlWriter&& arg);
24 ~XmlWriter(void);
25
26private: // construction/destruction
27 XmlWriter(const String& filename, bool indent, const char* encoding = "UTF-8");
28 XmlWriter(XmlBuffer& buffer, bool indent, const char* encoding);
29 XmlWriter(const XmlWriter& arg) = delete;
30 XmlWriter& operator=(const XmlWriter& arg) = delete;
31
32public: // static operations
33 static XmlWriter Create(const String& filename, bool indent, const char* encoding = "UTF-8");
34 static XmlWriter Create(XmlBuffer& buffer, bool indent, const char* encoding = "UTF-8");
35
36public: // operations
37 void WriteStartDocument(void);
38 void WriteEndDocument(void);
39 void WriteStartElement(const char* elementName);
40 void WriteEndElement(void);
41 void WriteElementContent(const String& value);
42 void WriteElementContentCData(const String& text);
43
56 template<class T>
57 void SetAttributeValue(const char* attributeName, const T& value)
58 {
59 try
60 {
61 SetAttributeValueInternal(attributeName, String::Format("{}", value));
62 }
63 catch(std::exception& e)
64 {
65 throw InvalidFormatException("Could not format Xml attribute '{}': {}", attributeName, e.what());
66 }
67 }
68 //overload for const char* to improve performance in this case
69 void SetAttributeValue(const char* attributeName, const char* value);
70 //fmt::MemoryWriter does not support String, so overload for String
71 void SetAttributeValue(const char* attributeName, const String& value);
72 //fmt::MemoryWriter writes true as 1 so overload for bool
73 void SetAttributeValue(const char* attributeName, boolean value);
74
75protected: // operations
76
77private: // static methods
78
79private: // methods
80 void SetAttributeValueInternal(const char* attributeName, const char *value);
81private: // fields
82
83 void *writer;
84 String encoding;
85
86private: // static fields
87
88};
89
90}}}} // end of namesapce Arp::System::Commons::Xml
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
static String Format(const String &format, const Args &... args)
Formats the format string using the .NET/Python syntax with the given variadic arguments.
Definition: String.inl:18
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
Definition: XmlWriter.cpp:199
void WriteStartElement(const char *elementName)
writes an start element with the given name <elementName>
Definition: XmlWriter.cpp:136
void WriteEndElement(void)
ends the currently open start element if no content was written inside this element,...
Definition: XmlWriter.cpp:151
static XmlWriter Create(const String &filename, bool indent, const char *encoding="UTF-8")
Creates a new Instance of the XmlWriter
Definition: XmlWriter.cpp:78
XmlWriter(XmlWriter &&arg)
Move contructor.
Definition: XmlWriter.cpp:59
void WriteStartDocument(void)
writes the start of an xml document e.g. xml tag
Definition: XmlWriter.cpp:106
void WriteEndDocument(void)
completes the xml document, must be called after the document data was written
Definition: XmlWriter.cpp:119
void WriteElementContentCData(const String &text)
writes the content inside the currently open element enclosed in a CDATA section characters not allow...
Definition: XmlWriter.cpp:216
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:57
~XmlWriter(void)
Destructs this instance and frees all resources.
Definition: XmlWriter.cpp:50
Root namespace for the PLCnext API