Arduino DLNA Server
Loading...
Searching...
No Matches
XMLParserPrint.h
Go to the documentation of this file.
1#pragma once
2
3#include "XMLParser.h"
4#include "basic/StrPrint.h"
5
6namespace tiny_dlna {
7
16class XMLParserPrint : public Print {
17 public:
24 p.setReportTextOnly(false);
25 }
26
32 size_t write(uint8_t ch) override { return buffer.write(ch); }
33
40 size_t write(const uint8_t* data, size_t size) override {
41 return buffer.write(data, size);
42 }
43
48 void setExpandEncoded(bool flag) { buffer.setExpandEncoded(flag); }
49
59 bool parse(Str& outNodeName, Vector<Str>& outPath, Str& outText,
60 Str& outAttributes) {
61 const char* data = buffer.c_str();
62 size_t total_len = buffer.length();
63 if (data == nullptr || total_len == 0) return false;
64
65 cbref.outNode = &outNodeName;
66 cbref.outPath = &outPath;
67 cbref.outText = &outText;
68 cbref.outAttrs = &outAttributes;
69 // clear previous outputs so callers get an empty result on failure
71 if (cbref.outPath) cbref.outPath->resize(0);
74
75 p.setXml(data);
76 // mark as not-yet-found
77 cbref.start = -1;
78 cbref.len = 0;
79 bool parsed = p.parseSingle();
81 return parsed;
82 }
83
87 void end() {
88 p.end();
89 buffer.reset();
91 if (cbref.outPath) cbref.outPath->resize(0);
94 }
95
100 const char* c_str() { return buffer.c_str(); }
101
106 size_t length() { return buffer.length(); }
107
108 protected:
113
118
123 struct CBRef {
125 int start = 0;
127 int len = 0;
129 Str* outNode = nullptr;
133 Str* outText = nullptr;
135 Str* outAttrs = nullptr;
137
150 static void wrapperCallback(Str& nodeName, Vector<Str>& path, Str& text,
151 Str& attributes, int start, int len, void* ref) {
152 CBRef* r = static_cast<CBRef*>(ref);
153 if (!r) return;
154 r->start = start;
155 r->len = len;
156 if (r->outNode) *(r->outNode) = nodeName;
157 if (r->outText) *(r->outText) = text;
158 if (r->outAttrs) {
159 if (attributes.isEmpty())
160 r->outAttrs->reset();
161 else
162 *(r->outAttrs) = attributes;
163 }
164 if (r->outPath) {
165 r->outPath->resize(0);
166 for (int i = 0; i < path.size(); ++i) {
167 r->outPath->push_back(path[i]);
168 }
169 }
170 }
171};
172
173} // namespace tiny_dlna
Print to a dynamic string.
Definition: StrPrint.h:13
const char * c_str()
Definition: StrPrint.h:39
size_t length()
Definition: StrPrint.h:41
size_t write(uint8_t ch) override
Definition: StrPrint.h:16
void reset()
Definition: StrPrint.h:43
void consume(int n)
Definition: StrPrint.h:45
void setExpandEncoded(bool flag)
Definition: StrPrint.h:47
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
bool isEmpty() const
True if empty.
Definition: Str.h:54
void reset()
Clear contents (alias of clear)
Definition: Str.h:157
void clear()
Clear contents (size -> 0)
Definition: Str.h:93
Lightweight wrapper around std::vector with Arduino-friendly helpers and a pluggable allocator.
Definition: Vector.h:39
Helper class that implements a Print interface to accumulate XML data and then parse it using XMLPars...
Definition: XMLParserPrint.h:16
XMLParserPrint()
Constructor.
Definition: XMLParserPrint.h:21
struct tiny_dlna::XMLParserPrint::CBRef cbref
StrPrint buffer
Internal buffer for accumulating XML data.
Definition: XMLParserPrint.h:112
const char * c_str()
Returns the internal buffer as a C string.
Definition: XMLParserPrint.h:100
size_t write(uint8_t ch) override
Writes a single byte to the buffer (Print interface)
Definition: XMLParserPrint.h:32
void setExpandEncoded(bool flag)
Forwards expand-entities setting to the underlying XMLParser.
Definition: XMLParserPrint.h:48
static void wrapperCallback(Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref)
Static wrapper used as XMLParser callback. Copies the first invocation's data into the CBRef and forw...
Definition: XMLParserPrint.h:150
void end()
Resets the internal buffer.
Definition: XMLParserPrint.h:87
size_t length()
Returns the length of the internal buffer.
Definition: XMLParserPrint.h:106
size_t write(const uint8_t *data, size_t size) override
Writes a block of data to the buffer (Print interface)
Definition: XMLParserPrint.h:40
XMLParser p
XMLParser instance used for parsing.
Definition: XMLParserPrint.h:117
bool parse(Str &outNodeName, Vector< Str > &outPath, Str &outText, Str &outAttributes)
Parses the accumulated XML data and returns results via output parameters.
Definition: XMLParserPrint.h:59
Lightweight streaming XML parser.
Definition: XMLParser.h:27
void setCallback(void(*cb)(Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref))
Set the callback to be invoked for parsed fragments.
Definition: XMLParser.h:78
void setReportTextOnly(bool flag)
report only nodes with text
Definition: XMLParser.h:128
void setXml(const char *xmlStr)
Set the XML buffer to parse.
Definition: XMLParser.h:68
void setReference(void *ref)
Attach an opaque user pointer to the parser instance.
Definition: XMLParser.h:60
void end()
Fully reset parser state (parse position, path stack and content starts). Use this when the underlyin...
Definition: XMLParser.h:118
bool parseSingle()
Parse a single fragment (one callback invocation) from the previously set XML buffer.
Definition: XMLParser.h:104
int getParsePos()
Expose current parse position for incremental wrappers.
Definition: XMLParser.h:131
Definition: Allocator.h:13
Internal callback reference used to capture the callback data from XMLParser and forward it to the ca...
Definition: XMLParserPrint.h:123
int len
Length of parsed data.
Definition: XMLParserPrint.h:127
int start
Start position of parsed data.
Definition: XMLParserPrint.h:125
Str * outNode
Output node name pointer.
Definition: XMLParserPrint.h:129
Str * outText
Output text pointer.
Definition: XMLParserPrint.h:133
Str * outAttrs
Output attributes pointer.
Definition: XMLParserPrint.h:135
Vector< Str > * outPath
Output path vector pointer.
Definition: XMLParserPrint.h:131