| 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 | |
| 16 | namespace Syndication |
| 17 | { |
| 18 | namespace Atom |
| 19 | { |
| 20 | class SYNDICATION_NO_EXPORT Content::ContentPrivate |
| 21 | { |
| 22 | public: |
| 23 | ContentPrivate() |
| 24 | : format(Format::PlainText) |
| 25 | , formatIdentified(false) |
| 26 | { |
| 27 | } |
| 28 | mutable Format format; |
| 29 | mutable bool formatIdentified; |
| 30 | }; |
| 31 | |
| 32 | Content::Content() |
| 33 | : ElementWrapper() |
| 34 | , d(new ContentPrivate) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | Content::Content(const QDomElement &element) |
| 39 | : ElementWrapper(element) |
| 40 | , d(new ContentPrivate) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Content::Content(const Content &other) |
| 45 | : ElementWrapper(other) |
| 46 | , d(other.d) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | Content::~Content() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | Content &Content::operator=(const Content &other) |
| 55 | { |
| 56 | ElementWrapper::operator=(other); |
| 57 | d = other.d; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | QString Content::type() const |
| 62 | { |
| 63 | return attribute(QStringLiteral("type" )); |
| 64 | } |
| 65 | |
| 66 | QString Content::src() const |
| 67 | { |
| 68 | return completeURI(uri: attribute(QStringLiteral("src" ))); |
| 69 | } |
| 70 | |
| 71 | QByteArray Content::asByteArray() const |
| 72 | { |
| 73 | if (!isBinary()) { |
| 74 | return QByteArray(); |
| 75 | } |
| 76 | return QByteArray::fromBase64(base64: text().trimmed().toLatin1()); |
| 77 | } |
| 78 | |
| 79 | Content::Format Content::mapTypeToFormat(const QString &typep, const QString &src) |
| 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" ) |
| 95 | || (type.startsWith(s: QLatin1String("text/" ), cs: Qt::CaseInsensitive) |
| 96 | && !type.startsWith(s: QLatin1String("text/xml" ), cs: Qt::CaseInsensitive))) { /* clang-format on */ |
| 97 | return PlainText; |
| 98 | } |
| 99 | |
| 100 | static QStringList xmltypes; |
| 101 | if (xmltypes.isEmpty()) { |
| 102 | xmltypes.reserve(asize: 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(str: type) |
| 116 | || type.endsWith(s: QLatin1String("+xml" ), cs: Qt::CaseInsensitive) |
| 117 | || type.endsWith(s: QLatin1String("/xml" ), cs: Qt::CaseInsensitive)) { /* clang-format on */ |
| 118 | return XML; |
| 119 | } |
| 120 | |
| 121 | return Binary; |
| 122 | } |
| 123 | |
| 124 | Content::Format Content::format() const |
| 125 | { |
| 126 | if (d->formatIdentified == false) { |
| 127 | d->format = mapTypeToFormat(typep: type(), src: src()); |
| 128 | d->formatIdentified = true; |
| 129 | } |
| 130 | return d->format; |
| 131 | } |
| 132 | |
| 133 | bool Content::isBinary() const |
| 134 | { |
| 135 | return format() == Binary; |
| 136 | } |
| 137 | |
| 138 | bool Content::isContained() const |
| 139 | { |
| 140 | return src().isEmpty(); |
| 141 | } |
| 142 | |
| 143 | bool Content::isPlainText() const |
| 144 | { |
| 145 | return format() == PlainText; |
| 146 | } |
| 147 | |
| 148 | bool Content::isEscapedHTML() const |
| 149 | { |
| 150 | return format() == EscapedHTML; |
| 151 | } |
| 152 | |
| 153 | bool Content::isXML() const |
| 154 | { |
| 155 | return format() == XML; |
| 156 | } |
| 157 | |
| 158 | QString Content::asString() const |
| 159 | { |
| 160 | Format f = format(); |
| 161 | |
| 162 | if (f == PlainText) { |
| 163 | return plainTextToHtml(plainText: 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 | |
| 173 | QString Content::debugInfo() const |
| 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 |
| 192 | |