File indexing completed on 2024-04-21 04:01:03

0001 /*
0002     This file is part of the syndication library
0003     SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "resource.h"
0009 #include "model.h"
0010 #include "model_p.h"
0011 #include "nodevisitor.h"
0012 #include "property.h"
0013 #include "statement.h"
0014 
0015 #include <QList>
0016 #include <QString>
0017 #include <QUuid>
0018 #include <QWeakPointer>
0019 
0020 namespace Syndication
0021 {
0022 namespace RDF
0023 {
0024 class SYNDICATION_NO_EXPORT Resource::ResourcePrivate
0025 {
0026 public:
0027     QString uri;
0028     QWeakPointer<Model::ModelPrivate> model;
0029     bool isAnon;
0030     unsigned int id;
0031 
0032     bool operator==(const ResourcePrivate &other) const
0033     {
0034         if (!isAnon && !other.isAnon) {
0035             return uri == other.uri;
0036         } else {
0037             return id == other.id;
0038         }
0039     }
0040 };
0041 
0042 Resource::Resource(const Resource &other)
0043     : Node(other)
0044 {
0045     *this = other;
0046 }
0047 
0048 Resource::Resource()
0049     : d()
0050 {
0051 }
0052 
0053 Resource::Resource(const QString &uri)
0054     : d(new ResourcePrivate)
0055 {
0056     if (uri.isNull()) {
0057         d->uri = QUuid().createUuid().toString();
0058         d->isAnon = true;
0059     } else {
0060         d->uri = uri;
0061         d->isAnon = false;
0062     }
0063 
0064     d->id = idCounter++;
0065 }
0066 
0067 Resource::~Resource()
0068 {
0069 }
0070 
0071 Resource &Resource::operator=(const Resource &other)
0072 {
0073     d = other.d;
0074     return *this;
0075 }
0076 
0077 bool Resource::operator==(const Resource &other) const
0078 {
0079     if (!d || !other.d) {
0080         return d == other.d;
0081     }
0082     return *d == *(other.d);
0083 }
0084 
0085 bool Resource::hasProperty(PropertyPtr property) const
0086 {
0087     if (!d) {
0088         return false;
0089     }
0090     const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
0091     if (!m) {
0092         return false;
0093     }
0094     return m->resourceHasProperty(this, property);
0095 }
0096 
0097 StatementPtr Resource::property(PropertyPtr property) const
0098 {
0099     StatementPtr ptr(new Statement());
0100     if (!d) {
0101         return ptr;
0102     }
0103     const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
0104     if (!m) {
0105         return ptr;
0106     }
0107     return m->resourceProperty(this, property);
0108 }
0109 
0110 QList<StatementPtr> Resource::properties(PropertyPtr property) const
0111 {
0112     if (!d) {
0113         return QList<StatementPtr>();
0114     }
0115     const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
0116     if (!m) {
0117         return QList<StatementPtr>();
0118     }
0119 
0120     return m->resourceProperties(this, property);
0121 }
0122 
0123 Resource *Resource::clone() const
0124 {
0125     return new Resource(*this);
0126 }
0127 
0128 void Resource::accept(NodeVisitor *visitor, NodePtr ptr)
0129 {
0130     ResourcePtr rptr = ptr.staticCast<Resource>();
0131     if (!visitor->visitResource(rptr)) {
0132         Node::accept(visitor, ptr);
0133     }
0134 }
0135 
0136 unsigned int Resource::id() const
0137 {
0138     return d ? d->id : 0;
0139 }
0140 
0141 bool Resource::isNull() const
0142 {
0143     return !d;
0144 }
0145 
0146 Model Resource::model() const
0147 {
0148     if (!d) {
0149         return Model();
0150     }
0151 
0152     const QSharedPointer<Model::ModelPrivate> mp = d->model.toStrongRef();
0153 
0154     Model m;
0155 
0156     if (mp) {
0157         m.d = mp;
0158     }
0159 
0160     return m;
0161 }
0162 
0163 bool Resource::isResource() const
0164 {
0165     return true;
0166 }
0167 
0168 bool Resource::isProperty() const
0169 {
0170     return false;
0171 }
0172 
0173 bool Resource::isLiteral() const
0174 {
0175     return false;
0176 }
0177 
0178 bool Resource::isAnon() const
0179 {
0180     return d ? d->isAnon : false;
0181 }
0182 
0183 bool Resource::isSequence() const
0184 {
0185     return false;
0186 }
0187 
0188 void Resource::setModel(const Model &model)
0189 {
0190     if (d) {
0191         d->model = model.d;
0192     }
0193 }
0194 
0195 void Resource::setId(unsigned int id)
0196 {
0197     if (d) {
0198         d->id = id;
0199     }
0200 }
0201 
0202 QString Resource::text() const
0203 {
0204     return QString();
0205 }
0206 
0207 QString Resource::uri() const
0208 {
0209     return d ? d->uri : QString();
0210 }
0211 
0212 } // namespace RDF
0213 } // namespace Syndication