document.cpp
00001 /* 00002 * This file is part of the syndication library 00003 * 00004 * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 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 QLatin1String("author")); 00060 QList<Person> list; 00061 00062 QList<QDomElement>::ConstIterator it = a.constBegin(); 00063 QList<QDomElement>::ConstIterator end = a.constEnd(); 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 QLatin1String("contributor")); 00079 QList<Person> list; 00080 00081 QList<QDomElement>::ConstIterator it = a.constBegin(); 00082 QList<QDomElement>::ConstIterator end = a.constEnd(); 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 QLatin1String("category")); 00098 QList<Category> list; 00099 00100 QList<QDomElement>::ConstIterator it = a.constBegin(); 00101 QList<QDomElement>::ConstIterator end = a.constEnd(); 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 QLatin1String("generator"))); 00116 } 00117 00118 QString FeedDocument::icon() const 00119 { 00120 return completeURI(extractElementTextNS(atom1Namespace(), 00121 QLatin1String("icon"))); 00122 00123 } 00124 00125 QString FeedDocument::logo() const 00126 { 00127 return completeURI(extractElementTextNS(atom1Namespace(), 00128 QLatin1String("logo"))); 00129 } 00130 00131 QString FeedDocument::id() const 00132 { 00133 return extractElementTextNS(atom1Namespace(), 00134 QLatin1String("id")); 00135 } 00136 00137 QString FeedDocument::rights() const 00138 { 00139 00140 return extractAtomText(*this, QLatin1String("rights")); 00141 } 00142 00143 QString FeedDocument::title() const 00144 { 00145 return extractAtomText(*this, QLatin1String("title")); 00146 } 00147 00148 QString FeedDocument::subtitle() const 00149 { 00150 return extractAtomText(*this, QLatin1String("subtitle")); 00151 } 00152 00153 time_t FeedDocument::updated() const 00154 { 00155 QString upd = extractElementTextNS(atom1Namespace(), 00156 QLatin1String("updated")); 00157 return parseDate(upd, ISODate); 00158 } 00159 00160 QList<Link> FeedDocument::links() const 00161 { 00162 QList<QDomElement> a = 00163 elementsByTagNameNS(atom1Namespace(), 00164 QLatin1String("link")); 00165 QList<Link> list; 00166 00167 QList<QDomElement>::ConstIterator it = a.constBegin(); 00168 QList<QDomElement>::ConstIterator end = a.constEnd(); 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 QLatin1String("entry")); 00184 QList<Entry> list; 00185 00186 QList<QDomElement>::ConstIterator it = a.constBegin(); 00187 QList<QDomElement>::ConstIterator end = a.constEnd(); 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 // TODO: do not hardcode this list here 00201 QList<ElementType> handled; 00202 handled.append(ElementType(QLatin1String("author"), atom1Namespace())); 00203 handled.append(ElementType(QLatin1String("contributor"), atom1Namespace())); 00204 handled.append(ElementType(QLatin1String("category"), atom1Namespace())); 00205 handled.append(ElementType(QLatin1String("generator"), atom1Namespace())); 00206 handled.append(ElementType(QLatin1String("icon"), atom1Namespace())); 00207 handled.append(ElementType(QLatin1String("logo"), atom1Namespace())); 00208 handled.append(ElementType(QLatin1String("id"), atom1Namespace())); 00209 handled.append(ElementType(QLatin1String("rights"), atom1Namespace())); 00210 handled.append(ElementType(QLatin1String("title"), atom1Namespace())); 00211 handled.append(ElementType(QLatin1String("subtitle"), atom1Namespace())); 00212 handled.append(ElementType(QLatin1String("updated"), atom1Namespace())); 00213 handled.append(ElementType(QLatin1String("link"), atom1Namespace())); 00214 handled.append(ElementType(QLatin1String("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 += QLatin1String("### FeedDocument: ###################\n"); 00241 if (!title().isEmpty()) 00242 info += QLatin1String("title: #") + title() + QLatin1String("#\n"); 00243 if (!subtitle().isEmpty()) 00244 info += QLatin1String("subtitle: #") + subtitle() + QLatin1String("#\n"); 00245 if (!id().isEmpty()) 00246 info += QLatin1String("id: #") + id() + QLatin1String("#\n"); 00247 00248 if (!rights().isEmpty()) 00249 info += QLatin1String("rights: #") + rights() + QLatin1String("#\n"); 00250 if (!icon().isEmpty()) 00251 info += QLatin1String("icon: #") + icon() + QLatin1String("#\n"); 00252 if (!logo().isEmpty()) 00253 info += QLatin1String("logo: #") + logo() + QLatin1String("#\n"); 00254 if (!generator().isNull()) 00255 info += generator().debugInfo(); 00256 00257 00258 QString dupdated = dateTimeToString(updated()); 00259 if (!dupdated.isNull()) 00260 info += QLatin1String("updated: #") + dupdated + QLatin1String("#\n"); 00261 00262 QList<Link> dlinks = links(); 00263 QList<Link>::ConstIterator endlinks = dlinks.constEnd(); 00264 for (QList<Link>::ConstIterator it = dlinks.constBegin(); it != endlinks; ++it) 00265 info += (*it).debugInfo(); 00266 00267 QList<Category> dcats = categories(); 00268 QList<Category>::ConstIterator endcats = dcats.constEnd(); 00269 for (QList<Category>::ConstIterator it = dcats.constBegin(); it != endcats; ++it) 00270 info += (*it).debugInfo(); 00271 00272 info += QLatin1String("### Authors: ###################\n"); 00273 00274 QList<Person> dauthors = authors(); 00275 QList<Person>::ConstIterator endauthors = dauthors.constEnd(); 00276 for (QList<Person>::ConstIterator it = dauthors.constBegin(); it != endauthors; ++it) 00277 info += (*it).debugInfo(); 00278 00279 info += QLatin1String("### Contributors: ###################\n"); 00280 00281 QList<Person> dcontri = contributors(); 00282 QList<Person>::ConstIterator endcontri = dcontri.constEnd(); 00283 for (QList<Person>::ConstIterator it = dcontri.constBegin(); it != endcontri; ++it) 00284 info += (*it).debugInfo(); 00285 00286 QList<Entry> dentries = entries(); 00287 QList<Entry>::ConstIterator endentries = dentries.constEnd(); 00288 for (QList<Entry>::ConstIterator it = dentries.constBegin(); it != endentries; ++it) 00289 info += (*it).debugInfo(); 00290 00291 info += QLatin1String("### 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 += QLatin1String("### EntryDocument: ##################\n"); 00324 00325 Entry dentry = entry(); 00326 if (!dentry.isNull()) 00327 info += dentry.debugInfo(); 00328 00329 info += QLatin1String("### EntryDocument end ###############\n"); 00330 return info; 00331 } 00332 00333 } // namespace Atom 00334 } // namespace Syndication