Line data Source code
1 : #ifndef RSSATOMPARSER_H
2 : #define RSSATOMPARSER_H
3 :
4 : #include <memory>
5 :
6 : #include <QString>
7 : #include <QStringView>
8 : #include <QByteArray>
9 : #include <QXmlStreamReader>
10 : #include <QStack>
11 :
12 : #include "RawFeed.h"
13 :
14 : /*!
15 : \brief Synchronous RSS/Atom XML parser.
16 :
17 : Public API: call RSSAtomParser::parse(data) to get a RawFeed.
18 : */
19 : class RSSAtomParser
20 : {
21 : public:
22 : // Parse a complete RSS/Atom XML document and return a RawFeed.
23 : static std::unique_ptr<RawFeed> parse(const QByteArray& data);
24 :
25 : private:
26 96 : RSSAtomParser() = default;
27 :
28 : struct ParseState {
29 : int numItems = 0;
30 : bool hasPodcastSignals = false;
31 : bool inAtomXHTML = false;
32 : QStack<QString> tagStack;
33 :
34 : // Per-element (set fresh on each start element)
35 : QString currentTag;
36 : QString currentPrefix;
37 : bool hasType = false;
38 :
39 : // Per-item accumulated text (cleared between items)
40 : QString title;
41 : QString subtitle;
42 : QString content;
43 : QString author;
44 : QString urlHref;
45 : QString urlData;
46 : QString pubdate;
47 : QString lastbuilddate;
48 : QString created;
49 : QString updated;
50 : QString date;
51 : QString guid;
52 : QString id;
53 : QString mediaImageURL;
54 : int mediaImageWidth = 0;
55 :
56 : void clearItemFields();
57 : };
58 :
59 : /*!
60 : \brief Parse a start element, e.g. <b>
61 : */
62 : void elementStart();
63 :
64 : /*!
65 : \brief Parse an end element, e.g. </b>
66 : */
67 : void elementEnd();
68 :
69 : /*!
70 : \brief Parse the contents, e.g. <tag>contents goes here</tag>
71 : */
72 : void elementContents();
73 :
74 : // Call this when we have a summary.
75 : void saveSummary();
76 :
77 : /*!
78 : \return The nth value in the tag stack, or an empty view.
79 : */
80 : QStringView getTagStackAt(qint32 n);
81 :
82 : std::unique_ptr<RawFeed> feed;
83 : std::shared_ptr<RawNews> currentItem;
84 : bool isValid = false;
85 : QXmlStreamReader xml;
86 : ParseState state;
87 : };
88 :
89 : #endif // RSSATOMPARSER_H
|