Arduino DLNA Server
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
tiny_dlna::XMLParser Class Reference

Lightweight streaming XML parser. More...

#include <XMLParser.h>

Collaboration diagram for tiny_dlna::XMLParser:
Collaboration graph
[legend]

Public Member Functions

 XMLParser ()=default
 
 XMLParser (const char *xmlStr, void(*callback)(Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref), bool textOnly=false)
 Construct with XML buffer and callback.
 
void setReference (void *ref)
 Attach an opaque user pointer to the parser instance.
 
void setXml (const char *xmlStr)
 Set the XML buffer to parse.
 
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.
 
void parse ()
 Parse the previously set XML buffer and invoke the callback.
 
bool parseSingle ()
 Parse a single fragment (one callback invocation) from the previously set XML buffer.
 
void resetParse ()
 Reset the internal parse position so subsequent parseSingle() calls start from the beginning of the buffer.
 
void resetParser ()
 Fully reset parser state (parse position, path stack and content starts). Use this when the underlying buffer has been trimmed/changed so the parser's internal stacks do not retain state from the previous buffer view.
 
void setReportTextOnly (bool flag)
 report only nodes with text
 
int getParsePos ()
 Expose current parse position for incremental wrappers.
 

Protected Member Functions

int findGt (const char *s, int start, int len)
 
void emitTextSegment (const char *s, int ts, int te)
 
void emitTagSegment (const char *s, int lt, int gt)
 
int handleEndTag (const char *s, int lt, int gt)
 
void handleStartTag (const char *s, int lt, int gt)
 
int handleCommentOrPI (int lt, const char *s, int len)
 
bool invokeCallback (Str &nodeName, Vector< Str > &fullPath, Str &text, Str &attributes, int start, int len)
 
void do_parse ()
 
bool do_parse_single ()
 

Protected Attributes

StrView str_view
 
Vector< Strpath {5}
 
Str empty_str {0}
 
Str node_name {40}
 
Str txt {100}
 
Str str {100}
 
Str last_attributes
 
bool report_text_only = true
 
void(* callback )(Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref) = nullptr
 
Vector< int > contentStarts
 
void * reference = nullptr
 
int parsePos = 0
 

Detailed Description

Lightweight streaming XML parser.

XMLParser performs a single-pass, forgiving parse of a provided XML buffer and calls a user-provided callback for discovered fragments. It is intentionally small and suitable for embedded use-cases in this project (SCPD fragments, DIDL snippets, etc.). It does not implement the full XML specification (no DTDs, no entity decoding, minimal attribute handling).

The callback receives both a small copied Str for convenience and the numeric start/length offsets into the original buffer so callers can avoid additional copies when desired.

Threading / lifetime notes:

Constructor & Destructor Documentation

◆ XMLParser() [1/2]

tiny_dlna::XMLParser::XMLParser ( )
default

Default constructor. Use setXml() and setCallback() before parse().

◆ XMLParser() [2/2]

tiny_dlna::XMLParser::XMLParser ( const char *  xmlStr,
void(*)(Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref)  callback,
bool  textOnly = false 
)
inline

Construct with XML buffer and callback.

Parameters
xmlStrNull-terminated XML buffer. The buffer is not copied; it must remain valid for the duration of parse().
callbackFunction pointer invoked for node events. Signature: void callback(const Str& nodeName, const Vector<Str>& path, const Str& text, int start, int len, void* ref)
  • nodeName: name of the current XML node (empty for no current node)
  • path: current element path (top is the current element)
  • text: element inner text for end-tag events (or trimmed text for text nodes)
  • start,len: numeric offset/length in the original buffer for the reported fragment
textOnlyIf true, only report nodes with text content.

Member Function Documentation

◆ do_parse()

void tiny_dlna::XMLParser::do_parse ( )
inlineprotected

◆ do_parse_single()

bool tiny_dlna::XMLParser::do_parse_single ( )
inlineprotected

◆ emitTagSegment()

void tiny_dlna::XMLParser::emitTagSegment ( const char *  s,
int  lt,
int  gt 
)
inlineprotected

◆ emitTextSegment()

void tiny_dlna::XMLParser::emitTextSegment ( const char *  s,
int  ts,
int  te 
)
inlineprotected

