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 | #ifndef SYNDICATION_RDF_MODEL_P_H |
9 | #define SYNDICATION_RDF_MODEL_P_H |
10 | |
11 | #include "literal.h" |
12 | #include "model.h" |
13 | #include "nodevisitor.h" |
14 | #include "property.h" |
15 | #include "rdfvocab.h" |
16 | #include "resource.h" |
17 | #include "sequence.h" |
18 | #include "statement.h" |
19 | |
20 | #include <QHash> |
21 | #include <QList> |
22 | #include <QString> |
23 | |
24 | namespace Syndication |
25 | { |
26 | namespace RDF |
27 | { |
28 | class SYNDICATION_NO_EXPORT Model::ModelPrivate |
29 | { |
30 | public: |
31 | long id; |
32 | static long idCounter; |
33 | LiteralPtr nullLiteral; |
34 | PropertyPtr nullProperty; |
35 | ResourcePtr nullResource; |
36 | StatementPtr nullStatement; |
37 | QHash<QString, StatementPtr> statements; |
38 | QHash<QString, QList<StatementPtr>> stmtsBySubject; |
39 | |
40 | QHash<int, NodePtr> nodes; |
41 | QHash<QString, ResourcePtr> resources; |
42 | QHash<QString, PropertyPtr> properties; |
43 | QHash<QString, SequencePtr> sequences; |
44 | bool initialized; |
45 | |
46 | class AddToHashesVisitor; |
47 | |
48 | ModelPrivate() |
49 | : id(idCounter++) |
50 | { |
51 | addToHashesVisitor = new AddToHashesVisitor(this); |
52 | initialized = false; |
53 | } |
54 | |
55 | ~ModelPrivate() |
56 | { |
57 | delete addToHashesVisitor; |
58 | } |
59 | |
60 | bool operator==(const ModelPrivate &other) const |
61 | { |
62 | return id == other.id; |
63 | } |
64 | |
65 | class AddToHashesVisitor : public NodeVisitor |
66 | { |
67 | public: |
68 | explicit AddToHashesVisitor(ModelPrivate *parent) |
69 | : p(parent) |
70 | { |
71 | } |
72 | |
73 | bool visitResource(ResourcePtr res) override |
74 | { |
75 | visitNode(node: res); |
76 | p->resources[res->uri()] = res; |
77 | return true; |
78 | } |
79 | |
80 | bool visitSequence(SequencePtr seq) override |
81 | { |
82 | visitResource(res: seq); |
83 | p->sequences[seq->uri()] = seq; |
84 | return true; |
85 | } |
86 | |
87 | bool visitProperty(PropertyPtr prop) override |
88 | { |
89 | visitResource(res: prop); |
90 | p->properties[prop->uri()] = prop; |
91 | return true; |
92 | } |
93 | |
94 | bool visitNode(NodePtr node) override |
95 | { |
96 | p->nodes[node->id()] = node; |
97 | return true; |
98 | } |
99 | |
100 | ModelPrivate *p; |
101 | }; |
102 | |
103 | AddToHashesVisitor *addToHashesVisitor; |
104 | |
105 | bool resourceHasProperty(const Resource *resource, PropertyPtr property) const; |
106 | |
107 | StatementPtr resourceProperty(const Resource *resource, PropertyPtr property) const; |
108 | |
109 | QList<StatementPtr> resourceProperties(const Resource *resource, PropertyPtr property) const; |
110 | |
111 | NodePtr nodeByID(uint id) const; |
112 | |
113 | ResourcePtr resourceByID(uint id) const; |
114 | |
115 | PropertyPtr propertyByID(uint id) const; |
116 | |
117 | LiteralPtr literalByID(uint id) const; |
118 | |
119 | void addToHashes(NodePtr node) |
120 | { |
121 | addToHashesVisitor->visit(node); |
122 | } |
123 | |
124 | void addToHashes(StatementPtr stmt, const QString &key) |
125 | { |
126 | statements[key] = stmt; |
127 | stmtsBySubject[stmt->subject()->uri()].append(t: stmt); |
128 | } |
129 | |
130 | void removeFromHashes(const QString &key) |
131 | { |
132 | StatementPtr stmt = statements[key]; |
133 | if (stmt) { |
134 | stmtsBySubject[stmt->subject()->uri()].removeAll(t: stmt); |
135 | } |
136 | statements.remove(key); |
137 | } |
138 | |
139 | void init(const QSharedPointer<ModelPrivate> &sharedThis) |
140 | { |
141 | if (!initialized) { |
142 | Model m; |
143 | m.d = sharedThis; |
144 | nullLiteral = LiteralPtr(new Literal()); |
145 | nullLiteral->setModel(m); |
146 | nullProperty = PropertyPtr(new Property()); |
147 | nullProperty->setModel(m); |
148 | nullResource = ResourcePtr(new Resource()); |
149 | nullResource->setModel(m); |
150 | nullStatement = StatementPtr(new Statement()); |
151 | initialized = true; |
152 | } |
153 | } |
154 | |
155 | private: |
156 | Q_DISABLE_COPY(ModelPrivate) |
157 | }; |
158 | |
159 | } // namespace RDF |
160 | } // namespace Syndication |
161 | |
162 | #endif // SYNDICATION_RDF_MODEL_P_H |
163 | |