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 | QString errorMsg; |
70 | int errorLine; |
71 | int errorColumn; |
72 | if (!d->domDoc.setContent(d->array, true, &errorMsg, &errorLine, &errorColumn)) { |
73 | qWarning() << errorMsg << "on line" << errorLine; |
74 | d->domDoc.clear(); |
75 | } |
76 | |
77 | d->parsed = true; |
78 | } |
79 | |
80 | return d->domDoc; |
81 | } |
82 | |
83 | unsigned int DocumentSource::size() const |
84 | { |
85 | return d->array.size(); |
86 | } |
87 | |
88 | unsigned int DocumentSource::hash() const |
89 | { |
90 | if (!d->calculatedHash) { |
91 | d->hash = calcHash(d->array); |
92 | d->calculatedHash = true; |
93 | } |
94 | |
95 | return d->hash; |
96 | } |
97 | |
98 | QString DocumentSource::url() const |
99 | { |
100 | return d->url; |
101 | } |
102 | |
103 | } // namespace Syndication |
104 | |