File indexing completed on 2024-09-29 03:41:26

0001 /*
0002     This file is part of the syndication library
0003     SPDX-FileCopyrightText: 2005 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "documentsource.h"
0009 #include "tools.h"
0010 
0011 #include <QByteArray>
0012 #include <QDebug>
0013 #include <QDomDocument>
0014 
0015 namespace Syndication
0016 {
0017 class SYNDICATION_NO_EXPORT DocumentSource::DocumentSourcePrivate
0018 {
0019 public:
0020     QByteArray array;
0021     QString url;
0022     mutable QDomDocument domDoc;
0023     mutable bool parsed;
0024     mutable unsigned int hash;
0025     mutable bool calculatedHash;
0026 };
0027 
0028 DocumentSource::DocumentSource()
0029     : d(new DocumentSourcePrivate)
0030 {
0031     d->parsed = true;
0032     d->calculatedHash = true;
0033     d->hash = 0;
0034 }
0035 
0036 DocumentSource::DocumentSource(const QByteArray &source, const QString &url)
0037     : d(new DocumentSourcePrivate)
0038 {
0039     d->array = source;
0040     d->url = url;
0041     d->calculatedHash = false;
0042     d->parsed = false;
0043 }
0044 
0045 DocumentSource::DocumentSource(const DocumentSource &other)
0046     : d()
0047 {
0048     *this = other;
0049 }
0050 
0051 DocumentSource::~DocumentSource()
0052 {
0053 }
0054 
0055 DocumentSource &DocumentSource::operator=(const DocumentSource &other)
0056 {
0057     d = other.d;
0058     return *this;
0059 }
0060 
0061 QByteArray DocumentSource::asByteArray() const
0062 {
0063     return d->array;
0064 }
0065 
0066 QDomDocument DocumentSource::asDomDocument() const
0067 {
0068     if (!d->parsed) {
0069         const auto result = d->domDoc.setContent(d->array, QDomDocument::ParseOption::UseNamespaceProcessing);
0070         if (!result) {
0071             qWarning() << result.errorMessage << "on line" << result.errorLine;
0072             d->domDoc.clear();
0073         }
0074 
0075         d->parsed = true;
0076     }
0077 
0078     return d->domDoc;
0079 }
0080 
0081 unsigned int DocumentSource::size() const
0082 {
0083     return d->array.size();
0084 }
0085 
0086 unsigned int DocumentSource::hash() const
0087 {
0088     if (!d->calculatedHash) {
0089         d->hash = calcHash(d->array);
0090         d->calculatedHash = true;
0091     }
0092 
0093     return d->hash;
0094 }
0095 
0096 QString DocumentSource::url() const
0097 {
0098     return d->url;
0099 }
0100 
0101 } // namespace Syndication