Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
handler.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4/*
5handler.cpp
6
7Provides a handler for processing XML elements found by the reader.
8
9The handler looks for <title> and <link> elements within <item> elements,
10and records the text found within them. Link information stored within
11rdf:about attributes of <item> elements is also recorded when it is
12available.
13
14For each item found, a signal is emitted which specifies its title and
15link information. This may be used by user interfaces for the purpose of
16displaying items as they are read.
17*/
18
19#include <QtGui>
20
21#include "handler.h"
22
23/*
24 Reset the state of the handler to ensure that new documents are
25 read correctly.
26
27 We return true to indicate that parsing should continue.
28*/
29
30bool Handler::startDocument()
31{
32 inItem = false;
33 inTitle = false;
34 inLink = false;
35
36 return true;
37}
38
39/*
40 Process each starting element in the XML document.
41
42 Nested item, title, or link elements are not allowed, so we return false
43 if we encounter any of these. We also prohibit multiple definitions of
44 title strings.
45
46 Link destinations are read by this function if they are specified as
47 attributes in item elements.
48
49 For all cases not explicitly checked for, we return true to indicate that
50 the element is acceptable, and that parsing should continue. By doing
51 this, we can ignore elements in which we are not interested.
52*/
53
54bool Handler::startElement(const QString &, const QString &,
55 const QString & qName, const QXmlAttributes &attr)
56{
57 if (qName == "item") {
58
59 if (inItem)
60 return false;
61 else {
62 inItem = true;
63 linkString = attr.value("rdf:about");
64 }
65 }
66 else if (qName == "title") {
67
68 if (inTitle)
69 return false;
70 else if (!titleString.isEmpty())
71 return false;
72 else if (inItem)
73 inTitle = true;
74 }
75 else if (qName == "link") {
76
77 if (inLink)
78 return false;
79 else if (inItem)
80 inLink = true;
81 }
82
83 return true;
84}
85
86/*
87 Process each ending element in the XML document.
88
89 For recognized elements, we reset flags to ensure that we can read new
90 instances of these elements. If we have read an item element, emit a
91 signal to indicate that a new item is available for display.
92
93 We return true to indicate that parsing should continue.
94*/
95
96bool Handler::endElement(const QString &, const QString &,
97 const QString & qName)
98{
99 if (qName == "title" && inTitle)
100 inTitle = false;
101 else if (qName == "link" && inLink)
102 inLink = false;
103 else if (qName == "item") {
104 if (!titleString.isEmpty() && !linkString.isEmpty())
105 emit newItem(titleString, linkString);
106 inItem = false;
107 titleString = "";
108 linkString = "";
109 }
110
111 return true;
112}
113
114/*
115 Collect characters when reading the contents of title or link elements
116 when they occur within an item element.
117
118 We return true to indicate that parsing should continue.
119*/
120
121bool Handler::characters (const QString &chars)
122{
123 if (inTitle)
124 titleString += chars;
125 else if (inLink)
126 linkString += chars;
127
128 return true;
129}
130
131/*
132 Report a fatal parsing error, and return false to indicate to the reader
133 that parsing should stop.
134*/
135
136bool Handler::fatalError (const QXmlParseException & exception)
137{
138 qWarning() << "Fatal error on line" << exception.lineNumber()
139 << ", column" << exception.columnNumber() << ':'
140 << exception.message();
141
142 return false;
143}
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
#define qWarning
Definition qlogging.h:162
#define emit