File indexing completed on 2024-12-08 03:45:04

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 #ifndef SYNDICATION_RDF_MODEL_P_H
0009 #define SYNDICATION_RDF_MODEL_P_H
0010 
0011 #include "literal.h"
0012 #include "model.h"
0013 #include "nodevisitor.h"
0014 #include "property.h"
0015 #include "rdfvocab.h"
0016 #include "resource.h"
0017 #include "sequence.h"
0018 #include "statement.h"
0019 
0020 #include <QHash>
0021 #include <QList>
0022 #include <QString>
0023 
0024 namespace Syndication
0025 {
0026 namespace RDF
0027 {
0028 class SYNDICATION_NO_EXPORT Model::ModelPrivate
0029 {
0030 public:
0031     long id;
0032     static long idCounter;
0033     LiteralPtr nullLiteral;
0034     PropertyPtr nullProperty;
0035     ResourcePtr nullResource;
0036     StatementPtr nullStatement;
0037     QHash<QString, StatementPtr> statements;
0038     QHash<QString, QList<StatementPtr>> stmtsBySubject;
0039 
0040     QHash<int, NodePtr> nodes;
0041     QHash<QString, ResourcePtr> resources;
0042     QHash<QString, PropertyPtr> properties;
0043     QHash<QString, SequencePtr> sequences;
0044     bool initialized;
0045 
0046     class AddToHashesVisitor;
0047 
0048     ModelPrivate()
0049         : id(idCounter++)
0050     {
0051         addToHashesVisitor = new AddToHashesVisitor(this);
0052         initialized = false;
0053     }
0054 
0055     ~ModelPrivate()
0056     {
0057         delete addToHashesVisitor;
0058     }
0059 
0060     bool operator==(const ModelPrivate &other) const
0061     {
0062         return id == other.id;
0063     }
0064 
0065     class AddToHashesVisitor : public NodeVisitor
0066     {
0067     public:
0068         explicit AddToHashesVisitor(ModelPrivate *parent)
0069             : p(parent)
0070         {
0071         }
0072 
0073         bool visitResource(ResourcePtr res) override
0074         {
0075             visitNode(res);
0076             p->resources[res->uri()] = res;
0077             return true;
0078         }
0079 
0080         bool visitSequence(SequencePtr seq) override
0081         {
0082             visitResource(seq);
0083             p->sequences[seq->uri()] = seq;
0084             return true;
0085         }
0086 
0087         bool visitProperty(PropertyPtr prop) override
0088         {
0089             visitResource(prop);
0090             p->properties[prop->uri()] = prop;
0091             return true;
0092         }
0093 
0094         bool visitNode(NodePtr node) override
0095         {
0096             p->nodes[node->id()] = node;
0097             return true;
0098         }
0099 
0100         ModelPrivate *p;
0101     };
0102 
0103     AddToHashesVisitor *addToHashesVisitor;
0104 
0105     bool resourceHasProperty(const Resource *resource, PropertyPtr property) const;
0106 
0107     StatementPtr resourceProperty(const Resource *resource, PropertyPtr property) const;
0108 
0109     QList<StatementPtr> resourceProperties(const Resource *resource, PropertyPtr property) const;
0110 
0111     NodePtr nodeByID(uint id) const;
0112 
0113     ResourcePtr resourceByID(uint id) const;
0114 
0115     PropertyPtr propertyByID(uint id) const;
0116 
0117     LiteralPtr literalByID(uint id) const;
0118 
0119     void addToHashes(NodePtr node)
0120     {
0121         addToHashesVisitor->visit(node);
0122     }
0123 
0124     void addToHashes(StatementPtr stmt, const QString &key)
0125     {
0126         statements[key] = stmt;
0127         stmtsBySubject[stmt->subject()->uri()].append(stmt);
0128     }
0129 
0130     void removeFromHashes(const QString &key)
0131     {
0132         StatementPtr stmt = statements[key];
0133         if (stmt) {
0134             stmtsBySubject[stmt->subject()->uri()].removeAll(stmt);
0135         }
0136         statements.remove(key);
0137     }
0138 
0139     void init(const QSharedPointer<ModelPrivate> &sharedThis)
0140     {
0141         if (!initialized) {
0142             Model m;
0143             m.d = sharedThis;
0144             nullLiteral = LiteralPtr(new Literal());
0145             nullLiteral->setModel(m);
0146             nullProperty = PropertyPtr(new Property());
0147             nullProperty->setModel(m);
0148             nullResource = ResourcePtr(new Resource());
0149             nullResource->setModel(m);
0150             nullStatement = StatementPtr(new Statement());
0151             initialized = true;
0152         }
0153     }
0154 
0155 private:
0156     Q_DISABLE_COPY(ModelPrivate)
0157 };
0158 
0159 } // namespace RDF
0160 } // namespace Syndication
0161 
0162 #endif // SYNDICATION_RDF_MODEL_P_H