From d5a4b60e8f72b76dd4d98b43f77bd9da26c0dc4a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 29 Jan 2011 20:53:08 +0000 Subject: Non-SPARQL version of slv2_plugin_get_value_by_qname. git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@2857 a436a847-0d15-0410-975c-d299462d15a1 --- src/plugin.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- src/query.c | 1 + src/slv2_internal.h | 1 + src/world.c | 11 +++++++++++ 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/plugin.c b/src/plugin.c index 4282be3..ffd2319 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -119,7 +119,6 @@ slv2_plugin_query_node(SLV2Plugin p, librdf_node* subject, librdf_node* predicat p, subject, predicate, NULL); if (librdf_stream_end(results)) { - librdf_free_stream(results); return NULL; } @@ -482,20 +481,50 @@ slv2_plugin_get_value(SLV2Plugin p, } -SLV2Values -slv2_plugin_get_value_by_qname(SLV2Plugin p, - const char* predicate) +static char* +slv2_qname_expand(SLV2Plugin p, const char* qname) { - char* query = slv2_strjoin( - "SELECT DISTINCT ?value WHERE { \n" - "<> ", predicate, " ?value . \n" - "}\n", NULL); + char* colon = strchr(qname, ':'); + if (!colon || colon == qname) { + SLV2_ERRORF("Invalid QName `%s'\n", qname); + return NULL; + } - SLV2Values result = slv2_plugin_query_variable(p, query, 0); + const size_t prefix_len = colon - qname; + char* prefix = malloc(prefix_len + 1); + memcpy(prefix, qname, prefix_len); + prefix[prefix_len] = '\0'; - free(query); + char* namespace = librdf_hash_get(p->world->namespaces, prefix); + free(prefix); + if (!namespace) { + SLV2_ERRORF("QName `%s' has Undefined prefix\n", qname); + return NULL; + } - return result; + const size_t qname_len = strlen(qname); + const size_t suffix_len = qname_len - prefix_len - 1; + const size_t namespace_len = strlen(namespace); + char* uri = malloc(namespace_len + suffix_len + 1); + memcpy(uri, namespace, namespace_len); + memcpy(uri + namespace_len, colon + 1, qname_len - prefix_len - 1); + uri[namespace_len + suffix_len] = '\0'; + + free(namespace); + return uri; +} + + +SLV2Values +slv2_plugin_get_value_by_qname(SLV2Plugin p, + const char* predicate) +{ + char* predicate_uri = slv2_qname_expand(p, predicate); + SLV2Value predicate_value = slv2_value_new_uri(p->world, predicate_uri); + SLV2Values ret = slv2_plugin_get_value(p, predicate_value); + slv2_value_free(predicate_value); + free(predicate_uri); + return ret; } diff --git a/src/query.c b/src/query.c index d190bf2..aebc87b 100644 --- a/src/query.c +++ b/src/query.c @@ -211,6 +211,7 @@ slv2_plugin_find_statements(SLV2Plugin plugin, librdf_node* predicate, librdf_node* object) { + slv2_plugin_load_if_necessary(plugin); librdf_statement* q = librdf_new_statement_from_nodes( plugin->world->world, subject, predicate, object); librdf_stream* results = librdf_model_find_statements(plugin->rdf, q); diff --git a/src/slv2_internal.h b/src/slv2_internal.h index 93ef6af..9ba8606 100644 --- a/src/slv2_internal.h +++ b/src/slv2_internal.h @@ -150,6 +150,7 @@ struct _SLV2World { librdf_storage* storage; librdf_model* model; librdf_parser* parser; + librdf_hash* namespaces; SLV2PluginClass lv2_plugin_class; SLV2PluginClasses plugin_classes; SLV2Plugins plugins; diff --git a/src/world.c b/src/world.c index a4e21ae..893d39c 100644 --- a/src/world.c +++ b/src/world.c @@ -79,6 +79,15 @@ slv2_world_new_internal(SLV2World world) world->lv2_plugin_class = slv2_plugin_class_new(world, NULL, librdf_node_get_uri(world->lv2_plugin_node), "Plugin"); + world->namespaces = librdf_new_hash_from_string( + world->world, NULL, + "rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'," + "rdfs='http://www.w3.org/2000/01/rdf-schema#'," + "doap='http://usefulinc.com/ns/doap#'," + "foaf='http://xmlns.com/foaf/0.1/'," + "lv2='http://lv2plug.in/ns/lv2core#'," + "lv2ev='http://lv2plug.in/ns/ext/event#'"); + return world; fail: @@ -182,6 +191,8 @@ slv2_world_free(SLV2World world) librdf_free_storage(world->storage); world->storage = NULL; + librdf_free_hash(world->namespaces); + if (world->local_world) librdf_free_world(world->world); -- cgit v1.2.1