| 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 | |
| 20 | namespace Syndication |
| 21 | { |
| 22 | namespace RDF |
| 23 | { |
| 24 | class SYNDICATION_NO_EXPORT Resource::ResourcePrivate |
| 25 | { |
| 26 | public: |
| 27 | QString uri; |
| 28 | QWeakPointer<Model::ModelPrivate> model; |
| 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 | |
| 42 | Resource::Resource(const Resource &other) |
| 43 | : Node(other) |
| 44 | { |
| 45 | *this = other; |
| 46 | } |
| 47 | |
| 48 | Resource::Resource() |
| 49 | : d() |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | Resource::Resource(const QString &uri) |
| 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 | |
| 67 | Resource::~Resource() |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | Resource &Resource::operator=(const Resource &other) |
| 72 | { |
| 73 | d = other.d; |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | bool 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 | |
| 85 | bool Resource::hasProperty(PropertyPtr property) const |
| 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(resource: this, property); |
| 95 | } |
| 96 | |
| 97 | StatementPtr Resource::property(PropertyPtr property) const |
| 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(resource: this, property); |
| 108 | } |
| 109 | |
| 110 | QList<StatementPtr> Resource::properties(PropertyPtr property) const |
| 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(resource: this, property); |
| 121 | } |
| 122 | |
| 123 | Resource *Resource::clone() const |
| 124 | { |
| 125 | return new Resource(*this); |
| 126 | } |
| 127 | |
| 128 | void Resource::accept(NodeVisitor *visitor, NodePtr ptr) |
| 129 | { |
| 130 | ResourcePtr rptr = ptr.staticCast<Resource>(); |
| 131 | if (!visitor->visitResource(resource: rptr)) { |
| 132 | Node::accept(visitor, ptr); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | unsigned int Resource::id() const |
| 137 | { |
| 138 | return d ? d->id : 0; |
| 139 | } |
| 140 | |
| 141 | bool Resource::isNull() const |
| 142 | { |
| 143 | return !d; |
| 144 | } |
| 145 | |
| 146 | Model Resource::model() const |
| 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 | |
| 163 | bool Resource::isResource() const |
| 164 | { |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool Resource::isProperty() const |
| 169 | { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | bool Resource::isLiteral() const |
| 174 | { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | bool Resource::isAnon() const |
| 179 | { |
| 180 | return d ? d->isAnon : false; |
| 181 | } |
| 182 | |
| 183 | bool Resource::isSequence() const |
| 184 | { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | void Resource::setModel(const Model &model) |
| 189 | { |
| 190 | if (d) { |
| 191 | d->model = model.d; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void Resource::setId(unsigned int id) |
| 196 | { |
| 197 | if (d) { |
| 198 | d->id = id; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | QString Resource::text() const |
| 203 | { |
| 204 | return QString(); |
| 205 | } |
| 206 | |
| 207 | QString Resource::uri() const |
| 208 | { |
| 209 | return d ? d->uri : QString(); |
| 210 | } |
| 211 | |
| 212 | } // namespace RDF |
| 213 | } // namespace Syndication |
| 214 |
