Chris@439
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@439
|
2
|
Chris@439
|
3 /*
|
Chris@439
|
4 Sonic Visualiser
|
Chris@439
|
5 An audio file viewer and annotation editor.
|
Chris@439
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@439
|
7 This file copyright 2008 QMUL.
|
Chris@439
|
8
|
Chris@439
|
9 This program is free software; you can redistribute it and/or
|
Chris@439
|
10 modify it under the terms of the GNU General Public License as
|
Chris@439
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@439
|
12 License, or (at your option) any later version. See the file
|
Chris@439
|
13 COPYING included with this distribution for more information.
|
Chris@439
|
14 */
|
Chris@439
|
15
|
Chris@439
|
16 #include "PluginRDFIndexer.h"
|
Chris@439
|
17
|
Chris@439
|
18 #include "SimpleSPARQLQuery.h"
|
Chris@439
|
19
|
Chris@439
|
20 #include "data/fileio/FileSource.h"
|
Chris@439
|
21 #include "plugin/PluginIdentifier.h"
|
Chris@439
|
22
|
Chris@439
|
23 #include <vamp-sdk/PluginHostAdapter.h>
|
Chris@439
|
24
|
Chris@439
|
25 #include <QFileInfo>
|
Chris@439
|
26 #include <QDir>
|
Chris@439
|
27 #include <QUrl>
|
Chris@439
|
28
|
Chris@439
|
29 #include <iostream>
|
Chris@439
|
30 using std::cerr;
|
Chris@439
|
31 using std::endl;
|
Chris@439
|
32 using std::vector;
|
Chris@439
|
33 using std::string;
|
Chris@439
|
34 using Vamp::PluginHostAdapter;
|
Chris@439
|
35
|
Chris@439
|
36 PluginRDFIndexer *
|
Chris@439
|
37 PluginRDFIndexer::m_instance = 0;
|
Chris@439
|
38
|
Chris@439
|
39 PluginRDFIndexer *
|
Chris@439
|
40 PluginRDFIndexer::getInstance()
|
Chris@439
|
41 {
|
Chris@439
|
42 if (!m_instance) m_instance = new PluginRDFIndexer();
|
Chris@439
|
43 return m_instance;
|
Chris@439
|
44 }
|
Chris@439
|
45
|
Chris@439
|
46 PluginRDFIndexer::PluginRDFIndexer()
|
Chris@439
|
47 {
|
Chris@439
|
48 vector<string> paths = PluginHostAdapter::getPluginPath();
|
Chris@439
|
49
|
Chris@439
|
50 QStringList filters;
|
Chris@439
|
51 filters << "*.n3";
|
Chris@439
|
52 filters << "*.N3";
|
Chris@439
|
53 filters << "*.rdf";
|
Chris@439
|
54 filters << "*.RDF";
|
Chris@439
|
55
|
Chris@439
|
56 // Search each Vamp plugin path for a .rdf file that either has
|
Chris@439
|
57 // name "soname", "soname:label" or "soname/label" plus RDF
|
Chris@439
|
58 // extension. Use that order of preference, and prefer n3 over
|
Chris@439
|
59 // rdf extension.
|
Chris@439
|
60
|
Chris@439
|
61 for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
|
Chris@439
|
62
|
Chris@439
|
63 QDir dir(i->c_str());
|
Chris@439
|
64 if (!dir.exists()) continue;
|
Chris@439
|
65
|
Chris@439
|
66 QStringList entries = dir.entryList
|
Chris@439
|
67 (filters, QDir::Files | QDir::Readable);
|
Chris@439
|
68
|
Chris@439
|
69 for (QStringList::const_iterator j = entries.begin();
|
Chris@439
|
70 j != entries.end(); ++j) {
|
Chris@439
|
71 QFileInfo fi(dir.filePath(*j));
|
Chris@439
|
72 indexFile(fi.absoluteFilePath());
|
Chris@439
|
73 }
|
Chris@439
|
74
|
Chris@439
|
75 QStringList subdirs = dir.entryList
|
Chris@439
|
76 (QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Readable);
|
Chris@439
|
77
|
Chris@439
|
78 for (QStringList::const_iterator j = subdirs.begin();
|
Chris@439
|
79 j != subdirs.end(); ++j) {
|
Chris@439
|
80 QDir subdir(dir.filePath(*j));
|
Chris@439
|
81 if (subdir.exists()) {
|
Chris@439
|
82 entries = subdir.entryList
|
Chris@439
|
83 (filters, QDir::Files | QDir::Readable);
|
Chris@439
|
84 for (QStringList::const_iterator k = entries.begin();
|
Chris@439
|
85 k != entries.end(); ++k) {
|
Chris@439
|
86 QFileInfo fi(subdir.filePath(*k));
|
Chris@439
|
87 indexFile(fi.absoluteFilePath());
|
Chris@439
|
88 }
|
Chris@439
|
89 }
|
Chris@439
|
90 }
|
Chris@439
|
91 }
|
Chris@439
|
92 }
|
Chris@439
|
93
|
Chris@439
|
94 PluginRDFIndexer::~PluginRDFIndexer()
|
Chris@439
|
95 {
|
Chris@439
|
96 while (!m_cache.empty()) {
|
Chris@439
|
97 delete *m_cache.begin();
|
Chris@439
|
98 m_cache.erase(m_cache.begin());
|
Chris@439
|
99 }
|
Chris@439
|
100 }
|
Chris@439
|
101
|
Chris@439
|
102 QString
|
Chris@439
|
103 PluginRDFIndexer::getURIForPluginId(QString pluginId)
|
Chris@439
|
104 {
|
Chris@439
|
105 if (m_idToUriMap.find(pluginId) == m_idToUriMap.end()) return "";
|
Chris@439
|
106 return m_idToUriMap[pluginId];
|
Chris@439
|
107 }
|
Chris@439
|
108
|
Chris@439
|
109 QString
|
Chris@439
|
110 PluginRDFIndexer::getIdForPluginURI(QString uri)
|
Chris@439
|
111 {
|
Chris@439
|
112 if (m_uriToIdMap.find(uri) == m_uriToIdMap.end()) {
|
Chris@439
|
113
|
Chris@439
|
114 // Haven't found this uri referenced in any document on the
|
Chris@439
|
115 // local filesystem; try resolving the pre-fragment part of
|
Chris@439
|
116 // the uri as a document URL and reading that if possible.
|
Chris@439
|
117
|
Chris@439
|
118 // Because we may want to refer to this document again, we
|
Chris@439
|
119 // cache it locally if it turns out to exist.
|
Chris@439
|
120
|
Chris@439
|
121 cerr << "PluginRDFIndexer::getIdForPluginURI: NOTE: Failed to find a local RDF document describing plugin <" << uri.toStdString() << ">: attempting to retrieve one remotely by guesswork" << endl;
|
Chris@439
|
122
|
Chris@439
|
123 QString baseUrl = QUrl(uri).toString(QUrl::RemoveFragment);
|
Chris@439
|
124
|
Chris@439
|
125 FileSource source(baseUrl);
|
Chris@439
|
126 if (source.isAvailable()) {
|
Chris@439
|
127 source.waitForData();
|
Chris@439
|
128 if (indexFile(source.getLocalFilename())) {
|
Chris@439
|
129 m_cache.insert(new FileSource(source));
|
Chris@439
|
130 }
|
Chris@439
|
131 }
|
Chris@439
|
132
|
Chris@439
|
133 if (m_uriToIdMap.find(uri) == m_uriToIdMap.end()) {
|
Chris@439
|
134 m_uriToIdMap[uri] = "";
|
Chris@439
|
135 }
|
Chris@439
|
136 }
|
Chris@439
|
137
|
Chris@439
|
138 return m_uriToIdMap[uri];
|
Chris@439
|
139 }
|
Chris@439
|
140
|
Chris@439
|
141 QString
|
Chris@439
|
142 PluginRDFIndexer::getDescriptionURLForPluginId(QString pluginId)
|
Chris@439
|
143 {
|
Chris@439
|
144 if (m_idToDescriptionMap.find(pluginId) == m_idToDescriptionMap.end()) return "";
|
Chris@439
|
145 return m_idToDescriptionMap[pluginId];
|
Chris@439
|
146 }
|
Chris@439
|
147
|
Chris@439
|
148 QString
|
Chris@439
|
149 PluginRDFIndexer::getDescriptionURLForPluginURI(QString uri)
|
Chris@439
|
150 {
|
Chris@439
|
151 QString id = getIdForPluginURI(uri);
|
Chris@439
|
152 if (id == "") return "";
|
Chris@439
|
153 return getDescriptionURLForPluginId(id);
|
Chris@439
|
154 }
|
Chris@439
|
155
|
Chris@439
|
156 bool
|
Chris@439
|
157 PluginRDFIndexer::indexFile(QString filepath)
|
Chris@439
|
158 {
|
Chris@439
|
159 QUrl url = QUrl::fromLocalFile(filepath);
|
Chris@439
|
160 QString urlString = url.toString();
|
Chris@439
|
161 return indexURL(urlString);
|
Chris@439
|
162 }
|
Chris@439
|
163
|
Chris@439
|
164 bool
|
Chris@439
|
165 PluginRDFIndexer::indexURL(QString urlString)
|
Chris@439
|
166 {
|
Chris@439
|
167 // cerr << "PluginRDFIndexer::indexURL: url = <" << urlString.toStdString() << ">" << endl;
|
Chris@439
|
168
|
Chris@439
|
169 SimpleSPARQLQuery query
|
Chris@439
|
170 (QString
|
Chris@439
|
171 (
|
Chris@439
|
172 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
|
Chris@439
|
173
|
Chris@439
|
174 " SELECT ?plugin ?library_id ?plugin_id "
|
Chris@439
|
175 " FROM <%1> "
|
Chris@439
|
176
|
Chris@439
|
177 " WHERE { "
|
Chris@439
|
178 " ?plugin a vamp:Plugin . "
|
Chris@439
|
179
|
Chris@439
|
180 // Make the identifier and library parts optional, so
|
Chris@439
|
181 // that we can check and report helpfully if one or both
|
Chris@439
|
182 // is absent instead of just getting no results
|
Chris@439
|
183
|
Chris@439
|
184 " OPTIONAL { ?plugin vamp:identifier ?plugin_id } . "
|
Chris@439
|
185
|
Chris@439
|
186 " OPTIONAL { "
|
Chris@439
|
187 " ?library a vamp:PluginLibrary ; "
|
Chris@439
|
188 " vamp:available_plugin ?plugin ; "
|
Chris@439
|
189 " vamp:identifier ?library_id "
|
Chris@439
|
190 " } "
|
Chris@439
|
191 " } "
|
Chris@439
|
192 )
|
Chris@439
|
193 .arg(urlString));
|
Chris@439
|
194
|
Chris@439
|
195 SimpleSPARQLQuery::ResultList results = query.execute();
|
Chris@439
|
196
|
Chris@439
|
197 if (!query.isOK()) {
|
Chris@439
|
198 cerr << "ERROR: PluginRDFIndexer::indexURL: ERROR: Failed to index document at <"
|
Chris@439
|
199 << urlString.toStdString() << ">: "
|
Chris@439
|
200 << query.getErrorString().toStdString() << endl;
|
Chris@439
|
201 return false;
|
Chris@439
|
202 }
|
Chris@439
|
203
|
Chris@439
|
204 if (results.empty()) {
|
Chris@439
|
205 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
|
Chris@439
|
206 << urlString.toStdString()
|
Chris@439
|
207 << "> does not describe any vamp:Plugin resources" << endl;
|
Chris@439
|
208 return false;
|
Chris@439
|
209 }
|
Chris@439
|
210
|
Chris@439
|
211 bool foundSomething = false;
|
Chris@439
|
212 bool addedSomething = false;
|
Chris@439
|
213
|
Chris@439
|
214 for (SimpleSPARQLQuery::ResultList::iterator i = results.begin();
|
Chris@439
|
215 i != results.end(); ++i) {
|
Chris@439
|
216
|
Chris@439
|
217 QString pluginUri = (*i)["plugin"].value;
|
Chris@439
|
218 QString soname = (*i)["library_id"].value;
|
Chris@439
|
219 QString identifier = (*i)["plugin_id"].value;
|
Chris@439
|
220
|
Chris@439
|
221 if (identifier == "") {
|
Chris@439
|
222 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
|
Chris@439
|
223 << urlString.toStdString()
|
Chris@439
|
224 << "> fails to define any vamp:identifier for plugin <"
|
Chris@439
|
225 << pluginUri.toStdString() << ">"
|
Chris@439
|
226 << endl;
|
Chris@439
|
227 continue;
|
Chris@439
|
228 }
|
Chris@439
|
229 if (soname == "") {
|
Chris@439
|
230 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
|
Chris@439
|
231 << urlString.toStdString() << "> does not associate plugin <"
|
Chris@439
|
232 << pluginUri.toStdString() << "> with any implementation library"
|
Chris@439
|
233 << endl;
|
Chris@439
|
234 continue;
|
Chris@439
|
235 }
|
Chris@439
|
236 /*
|
Chris@439
|
237 cerr << "PluginRDFIndexer::indexURL: Document for plugin \""
|
Chris@439
|
238 << soname.toStdString() << ":" << identifier.toStdString()
|
Chris@439
|
239 << "\" (uri <" << pluginUri.toStdString() << ">) is at url <"
|
Chris@439
|
240 << urlString.toStdString() << ">" << endl;
|
Chris@439
|
241 */
|
Chris@439
|
242 QString pluginId = PluginIdentifier::createIdentifier
|
Chris@439
|
243 ("vamp", soname, identifier);
|
Chris@439
|
244
|
Chris@439
|
245 foundSomething = true;
|
Chris@439
|
246
|
Chris@439
|
247 if (m_idToDescriptionMap.find(pluginId) != m_idToDescriptionMap.end()) {
|
Chris@439
|
248 cerr << "PluginRDFIndexer::indexURL: NOTE: Plugin id \""
|
Chris@439
|
249 << pluginId.toStdString() << "\", described in document at <"
|
Chris@439
|
250 << urlString.toStdString()
|
Chris@439
|
251 << ">, has already been described in document <"
|
Chris@439
|
252 << m_idToDescriptionMap[pluginId].toStdString()
|
Chris@439
|
253 << ">: ignoring this new description" << endl;
|
Chris@439
|
254 continue;
|
Chris@439
|
255 }
|
Chris@439
|
256
|
Chris@439
|
257 m_idToDescriptionMap[pluginId] = urlString;
|
Chris@439
|
258 m_idToUriMap[pluginId] = pluginUri;
|
Chris@439
|
259
|
Chris@439
|
260 addedSomething = true;
|
Chris@439
|
261
|
Chris@439
|
262 if (pluginUri != "") {
|
Chris@439
|
263 if (m_uriToIdMap.find(pluginUri) != m_uriToIdMap.end()) {
|
Chris@439
|
264 cerr << "PluginRDFIndexer::indexURL: WARNING: Found multiple plugins with the same URI:" << endl;
|
Chris@439
|
265 cerr << " 1. Plugin id \"" << m_uriToIdMap[pluginUri].toStdString() << "\"" << endl;
|
Chris@439
|
266 cerr << " described in <" << m_idToDescriptionMap[m_uriToIdMap[pluginUri]].toStdString() << ">" << endl;
|
Chris@439
|
267 cerr << " 2. Plugin id \"" << pluginId.toStdString() << "\"" << endl;
|
Chris@439
|
268 cerr << " described in <" << urlString.toStdString() << ">" << endl;
|
Chris@439
|
269 cerr << "both claim URI <" << pluginUri.toStdString() << ">" << endl;
|
Chris@439
|
270 } else {
|
Chris@439
|
271 m_uriToIdMap[pluginUri] = pluginId;
|
Chris@439
|
272 }
|
Chris@439
|
273 }
|
Chris@439
|
274 }
|
Chris@439
|
275
|
Chris@439
|
276 if (!foundSomething) {
|
Chris@439
|
277 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
|
Chris@439
|
278 << urlString.toStdString()
|
Chris@439
|
279 << "> does not sufficiently describe any plugins" << endl;
|
Chris@439
|
280 }
|
Chris@439
|
281
|
Chris@439
|
282 return addedSomething;
|
Chris@439
|
283 }
|
Chris@439
|
284
|
Chris@439
|
285
|
Chris@439
|
286
|