1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 /*
52 handler.cpp
53
54 Provides a handler for processing XML elements found by the reader.
55
56 The handler looks for <title> and <link> elements within <item> elements,
57 and records the text found within them. Link information stored within
58 rdf:about attributes of <item> elements is also recorded when it is
59 available.
60
61 For each item found, a signal is emitted which specifies its title and
62 link information. This may be used by user interfaces for the purpose of
63 displaying items as they are read.
64 */
65
66 #include <QtGui>
67
68 #include "handler.h"
69
70 /*
71 Reset the state of the handler to ensure that new documents are
72 read correctly.
73
74 We return true to indicate that parsing should continue.
75 */
76
startDocument()77 bool Handler::startDocument()
78 {
79 inItem = false;
80 inTitle = false;
81 inLink = false;
82
83 return true;
84 }
85
86 /*
87 Process each starting element in the XML document.
88
89 Nested item, title, or link elements are not allowed, so we return false
90 if we encounter any of these. We also prohibit multiple definitions of
91 title strings.
92
93 Link destinations are read by this function if they are specified as
94 attributes in item elements.
95
96 For all cases not explicitly checked for, we return true to indicate that
97 the element is acceptable, and that parsing should continue. By doing
98 this, we can ignore elements in which we are not interested.
99 */
100
startElement(const QString &,const QString &,const QString & qName,const QXmlAttributes & attr)101 bool Handler::startElement(const QString &, const QString &,
102 const QString & qName, const QXmlAttributes &attr)
103 {
104 if (qName == "item") {
105
106 if (inItem)
107 return false;
108 else {
109 inItem = true;
110 linkString = attr.value("rdf:about");
111 }
112 }
113 else if (qName == "title") {
114
115 if (inTitle)
116 return false;
117 else if (!titleString.isEmpty())
118 return false;
119 else if (inItem)
120 inTitle = true;
121 }
122 else if (qName == "link") {
123
124 if (inLink)
125 return false;
126 else if (inItem)
127 inLink = true;
128 }
129
130 return true;
131 }
132
133 /*
134 Process each ending element in the XML document.
135
136 For recognized elements, we reset flags to ensure that we can read new
137 instances of these elements. If we have read an item element, emit a
138 signal to indicate that a new item is available for display.
139
140 We return true to indicate that parsing should continue.
141 */
142
endElement(const QString &,const QString &,const QString & qName)143 bool Handler::endElement(const QString &, const QString &,
144 const QString & qName)
145 {
146 if (qName == "title" && inTitle)
147 inTitle = false;
148 else if (qName == "link" && inLink)
149 inLink = false;
150 else if (qName == "item") {
151 if (!titleString.isEmpty() && !linkString.isEmpty())
152 emit newItem(titleString, linkString);
153 inItem = false;
154 titleString = "";
155 linkString = "";
156 }
157
158 return true;
159 }
160
161 /*
162 Collect characters when reading the contents of title or link elements
163 when they occur within an item element.
164
165 We return true to indicate that parsing should continue.
166 */
167
characters(const QString & chars)168 bool Handler::characters (const QString &chars)
169 {
170 if (inTitle)
171 titleString += chars;
172 else if (inLink)
173 linkString += chars;
174
175 return true;
176 }
177
178 /*
179 Report a fatal parsing error, and return false to indicate to the reader
180 that parsing should stop.
181 */
182
183 //! [0]
fatalError(const QXmlParseException & exception)184 bool Handler::fatalError (const QXmlParseException & exception)
185 {
186 qWarning() << "Fatal error on line" << exception.lineNumber()
187 << ", column" << exception.columnNumber() << ':'
188 << exception.message();
189
190 return false;
191 }
192 //! [0]
193