| 1 | /* |
|---|---|
| 2 | This file is part of the syndication library |
| 3 | SPDX-FileCopyrightText: 2005 Frank Osterfeld <osterfeld@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "documentsource.h" |
| 9 | #include "tools.h" |
| 10 | |
| 11 | #include <QByteArray> |
| 12 | #include <QDebug> |
| 13 | #include <QDomDocument> |
| 14 | |
| 15 | namespace Syndication |
| 16 | { |
| 17 | class SYNDICATION_NO_EXPORT DocumentSource::DocumentSourcePrivate |
| 18 | { |
| 19 | public: |
| 20 | QByteArray array; |
| 21 | QString url; |
| 22 | mutable QDomDocument domDoc; |
| 23 | mutable bool parsed; |
| 24 | mutable unsigned int hash; |
| 25 | mutable bool calculatedHash; |
| 26 | }; |
| 27 | |
| 28 | DocumentSource::DocumentSource() |
| 29 | : d(new DocumentSourcePrivate) |
| 30 | { |
| 31 | d->parsed = true; |
| 32 | d->calculatedHash = true; |
| 33 | d->hash = 0; |
| 34 | } |
| 35 | |
| 36 | DocumentSource::DocumentSource(const QByteArray &source, const QString &url) |
| 37 | : d(new DocumentSourcePrivate) |
| 38 | { |
| 39 | d->array = source; |
| 40 | d->url = url; |
| 41 | d->calculatedHash = false; |
| 42 | d->parsed = false; |
| 43 | } |
| 44 | |
| 45 | DocumentSource::DocumentSource(const DocumentSource &other) |
| 46 | : d() |
| 47 | { |
| 48 | *this = other; |
| 49 | } |
| 50 | |
| 51 | DocumentSource::~DocumentSource() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | DocumentSource &DocumentSource::operator=(const DocumentSource &other) |
| 56 | { |
| 57 | d = other.d; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | QByteArray DocumentSource::asByteArray() const |
| 62 | { |
| 63 | return d->array; |
| 64 | } |
| 65 | |
| 66 | QDomDocument DocumentSource::asDomDocument() const |
| 67 | { |
| 68 | if (!d->parsed) { |
| 69 | const auto result = d->domDoc.setContent(data: d->array, options: QDomDocument::ParseOption::UseNamespaceProcessing); |
| 70 | if (!result) { |
| 71 | qWarning() << result.errorMessage << "on line"<< result.errorLine; |
| 72 | d->domDoc.clear(); |
| 73 | } |
| 74 | |
| 75 | d->parsed = true; |
| 76 | } |
| 77 | |
| 78 | return d->domDoc; |
| 79 | } |
| 80 | |
| 81 | unsigned int DocumentSource::size() const |
| 82 | { |
| 83 | return d->array.size(); |
| 84 | } |
| 85 | |
| 86 | unsigned int DocumentSource::hash() const |
| 87 | { |
| 88 | if (!d->calculatedHash) { |
| 89 | d->hash = calcHash(array: d->array); |
| 90 | d->calculatedHash = true; |
| 91 | } |
| 92 | |
| 93 | return d->hash; |
| 94 | } |
| 95 | |
| 96 | QString DocumentSource::url() const |
| 97 | { |
| 98 | return d->url; |
| 99 | } |
| 100 | |
| 101 | } // namespace Syndication |
| 102 |
