static int sd_add_dataset_description(librdf_model *sd_model, librdf_node *service_node) { librdf_node *dataset_node = NULL, *default_graph_node = NULL; int triple_count = librdf_storage_size(storage); dataset_node = librdf_new_node(world); if (!dataset_node) { redstore_error("Failed to create default dataset bnode - librdf_new_node returned NULL"); return 1; } librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "defaultDatasetDescription"), librdf_new_node_from_node(dataset_node) ); librdf_model_add(sd_model, librdf_new_node_from_node(dataset_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "Dataset") ); default_graph_node = librdf_new_node(world); if (!default_graph_node) { redstore_error("Failed to create default graph bnode - librdf_new_node returned NULL"); return 1; } librdf_model_add(sd_model, librdf_new_node_from_node(dataset_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "defaultGraph"), librdf_new_node_from_node(default_graph_node) ); librdf_model_add(sd_model, librdf_new_node_from_node(default_graph_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "Graph") ); if (triple_count>=0) { librdf_model_add(sd_model, librdf_new_node_from_node(default_graph_node), librdf_new_node_from_uri_local_name(world, void_ns_uri, (const unsigned char *) "triples"), new_node_from_integer(world, triple_count) ); } if (dataset_node) librdf_free_node(dataset_node); if (default_graph_node) librdf_free_node(default_graph_node); return 0; }
static int sd_add_query_languages(librdf_model *sd_model, librdf_node *service_node) { int i,n; for(i=0; 1; i++) { const raptor_syntax_description* desc = librdf_query_language_get_description(world, i); librdf_node *lang_node = NULL; if (!desc) break; for (n = 0; desc->names[n]; n++) { if (strcmp(desc->names[n], "sparql10")==0) { lang_node = librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "SPARQL10Query"); break; } else if (strcmp(desc->names[n], "sparql11-query")==0) { lang_node = librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "SPARQL11Query"); break; } } if (lang_node) { librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) "supportedLanguage"), librdf_new_node_from_node(lang_node) ); librdf_model_add(sd_model, librdf_new_node_from_node(lang_node), librdf_new_node_from_node(LIBRDF_S_comment(world)), librdf_new_node_from_literal(world, (const unsigned char *) desc->label, NULL, 0) ); if (desc->uri_strings) { for (n = 0; desc->uri_strings[n]; n++) { librdf_model_add(sd_model, librdf_new_node_from_node(lang_node), librdf_new_node_from_node(LIBRDF_S_seeAlso(world)), librdf_new_node_from_uri_string(world, (const unsigned char *) desc->uri_strings[n]) ); } } librdf_free_node(lang_node); } } return 0; }
/** * librdf_init_concepts: * @world: redland world object * * INTERNAL - Initialise the concepts module. * **/ void librdf_init_concepts(librdf_world *world) { int i; /* Create the Unique URI objects */ world->concept_ms_namespace_uri=librdf_new_uri(world, librdf_concept_ms_namespace); world->concept_schema_namespace_uri=librdf_new_uri(world, librdf_concept_schema_namespace); if(!world->concept_ms_namespace_uri || !world->concept_schema_namespace_uri) LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Failed to create M&S or Schema URIs"); /* Create arrays for the M&S and Schema resource nodes and uris */ world->concept_uris= (librdf_uri**)LIBRDF_CALLOC(ptrarray, LIBRDF_CONCEPT_LAST+1, sizeof(librdf_uri*)); world->concept_resources= (librdf_node**)LIBRDF_CALLOC(ptrarray, LIBRDF_CONCEPT_LAST+1, sizeof(librdf_node*)); if(!world->concept_uris || !world->concept_resources) LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Out of memory creating node/uri arrays"); /* Create the M&S and Schema resource nodes */ for (i=0; i<= LIBRDF_CONCEPT_LAST; i++) { librdf_uri* ns_uri=(i < LIBRDF_CONCEPT_FIRST_S_ID) ? world->concept_ms_namespace_uri : world->concept_schema_namespace_uri; const unsigned char * token=(const unsigned char *)librdf_concept_tokens[i]; world->concept_resources[i]=librdf_new_node_from_uri_local_name(world, ns_uri, token); if(!world->concept_resources[i]) LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Failed to create Node from URI\n"); /* keep shared copy of URI from node */ world->concept_uris[i]=librdf_node_get_uri(world->concept_resources[i]); } }
/** * librdf_init_concepts - Initialise the concepts module. * @world: redland world object * **/ void librdf_init_concepts(librdf_world *world) { int i; /* Create the Unique URI objects */ librdf_concept_ms_namespace_uri=librdf_new_uri(world, librdf_concept_ms_namespace); librdf_concept_schema_namespace_uri=librdf_new_uri(world, librdf_concept_schema_namespace); /* Create the M&S and Schema resource nodes */ for (i=0; i< LIBRDF_CONCEPT_LAST; i++) { librdf_uri* ns_uri=(i < LIBRDF_CONCEPT_FIRST_S_ID) ? librdf_concept_ms_namespace_uri : librdf_concept_schema_namespace_uri; const unsigned char * token=(const unsigned char *)librdf_concept_tokens[i]; librdf_concept_resources[i]=librdf_new_node_from_uri_local_name(world, ns_uri, token); if(!librdf_concept_resources[i]) LIBRDF_FATAL1(world, "Failed to create Node from URI\n"); /* keep shared copy of URI from node */ librdf_concept_uris[i]=librdf_node_get_uri(librdf_concept_resources[i]); } }
int main(int argc, char *argv[]) { librdf_world *world; librdf_uri* prefix_uri; librdf_node* nodes[ITERATOR_NODES_COUNT]; int i; librdf_iterator* iterator; int count; char *program=argv[0]; world=librdf_new_world(); librdf_world_init_mutex(world); librdf_init_hash(world); librdf_init_uri(world); librdf_init_node(world); prefix_uri=librdf_new_uri(world, (const unsigned char*)NODE_URI_PREFIX); if(!prefix_uri) { fprintf(stderr, "%s: Failed to create prefix URI\n", program); return(1); } for(i=0; i < ITERATOR_NODES_COUNT; i++) { unsigned char buf[2]; buf[0]='a'+i; buf[1]='\0'; nodes[i]=librdf_new_node_from_uri_local_name(world, prefix_uri, buf); if(!nodes[i]) { fprintf(stderr, "%s: Failed to create node %i (%s)\n", program, i, buf); return(1); } } fprintf(stdout, "%s: Creating static node iterator\n", program); iterator=librdf_node_static_iterator_create(nodes, ITERATOR_NODES_COUNT); if(!iterator) { fprintf(stderr, "%s: Failed to createstatic node iterator\n", program); return(1); } fprintf(stdout, "%s: Listing static node iterator\n", program); count=0; while(!librdf_iterator_end(iterator)) { librdf_node* i_node=(librdf_node*)librdf_iterator_get_object(iterator); if(!i_node) { fprintf(stderr, "%s: librdf_iterator_current return NULL when not end o fiterator\n", program); return(1); } fprintf(stdout, "%s: node %d is: ", program, count); librdf_node_print(i_node, stdout); fputc('\n', stdout); if(!librdf_node_equals(i_node, nodes[count])) { fprintf(stderr, "%s: static node iterator node %i returned unexpected node\n", program, count); librdf_node_print(i_node, stderr); fputs(" rather than ", stdout); librdf_node_print(nodes[count], stderr); fputc('\n', stdout); return(1); } librdf_iterator_next(iterator); count++; } librdf_free_iterator(iterator); if(count != ITERATOR_NODES_COUNT) { fprintf(stderr, "%s: Iterator returned %d nodes, expected %d\n", program, count, ITERATOR_NODES_COUNT); return(1); } fprintf(stdout, "%s: Static node iterator worked ok\n", program); fprintf(stdout, "%s: Freeing nodes\n", program); for (i=0; i<ITERATOR_NODES_COUNT; i++) { librdf_free_node(nodes[i]); } librdf_free_uri(prefix_uri); librdf_finish_node(world); librdf_finish_uri(world); librdf_finish_hash(world); LIBRDF_FREE(librdf_world, world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { librdf_statement *statement; librdf_stream* stream; const char *program=librdf_basename((const char*)argv[0]); librdf_world *world; librdf_uri* prefix_uri; librdf_node* nodes[STREAM_NODES_COUNT]; int i; librdf_iterator* iterator; int count; world=librdf_new_world(); librdf_world_open(world); prefix_uri=librdf_new_uri(world, (const unsigned char*)NODE_URI_PREFIX); if(!prefix_uri) { fprintf(stderr, "%s: Failed to create prefix URI\n", program); return(1); } for(i=0; i < STREAM_NODES_COUNT; i++) { unsigned char buf[2]; buf[0]='a'+i; buf[1]='\0'; nodes[i]=librdf_new_node_from_uri_local_name(world, prefix_uri, buf); if(!nodes[i]) { fprintf(stderr, "%s: Failed to create node %i (%s)\n", program, i, buf); return(1); } } fprintf(stdout, "%s: Creating static node iterator\n", program); iterator = librdf_node_new_static_node_iterator(world, nodes, STREAM_NODES_COUNT); if(!iterator) { fprintf(stderr, "%s: Failed to create static node iterator\n", program); return(1); } statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/resource"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/property"), NULL); if(!statement) { fprintf(stderr, "%s: Failed to create statement\n", program); return(1); } fprintf(stdout, "%s: Creating stream from node iterator\n", program); stream=librdf_new_stream_from_node_iterator(iterator, statement, LIBRDF_STATEMENT_OBJECT); if(!stream) { fprintf(stderr, "%s: Failed to createstatic node stream\n", program); return(1); } /* This is to check that the stream_from_node_iterator code * *really* takes a copy of what it needs from statement */ fprintf(stdout, "%s: Freeing statement\n", program); librdf_free_statement(statement); fprintf(stdout, "%s: Listing static node stream\n", program); count=0; while(!librdf_stream_end(stream)) { librdf_statement* s_statement=librdf_stream_get_object(stream); if(!s_statement) { fprintf(stderr, "%s: librdf_stream_current returned NULL when not end of stream\n", program); return(1); } fprintf(stdout, "%s: statement %d is: ", program, count); librdf_statement_print(s_statement, stdout); fputc('\n', stdout); librdf_stream_next(stream); count++; } if(count != STREAM_NODES_COUNT) { fprintf(stderr, "%s: Stream returned %d statements, expected %d\n", program, count, STREAM_NODES_COUNT); return(1); } fprintf(stdout, "%s: stream from node iterator worked ok\n", program); fprintf(stdout, "%s: Freeing stream\n", program); librdf_free_stream(stream); fprintf(stdout, "%s: Freeing nodes\n", program); for (i=0; i<STREAM_NODES_COUNT; i++) { librdf_free_node(nodes[i]); } librdf_free_uri(prefix_uri); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
static librdf_model * create_service_description(librdf_storage *sd_storage, const char * request_url) { librdf_model *sd_model = NULL; librdf_node *service_node = NULL; char *comment = NULL; sd_model = librdf_new_model(world, sd_storage, NULL); if (!sd_model) { redstore_error("Failed to create model for service description."); return NULL; } service_node = librdf_new_node(world); if (!service_node) { redstore_error("Failed to create service description bnode - librdf_new_node returned NULL"); librdf_free_model(sd_model); return NULL; } librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "Service") ); sd_add_format_descriptions(sd_model, service_node, librdf_parser_get_description, "inputFormat"); sd_add_format_descriptions(sd_model, service_node, librdf_serializer_get_description, "resultFormat"); sd_add_format_descriptions(sd_model, service_node, librdf_query_results_formats_get_description, "resultFormat"); sd_add_query_languages(sd_model, service_node); sd_add_dataset_description(sd_model, service_node); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_node(LIBRDF_S_label(world)), librdf_new_node_from_literal(world, (unsigned char *) storage_name, NULL, 0) ); #define COMMENT_MAX_LEN (128) comment = malloc(COMMENT_MAX_LEN); snprintf(comment, COMMENT_MAX_LEN, "RedStore %s endpoint using the '%s' storage module.", PACKAGE_VERSION, storage_type); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_node(LIBRDF_S_comment(world)), librdf_new_node_from_literal(world, (unsigned char *) comment, NULL, 0) ); free(comment); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "endpoint"), sd_get_endpoint_node(request_url) ); // Redland's default graph is the union of all other graphs librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "feature"), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (unsigned char *) "UnionDefaultGraph") ); librdf_free_node(service_node); return sd_model; }
static int sd_add_format_descriptions(librdf_model *sd_model, librdf_node *service_node, description_proc_t desc_proc, const char *type) { librdf_node *format_node = NULL; unsigned int i,n; for(i=0; 1; i++) { const raptor_syntax_description* desc = NULL; int uri_index = 0; desc = desc_proc(world, i); if (!desc) break; // Hack to remove the 'guess' format from the service description if (strcmp(desc->names[0], "guess") == 0) continue; // If the format has a format URI, use that, otherwise create a bnode if (desc->uri_strings && desc->uri_strings[0] && strncmp("http://www.w3.org/ns/formats/", desc->uri_strings[0], 29) == 0) { format_node = librdf_new_node_from_uri_string(world, (const unsigned char *) desc->uri_strings[0]); uri_index++; } else { format_node = librdf_new_node(world); } if (!format_node) { redstore_error("Failed to create new node for format."); return -1; } librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_MS_type(world)), librdf_new_node_from_uri_local_name(world, format_ns_uri, (const unsigned char *) "Format") ); librdf_model_add(sd_model, librdf_new_node_from_node(service_node), librdf_new_node_from_uri_local_name(world, sd_ns_uri, (const unsigned char *) type), librdf_new_node_from_node(format_node) ); for (n = 0; desc->names[n]; n++) { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_label(world)), librdf_new_node_from_literal(world, (const unsigned char *) desc->names[n], NULL, 0) ); } if (desc->label) { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_comment(world)), librdf_new_node_from_literal(world, (const unsigned char *) desc->label, NULL, 0) ); } for (n = 0; n < desc->mime_types_count; n++) { const raptor_type_q mime_type = desc->mime_types[n]; librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_uri_local_name(world, format_ns_uri, (const unsigned char *) "media_type"), librdf_new_node_from_literal(world, (unsigned char *) mime_type.mime_type, NULL, 0) ); } if (desc->uri_strings) { int firstUri = uri_index; for (; desc->uri_strings[uri_index]; uri_index++) { const unsigned char *uri_string = (const unsigned char *) desc->uri_strings[uri_index]; if (firstUri == uri_index) { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_isDefinedBy(world)), librdf_new_node_from_uri_string(world, uri_string) ); } else { librdf_model_add(sd_model, librdf_new_node_from_node(format_node), librdf_new_node_from_node(LIBRDF_S_seeAlso(world)), librdf_new_node_from_uri_string(world, uri_string) ); } } } librdf_free_node(format_node); } return 0; }