Syndication Library
documentsource.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "documentsource.h"
00024 #include "tools.h"
00025
00026 #include <QtCore/QByteArray>
00027 #include <QtXml/QDomDocument>
00028 #include <QtXml/QXmlSimpleReader>
00029
00030 namespace Syndication {
00031
00032 class DocumentSource::DocumentSourcePrivate
00033 {
00034 public:
00035 QByteArray array;
00036 QString url;
00037 mutable QDomDocument domDoc;
00038 mutable bool parsed;
00039 mutable unsigned int hash;
00040 mutable bool calculatedHash;
00041 };
00042
00043 DocumentSource::DocumentSource() : d(new DocumentSourcePrivate)
00044 {
00045 d->parsed = true;
00046 d->calculatedHash = true;
00047 d->hash = 0;
00048 }
00049
00050
00051 DocumentSource::DocumentSource(const QByteArray& source, const QString& url) : d(new DocumentSourcePrivate)
00052 {
00053 d->array = source;
00054 d->url = url;
00055 d->calculatedHash = false;
00056 d->parsed = false;
00057 }
00058
00059 DocumentSource::DocumentSource(const DocumentSource& other) : d()
00060 {
00061 *this = other;
00062 }
00063
00064 DocumentSource::~DocumentSource()
00065 {
00066 }
00067
00068 DocumentSource& DocumentSource::operator=(const DocumentSource& other)
00069 {
00070 d = other.d;
00071 return *this;
00072 }
00073
00074 QByteArray DocumentSource::asByteArray() const
00075 {
00076 return d->array;
00077 }
00078
00079 QDomDocument DocumentSource::asDomDocument() const
00080 {
00081 if (!d->parsed)
00082 {
00083 QXmlInputSource source;
00084 source.setData(d->array);
00085
00086 QXmlSimpleReader reader;
00087 reader.setFeature("http://xml.org/sax/features/namespaces", true);
00088
00089 if (!d->domDoc.setContent(&source, &reader))
00090 d->domDoc.clear();
00091
00092 d->parsed = true;
00093 }
00094
00095 return d->domDoc;
00096 }
00097
00098 unsigned int DocumentSource::size() const
00099 {
00100 return d->array.size();
00101 }
00102
00103 unsigned int DocumentSource::hash() const
00104 {
00105 if (!d->calculatedHash)
00106 {
00107 d->hash = calcHash(d->array);
00108 d->calculatedHash = true;
00109 }
00110
00111 return d->hash;
00112 }
00113
00114 QString DocumentSource::url() const
00115 {
00116 return d->url;
00117 }
00118
00119 }