File indexing completed on 2024-04-28 15:34:25

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 Node &other) const
0078 {
0079     const Resource *o2 = dynamic_cast<const Resource *>(&other);
0080     if (!o2) {
0081         return false;
0082     }
0083 
0084     if (!d || !o2->d) {
0085         return d == o2->d;
0086     }
0087     return *d == *(o2->d);
0088 }
0089 
0090 bool Resource::hasProperty(PropertyPtr property) const
0091 {
0092     if (!d) {
0093         return false;
0094     }
0095     const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
0096     if (!m) {
0097         return false;
0098     }
0099     return m->resourceHasProperty(this, property);
0100 }
0101 
0102 StatementPtr Resource::property(PropertyPtr property) const
0103 {
0104     StatementPtr ptr(new Statement());
0105     if (!d) {
0106         return ptr;
0107     }
0108     const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
0109     if (!m) {
0110         return ptr;
0111     }
0112     return m->resourceProperty(this, property);
0113 }
0114 
0115 QList<StatementPtr> Resource::properties(PropertyPtr property) const
0116 {
0117     if (!d) {
0118         return QList<StatementPtr>();
0119     }
0120     const QSharedPointer<Model::ModelPrivate> m = d->model.toStrongRef();
0121     if (!m) {
0122         return QList<StatementPtr>();
0123     }
0124 
0125     return m->resourceProperties(this, property);
0126 }
0127 
0128 Resource *Resource::clone() const
0129 {
0130     return new Resource(*this);
0131 }
0132 
0133 void Resource::accept(NodeVisitor *visitor, NodePtr ptr)
0134 {
0135     ResourcePtr rptr = ptr.staticCast<Resource>();
0136     if (!visitor->visitResource(rptr)) {
0137         Node::accept(visitor, ptr);
0138     }
0139 }
0140 
0141 unsigned int Resource::id() const
0142 {
0143     return d ? d->id : 0;
0144 }
0145 
0146 bool Resource::isNull() const
0147 {
0148     return !d;
0149 }
0150 
0151 Model Resource::model() const
0152 {
0153     if (!d) {
0154         return Model();
0155     }
0156 
0157     const QSharedPointer<Model::ModelPrivate> mp = d->model.toStrongRef();
0158 
0159     Model m;
0160 
0161     if (mp) {
0162         m.d = mp;
0163     }
0164 
0165     return m;
0166 }
0167 
0168 bool Resource::isResource() const
0169 {
0170     return true;
0171 }
0172 
0173 bool Resource::isProperty() const
0174 {
0175     return false;
0176 }
0177 
0178 bool Resource::isLiteral() const
0179 {
0180     return false;
0181 }
0182 
0183 bool Resource::isAnon() const
0184 {
0185     return d ? d->isAnon : false;
0186 }
0187 
0188 bool Resource::isSequence() const
0189 {
0190     return false;
0191 }
0192 
0193 void Resource::setModel(const Model &model)
0194 {
0195     if (d) {
0196         d->model = model.d;
0197     }
0198 }
0199 
0200 void Resource::setId(unsigned int id)
0201 {
0202     if (d) {
0203         d->id = id;
0204     }
0205 }
0206 
0207 QString Resource::text() const
0208 {
0209     return QString();
0210 }
0211 
0212 QString Resource::uri() const
0213 {
0214     return d ? d->uri : QString();
0215 }
0216 
0217 } // namespace RDF
0218 } // namespace Syndication