Syndication

content.cpp
1/*
2 This file is part of the syndication library
3 SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "content.h"
9
10#include <tools.h>
11
12#include <QByteArray>
13#include <QDomElement>
14#include <QStringList>
15
16namespace Syndication
17{
18namespace Atom
19{
20class SYNDICATION_NO_EXPORT Content::ContentPrivate
21{
22public:
23 ContentPrivate()
24 : format(Format::PlainText)
25 , formatIdentified(false)
26 {
27 }
28 mutable Format format;
29 mutable bool formatIdentified;
30};
31
34 , d(new ContentPrivate)
35{
36}
37
39 : ElementWrapper(element)
40 , d(new ContentPrivate)
41{
42}
43
45 : ElementWrapper(other)
46 , d(other.d)
47{
48}
49
53
55{
57 d = other.d;
58 return *this;
59}
60
62{
63 return attribute(QStringLiteral("type"));
64}
65
67{
68 return completeURI(attribute(QStringLiteral("src")));
69}
70
72{
73 if (!isBinary()) {
74 return QByteArray();
75 }
76 return QByteArray::fromBase64(text().trimmed().toLatin1());
77}
78
80{
81 QString type = typep;
82 //"If neither the type attribute nor the src attribute is provided,
83 // Atom Processors MUST behave as though the type attribute were
84 // present with a value of "text""
85 if (type.isNull() && src.isEmpty()) {
86 type = QStringLiteral("text");
87 }
88
89 if (type == QLatin1String("html") || type == QLatin1String("text/html")) {
90 return EscapedHTML;
91 }
92
93 /* clang-format off */
94 if (type == QLatin1String("text")
96 && !type.startsWith(QLatin1String("text/xml"), Qt::CaseInsensitive))) { /* clang-format on */
97 return PlainText;
98 }
99
100 static QStringList xmltypes;
101 if (xmltypes.isEmpty()) {
102 xmltypes.reserve(8);
103 xmltypes.append(QStringLiteral("xhtml"));
104 xmltypes.append(QStringLiteral("application/xhtml+xml"));
105 // XML media types as defined in RFC3023:
106 xmltypes.append(QStringLiteral("text/xml"));
107 xmltypes.append(QStringLiteral("application/xml"));
108 xmltypes.append(QStringLiteral("text/xml-external-parsed-entity"));
109 xmltypes.append(QStringLiteral("application/xml-external-parsed-entity"));
110 xmltypes.append(QStringLiteral("application/xml-dtd"));
111 xmltypes.append(QStringLiteral("text/x-dtd")); // from shared-mime-info
112 }
113
114 /* clang-format off */
115 if (xmltypes.contains(type)
117 || type.endsWith(QLatin1String("/xml"), Qt::CaseInsensitive)) { /* clang-format on */
118 return XML;
119 }
120
121 return Binary;
122}
123
125{
126 if (d->formatIdentified == false) {
127 d->format = mapTypeToFormat(type(), src());
128 d->formatIdentified = true;
129 }
130 return d->format;
131}
132
134{
135 return format() == Binary;
136}
137
139{
140 return src().isEmpty();
141}
142
144{
145 return format() == PlainText;
146}
147
149{
150 return format() == EscapedHTML;
151}
152
153bool Content::isXML() const
154{
155 return format() == XML;
156}
157
159{
160 Format f = format();
161
162 if (f == PlainText) {
163 return plainTextToHtml(text()).trimmed();
164 } else if (f == EscapedHTML) {
165 return text().trimmed();
166 } else if (f == XML) {
167 return childNodesAsXML().trimmed();
168 }
169
170 return QString();
171}
172
174{
175 QString info = QLatin1String("### Content: ###################\n");
176 info += QLatin1String("type: #") + type() + QLatin1String("#\n");
177 if (!src().isNull()) {
178 info += QLatin1String("src: #") + src() + QLatin1String("#\n");
179 }
180 if (!isBinary()) {
181 info += QLatin1String("content: #") + asString() + QLatin1String("#\n");
182 } else {
183 info += QLatin1String("binary length: #") + QString::number(asByteArray().size()) + QLatin1String("#\n");
184 }
185 info += QLatin1String("### Content end ################\n");
186
187 return info;
188}
189
190} // namespace Atom
191} // namespace Syndication
The content element either contains or links the content of an entry.
Definition content.h:31
QString debugInfo() const
returns a description of the content object for debugging purposes
Definition content.cpp:173
~Content() override
destructor
Definition content.cpp:50
QByteArray asByteArray() const
returns binary content as byte array.
Definition content.cpp:71
bool isContained() const
returns whether the content is contained in the feed source, or not.
Definition content.cpp:138
bool isEscapedHTML() const
returns whether the content is escaped HTML or not Use asString() to access it
Definition content.cpp:148
bool isBinary() const
returns whether the content is binary content or not.
Definition content.cpp:133
bool isPlainText() const
returns whether the content is plain text or not.
Definition content.cpp:143
QString src() const
If src() is set, the content of this entry is not contained in the feed source, but available on the ...
Definition content.cpp:66
Content()
creates a null content object.
Definition content.cpp:32
Content & operator=(const Content &other)
assigns another content objecct
Definition content.cpp:54
bool isXML() const
returns whether the content is embedded XML.
Definition content.cpp:153
QString type() const
the type of the content.
Definition content.cpp:61
Format format() const
returns the content format
Definition content.cpp:124
QString asString() const
returns the content as string.
Definition content.cpp:158
static Format mapTypeToFormat(const QString &type, const QString &src=QString())
maps a mimetype to Format enum according to the Atom 1.0 specification
Definition content.cpp:79
Format
format of the content.
Definition content.h:36
@ XML
the content is embedded XML
Definition content.h:42
@ EscapedHTML
the content is escaped HTML, (i.e., "<", ">" etc.
Definition content.h:40
@ PlainText
the content is plain text (i.e.
Definition content.h:37
@ Binary
the content is base64-encoded binary content
Definition content.h:43
A wrapper for XML elements.
QString text() const
Returns the wrapped element's text or an empty string.
QString completeURI(const QString &uri) const
completes relative URIs with a prefix specified via xml:base.
bool isNull() const
returns whether the wrapped element is a null element
QString childNodesAsXML() const
returns the child nodes of the wrapped element as XML.
ElementWrapper & operator=(const ElementWrapper &other)
Assigns another element wrapper to this one.
QString attribute(const QString &name, const QString &defValue=QString()) const
Returns the attribute called name.
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
void append(QList< T > &&value)
bool isEmpty() const const
void reserve(qsizetype size)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool isNull() const const
QString number(double n, char format, int precision)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.