◆ findGt()

int tiny_dlna::XMLParser::findGt ( const char *  s,
int  start,
int  len 
)
inlineprotected

◆ getParsePos()

int tiny_dlna::XMLParser::getParsePos ( )
inline

Expose current parse position for incremental wrappers.

◆ handleCommentOrPI()

int tiny_dlna::XMLParser::handleCommentOrPI ( int  lt,
const char *  s,
int  len 
)
inlineprotected

◆ handleEndTag()

int tiny_dlna::XMLParser::handleEndTag ( const char *  s,
int  lt,
int  gt 
)
inlineprotected

◆ handleStartTag()

void tiny_dlna::XMLParser::handleStartTag ( const char *  s,
int  lt,
int  gt 
)
inlineprotected

◆ invokeCallback()

bool tiny_dlna::XMLParser::invokeCallback ( Str nodeName,
Vector< Str > &  fullPath,
Str text,
Str attributes,
int  start,
int  len 
)
inlineprotected

◆ parse()

void tiny_dlna::XMLParser::parse ( )
inline

Parse the previously set XML buffer and invoke the callback.

The callback is invoked synchronously while parse() runs. After parse() returns there are no outstanding callbacks and the caller may reuse or free the XML buffer.

◆ parseSingle()

bool tiny_dlna::XMLParser::parseSingle ( )
inline

Parse a single fragment (one callback invocation) from the previously set XML buffer.

This allows incremental parsing of a larger buffer by repeatedly calling parseSingle() until it returns false. The parser will resume from the last position between calls. Call resetParse() to start parsing from the beginning again.

Returns
true if a fragment was parsed and the callback invoked, false if no more parseable fragments are available.

◆ resetParse()

void tiny_dlna::XMLParser::resetParse ( )
inline

Reset the internal parse position so subsequent parseSingle() calls start from the beginning of the buffer.

◆ resetParser()

void tiny_dlna::XMLParser::resetParser ( )
inline

Fully reset parser state (parse position, path stack and content starts). Use this when the underlying buffer has been trimmed/changed so the parser's internal stacks do not retain state from the previous buffer view.

◆ setCallback()

void tiny_dlna::XMLParser::setCallback ( void(*)(Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref)  cb)
inline

Set the callback to be invoked for parsed fragments.

Parameters
cbFunction pointer with signature described in the constructor docs.

◆ setReference()

void tiny_dlna::XMLParser::setReference ( void *  ref)
inline

Attach an opaque user pointer to the parser instance.

Parameters
refArbitrary user data. The parser does not own or manipulate this pointer; it is provided for callbacks or external use.

◆ setReportTextOnly()

void tiny_dlna::XMLParser::setReportTextOnly ( bool  flag)
inline

report only nodes with text

◆ setXml()

void tiny_dlna::XMLParser::setXml ( const char *  xmlStr)
inline

Set the XML buffer to parse.

Parameters
xmlStrNull-terminated XML buffer. The parser stores a non-owning view — the buffer must remain valid until parsing finishes.

Member Data Documentation

◆ callback

void(* tiny_dlna::XMLParser::callback) (Str &nodeName, Vector< Str > &path, Str &text, Str &attributes, int start, int len, void *ref) = nullptr
protected

◆ contentStarts

Vector<int> tiny_dlna::XMLParser::contentStarts
protected

◆ empty_str

Str tiny_dlna::XMLParser::empty_str {0}
protected

◆ last_attributes

Str tiny_dlna::XMLParser::last_attributes
protected

◆ node_name

Str tiny_dlna::XMLParser::node_name {40}
protected

◆ parsePos

int tiny_dlna::XMLParser::parsePos = 0
protected

◆ path

Vector<Str> tiny_dlna::XMLParser::path {5}
protected

◆ reference

void* tiny_dlna::XMLParser::reference = nullptr
protected

◆ report_text_only

bool tiny_dlna::XMLParser::report_text_only = true
protected

◆ str

Str tiny_dlna::XMLParser::str {100}
protected

◆ str_view

StrView tiny_dlna::XMLParser::str_view
protected

◆ txt

Str tiny_dlna::XMLParser::txt {100}
protected

The documentation for this class was generated from the following file: