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 SYNDICATION_NO_EXPORT Content::ContentPrivate
21 {
22 public:
23  ContentPrivate()
24  : formatIdentified(false)
25  {
26  }
27  mutable Format format;
28  mutable bool formatIdentified;
29 };
30 
31 Content::Content()
32  : ElementWrapper()
33  , d(new ContentPrivate)
34 {
35 }
36 
37 Content::Content(const QDomElement &element)
38  : ElementWrapper(element)
39  , d(new ContentPrivate)
40 {
41 }
42 
43 Content::Content(const Content &other)
44  : ElementWrapper(other)
45  , d(other.d)
46 {
47 }
48 
49 Content::~Content()
50 {
51 }
52 
53 Content &Content::operator=(const Content &other)
54 {
55  ElementWrapper::operator=(other);
56  d = other.d;
57  return *this;
58 }
59 
60 QString Content::type() const
61 {
62  return attribute(QStringLiteral("type"));
63 }
64 
65 QString Content::src() const
66 {
67  return completeURI(attribute(QStringLiteral("src")));
68 }
69 
70 QByteArray Content::asByteArray() const
71 {
72  if (!isBinary()) {
73  return QByteArray();
74  }
75  return QByteArray::fromBase64(text().trimmed().toLatin1());
76 }
77 
78 Content::Format Content::mapTypeToFormat(const QString &typep, const QString &src)
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")
94  || (type.startsWith(QLatin1String("text/"), Qt::CaseInsensitive)
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)
115  || type.endsWith(QLatin1String("+xml"), Qt::CaseInsensitive)
116  || type.endsWith(QLatin1String("/xml"), Qt::CaseInsensitive)) { /* clang-format on */
117  return XML;
118  }
119 
120  return Binary;
121 }
122 
123 Content::Format Content::format() const
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 
137 bool Content::isContained() const
138 {
139  return src().isEmpty();
140 }
141 
142 bool Content::isPlainText() const
143 {
144  return format() == PlainText;
145 }
146 
147 bool Content::isEscapedHTML() const
148 {
149  return format() == EscapedHTML;
150 }
151 
152 bool Content::isXML() const
153 {
154  return format() == XML;
155 }
156 
157 QString Content::asString() const
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 
172 QString Content::debugInfo() const
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)
QString number(int n, int base)
CaseInsensitive
Type type(const QSqlDatabase &db)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QString trimmed() const const
void reserve(int alloc)
bool isEmpty() const const
QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:57:11 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.