/** * librdf_new_serializer_from_factory: * @world: redland world object * @factory: the serializer factory to use to create this serializer * * Constructor - create a new #librdf_serializer object. * * Return value: new #librdf_serializer object or NULL **/ librdf_serializer* librdf_new_serializer_from_factory(librdf_world *world, librdf_serializer_factory *factory) { librdf_serializer* d; librdf_world_open(world); LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(factory, librdf_serializer_factory, NULL); d=(librdf_serializer*)LIBRDF_CALLOC(librdf_serializer, 1, sizeof(librdf_serializer)); if(!d) return NULL; d->context=(char*)LIBRDF_CALLOC(serializer_context, 1, factory->context_length); if(!d->context) { librdf_free_serializer(d); return NULL; } d->world=world; d->factory=factory; if(factory->init) if(factory->init(d, d->context)) { librdf_free_serializer(d); return NULL; } return d; }
void RdfStorePrivate::Save(std::string filename, std::string format) { if (format == "") format = "turtle"; librdf_uri* baseUri = RdfUriToLibRdfUri(m_BaseUri); librdf_serializer* s = librdf_new_serializer(m_World, format.c_str(), nullptr, nullptr); if(!s) { mitkThrow() << "RDF Library Error"; } for (PrefixMap::const_iterator i = m_Prefixes.begin(); i != m_Prefixes.end(); i++) { librdf_serializer_set_namespace(s, RdfUriToLibRdfUri(i->second), i->first.c_str()); } FILE* f = fopen(filename.c_str(), "w+"); librdf_serializer_serialize_model_to_file_handle(s, f, baseUri, m_Model); librdf_free_serializer(s); librdf_free_uri(baseUri); fclose(f); }
/** * @brief Serialize the rdf storage and return as string. * * * @return The storage. */ unsigned char* RDF_GetRDF(void) { librdf_serializer* serializer; unsigned char* result; serializer = librdf_new_serializer(world, "rdfxml", NULL, NULL); result = librdf_serializer_serialize_model_to_string(serializer, NULL, model); librdf_free_serializer(serializer); return result; }
int lua_bindings_redland_serializer_gc(lua_State *L) { librdf_serializer **pp_serializer = (librdf_serializer **) luaL_checkudata( L , -1 , serializer_userdata_type ); lua_pop(L, 1); librdf_free_serializer(*pp_serializer); return 0; }
/* * call-seq: * graph.serialized_as( format, nshash={} ) -> string * * Return the graph serialized to a String in the specified +format+. Valid +format+s are keys * of the Hash returned by ::serializers. * * The +nshash+ argument can be used to set namespaces in the output (for serializers that * support them). It should be of the form: * * { :nsname => <namespace URI> } * * Examples: * turtle = graph.serialized_as( 'turtle' ) * xml = graph.serialized_as( 'rdfxml-abbrev', :foaf => 'http://xmlns.com/foaf/0.1/' ) * */ static VALUE rleaf_redleaf_graph_serialized_as( int argc, VALUE *argv, VALUE self ) { rleaf_GRAPH *ptr = rleaf_get_graph( self ); librdf_serializer *serializer; size_t length = 0; const char *formatname; unsigned char *serialized; VALUE format = Qnil; VALUE nshash = Qnil; rb_scan_args( argc, argv, "11", &format, &nshash ); rleaf_log_with_context( self, "debug", "Serializing as %s. Namespace hash is: %s", RSTRING_PTR(rb_inspect( format )), RSTRING_PTR(rb_inspect( nshash )) ); formatname = StringValuePtr( format ); rleaf_log_with_context( self, "debug", "trying to serialize as '%s'", formatname ); if ( !RTEST(rb_funcall(CLASS_OF(self), valid_format_p, 1, format)) ) rb_raise( rleaf_eRedleafFeatureError, "unsupported serialization format '%s'", formatname ); rleaf_log_with_context( self, "debug", "valid format '%s' specified.", formatname ); serializer = librdf_new_serializer( rleaf_rdf_world, formatname, NULL, NULL ); if ( !serializer ) rb_raise( rleaf_eRedleafError, "could not create a '%s' serializer", formatname ); /* Set namespaces in the serializer for entries in the argshash */ if ( RTEST(nshash) ) rb_iterate( rb_each, nshash, rleaf_set_serializer_ns, (VALUE)serializer ); /* :TODO: Support for the 'baseuri' argument? */ serialized = librdf_serializer_serialize_model_to_counted_string( serializer, NULL, ptr->model, &length ); librdf_free_serializer( serializer ); if ( !serialized ) rb_raise( rleaf_eRedleafError, "could not serialize model as '%s'", formatname ); rleaf_log_with_context( self, "debug", "got %d bytes of '%s'", length, formatname ); return rb_str_new( (char *)serialized, length ); }
/** * @brief Executes the given query and returns the result in the given format. * * * @param query The query to execute. * @param format The format of the result. * * @return The results. */ unsigned char* RDF_ExecQuery(unsigned char* query, unsigned char* format) { librdf_query* rdfquery; librdf_query_results* results; rdfquery = librdf_new_query(world, "sparql", NULL, query, NULL); results = librdf_model_query_execute(model, rdfquery); librdf_serializer* serializer; librdf_stream* stream; serializer = librdf_new_serializer(world, format, NULL, NULL); if(!serializer) { fprintf(stderr, "Failed to create triples serializer for format '%s'\n", format); return NULL; } stream = librdf_query_results_as_stream(results); unsigned char* result = librdf_serializer_serialize_stream_to_string(serializer, NULL, stream); if(!result) { fprintf(stderr, "Failed to turn query results to format '%s'\n", format); return NULL; } librdf_free_query(rdfquery); librdf_free_query_results(results); librdf_free_serializer(serializer); librdf_free_stream(stream); return result; }
static int librdf_storage_file_sync(librdf_storage *storage) { librdf_storage_file_instance* context=(librdf_storage_file_instance*)storage->instance; char *backup_name; char *new_name; librdf_serializer* serializer; FILE *fh; int rc=0; if(!context->changed) return 0; if(!context->name) { /* FIXME - URI cannot be written */ context->changed=0; return 0; } backup_name=NULL; if(!access((const char*)context->name, F_OK)) { /* name"~\0" */ backup_name=(char*)LIBRDF_MALLOC(cstring, context->name_len+2); if(!backup_name) return 1; strcpy(backup_name, (const char*)context->name); backup_name[context->name_len]='~'; backup_name[context->name_len+1]='\0'; if(rename(context->name, backup_name) < 0) { librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "rename of '%s' to '%s' failed - %s", context->name, backup_name, strerror(errno)); LIBRDF_FREE(cstring, backup_name); return 1; } } /* name".new\0" */ new_name=(char*)LIBRDF_MALLOC(cstring, context->name_len+5); if(!new_name) return 1; strcpy(new_name, (const char*)context->name); strcpy(new_name+context->name_len, ".new"); serializer = librdf_new_serializer(storage->world, context->format_name, NULL, NULL); if(!serializer) { LIBRDF_FREE(cstring, new_name); if(backup_name) LIBRDF_FREE(cstring, backup_name); return 1; } fh=fopen(new_name, "w+"); if(!fh) { librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "failed to open file '%s' for writing - %s", new_name, strerror(errno)); rc=1; } else { librdf_serializer_serialize_model_to_file_handle(serializer, fh, context->uri, context->model); fclose(fh); } librdf_free_serializer(serializer); if(fh && rename(new_name, context->name) < 0) { librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "rename of '%s' to '%s' failed - %s (%d)", new_name, context->name, strerror(errno), errno); fh=NULL; rc=1; } LIBRDF_FREE(cstring, new_name); /* restore backup on failure (fh=NULL) */ if(!fh && backup_name && rename(backup_name, context->name) < 0) { librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL, "rename of '%s' to '%s' failed - %s", backup_name, context->name, strerror(errno)); rc=1; } if(backup_name) LIBRDF_FREE(cstring, backup_name); context->changed=0; return rc; }
int main(int argc, char *argv[]) { const char *program=librdf_basename((const char*)argv[0]); const char *test_serializer_types[]={"rdfxml", "ntriples", NULL}; int i; const char *type; unsigned char *string; size_t string_length; librdf_world *world; librdf_storage *storage; librdf_model* model; librdf_uri* base_uri; librdf_statement* statement; librdf_serializer* serializer; librdf_parser* parser; librdf_stream* stream; FILE *fh; struct stat st_buf; world=librdf_new_world(); librdf_world_open(world); librdf_world_set_logger(world, &LogData, log_handler); for(i=0; (type=test_serializer_types[i]); i++) { fprintf(stderr, "%s: Trying to create new %s serializer\n", program, type); serializer=librdf_new_serializer(world, type, NULL, NULL); if(!serializer) { fprintf(stderr, "%s: Failed to create new serializer type %s\n", program, type); continue; } fprintf(stderr, "%s: Freeing serializer\n", program); librdf_free_serializer(serializer); } storage=librdf_new_storage(world, NULL, NULL, NULL); model=librdf_new_model(world, storage, NULL); /* ERROR: Subject URI is bad UTF-8 */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo\xfc"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/bar"), librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); /* ERROR: Predicate URI is not serializable */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://bad.example.org/"), librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); /* ERROR: Object literal is bad UTF-8 */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/abc"), librdf_new_node_from_literal(world, (const unsigned char*)"\xfc", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); serializer=librdf_new_serializer(world, "rdfxml", NULL, NULL); base_uri=librdf_new_uri(world, (const unsigned char*)"http://example.org/base#"); string=librdf_serializer_serialize_model_to_counted_string(serializer, base_uri, model, &string_length); #define EXPECTED_BAD_STRING_LENGTH 382 if(string_length != EXPECTED_BAD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising model to RDF/XML returned string '%s' size %d, expected %d\n", program, string, (int)string_length, EXPECTED_BAD_STRING_LENGTH); return 1; } if(string) free(string); librdf_free_uri(base_uri); base_uri=NULL; librdf_free_model(model); model=NULL; librdf_free_storage(storage); storage=NULL; if(LogData.errors != EXPECTED_ERRORS) { fprintf(stderr, "%s: Serialising to RDF/XML returned %d errors, expected %d\n", program, LogData.errors, EXPECTED_ERRORS); return 1; } if(LogData.warnings != EXPECTED_WARNINGS) { fprintf(stderr, "%s: Serialising to RDF/XML returned %d warnings, expected %d\n", program, LogData.warnings, EXPECTED_WARNINGS); return 1; } /* Good model to serialize */ storage=librdf_new_storage(world, NULL, NULL, NULL); model=librdf_new_model(world, storage, NULL); parser=librdf_new_parser(world, SYNTAX_TYPE, NULL, NULL); if(!parser) { fprintf(stderr, "%s: Failed to create new parser type %s\n", program, SYNTAX_TYPE); return 1; } fprintf(stderr, "%s: Adding %s string content\n", program, SYNTAX_TYPE); if(librdf_parser_parse_string_into_model(parser, (const unsigned char*)SYNTAX_CONTENT, NULL /* no base URI*/, model)) { fprintf(stderr, "%s: Failed to parse RDF from %s string into model\n", SYNTAX_TYPE, program); return 1; } librdf_free_parser(parser); fprintf(stderr, "%s: Serializing stream to a string\n", program); stream=librdf_model_as_stream(model); string_length=0; string=librdf_serializer_serialize_stream_to_counted_string(serializer, NULL, stream, &string_length); #define EXPECTED_GOOD_STRING_LENGTH 668 if(string_length != EXPECTED_GOOD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising stream to RDF/XML returned string '%s' size %d, expected %d\n", program, string, (int)string_length, EXPECTED_GOOD_STRING_LENGTH); return 1; } librdf_free_stream(stream); if(string) free(string); fprintf(stderr, "%s: Serializing stream to a file handle\n", program); stream=librdf_model_as_stream(model); #define FILENAME "test.rdf" fh=fopen(FILENAME, "w"); if(!fh) { fprintf(stderr, "%s: Failed to fopen for writing '%s' - %s\n", program, FILENAME, strerror(errno)); return 1; } librdf_serializer_serialize_stream_to_file_handle(serializer, fh, NULL, stream); fclose(fh); stat(FILENAME, &st_buf); if((int)st_buf.st_size != EXPECTED_GOOD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising stream to file handle returned file '%s' of size %d bytes, expected %d\n", program, FILENAME, (int)st_buf.st_size, EXPECTED_GOOD_STRING_LENGTH); return 1; } unlink(FILENAME); librdf_free_stream(stream); librdf_free_serializer(serializer); serializer=NULL; librdf_free_model(model); model=NULL; librdf_free_storage(storage); storage=NULL; librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
void run_query(librdf_world* world, librdf_model* model, const std::string& q) { librdf_stream* strm; std::cout << "** Query: " << q << std::endl; librdf_uri* uri1 = librdf_new_uri(world, (const unsigned char*) "http://bunchy.org"); if (uri1 == 0) throw std::runtime_error("Couldn't parse URI"); librdf_uri* uri2 = librdf_new_uri(world, (const unsigned char*) "http://bunchy.org"); if (uri2 == 0) throw std::runtime_error("Couldn't parse URI"); librdf_query* qry = librdf_new_query(world, "sparql", uri1, (const unsigned char*) q.c_str(), uri2); librdf_free_uri(uri1); librdf_free_uri(uri2); if (qry == 0) throw std::runtime_error("Couldn't parse query."); librdf_query_results* results = librdf_query_execute(qry, model); if (results == 0) throw std::runtime_error("Couldn't execute query"); librdf_free_query(qry); strm = librdf_query_results_as_stream(results); if (strm == 0) throw std::runtime_error("Couldn't stream results"); librdf_serializer* srl = librdf_new_serializer(world, "ntriples", 0, 0); if (srl == 0) throw std::runtime_error("Couldn't create serialiser"); size_t len; unsigned char* out = librdf_serializer_serialize_stream_to_counted_string(srl, 0, strm, &len); std::cout << "--------------------------------------------------------" << std::endl; write(1, out, len); std::cout << "--------------------------------------------------------" << std::endl; free(out); librdf_free_serializer(srl); librdf_free_stream(strm); librdf_free_query_results(results); }
int main(int argc, char** argv) { try { /*********************************************************************/ /* Initialise */ /*********************************************************************/ std::cout << "** Initialise" << std::endl; librdf_world* world = librdf_new_world(); if (world == 0) throw std::runtime_error("Didn't get world"); librdf_storage* storage = librdf_new_storage(world, STORE, STORE_NAME, "new='yes'"); if (storage == 0) throw std::runtime_error("Didn't get storage"); librdf_model* model = librdf_new_model(world, storage, 0); if (model == 0) throw std::runtime_error("Couldn't construct model"); /*********************************************************************/ /* Create in-memory model */ /*********************************************************************/ std::cout << "** Create in-memory model" << std::endl; librdf_storage* mstorage = librdf_new_storage(world, "memory", 0, 0); if (storage == 0) throw std::runtime_error("Didn't get storage"); librdf_model* mmodel = librdf_new_model(world, mstorage, 0); if (model == 0) throw std::runtime_error("Couldn't construct model"); for(int i = 0; i < 10; i++) { char sbuf[256]; char obuf[256]; sprintf(sbuf, "http://gaffer.test/number#%d", i); sprintf(obuf, "http://gaffer.test/number#%d", i+1); librdf_node *s = librdf_new_node_from_uri_string(world, (const unsigned char *) sbuf); librdf_node *p = librdf_new_node_from_uri_string(world, (const unsigned char *) "http://gaffer.test/number#is_before"); librdf_node *o = librdf_new_node_from_uri_string(world, (const unsigned char*) obuf); librdf_statement* st = librdf_new_statement_from_nodes(world, s, p, o); librdf_model_add_statement(mmodel, st); librdf_free_statement(st); } /*********************************************************************/ /* Size */ /*********************************************************************/ std::cout << "** Model size is " << librdf_model_size(model) << std::endl; /*********************************************************************/ /* Add statement */ /*********************************************************************/ std::cout << "** Add statement" << std::endl; const char* fred = "http://gaffer.test/#fred"; const char* is_a = "http://gaffer.test/#is_a"; const char* cat = "http://gaffer.test/#cat"; librdf_node *s = librdf_new_node_from_uri_string(world, (const unsigned char *) fred); librdf_node *p = librdf_new_node_from_uri_string(world, (const unsigned char *) is_a); librdf_node *o = librdf_new_node_from_uri_string(world, (const unsigned char *) cat); librdf_statement* st = librdf_new_statement_from_nodes(world, s, p, o); librdf_model_add_statement(model, st); /*********************************************************************/ /* Add memory model statements */ /*********************************************************************/ std::cout << "** Add statements" << std::endl; librdf_stream* strm = librdf_model_as_stream(mmodel); if (strm == 0) throw std::runtime_error("Couldn't get memory stream"); int ret = librdf_model_add_statements(model, strm); if (ret != 0) throw std::runtime_error("Couldn't add_statements"); librdf_free_stream(strm); librdf_free_model(mmodel); librdf_free_storage(mstorage); /*********************************************************************/ /* Run queries */ /*********************************************************************/ run_query(world, model, query_string8); run_query(world, model, query_string1); run_query(world, model, query_string2); run_query(world, model, query_string3); run_query(world, model, query_string4); run_query(world, model, query_string5); run_query(world, model, query_string6); run_query2(world, model, query_string7); /*********************************************************************/ /* Remove statement */ /*********************************************************************/ std::cout << "** Remove statements" << std::endl; librdf_model_remove_statement(model, st); /*********************************************************************/ /* Serialise */ /*********************************************************************/ std::cout << "** Serialise" << std::endl; strm = librdf_model_as_stream(model); if (strm == 0) throw std::runtime_error("Couldn't get model as stream"); librdf_serializer* srl = librdf_new_serializer(world, "ntriples", 0, 0); if (srl == 0) throw std::runtime_error("Couldn't create serialiser"); size_t len; unsigned char* out = librdf_serializer_serialize_stream_to_counted_string(srl, 0, strm, &len); std::cout << "--------------------------------------------------------" << std::endl; write(1, out, len); std::cout << "--------------------------------------------------------" << std::endl; free(out); librdf_free_stream(strm); librdf_free_serializer(srl); /*********************************************************************/ /* Cleanup */ /*********************************************************************/ librdf_free_statement(st); librdf_free_model(model); librdf_free_storage(storage); librdf_free_world(world); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } }
int print_query_results(librdf_world* world, librdf_model* model, librdf_query_results *results) { int i; char *name; librdf_stream* stream; librdf_serializer* serializer; const char *query_graph_serializer_syntax_name="rdfxml"; if(librdf_query_results_is_bindings(results)) { fprintf(stdout, ": Query returned bindings results:\n"); while(!librdf_query_results_finished(results)) { fputs("result: [", stdout); for(i=0; i<librdf_query_results_get_bindings_count(results); i++) { librdf_node *value=librdf_query_results_get_binding_value(results, i); name=(char*)librdf_query_results_get_binding_name(results, i); if(i>0) fputs(", ", stdout); fprintf(stdout, "%s=", name); if(value) { librdf_node_print(value, stdout); librdf_free_node(value); } else fputs("NULL", stdout); } fputs("]\n", stdout); librdf_query_results_next(results); } fprintf(stdout, ": Query returned %d results\n", librdf_query_results_get_count(results)); } else if(librdf_query_results_is_boolean(results)) { fprintf(stdout, ": Query returned boolean result: %s\n", librdf_query_results_get_boolean(results) ? "true" : "false"); } else if(librdf_query_results_is_graph(results)) { librdf_storage* tmp_storage; librdf_model* tmp_model; tmp_storage=librdf_new_storage(world, NULL, NULL, NULL); tmp_model=librdf_new_model(world, tmp_storage, NULL); fprintf(stdout, ": Query returned graph result:\n"); stream=librdf_query_results_as_stream(results); if(!stream) { fprintf(stderr, ": Failed to get query results graph\n"); return -1; } librdf_model_add_statements(tmp_model, stream); librdf_free_stream(stream); fprintf(stdout, ": Total %d triples\n", librdf_model_size(model)); serializer=librdf_new_serializer(world, query_graph_serializer_syntax_name, NULL, NULL); if(!serializer) { fprintf(stderr, ": Failed to create serializer type %s\n", query_graph_serializer_syntax_name); return -1; } librdf_serializer_serialize_model_to_file_handle(serializer, stdout, NULL, tmp_model); librdf_free_serializer(serializer); librdf_free_model(tmp_model); librdf_free_storage(tmp_storage); } else { fprintf(stdout, ": Query returned unknown result format\n"); return -1; } return 0; }