Syndication

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

KDE's Doxygen guidelines are available online.