Arduino DLNA Server
XMLPrinter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Print.h"
4 #include "assert.h"
5 #include "basic/StrView.h"
6 #include "basic/Vector.h"
7 
8 namespace tiny_dlna {
13 struct XMLNode {
14  const char* node = nullptr;
15  const char* attributes = nullptr;
16  const char* content = nullptr;
17  XMLNode() = default;
18  XMLNode(const char* node, const char* content, const char* attr = nullptr) {
19  this->node = node;
20  this->attributes = attr;
21  }
22 };
23 
31 struct XMLPrinter {
32  XMLPrinter() = default;
33  XMLPrinter(Print& output) { setOutput(output); }
34 
36  void setOutput(Print& output) { p_out = &output; }
37 
38  size_t printXMLHeader() {
39  assert(p_out != nullptr);
40  return p_out->println("<?xml version=\"1.0\"?>");
41  }
42 
43  size_t printNode(XMLNode node) {
44  return printNode(node.node, node.content, node.attributes);
45  }
46 
47  size_t printNode(const char* node, XMLNode child,
48  const char* attributes = nullptr) {
49  Vector<XMLNode> children;
50  children.push_back(child);
51  return printNode(node, children, attributes);
52  }
53 
54  size_t printNode(const char* node, Vector<XMLNode> children,
55  const char* attributes = nullptr) {
56  assert(p_out != nullptr);
57  size_t result = printNodeBeginNl(node, attributes);
58  result += printChildren(children);
59  result += printNodeEnd(node);
60  return result;
61  }
62 
63  size_t printNode(const char* node, const char* txt = nullptr,
64  const char* attributes = nullptr) {
65  assert(p_out != nullptr);
66  int result = 0;
67  if (StrView(txt).isEmpty()) {
68  result += p_out->print("<");
69  result += p_out->print(node);
70  result += p_out->println("/>");
71  } else {
72  result += printNodeBegin(node, attributes);
73  result += p_out->print(txt);
74  result += printNodeEnd(node);
75  }
76  return result;
77  }
78 
79  size_t printNode(const char* node, int txt,
80  const char* attributes = nullptr) {
81  assert(p_out != nullptr);
82  size_t result = printNodeBegin(node, attributes);
83  result += p_out->print(txt);
84  result += printNodeEnd(node);
85  return result;
86  }
87 
88  size_t printNode(const char* node, std::function<size_t(void)> callback,
89  const char* attributes = nullptr) {
90  assert(p_out != nullptr);
91  size_t result = printNodeBeginNl(node, attributes);
92  result += callback();
93  result += printNodeEnd(node);
94  return result;
95  }
96 
97  size_t printNodeBegin(const char* node, const char* attributes = nullptr,
98  const char* ns = nullptr) {
99  assert(p_out != nullptr);
100  size_t result = 0;
101  result += p_out->print("<");
102  if (ns != nullptr) {
103  result += p_out->print(ns);
104  result += p_out->print(":");
105  }
106  result += p_out->print(node);
107  if (attributes != nullptr) {
108  result += p_out->print(" ");
109  result += p_out->print(attributes);
110  }
111  result += p_out->print(">");
112  return result;
113  }
114 
115  size_t printNodeBeginNl(const char* node, const char* attributes = nullptr,
116  const char* ns = nullptr) {
117  size_t result = printNodeBegin(node, attributes, ns);
118  result = +p_out->println();
119  return result;
120  }
121 
122  size_t printNodeEnd(const char* node, const char* ns = nullptr) {
123  size_t result = p_out->print("</");
124  if (ns != nullptr) {
125  result += p_out->print(ns);
126  result += p_out->print(":");
127  }
128  result += p_out->print(node);
129  result += p_out->println(">");
130  return result;
131  }
132 
133  protected:
134  Print* p_out = &Serial;
135 
136  size_t println(const char* txt) {
137  assert(p_out != nullptr);
138  return p_out->println(txt);
139  }
140 
141  protected:
142  size_t printChildren(Vector<XMLNode>& children) {
143  size_t result = 0;
144  for (auto& node : children) {
145  result += printNode(node);
146  }
147  return result;
148  }
149 };
150 
151 } // namespace tiny_dlna
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:25
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
void push_back(T &&value)
Definition: Vector.h:167
Definition: Allocator.h:6
Represents a single XML element.
Definition: XMLPrinter.h:13
const char * node
Definition: XMLPrinter.h:14
const char * content
Definition: XMLPrinter.h:16
XMLNode(const char *node, const char *content, const char *attr=nullptr)
Definition: XMLPrinter.h:18
const char * attributes
Definition: XMLPrinter.h:15
Functions to efficiently output XML. XML data contains a lot of redundancy so it is more memory effic...
Definition: XMLPrinter.h:31
size_t printNodeBegin(const char *node, const char *attributes=nullptr, const char *ns=nullptr)
Definition: XMLPrinter.h:97
size_t printNodeBeginNl(const char *node, const char *attributes=nullptr, const char *ns=nullptr)
Definition: XMLPrinter.h:115
size_t printNode(const char *node, XMLNode child, const char *attributes=nullptr)
Definition: XMLPrinter.h:47
Print * p_out
Definition: XMLPrinter.h:134
size_t printNode(const char *node, Vector< XMLNode > children, const char *attributes=nullptr)
Definition: XMLPrinter.h:54
size_t printNode(XMLNode node)
Definition: XMLPrinter.h:43
size_t printNode(const char *node, int txt, const char *attributes=nullptr)
Definition: XMLPrinter.h:79
size_t printNode(const char *node, std::function< size_t(void)> callback, const char *attributes=nullptr)
Definition: XMLPrinter.h:88
XMLPrinter(Print &output)
Definition: XMLPrinter.h:33
size_t printNodeEnd(const char *node, const char *ns=nullptr)
Definition: XMLPrinter.h:122
size_t println(const char *txt)
Definition: XMLPrinter.h:136
size_t printChildren(Vector< XMLNode > &children)
Definition: XMLPrinter.h:142
void setOutput(Print &output)
Defines the output.
Definition: XMLPrinter.h:36
size_t printNode(const char *node, const char *txt=nullptr, const char *attributes=nullptr)
Definition: XMLPrinter.h:63
size_t printXMLHeader()
Definition: XMLPrinter.h:38