• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

syndication/rdf

  • sources
  • kde-4.14
  • kdepimlibs
  • syndication
  • rdf
parser.cpp
1 /*
2  * This file is part of the syndication library
3  *
4  * Copyright (C) 2006 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 "parser.h"
24 #include "document.h"
25 #include "model.h"
26 #include "modelmaker.h"
27 #include "property.h"
28 #include "rdfvocab.h"
29 #include "resource.h"
30 #include "rssvocab.h"
31 #include "statement.h"
32 
33 #include <documentsource.h>
34 
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>
42 
43 namespace Syndication {
44 namespace RDF {
45 
46 class Parser::ParserPrivate
47 {
48  public:
49  QDomDocument addEnumeration(const QDomDocument& doc);
50  void map09to10(Model model);
51  void addSequenceFor09(Model model);
52 
53  QString strInternalNs;
54  QString strItemIndex;
55 };
56 
57 bool Parser::accept(const DocumentSource& source) const
58 {
59  QDomDocument doc = source.asDomDocument();
60 
61  if (doc.isNull())
62  return false;
63  QDomElement root = doc.documentElement();
64 
65  if (!root.isElement())
66  return false;
67 
68  return root.namespaceURI() == RDFVocab::self()->namespaceURI();
69 }
70 
71 SpecificDocumentPtr Parser::parse(const DocumentSource& source) const
72 {
73  QDomDocument doc = source.asDomDocument();
74 
75  if (doc.isNull())
76  return Syndication::SpecificDocumentPtr(new Document());
77 
78  doc = d->addEnumeration(doc);
79 
80  ModelMaker maker;
81  Model model = maker.createFromXML(doc);
82 
83  bool is09 = !model.resourcesWithType(RSS09Vocab::self()->channel()).isEmpty();
84 
85  if (is09)
86  {
87  d->map09to10(model);
88  d->addSequenceFor09(model);
89  }
90 
91  QList<ResourcePtr> channels = model.resourcesWithType(RSSVocab::self()->channel());
92 
93  if (channels.isEmpty())
94  return Syndication::SpecificDocumentPtr(new Document());
95 
96  return DocumentPtr(new Document(*(channels.begin())));
97 }
98 
99 QDomDocument Parser::ParserPrivate::addEnumeration(const QDomDocument& docp)
100 {
101  QDomDocument doc(docp);
102 
103  QDomNodeList list = doc.elementsByTagNameNS(RSS09Vocab::self()->namespaceURI(),
104  QLatin1String("item"));
105 
106  for (int i = 0; i < list.size(); ++i)
107  {
108  QDomElement item = list.item(i).toElement();
109  if (!item.isNull())
110  {
111  QDomElement ie = doc.createElementNS(strInternalNs, strItemIndex);
112  item.appendChild(ie);
113  ie.appendChild(doc.createTextNode(QString::number(i)));
114 
115  }
116  }
117 
118  return doc;
119 }
120 
121 void Parser::ParserPrivate::map09to10(Model model)
122 {
123  QHash<QString, PropertyPtr> hash;
124 
125  hash.insert(RSS09Vocab::self()->title()->uri(), RSSVocab::self()->title());
126  hash.insert(RSS09Vocab::self()->description()->uri(), RSSVocab::self()->description());
127  hash.insert(RSS09Vocab::self()->link()->uri(), RSSVocab::self()->link());
128  hash.insert(RSS09Vocab::self()->name()->uri(), RSSVocab::self()->name());
129  hash.insert(RSS09Vocab::self()->url()->uri(), RSSVocab::self()->url());
130  hash.insert(RSS09Vocab::self()->image()->uri(), RSSVocab::self()->image());
131  hash.insert(RSS09Vocab::self()->textinput()->uri(), RSSVocab::self()->textinput());
132 
133  QStringList uris09 = RSS09Vocab::self()->properties();
134 
135  // map statement predicates to RSS 1.0
136 
137  QList<StatementPtr> statements = model.statements();
138  QList<StatementPtr>::ConstIterator it = statements.constBegin();
139  QList<StatementPtr>::ConstIterator end = statements.constEnd();
140 
141  for ( ; it != end; ++it)
142  {
143  StatementPtr stmt = *it;
144 
145  QString predUri = stmt->predicate()->uri();
146  if (uris09.contains(predUri))
147  {
148  model.addStatement(stmt->subject(), hash[predUri], stmt->object());
149  }
150  }
151  // map channel type
152  QList<ResourcePtr> channels = model.resourcesWithType(RSS09Vocab::self()->channel());
153 
154  ResourcePtr channel;
155 
156  if (!channels.isEmpty())
157  {
158  channel = *(channels.begin());
159 
160  model.removeStatement(channel, RDFVocab::self()->type(), RSS09Vocab::self()->channel());
161  model.addStatement(channel, RDFVocab::self()->type(), RSSVocab::self()->channel());
162  }
163 }
164 
165 void Parser::ParserPrivate::addSequenceFor09(Model model)
166 {
167  //RDF 0.9 doesn't contain an item sequence, and the items don't have rdf:about, so add both
168 
169  const QList<ResourcePtr> items = model.resourcesWithType(RSS09Vocab::self()->item());
170 
171  if (items.isEmpty())
172  return;
173 
174  const QList<ResourcePtr> channels = model.resourcesWithType(RSSVocab::self()->channel());
175 
176  if (channels.isEmpty())
177  return;
178 
179  PropertyPtr itemIndex = model.createProperty(strInternalNs + strItemIndex);
180 
181  // use QMap here, not QHash. as we need the sorting functionality
182  QMap<uint, ResourcePtr> sorted;
183 
184  foreach (const ResourcePtr &i, items)
185  {
186  QString numstr = i->property(itemIndex)->asString();
187  bool ok = false;
188  uint num = numstr.toUInt(&ok);
189  if (ok)
190  {
191  sorted[num] = i;
192  }
193  }
194 
195  SequencePtr seq = model.createSequence();
196  model.addStatement(channels.first(), RSSVocab::self()->items(), seq);
197 
198  foreach (const ResourcePtr &i, sorted)
199  {
200  seq->append(i);
201  // add rdf:about (type)
202  model.addStatement(i, RDFVocab::self()->type(), RSSVocab::self()->item());
203 
204  //add to items sequence
205  model.addStatement(seq, RDFVocab::self()->li(), i);
206  }
207 }
208 
209 Parser::Parser() : d(new ParserPrivate)
210 {
211  d->strInternalNs = QLatin1String("http://akregator.sf.net/libsyndication/internal#");
212  d->strItemIndex = QLatin1String("itemIndex");
213 }
214 
215 Parser::~Parser()
216 {
217  delete d;
218 }
219 
220 Parser::Parser(const Parser& other) : AbstractParser(other), d(0) {}
221 Parser& Parser::operator=(const Parser& /*other*/) { return *this; }
222 
223 QString Parser::format() const
224 {
225  return QLatin1String("rdf");
226 }
227 
228 
229 } // namespace RDF
230 } // namespace Syndication
Syndication::RDF::Parser
Parser implementation for RDF-based RSS 0.9 and RSS 1.0 feeds.
Definition: parser.h:41
QHash::insert
iterator insert(const Key &key, const T &value)
QDomNodeList::item
QDomNode item(int index) const
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
Syndication::RDF::Model
An RDF model, a set of RDF statements.
Definition: model.h:49
Syndication::RDF::Document
Document implementation for RDF, representing an RSS 1.0 feed.
Definition: document.h:51
QDomNode::isElement
bool isElement() const
QDomNodeList
Syndication::RDF::RSS09Vocab::properties
QStringList properties() const
returns a list containing all URIs representing properties in this vocabulary
Definition: rssvocab.cpp:268
QMap
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QDomNode::namespaceURI
QString namespaceURI() const
Syndication::RDF::Parser::accept
virtual bool accept(const DocumentSource &source) const
returns whether the passed document looks like an RSS 0.9 or RSS 1.0 document.
Definition: parser.cpp:57
Syndication::RDF::ModelMaker::createFromXML
Model createFromXML(const QDomDocument &doc)
parses an RDF model from RDF/XML
Definition: modelmaker.cpp:39
QDomDocument::documentElement
QDomElement documentElement() const
Syndication::RDF::RSS09Vocab::self
static RSS09Vocab * self()
returns the singleton instance
Definition: rssvocab.cpp:204
Syndication::RDF::RSSVocab::items
PropertyPtr items() const
RSS 1.0 items property, see Document::items() for more details.
Definition: rssvocab.cpp:132
Syndication::RDF::ModelMaker
An RDF parser, used to parse an RDF model from RDF/XML.
Definition: modelmaker.h:49
Syndication::RDF::RDFVocab::namespaceURI
QString namespaceURI()
the RDF namespace, which is http://www.w3.org/1999/02/22-rdf-syntax-ns#
Definition: rdfvocab.cpp:92
QDomNode::toElement
QDomElement toElement() const
Syndication::RDF::Parser::parse
virtual SpecificDocumentPtr parse(const DocumentSource &source) const
Parses an RSS 0.9/1.0 document from a feed source.
Definition: parser.cpp:71
QString::number
QString number(int n, int base)
Syndication::RDF::RSSVocab::self
static RSSVocab * self()
returns the singleton instance
Definition: rssvocab.cpp:81
Syndication::RDF::Parser::format
virtual QString format() const
format string of this parser, which is "rdf".
Definition: parser.cpp:223
QHash< QString, PropertyPtr >
Syndication::RDF::Parser::~Parser
virtual ~Parser()
destructor
Definition: parser.cpp:215
QList::isEmpty
bool isEmpty() const
Syndication::RDF::Parser::Parser
Parser()
default constructor
Definition: parser.cpp:209
QList::first
T & first()
QString
QList
Definition: document.h:30
Syndication::RDF::Model::resourcesWithType
virtual QList< ResourcePtr > resourcesWithType(ResourcePtr type) const
returns all resources of a given type.
Definition: model.cpp:300
QStringList
QDomDocument
QDomNode::isNull
bool isNull() const
QLatin1String
QDomNodeList::size
int size() const
Syndication::RDF::RDFVocab::self
static RDFVocab * self()
returns the singleton instance
Definition: rdfvocab.cpp:51
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QDomElement
QList::begin
iterator begin()
QString::toUInt
uint toUInt(bool *ok, int base) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

syndication/rdf

Skip menu "syndication/rdf"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal