File indexing completed on 2024-04-14 14:31:30

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         QString errorMsg;
0070         int errorLine;
0071         int errorColumn;
0072         if (!d->domDoc.setContent(d->array, true, &errorMsg, &errorLine, &errorColumn)) {
0073             qWarning() << errorMsg << "on line" << errorLine;
0074             d->domDoc.clear();
0075         }
0076 
0077         d->parsed = true;
0078     }
0079 
0080     return d->domDoc;
0081 }
0082 
0083 unsigned int DocumentSource::size() const
0084 {
0085     return d->array.size();
0086 }
0087 
0088 unsigned int DocumentSource::hash() const
0089 {
0090     if (!d->calculatedHash) {
0091         d->hash = calcHash(d->array);
0092         d->calculatedHash = true;
0093     }
0094 
0095     return d->hash;
0096 }
0097 
0098 QString DocumentSource::url() const
0099 {
0100     return d->url;
0101 }
0102 
0103 } // namespace Syndication