• 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
modelmaker.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 "modelmaker.h"
24 #include "literal.h"
25 #include "model.h"
26 #include "property.h"
27 #include "rdfvocab.h"
28 #include "resource.h"
29 #include "sequence.h"
30 #include "statement.h"
31 
32 #include <QtXml/QDomElement>
33 #include <QtCore/QString>
34 
35 namespace Syndication {
36 namespace RDF {
37 
38 
39 Model ModelMaker::createFromXML(const QDomDocument& doc)
40 {
41  Model model;
42 
43  if (doc.isNull())
44  return model;
45 
46  QDomElement rdfNode = doc.documentElement();
47 
48  QDomNodeList list = rdfNode.childNodes();
49 
50  for (uint i = 0; i < list.length(); ++i)
51  {
52  if (list.item(i).isElement())
53  {
54  QDomElement el = list.item(i).toElement();
55  readResource(model, el);
56  }
57  }
58 
59  return model;
60 }
61 
62 ResourcePtr ModelMaker::readResource(Model& model, const QDomElement& el)
63 {
64  QString rdfns = RDFVocab::self()->namespaceURI();
65  QString about = QLatin1String("about");
66  QString resource = QLatin1String("resource");
67  QString descriptionStr = QLatin1String("Description");
68 
69  ResourcePtr res;
70 
71  ResourcePtr type = model.createResource(el.namespaceURI() + el.localName());
72 
73  if (*type == *(RDFVocab::self()->seq()))
74  {
75  SequencePtr seq = model.createSequence(el.attribute(about));
76 
77  res = seq;
78  }
79  else
80  {
81  res = model.createResource(el.attribute(about));
82  }
83 
84  model.addStatement(res, RDFVocab::self()->type(), type);
85 
86  QDomNodeList children = el.childNodes();
87 
88  bool isSeq = res->isSequence();
89 
90  for (uint i = 0; i < children.length(); ++i)
91  {
92  if (children.item(i).isElement())
93  {
94  QDomElement ce = children.item(i).toElement();
95 
96  PropertyPtr pred = model.createProperty(ce.namespaceURI() + ce.localName());
97 
98  if (ce.hasAttribute(resource)) // referenced Resource via (rdf:)resource
99  {
100  NodePtr obj = model.createResource(ce.attribute(resource));
101 
102  if (isSeq && *pred == *(RDFVocab::self()->li()))
103  {
104  SequencePtr tseq = boost::static_pointer_cast<Sequence>(res);
105  tseq->append(obj);
106  }
107  else
108  model.addStatement(res, pred, obj);
109  }
110  else if (!ce.text().isEmpty() && ce.lastChildElement().isNull()) // Literal
111  {
112  NodePtr obj = model.createLiteral(ce.text());
113 
114  if (isSeq && *pred == *(RDFVocab::self()->li()))
115  {
116  SequencePtr tseq = boost::static_pointer_cast<Sequence>(res);
117  tseq->append(obj);
118  }
119  else
120  model.addStatement(res, pred, obj);
121  }
122  else // embedded description
123  {
124  QDomElement re = ce.lastChildElement();
125 
126  QString uri = re.attribute(about);
127 
128  // read recursively
129  NodePtr obj = readResource(model, re);
130 
131  if (isSeq && *pred == *(RDFVocab::self()->li()))
132  {
133  SequencePtr tseq = boost::static_pointer_cast<Sequence>(res);
134  tseq->append(obj);
135  }
136  else
137  model.addStatement(res, pred, obj);
138 
139  }
140 
141  //TODO: bag, reification (nice to have, but not important for basic RSS 1.0)
142  }
143  }
144 
145  return res;
146 }
147 
148 } // namespace RDF
149 } // namespace Syndication
QDomNodeList::item
QDomNode item(int index) const
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
Syndication::RDF::Model
An RDF model, a set of RDF statements.
Definition: model.h:49
Syndication::RDF::Model::createProperty
virtual PropertyPtr createProperty(const QString &uri)
creates a property and associates it with this model.
Definition: model.cpp:55
QDomNode::isElement
bool isElement() const
QDomNodeList
QDomNode::namespaceURI
QString namespaceURI() const
Syndication::RDF::ModelMaker::createFromXML
Model createFromXML(const QDomDocument &doc)
parses an RDF model from RDF/XML
Definition: modelmaker.cpp:39
Syndication::RDF::Model::createSequence
virtual SequencePtr createSequence(const QString &uri=QString())
creates a sequence and associates it with this model.
Definition: model.cpp:98
QDomDocument::documentElement
QDomElement documentElement() const
QDomNode::childNodes
QDomNodeList childNodes() const
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::Model::addStatement
virtual StatementPtr addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
adds a statement to the model.
Definition: model.cpp:146
QDomNode::localName
QString localName() const
QDomElement::text
QString text() const
QDomElement::hasAttribute
bool hasAttribute(const QString &name) const
QString::isEmpty
bool isEmpty() const
QString
QDomDocument
QDomNode::isNull
bool isNull() const
QLatin1String
QDomNode::lastChildElement
QDomElement lastChildElement(const QString &tagName) const
Syndication::RDF::Model::createLiteral
virtual LiteralPtr createLiteral(const QString &text)
creates a literal and associates it with this model.
Definition: model.cpp:123
Syndication::RDF::Model::createResource
virtual ResourcePtr createResource(const QString &uri=QString())
creates a resource and associates it with this model.
Definition: model.cpp:80
QDomNodeList::length
uint length() const
Syndication::RDF::RDFVocab::self
static RDFVocab * self()
returns the singleton instance
Definition: rdfvocab.cpp:51
QDomElement
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