syndication/atom
document.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 "document.h"
00024 #include "category.h"
00025 #include "constants.h"
00026 #include "entry.h"
00027 #include "generator.h"
00028 #include "link.h"
00029 #include "person.h"
00030 #include "atomtools.h"
00031
00032 #include <documentvisitor.h>
00033 #include <tools.h>
00034
00035 #include <QtXml/QDomElement>
00036 #include <QtCore/QList>
00037 #include <QtCore/QString>
00038
00039 namespace Syndication {
00040 namespace Atom {
00041
00042 FeedDocument::FeedDocument() : ElementWrapper()
00043 {
00044 }
00045
00046 FeedDocument::FeedDocument(const QDomElement& element) : ElementWrapper(element)
00047 {
00048 }
00049
00050 bool FeedDocument::accept(DocumentVisitor* visitor)
00051 {
00052 return visitor->visitAtomFeedDocument(this);
00053 }
00054
00055 QList<Person> FeedDocument::authors() const
00056 {
00057 QList<QDomElement> a =
00058 elementsByTagNameNS(atom1Namespace(),
00059 QString::fromUtf8("author"));
00060 QList<Person> list;
00061
00062 QList<QDomElement>::ConstIterator it = a.begin();
00063 QList<QDomElement>::ConstIterator end = a.end();
00064
00065
00066 for ( ; it != end; ++it)
00067 {
00068 list.append(Person(*it));
00069 }
00070
00071 return list;
00072 }
00073
00074 QList<Person> FeedDocument::contributors() const
00075 {
00076 QList<QDomElement> a =
00077 elementsByTagNameNS(atom1Namespace(),
00078 QString::fromUtf8("contributor"));
00079 QList<Person> list;
00080
00081 QList<QDomElement>::ConstIterator it = a.begin();
00082 QList<QDomElement>::ConstIterator end = a.end();
00083
00084
00085 for ( ; it != end; ++it)
00086 {
00087 list.append(Person(*it));
00088 }
00089
00090 return list;
00091 }
00092
00093 QList<Category> FeedDocument::categories() const
00094 {
00095 QList<QDomElement> a =
00096 elementsByTagNameNS(atom1Namespace(),
00097 QString::fromUtf8("category"));
00098 QList<Category> list;
00099
00100 QList<QDomElement>::ConstIterator it = a.begin();
00101 QList<QDomElement>::ConstIterator end = a.end();
00102
00103
00104 for ( ; it != end; ++it)
00105 {
00106 list.append(Category(*it));
00107 }
00108
00109 return list;
00110 }
00111
00112 Generator FeedDocument::generator() const
00113 {
00114 return Generator(firstElementByTagNameNS(atom1Namespace(),
00115 QString::fromUtf8("generator")));
00116 }
00117
00118 QString FeedDocument::icon() const
00119 {
00120 return completeURI(extractElementTextNS(atom1Namespace(),
00121 QString::fromUtf8("icon")));
00122
00123 }
00124
00125 QString FeedDocument::logo() const
00126 {
00127 return completeURI(extractElementTextNS(atom1Namespace(),
00128 QString::fromUtf8("logo")));
00129 }
00130
00131 QString FeedDocument::id() const
00132 {
00133 return extractElementTextNS(atom1Namespace(),
00134 QString::fromUtf8("id"));
00135 }
00136
00137 QString FeedDocument::rights() const
00138 {
00139
00140 return extractAtomText(*this, QString::fromUtf8("rights"));
00141 }
00142
00143 QString FeedDocument::title() const
00144 {
00145 return extractAtomText(*this, QString::fromUtf8("title"));
00146 }
00147
00148 QString FeedDocument::subtitle() const
00149 {
00150 return extractAtomText(*this, QString::fromUtf8("subtitle"));
00151 }
00152
00153 time_t FeedDocument::updated() const
00154 {
00155 QString upd = extractElementTextNS(atom1Namespace(),
00156 QString::fromUtf8("updated"));
00157 return parseDate(upd, ISODate);
00158 }
00159
00160 QList<Link> FeedDocument::links() const
00161 {
00162 QList<QDomElement> a =
00163 elementsByTagNameNS(atom1Namespace(),
00164 QString::fromUtf8("link"));
00165 QList<Link> list;
00166
00167 QList<QDomElement>::ConstIterator it = a.begin();
00168 QList<QDomElement>::ConstIterator end = a.end();
00169
00170
00171 for ( ; it != end; ++it)
00172 {
00173 list.append(Link(*it));
00174 }
00175
00176 return list;
00177 }
00178
00179 QList<Entry> FeedDocument::entries() const
00180 {
00181 QList<QDomElement> a =
00182 elementsByTagNameNS(atom1Namespace(),
00183 QString::fromUtf8("entry"));
00184 QList<Entry> list;
00185
00186 QList<QDomElement>::ConstIterator it = a.begin();
00187 QList<QDomElement>::ConstIterator end = a.end();
00188
00189
00190 for ( ; it != end; ++it)
00191 {
00192 list.append(Entry(*it));
00193 }
00194
00195 return list;
00196 }
00197
00198 QList<QDomElement> FeedDocument::unhandledElements() const
00199 {
00200
00201 QList<ElementType> handled;
00202 handled.append(ElementType(QString::fromUtf8("author"), atom1Namespace()));
00203 handled.append(ElementType(QString::fromUtf8("contributor"), atom1Namespace()));
00204 handled.append(ElementType(QString::fromUtf8("category"), atom1Namespace()));
00205 handled.append(ElementType(QString::fromUtf8("generator"), atom1Namespace()));
00206 handled.append(ElementType(QString::fromUtf8("icon"), atom1Namespace()));
00207 handled.append(ElementType(QString::fromUtf8("logo"), atom1Namespace()));
00208 handled.append(ElementType(QString::fromUtf8("id"), atom1Namespace()));
00209 handled.append(ElementType(QString::fromUtf8("rights"), atom1Namespace()));
00210 handled.append(ElementType(QString::fromUtf8("title"), atom1Namespace()));
00211 handled.append(ElementType(QString::fromUtf8("subtitle"), atom1Namespace()));
00212 handled.append(ElementType(QString::fromUtf8("updated"), atom1Namespace()));
00213 handled.append(ElementType(QString::fromUtf8("link"), atom1Namespace()));
00214 handled.append(ElementType(QString::fromUtf8("entry"), atom1Namespace()));
00215
00216 QList<QDomElement> notHandled;
00217
00218 QDomNodeList children = element().childNodes();
00219 for (int i = 0; i < children.size(); ++i)
00220 {
00221 QDomElement el = children.at(i).toElement();
00222 if (!el.isNull()
00223 && !handled.contains(ElementType(el.localName(), el.namespaceURI())))
00224 {
00225 notHandled.append(el);
00226 }
00227 }
00228
00229 return notHandled;
00230 }
00231
00232 bool FeedDocument::isValid() const
00233 {
00234 return !isNull();
00235 }
00236
00237 QString FeedDocument::debugInfo() const
00238 {
00239 QString info;
00240 info += "### FeedDocument: ###################\n";
00241 if (!title().isEmpty())
00242 info += "title: #" + title() + "#\n";
00243 if (!subtitle().isEmpty())
00244 info += "subtitle: #" + subtitle() + "#\n";
00245 if (!id().isEmpty())
00246 info += "id: #" + id() + "#\n";
00247
00248 if (!rights().isEmpty())
00249 info += "rights: #" + rights() + "#\n";
00250 if (!icon().isEmpty())
00251 info += "icon: #" + icon() + "#\n";
00252 if (!logo().isEmpty())
00253 info += "logo: #" + logo() + "#\n";
00254 if (!generator().isNull())
00255 info += generator().debugInfo();
00256
00257
00258 QString dupdated = dateTimeToString(updated());
00259 if (!dupdated.isNull())
00260 info += "updated: #" + dupdated + "#\n";
00261
00262 QList<Link> dlinks = links();
00263 QList<Link>::ConstIterator endlinks = dlinks.end();
00264 for (QList<Link>::ConstIterator it = dlinks.begin(); it != endlinks; ++it)
00265 info += (*it).debugInfo();
00266
00267 QList<Category> dcats = categories();
00268 QList<Category>::ConstIterator endcats = dcats.end();
00269 for (QList<Category>::ConstIterator it = dcats.begin(); it != endcats; ++it)
00270 info += (*it).debugInfo();
00271
00272 info += "### Authors: ###################\n";
00273
00274 QList<Person> dauthors = authors();
00275 QList<Person>::ConstIterator endauthors = dauthors.end();
00276 for (QList<Person>::ConstIterator it = dauthors.begin(); it != endauthors; ++it)
00277 info += (*it).debugInfo();
00278
00279 info += "### Contributors: ###################\n";
00280
00281 QList<Person> dcontri = contributors();
00282 QList<Person>::ConstIterator endcontri = dcontri.end();
00283 for (QList<Person>::ConstIterator it = dcontri.begin(); it != endcontri; ++it)
00284 info += (*it).debugInfo();
00285
00286 QList<Entry> dentries = entries();
00287 QList<Entry>::ConstIterator endentries = dentries.end();
00288 for (QList<Entry>::ConstIterator it = dentries.begin(); it != endentries; ++it)
00289 info += (*it).debugInfo();
00290
00291 info += "### FeedDocument end ################\n";
00292
00293 return info;
00294 }
00295
00296 EntryDocument::EntryDocument() : ElementWrapper()
00297 {
00298 }
00299
00300 EntryDocument::EntryDocument(const QDomElement& element) : ElementWrapper(element)
00301 {
00302 }
00303
00304 bool EntryDocument::accept(DocumentVisitor* visitor)
00305 {
00306 return visitor->visitAtomEntryDocument(this);
00307 }
00308
00309 Entry EntryDocument::entry() const
00310 {
00311 return Entry(element());
00312 }
00313
00314
00315 bool EntryDocument::isValid() const
00316 {
00317 return !isNull();
00318 }
00319
00320 QString EntryDocument::debugInfo() const
00321 {
00322 QString info;
00323 info += "### EntryDocument: ##################\n";
00324
00325 Entry dentry = entry();
00326 if (!dentry.isNull())
00327 info += dentry.debugInfo();
00328
00329 info += "### EntryDocument end ###############\n";
00330 return info;
00331 }
00332
00333 }
00334 }