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
12class XMLParserPrint : public Print {
13 public:
14 XMLParserPrint(int reserve = 80) : buffer(reserve) {
17 p.setReportTextOnly(false);
18 }
19
20 // Print interface
21 size_t write(uint8_t ch) override { return buffer.write(ch); }
22
23 size_t write(const uint8_t* data, size_t size) override {
24 return buffer.write(data, size);
25 }
26
28 void setExpandEncoded(bool flag) { buffer.setExpandEncoded(flag); }
29
30 bool parse(Str& outNodeName, Vector<Str>& outPath, Str& outText,
31 Str& outAttributes) {
32 const char* data = buffer.c_str();
33 size_t total_len = buffer.length();
34 if (data == nullptr || total_len == 0) return false;
35
36 cbref.outNode = &outNodeName;
37 cbref.outPath = &outPath;
38 cbref.outText = &outText;
39 cbref.outAttrs = &outAttributes;
40
41 p.setXml(data);
42 // clear any previous cbref values
43 cbref.start = 0;
44 cbref.len = 0;
45 bool parsed = p.parseSingle();
47 return parsed;
48 }
49
50 void reset() { buffer.reset(); }
51
52 const char* c_str() { return buffer.c_str(); }
53
54 size_t length() { return buffer.length(); }
55
56 protected:
59 // Internal callback reference used to capture the callback data
60 // from XMLParser and forward it to the caller.
61 struct CBRef {
62 int start = 0;
63 int len = 0;
64 Str* outNode = nullptr;
65 Vector<Str>* outPath = nullptr;
66 Str* outText = nullptr;
67 Str* outAttrs = nullptr;
69
70 // Static wrapper used as XMLParser callback. It copies the first
71 // invocation's data into the CBRef and forwards the event to the
72 // user-provided callback if present.
73 static void wrapperCallback(Str& nodeName, Vector<Str>& path, Str& text,
74 Str& attributes, int start, int len, void* ref) {
75 CBRef* r = static_cast<CBRef*>(ref);
76 if (!r) return;
77 r->start = start;
78 r->len = len;
79 if (r->outNode) *(r->outNode) = nodeName.c_str();
80 if (r->outText) *(r->outText) = text.c_str();
81 if (r->outAttrs) {
82 if (attributes.isEmpty())
83 r->outAttrs->reset();
84 else
85 *(r->outAttrs) = attributes.c_str();
86 }
87 if (r->outPath) {
88 r->outPath->resize(0);
89 for (int i = 0; i < path.size(); ++i) {
90 r->outPath->push_back(Str(path[i].c_str()));
91 }
92 }
93 }
94};
95
96} // namespace tiny_dlna
Print to a dynamic string.
Definition: StrPrint.h:12
const char * c_str()
Definition: StrPrint.h:38
size_t length()
Definition: StrPrint.h:40
size_t write(uint8_t ch) override
Definition: StrPrint.h:15
void reset()
Definition: StrPrint.h:42
void consume(int n)
Definition: StrPrint.h:44
void setExpandEncoded(bool flag)
Definition: StrPrint.h:46
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:383
String implementation which keeps the data on the heap. We grow the allocated memory only if the copy...
Definition: Str.h:22
const char * c_str()
provides the string value as const char*
Definition: Str.h:187
void reset()
Definition: Str.h:190
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
int size()
Definition: Vector.h:167
Helper that implements a Print interface to accumulate XML data and then parse it using XMLParser.
Definition: XMLParserPrint.h:12
struct tiny_dlna::XMLParserPrint::CBRef cbref
StrPrint buffer
Definition: XMLParserPrint.h:57
const char * c_str()
Definition: XMLParserPrint.h:52
void reset()
Definition: XMLParserPrint.h:50
size_t write(uint8_t ch) override
Definition: XMLParserPrint.h:21
void setExpandEncoded(bool flag)
Forward expand-entities setting to the underlying XMLParser.
Definition: XMLParserPrint.h:28
static void wrapperCallback(Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref)
Definition: XMLParserPrint.h:73
size_t length()
Definition: XMLParserPrint.h:54
XMLParserPrint(int reserve=80)
Definition: XMLParserPrint.h:14
size_t write(const uint8_t *data, size_t size) override
Definition: XMLParserPrint.h:23
XMLParser p
Definition: XMLParserPrint.h:58
bool parse(Str &outNodeName, Vector< Str > &outPath, Str &outText, Str &outAttributes)
Definition: XMLParserPrint.h:30
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
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:6
Definition: XMLParserPrint.h:61
int len
Definition: XMLParserPrint.h:63
int start
Definition: XMLParserPrint.h:62
Str * outNode
Definition: XMLParserPrint.h:64
Str * outText
Definition: XMLParserPrint.h:66
Str * outAttrs
Definition: XMLParserPrint.h:67
Vector< Str > * outPath
Definition: XMLParserPrint.h:65