22 const char*
node =
nullptr;
45 this->attributes = attr;
80 assert(
p_out !=
nullptr);
81 return p_out->println(
"<?xml version=\"1.0\"?>");
101 const char* attributes =
nullptr) {
103 children.push_back(child);
104 return printNode(node, children, attributes);
115 const char* attributes =
nullptr) {
116 assert(
p_out !=
nullptr);
130 size_t printNode(
const char* node,
const char* txt =
nullptr,
131 const char* attributes =
nullptr) {
132 assert(
p_out !=
nullptr);
135 result +=
p_out->print(
"<");
136 result +=
p_out->print(node);
137 result +=
p_out->println(
"/>");
140 result +=
p_out->print(txt);
154 const char* attributes =
nullptr) {
155 assert(
p_out !=
nullptr);
157 result +=
p_out->print(txt);
169 size_t printNode(
const char* node, std::function<
size_t(
void)> callback,
170 const char* attributes =
nullptr) {
171 assert(
p_out !=
nullptr);
173 result += callback();
186 size_t printNode(
const char* node, std::function<
size_t(
void*)> callback,
187 void* ref,
const char* attributes =
nullptr) {
188 assert(
p_out !=
nullptr);
190 result += callback(ref);
203 size_t printNode(
const char* node,
size_t (*callback)(
void*),
void* ref,
204 const char* attributes =
nullptr) {
205 assert(
p_out !=
nullptr);
207 result += callback(ref);
221 std::function<
size_t(Print&,
void*)> callback,
222 void* ref,
const char* attributes =
nullptr) {
223 assert(
p_out !=
nullptr);
225 result += callback(*
p_out, ref);
233 size_t printNode(
const char* node,
size_t (*callback)(Print&,
void*),
234 void* ref,
const char* attributes =
nullptr) {
235 assert(
p_out !=
nullptr);
237 result += callback(*
p_out, ref);
249 assert(
p_out !=
nullptr);
253 int n = ::vsnprintf(buf,
sizeof(buf), fmt, ap);
255 if (n <= 0)
return 0;
256 if (n >= (
int)
sizeof(buf)) n = (int)
sizeof(buf) - 1;
257 return p_out->print(buf);
268 const char* relatedStateVariable =
nullptr) {
269 assert(
p_out !=
nullptr);
273 result +=
printNode(
"direction", direction);
274 if (relatedStateVariable && relatedStateVariable[0]) {
275 result +=
printNode(
"relatedStateVariable", relatedStateVariable);
290 bool sendEvents =
false,
291 std::function<
void()> extra =
nullptr) {
292 assert(
p_out !=
nullptr);
295 strcpy(attrBuf,
"sendEvents=\"yes\"");
297 strcpy(attrBuf,
"sendEvents=\"no\"");
301 result +=
printNode(
"dataType", dataType);
315 const char* ns =
nullptr) {
316 assert(
p_out !=
nullptr);
318 result +=
p_out->print(
"<");
320 result +=
p_out->print(ns);
321 result +=
p_out->print(
":");
323 result +=
p_out->print(node);
324 if (attributes !=
nullptr) {
325 result +=
p_out->print(
" ");
326 result +=
p_out->print(attributes);
328 result +=
p_out->print(
">");
340 const char* ns =
nullptr) {
342 result +=
p_out->println();
353 size_t result =
p_out->print(
"</");
355 result +=
p_out->print(ns);
356 result +=
p_out->print(
":");
358 result +=
p_out->print(node);
359 result +=
p_out->println(
">");
373 assert(
p_out !=
nullptr);
374 return p_out->println(txt);
379 for (
auto& node : children) {
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:18
Lightweight wrapper around std::vector with Arduino-friendly helpers and a pluggable allocator.
Definition: Vector.h:39
#define MAX_PRINTF_SIZE
Max printf buffer size.
Definition: dlna_config.h:50
Definition: Allocator.h:13
Represents a single XML element.
Definition: XMLPrinter.h:18
XMLNode()=default
Default constructor.
const char * node
Name of the XML node.
Definition: XMLPrinter.h:22
const char * content
Content of the XML node.
Definition: XMLPrinter.h:30
XMLNode(const char *node, const char *content, const char *attr=nullptr)
Constructor with node name, content, and optional attributes.
Definition: XMLPrinter.h:43
const char * attributes
Attributes of the XML node.
Definition: XMLPrinter.h:26
Functions to efficiently output XML. XML data contains a lot of redundancy so it is more memory effic...
Definition: XMLPrinter.h:56
size_t printNode(const char *node, size_t(*callback)(void *), void *ref, const char *attributes=nullptr)
Prints an XML node using a plain function pointer that receives a context pointer.
Definition: XMLPrinter.h:203
size_t printNodeBegin(const char *node, const char *attributes=nullptr, const char *ns=nullptr)
Prints the beginning of an XML node.
Definition: XMLPrinter.h:314
size_t printNodeBeginNl(const char *node, const char *attributes=nullptr, const char *ns=nullptr)
Prints the beginning of an XML node and a newline.
Definition: XMLPrinter.h:339
size_t printNode(const char *node, size_t(*callback)(Print &, void *), void *ref, const char *attributes=nullptr)
Prints an XML node using a plain function pointer that receives Print& and a context pointer.
Definition: XMLPrinter.h:233
size_t printNode(const char *node, XMLNode child, const char *attributes=nullptr)
Prints an XML node with a single child.
Definition: XMLPrinter.h:100
Print * p_out
Definition: XMLPrinter.h:370
size_t printNode(const char *node, Vector< XMLNode > children, const char *attributes=nullptr)
Prints an XML node with multiple children.
Definition: XMLPrinter.h:114
size_t printNode(XMLNode node)
Prints an XML node from XMLNode struct.
Definition: XMLPrinter.h:89
size_t printNode(const char *node, int txt, const char *attributes=nullptr)
Prints an XML node with integer content.
Definition: XMLPrinter.h:153
size_t printNode(const char *node, std::function< size_t(void)> callback, const char *attributes=nullptr)
Prints an XML node using a callback for content.
Definition: XMLPrinter.h:169
size_t printf(const char *fmt,...)
printf-style helper that formats into an internal buffer and writes to the configured Print output.
Definition: XMLPrinter.h:248
size_t printNode(const char *node, std::function< size_t(Print &, void *)> callback, void *ref, const char *attributes=nullptr)
Prints an XML node using a callback that receives the Print& and a context pointer.
Definition: XMLPrinter.h:220
XMLPrinter(Print &output)
Constructor with output.
Definition: XMLPrinter.h:67
size_t printNodeEnd(const char *node, const char *ns=nullptr)
Prints the end of an XML node.
Definition: XMLPrinter.h:352
size_t println(const char *txt)
Definition: XMLPrinter.h:372
XMLPrinter()=default
Default constructor.
size_t printNode(const char *node, std::function< size_t(void *)> callback, void *ref, const char *attributes=nullptr)
Prints an XML node using a callback that receives a context pointer.
Definition: XMLPrinter.h:186
size_t printArgument(const char *name, const char *direction, const char *relatedStateVariable=nullptr)
Helper to print a UPnP <argument> element with name, direction and optional relatedStateVariable.
Definition: XMLPrinter.h:267
size_t printChildren(Vector< XMLNode > &children)
Definition: XMLPrinter.h:377
void setOutput(Print &output)
Defines the output Print object.
Definition: XMLPrinter.h:73
size_t printNode(const char *node, const char *txt=nullptr, const char *attributes=nullptr)
Prints an XML node with text content.
Definition: XMLPrinter.h:130
size_t printXMLHeader()
Prints the XML header.
Definition: XMLPrinter.h:79
void clear()
Definition: XMLPrinter.h:363
size_t printStateVariable(const char *name, const char *dataType, bool sendEvents=false, std::function< void()> extra=nullptr)
Helper to print a UPnP <stateVariable> element with name, dataType and optional sendEvents attribute ...
Definition: XMLPrinter.h:289