1/*
2 * This file is part of the syndication library
3 *
4 * Copyright (C) 2005 Frank Osterfeld <osterfeld@kde.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "documentsource.h"
24#include "tools.h"
25
26#include <QtCore/QByteArray>
27#include <QtXml/QDomDocument>
28#include <QtXml/QXmlSimpleReader>
29
30namespace Syndication {
31
32class DocumentSource::DocumentSourcePrivate
33{
34 public:
35 QByteArray array;
36 QString url;
37 mutable QDomDocument domDoc;
38 mutable bool parsed;
39 mutable unsigned int hash;
40 mutable bool calculatedHash;
41};
42
43DocumentSource::DocumentSource() : d(new DocumentSourcePrivate)
44{
45 d->parsed = true;
46 d->calculatedHash = true;
47 d->hash = 0;
48}
49
50
51DocumentSource::DocumentSource(const QByteArray& source, const QString& url) : d(new DocumentSourcePrivate)
52{
53 d->array = source;
54 d->url = url;
55 d->calculatedHash = false;
56 d->parsed = false;
57}
58
59DocumentSource::DocumentSource(const DocumentSource& other) : d()
60{
61 *this = other;
62}
63
64DocumentSource::~DocumentSource()
65{
66}
67
68DocumentSource& DocumentSource::operator=(const DocumentSource& other)
69{
70 d = other.d;
71 return *this;
72}
73
74QByteArray DocumentSource::asByteArray() const
75{
76 return d->array;
77}
78
79QDomDocument DocumentSource::asDomDocument() const
80{
81 if (!d->parsed)
82 {
83 QXmlInputSource source;
84 source.setData(d->array);
85
86 QXmlSimpleReader reader;
87 reader.setFeature(QLatin1String("http://xml.org/sax/features/namespaces"), true);
88
89 if (!d->domDoc.setContent(&source, &reader))
90 d->domDoc.clear();
91
92 d->parsed = true;
93 }
94
95 return d->domDoc;
96}
97
98unsigned int DocumentSource::size() const
99{
100 return d->array.size();
101}
102
103unsigned int DocumentSource::hash() const
104{
105 if (!d->calculatedHash)
106 {
107 d->hash = calcHash(d->array);
108 d->calculatedHash = true;
109 }
110
111 return d->hash;
112}
113
114QString DocumentSource::url() const
115{
116 return d->url;
117}
118
119} // namespace Syndication
120