Syndication

resource.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 "resource.h"
9#include "model.h"
10#include "model_p.h"
11#include "nodevisitor.h"
12#include "property.h"
13#include "statement.h"
14
15#include <QList>
16#include <QString>
17#include <QUuid>
18#include <QWeakPointer>
19
20namespace Syndication
21{
22namespace RDF
23{
24class SYNDICATION_NO_EXPORT Resource::ResourcePrivate
25{
26public:
27 QString uri;
29 bool isAnon;
30 unsigned int id;
31
32 bool operator==(const ResourcePrivate &other) const
33 {
34 if (!isAnon && !other.isAnon) {
35 return uri == other.uri;
36 } else {
37 return id == other.id;
38 }
39 }
40};
41
43 : Node(other)
44{
45 *this = other;
46}
47
49 : d()
50{
51}
52
54 : d(new ResourcePrivate)
55{
56 if (uri.isNull()) {
57 d->uri = QUuid().createUuid().toString();
58 d->isAnon = true;
59 } else {
60 d->uri = uri;
61 d->isAnon = false;
62 }
63
64 d->id = idCounter++;
65}
66
70
72{
73 d = other.d;
74 return *this;
75}
76
77bool Resource::operator==(const Resource &other) const
78{
79 if (!d || !other.d) {
80 return d == other.d;
81 }
82 return *d == *(other.d);
83}
84
86{
87 if (!d) {
88 return false;
89 }
90 const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
91 if (!m) {
92 return false;
93 }
94 return m->resourceHasProperty(this, property);
95}
96
98{
99 StatementPtr ptr(new Statement());
100 if (!d) {
101 return ptr;
102 }
103 const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
104 if (!m) {
105 return ptr;
106 }
107 return m->resourceProperty(this, property);
108}
109
111{
112 if (!d) {
113 return QList<StatementPtr>();
114 }
115 const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
116 if (!m) {
117 return QList<StatementPtr>();
118 }
119
120 return m->resourceProperties(this, property);
121}
122
124{
125 return new Resource(*this);
126}
127
128void Resource::accept(NodeVisitor *visitor, NodePtr ptr)
129{
130 ResourcePtr rptr = ptr.staticCast<Resource>();
131 if (!visitor->visitResource(rptr)) {
132 Node::accept(visitor, ptr);
133 }
134}
135
136unsigned int Resource::id() const
137{
138 return d ? d->id : 0;
139}
140
142{
143 return !d;
144}
145
147{
148 if (!d) {
149 return Model();
150 }
151
152 const QSharedPointer<Model::ModelPrivate> mp = d->model.toStrongRef();
153
154 Model m;
155
156 if (mp) {
157 m.d = mp;
158 }
159
160 return m;
161}
162
164{
165 return true;
166}
167
169{
170 return false;
171}
172
174{
175 return false;
176}
177
179{
180 return d ? d->isAnon : false;
181}
182
184{
185 return false;
186}
187
188void Resource::setModel(const Model &model)
189{
190 if (d) {
191 d->model = model.d;
192 }
193}
194
195void Resource::setId(unsigned int id)
196{
197 if (d) {
198 d->id = id;
199 }
200}
201
203{
204 return QString();
205}
206
208{
209 return d ? d->uri : QString();
210}
211
212} // namespace RDF
213} // namespace Syndication
An RDF model, a set of RDF statements.
Definition model.h:37
Visitor interface, following the Visitor design pattern.
Definition nodevisitor.h:42
virtual bool visitResource(ResourcePtr resource)
reimplement this method to handle resources.
an RDF node, abstract baseclass for all RDF node types, like resources and literals
Definition node.h:32
virtual void accept(NodeVisitor *visitor, NodePtr ptr)
Used by visitors for double dispatch.
Definition node.cpp:17
static unsigned int idCounter
used to generate unique IDs for node objects
Definition node.h:113
Resources are the entities in the RDF graph.
Definition resource.h:37
bool isSequence() const override
returns true if this resource is also a sequence, false otherwise.
Definition resource.cpp:183
void accept(NodeVisitor *visitor, NodePtr ptr) override
Used by visitors for double dispatch.
Definition resource.cpp:128
void setId(unsigned int id) override
used in Model
Definition resource.cpp:195
virtual QString uri() const
returns the URI of the resource
Definition resource.cpp:207
bool isAnon() const override
returns whether this resource is an anonymous resource
Definition resource.cpp:178
Resource * clone() const override
creates a copy of the resource object
Definition resource.cpp:123
virtual bool hasProperty(PropertyPtr property) const
returns whether the resource has a property property in the associated model.
Definition resource.cpp:85
void setModel(const Model &model) override
used in Model
Definition resource.cpp:188
virtual QList< StatementPtr > properties(PropertyPtr property) const
returns the list of all statements from the associated model where this resource is the subject and t...
Definition resource.cpp:110
bool operator==(const Resource &other) const
checks two resources for equality.
Definition resource.cpp:77
Resource()
creates a null resource
Definition resource.cpp:48
~Resource() override
destructor
Definition resource.cpp:67
bool isLiteral() const override
returns false
Definition resource.cpp:173
bool isResource() const override
returns true
Definition resource.cpp:163
bool isProperty() const override
returns true if this resource is also a property, false otherwise
Definition resource.cpp:168
unsigned int id() const override
the identifier of this node.
Definition resource.cpp:136
Resource & operator=(const Resource &other)
assigns a resource
Definition resource.cpp:71
bool isNull() const override
returns whether the resource is a null resource
Definition resource.cpp:141
QString text() const override
returns a null string
Definition resource.cpp:202
virtual Model model() const
the model this resource belongs to
Definition resource.cpp:146
virtual StatementPtr property(PropertyPtr property) const
returns a statement from the associated model where this resource is the subject and the given proper...
Definition resource.cpp:97
An RDF statement, consisting of a triple (subject, predicate, object).
Definition statement.h:29
KIOCORE_EXPORT bool operator==(const UDSEntry &entry, const UDSEntry &other)
QSharedPointer< X > staticCast() const const
bool isNull() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:48:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.