Syndication

atom/document.cpp
1 /*
2  This file is part of the syndication library
3  SPDX-FileCopyrightText: 2006 Frank Osterfeld <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "document.h"
9 #include "category.h"
10 #include "constants.h"
11 #include "entry.h"
12 #include "generator.h"
13 #include "link.h"
14 #include "person.h"
15 #include "atomtools.h"
16 
17 #include <documentvisitor.h>
18 #include <tools.h>
19 
20 #include <QDomElement>
21 #include <QList>
22 #include <QString>
23 
24 #include <vector>
25 
26 namespace Syndication
27 {
28 namespace Atom
29 {
30 
32 {
33 }
34 
36 {
37 }
38 
40 {
41  return visitor->visitAtomFeedDocument(this);
42 }
43 
45 {
48  QStringLiteral("author"));
49  QList<Person> list;
50  list.reserve(a.count());
51 
54 
55  for (; it != end; ++it) {
56  list.append(Person(*it));
57  }
58 
59  return list;
60 }
61 
63 {
66  QStringLiteral("contributor"));
67  QList<Person> list;
68  list.reserve(a.count());
69 
72 
73  for (; it != end; ++it) {
74  list.append(Person(*it));
75  }
76 
77  return list;
78 }
79 
81 {
84  QStringLiteral("category"));
85  QList<Category> list;
86  list.reserve(a.count());
87 
90 
91  for (; it != end; ++it) {
92  list.append(Category(*it));
93  }
94 
95  return list;
96 }
97 
99 {
101  QStringLiteral("generator")));
102 }
103 
105 {
106  const QString iconPath = extractElementTextNS(atom1Namespace(),
107  QStringLiteral("icon"));
108  if (iconPath.isEmpty()) {
109  return {};
110  }
111  return completeURI(iconPath);
112 
113 }
114 
116 {
118  QStringLiteral("logo")));
119 }
120 
122 {
124  QStringLiteral("id"));
125 }
126 
128 {
129 
130  return extractAtomText(*this, QStringLiteral("rights"));
131 }
132 
134 {
135  return extractAtomText(*this, QStringLiteral("title"));
136 }
137 
139 {
140  return extractAtomText(*this, QStringLiteral("subtitle"));
141 }
142 
143 time_t FeedDocument::updated() const
144 {
146  QStringLiteral("updated"));
147  return parseDate(upd, ISODate);
148 }
149 
151 {
154  QStringLiteral("link"));
155  QList<Link> list;
156  list.reserve(a.count());
157 
160 
161  for (; it != end; ++it) {
162  list.append(Link(*it));
163  }
164 
165  return list;
166 }
167 
169 {
172  QStringLiteral("entry"));
173  QList<Entry> list;
174  list.reserve(a.count());
175 
176  QList<Person> feedAuthors = authors();
179 
180  for (; it != end; ++it) {
181  Entry entry(*it);
182  entry.setFeedAuthors(feedAuthors);
183  list.append(entry);
184  }
185 
186  return list;
187 }
188 
190 {
191  // TODO: do not hardcode this list here
192  static std::vector<ElementType> handled; // QVector would require a default ctor, and ElementType is too big for QList
193  if (handled.empty()) {
194  handled.reserve(13);
195  handled.push_back(ElementType(QStringLiteral("author"), atom1Namespace()));
196  handled.push_back(ElementType(QStringLiteral("contributor"), atom1Namespace()));
197  handled.push_back(ElementType(QStringLiteral("category"), atom1Namespace()));
198  handled.push_back(ElementType(QStringLiteral("generator"), atom1Namespace()));
199  handled.push_back(ElementType(QStringLiteral("icon"), atom1Namespace()));
200  handled.push_back(ElementType(QStringLiteral("logo"), atom1Namespace()));
201  handled.push_back(ElementType(QStringLiteral("id"), atom1Namespace()));
202  handled.push_back(ElementType(QStringLiteral("rights"), atom1Namespace()));
203  handled.push_back(ElementType(QStringLiteral("title"), atom1Namespace()));
204  handled.push_back(ElementType(QStringLiteral("subtitle"), atom1Namespace()));
205  handled.push_back(ElementType(QStringLiteral("updated"), atom1Namespace()));
206  handled.push_back(ElementType(QStringLiteral("link"), atom1Namespace()));
207  handled.push_back(ElementType(QStringLiteral("entry"), atom1Namespace()));
208  }
209 
210  QList<QDomElement> notHandled;
211 
212  QDomNodeList children = element().childNodes();
213  const int numChildren = children.size();
214  for (int i = 0; i < numChildren; ++i) {
215  QDomElement el = children.at(i).toElement();
216  if (!el.isNull()
217  && std::find(handled.cbegin(), handled.cend(), ElementType(el.localName(), el.namespaceURI())) == handled.cend()) {
218  notHandled.append(el);
219  }
220  }
221 
222  return notHandled;
223 }
224 
226 {
227  return !isNull();
228 }
229 
231 {
232  QString info = QLatin1String("### FeedDocument: ###################\n");
233  if (!title().isEmpty()) {
234  info += QLatin1String("title: #") + title() + QLatin1String("#\n");
235  }
236  if (!subtitle().isEmpty()) {
237  info += QLatin1String("subtitle: #") + subtitle() + QLatin1String("#\n");
238  }
239  if (!id().isEmpty()) {
240  info += QLatin1String("id: #") + id() + QLatin1String("#\n");
241  }
242 
243  if (!rights().isEmpty()) {
244  info += QLatin1String("rights: #") + rights() + QLatin1String("#\n");
245  }
246  if (!icon().isEmpty()) {
247  info += QLatin1String("icon: #") + icon() + QLatin1String("#\n");
248  }
249  if (!logo().isEmpty()) {
250  info += QLatin1String("logo: #") + logo() + QLatin1String("#\n");
251  }
252  if (!generator().isNull()) {
253  info += generator().debugInfo();
254  }
255 
256  QString dupdated = dateTimeToString(updated());
257  if (!dupdated.isNull()) {
258  info += QLatin1String("updated: #") + dupdated + QLatin1String("#\n");
259  }
260 
261  QList<Link> dlinks = links();
262  QList<Link>::ConstIterator endlinks = dlinks.constEnd();
263  for (QList<Link>::ConstIterator it = dlinks.constBegin(); it != endlinks; ++it) {
264  info += (*it).debugInfo();
265  }
266 
267  QList<Category> dcats = categories();
268  QList<Category>::ConstIterator endcats = dcats.constEnd();
269  for (QList<Category>::ConstIterator it = dcats.constBegin(); it != endcats; ++it) {
270  info += (*it).debugInfo();
271  }
272 
273  info += QLatin1String("### Authors: ###################\n");
274 
275  QList<Person> dauthors = authors();
276  QList<Person>::ConstIterator endauthors = dauthors.constEnd();
277  for (QList<Person>::ConstIterator it = dauthors.constBegin(); it != endauthors; ++it) {
278  info += (*it).debugInfo();
279  }
280 
281  info += QLatin1String("### Contributors: ###################\n");
282 
283  QList<Person> dcontri = contributors();
284  QList<Person>::ConstIterator endcontri = dcontri.constEnd();
285  for (QList<Person>::ConstIterator it = dcontri.constBegin(); it != endcontri; ++it) {
286  info += (*it).debugInfo();
287  }
288 
289  QList<Entry> dentries = entries();
290  QList<Entry>::ConstIterator endentries = dentries.constEnd();
291  for (QList<Entry>::ConstIterator it = dentries.constBegin(); it != endentries; ++it) {
292  info += (*it).debugInfo();
293  }
294 
295  info += QLatin1String("### FeedDocument end ################\n");
296 
297  return info;
298 }
299 
301 {
302 }
303 
305 {
306 }
307 
309 {
310  return visitor->visitAtomEntryDocument(this);
311 }
312 
314 {
315  return Entry(element());
316 }
317 
319 {
320  return !isNull();
321 }
322 
324 {
325  QString info;
326  info += QLatin1String("### EntryDocument: ##################\n");
327 
328  Entry dentry = entry();
329  if (!dentry.isNull()) {
330  info += dentry.debugInfo();
331  }
332 
333  info += QLatin1String("### EntryDocument end ###############\n");
334  return info;
335 }
336 
337 } // namespace Atom
338 } // namespace Syndication
QList< QDomElement > elementsByTagNameNS(const QString &nsURI, const QString &tagName) const
returns all child elements with tag name tagname and namespace URI nsURI.
A category for categorizing items or whole feeds.
Definition: atom/category.h:32
Description of the agent used to generate the feed.
Definition: generator.h:26
QString rights() const
copyright information (optional)
QList< Person > authors() const
a list of persons who are the authors of this feed.
QString debugInfo() const override
returns a description of this feed document for debugging purposes.
A wrapper for XML elements.
void reserve(int alloc)
QList< Category > categories() const
a list of categories this feed is assigned to (optional)
QString namespaceURI() const const
bool isValid() const override
returns whether this document is valid or not.
QList< Link > links() const
a list of links.
QString debugInfo() const
a description of this generator for debugging purposes.
Definition: generator.cpp:41
QDomElement firstElementByTagNameNS(const QString &nsURI, const QString &tagName) const
searches the direct children of the wrapped element for an element with a given namespace and tag nam...
QDomNodeList childNodes() const const
bool isNull() const const
bool isValid() const override
returns whether this document is valid or not.
QDomElement toElement() const const
QString title() const
feed title (required).
bool accept(DocumentVisitor *visitor) override
Used by visitors for double dispatch.
int count(const T &value) const const
void append(const T &value)
QString localName() const const
FeedDocument()
default constructor, creates a null feed, which is invalid.
virtual bool visitAtomEntryDocument(Syndication::Atom::EntryDocument *document)
reimplement this method to handle Atom entry documents.
QList< Entry > entries() const
a list of the entries (items) in this feed.
EntryDocument()
default constructor, creates a null document, which is invalid.
bool isEmpty() const const
KCALUTILS_EXPORT QString dateTimeToString(const QDateTime &date, bool dateOnly=false, bool shortfmt=true)
QString atom1Namespace()
namespace used by Atom 1.0 elements
void setFeedAuthors(const QList< Person > &feedAuthors)
Sets the list of the containing feed&#39;s authors, which will be used as a fallback in authors() in case...
Definition: entry.cpp:38
QString logo() const
URL of an image serving as a feed logo (optional)
QList< QDomElement > unhandledElements() const
returns all child elements of this feed not covered by this class.
QString extractAtomText(const Syndication::ElementWrapper &parent, const QString &tagname)
extracts the content of an atomTextConstruct.
Definition: atomtools.cpp:23
QString icon() const
URL of an image serving as a feed icon (optional)
bool isNull() const
returns whether the wrapped element is a null element
QString completeURI(const QString &uri) const
completes relative URIs with a prefix specified via xml:base.
bool isNull() const const
an Atom entry, equivalent to the "items" in the RSS world.
Definition: entry.h:40
virtual bool visitAtomFeedDocument(Syndication::Atom::FeedDocument *document)
reimplement this method to handle Atom feed documents (most Atom feeds are of this type)...
QString subtitle() const
description or subtitle of the feed (optional).
QString id() const
a string that unambigously identifies the feed (required)
QString debugInfo() const override
returns a description of this entry document for debugging purposes.
bool accept(DocumentVisitor *visitor) override
Used by visitors for double dispatch.
const QDomElement & element() const
returns the wrapped resource.
QString extractElementTextNS(const QString &namespaceURI, const QString &localName) const
extracts the text from a child element, respecting namespaces.
Visitor interface, following the Visitor design pattern.
describes a person, with name and optional URI and e-mail address.
Definition: atom/person.h:27
QString debugInfo() const
returns a description of this entry for debugging purposes
Definition: entry.cpp:206
int size() const const
QList::const_iterator constEnd() const const
Generator generator() const
description of the agent used to generate the feed.
QList::const_iterator constBegin() const const
Entry entry() const
returns the single entry described in the source.
QList< Person > contributors() const
a list of persons who contribute to this feed.
time_t updated() const
The datetime of the last modification of the feed content.
QDomNode at(int index) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 12 2020 23:02:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.