26 #include "modelmaker.h"
31 #include "statement.h"
33 #include <documentsource.h>
35 #include <QtXml/QDomDocument>
36 #include <QtXml/QDomNodeList>
37 #include <QtCore/QHash>
38 #include <QtCore/QList>
39 #include <QtCore/QMap>
40 #include <QtCore/QString>
41 #include <QtCore/QStringList>
43 namespace Syndication {
46 class Parser::ParserPrivate
49 QDomDocument addEnumeration(
const QDomDocument& doc);
50 void map09to10(Model model);
51 void addSequenceFor09(Model model);
53 QString strInternalNs;
59 QDomDocument doc = source.asDomDocument();
63 QDomElement root = doc.documentElement();
65 if (!root.isElement())
73 QDomDocument doc = source.asDomDocument();
76 return Syndication::SpecificDocumentPtr(
new Document());
78 doc = d->addEnumeration(doc);
88 d->addSequenceFor09(model);
93 if (channels.isEmpty())
94 return Syndication::SpecificDocumentPtr(
new Document());
96 return DocumentPtr(
new Document(*(channels.begin())));
99 QDomDocument Parser::ParserPrivate::addEnumeration(
const QDomDocument& docp)
101 QDomDocument doc(docp);
103 QDomNodeList list = doc.elementsByTagNameNS(
RSS09Vocab::self()->namespaceURI(),
104 QLatin1String(
"item"));
106 for (
int i = 0; i < list.size(); ++i)
108 QDomElement item = list.item(i).toElement();
111 QDomElement ie = doc.createElementNS(strInternalNs, strItemIndex);
112 item.appendChild(ie);
113 ie.appendChild(doc.createTextNode(QString::number(i)));
121 void Parser::ParserPrivate::map09to10(Model model)
123 QHash<QString, PropertyPtr> hash;
137 QList<StatementPtr> statements = model.statements();
138 QList<StatementPtr>::ConstIterator it = statements.constBegin();
139 QList<StatementPtr>::ConstIterator end = statements.constEnd();
141 for ( ; it != end; ++it)
143 StatementPtr stmt = *it;
145 QString predUri = stmt->predicate()->uri();
146 if (uris09.contains(predUri))
148 model.addStatement(stmt->subject(), hash[predUri], stmt->object());
152 QList<ResourcePtr> channels = model.resourcesWithType(
RSS09Vocab::self()->channel());
156 if (!channels.isEmpty())
158 channel = *(channels.begin());
165 void Parser::ParserPrivate::addSequenceFor09(Model model)
169 const QList<ResourcePtr> items = model.resourcesWithType(
RSS09Vocab::self()->item());
174 const QList<ResourcePtr> channels = model.resourcesWithType(
RSSVocab::self()->channel());
176 if (channels.isEmpty())
179 PropertyPtr itemIndex = model.createProperty(strInternalNs + strItemIndex);
182 QMap<uint, ResourcePtr> sorted;
184 foreach (
const ResourcePtr &i, items)
186 QString numstr = i->property(itemIndex)->asString();
188 uint num = numstr.toUInt(&ok);
195 SequencePtr seq = model.createSequence();
198 foreach (
const ResourcePtr &i, sorted)
211 d->strInternalNs = QLatin1String(
"http://akregator.sf.net/libsyndication/internal#");
212 d->strItemIndex = QLatin1String(
"itemIndex");
221 Parser& Parser::operator=(
const Parser& ) {
return *
this; }
225 return QLatin1String(
"rdf");
Parser implementation for RDF-based RSS 0.9 and RSS 1.0 feeds.
An RDF model, a set of RDF statements.
Document implementation for RDF, representing an RSS 1.0 feed.
virtual bool accept(const DocumentSource &source) const
returns whether the passed document looks like an RSS 0.9 or RSS 1.0 document.
Model createFromXML(const QDomDocument &doc)
parses an RDF model from RDF/XML
An RDF parser, used to parse an RDF model from RDF/XML.
QString namespaceURI()
the RDF namespace, which is http://www.w3.org/1999/02/22-rdf-syntax-ns#
virtual SpecificDocumentPtr parse(const DocumentSource &source) const
Parses an RSS 0.9/1.0 document from a feed source.
virtual QString format() const
format string of this parser, which is "rdf".
virtual ~Parser()
destructor
Parser()
default constructor
virtual QList< ResourcePtr > resourcesWithType(ResourcePtr type) const
returns all resources of a given type.
static RDFVocab * self()
returns the singleton instance