Syndication

documentsource.cpp
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
15namespace Syndication
16{
17class SYNDICATION_NO_EXPORT DocumentSource::DocumentSourcePrivate
18{
19public:
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
28DocumentSource::DocumentSource()
29 : d(new DocumentSourcePrivate)
30{
31 d->parsed = true;
32 d->calculatedHash = true;
33 d->hash = 0;
34}
35
36DocumentSource::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
45DocumentSource::DocumentSource(const DocumentSource &other)
46 : d()
47{
48 *this = other;
49}
50
51DocumentSource::~DocumentSource()
52{
53}
54
55DocumentSource &DocumentSource::operator=(const DocumentSource &other)
56{
57 d = other.d;
58 return *this;
59}
60
61QByteArray DocumentSource::asByteArray() const
62{
63 return d->array;
64}
65
66QDomDocument DocumentSource::asDomDocument() const
67{
68 if (!d->parsed) {
69 const auto result = d->domDoc.setContent(d->array, 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
81unsigned int DocumentSource::size() const
82{
83 return d->array.size();
84}
85
86unsigned int DocumentSource::hash() const
87{
88 if (!d->calculatedHash) {
89 d->hash = calcHash(d->array);
90 d->calculatedHash = true;
91 }
92
93 return d->hash;
94}
95
96QString DocumentSource::url() const
97{
98 return d->url;
99}
100
101} // namespace Syndication
ParseResult setContent(QAnyStringView text, ParseOptions options)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:15 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.