Syndication

modelmaker.cpp
1/*
2 This file is part of the syndication library
3 SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "modelmaker.h"
9#include "literal.h"
10#include "model.h"
11#include "property.h"
12#include "rdfvocab.h"
13#include "resource.h"
14#include "sequence.h"
15#include "statement.h"
16
17#include <QDomElement>
18#include <QString>
19
20namespace Syndication
21{
22namespace RDF
23{
25{
26 Model model;
27
28 if (doc.isNull()) {
29 return model;
30 }
31
32 const QDomElement rdfNode = doc.documentElement();
33
34 const QDomNodeList list = rdfNode.childNodes();
35
36 for (int i = 0; i < list.length(); ++i) {
37 if (list.item(i).isElement()) {
38 const QDomElement el = list.item(i).toElement();
39 readResource(model, el);
40 }
41 }
42
43 return model;
44}
45
46ResourcePtr ModelMaker::readResource(Model &model, const QDomElement &el)
47{
48 QString about = QStringLiteral("about");
49 QString resource = QStringLiteral("resource");
50
51 ResourcePtr res;
52
53 ResourcePtr type = model.createResource(el.namespaceURI() + el.localName());
54
55 if (*type == *(RDFVocab::self()->seq())) {
56 SequencePtr seq = model.createSequence(el.attribute(about));
57
58 res = seq;
59 } else {
60 res = model.createResource(el.attribute(about));
61 }
62
63 model.addStatement(res, RDFVocab::self()->type(), type);
64
65 const QDomNodeList children = el.childNodes();
66
67 bool isSeq = res->isSequence();
68
69 for (int i = 0; i < children.length(); ++i) {
70 if (children.item(i).isElement()) {
71 QDomElement ce = children.item(i).toElement();
72
73 PropertyPtr pred = model.createProperty(ce.namespaceURI() + ce.localName());
74
75 if (ce.hasAttribute(resource)) { // referenced Resource via (rdf:)resource
76 NodePtr obj = model.createResource(ce.attribute(resource));
77
78 if (isSeq && *pred == *(RDFVocab::self()->li())) {
79 SequencePtr tseq = res.staticCast<Sequence>();
80 tseq->append(obj);
81 } else {
82 model.addStatement(res, pred, obj);
83 }
84 } else if (!ce.text().isEmpty() && ce.lastChildElement().isNull()) { // Literal
85 NodePtr obj = model.createLiteral(ce.text());
86
87 if (isSeq && *pred == *(RDFVocab::self()->li())) {
88 SequencePtr tseq = res.staticCast<Sequence>();
89 tseq->append(obj);
90 } else {
91 model.addStatement(res, pred, obj);
92 }
93 } else { // embedded description
95
96 // QString uri = re.attribute(about);
97
98 // read recursively
99 NodePtr obj = readResource(model, re);
100
101 if (isSeq && *pred == *(RDFVocab::self()->li())) {
102 SequencePtr tseq = res.staticCast<Sequence>();
103 tseq->append(obj);
104 } else {
105 model.addStatement(res, pred, obj);
106 }
107 }
108
109 // TODO: bag, reification (nice to have, but not important for basic RSS 1.0)
110 }
111 }
112
113 return res;
114}
115
116} // namespace RDF
117} // namespace Syndication
Model createFromXML(const QDomDocument &doc)
parses an RDF model from RDF/XML
An RDF model, a set of RDF statements.
Definition model.h:37
virtual StatementPtr addStatement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
adds a statement to the model.
Definition model.cpp:122
virtual ResourcePtr createResource(const QString &uri=QString())
creates a resource and associates it with this model.
Definition model.cpp:62
virtual SequencePtr createSequence(const QString &uri=QString())
creates a sequence and associates it with this model.
Definition model.cpp:77
virtual PropertyPtr createProperty(const QString &uri)
creates a property and associates it with this model.
Definition model.cpp:42
virtual LiteralPtr createLiteral(const QString &text)
creates a literal and associates it with this model.
Definition model.cpp:98
static RDFVocab * self()
returns the singleton instance
Definition rdfvocab.cpp:36
QDomElement documentElement() const const
QString attribute(const QString &name, const QString &defValue) const const
bool hasAttribute(const QString &name) const const
QString text() const const
QDomNodeList childNodes() const const
bool isElement() const const
bool isNull() const const
QDomElement lastChildElement(const QString &tagName, const QString &namespaceURI) const const
QString localName() const const
QString namespaceURI() const const
QDomElement toElement() const const
QDomNode item(int index) const const
int length() const const
qsizetype length() const const
QSharedPointer< X > staticCast() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.