int add_triple_typed(librdf_world *world, librdf_node *context, librdf_model* model, const char *s, const char *p, const char *o) { librdf_node *subject, *predicate, *object; librdf_statement* statement=NULL; int rc; if(librdf_heuristic_is_blank_node(s)) subject=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(s)); else subject=librdf_new_node_from_uri_string(world, (const unsigned char *)s); predicate=librdf_new_node_from_uri_string(world, (const unsigned char *)p); if(librdf_heuristic_is_blank_node(o)) object=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(o)); else object=librdf_new_node_from_literal(world, (const unsigned char *)o, NULL, 0); statement=librdf_new_statement(world); librdf_statement_set_subject(statement, subject); librdf_statement_set_predicate(statement, predicate); librdf_statement_set_object(statement, object); rc=librdf_model_context_add_statement(model, context, statement); librdf_free_statement(statement); return rc; }
librdf_node* RdfStorePrivate::RdfNodeToLibRdfNode(RdfNode node) const { librdf_node* newNode = nullptr; switch (node.GetType()) { case RdfNode::NOTHING: break; case RdfNode::BLANK: newNode = librdf_new_node_from_blank_identifier(m_World, (const unsigned char*) node.GetValue().c_str()); break; case RdfNode::LITERAL: { if (node.GetDatatype() != RdfUri()) { librdf_uri* typeUri = RdfUriToLibRdfUri(node.GetDatatype()); newNode = librdf_new_node_from_typed_literal(m_World, (const unsigned char*) node.GetValue().c_str(), nullptr, typeUri); } else { newNode = librdf_new_node_from_literal(m_World, (const unsigned char*) node.GetValue().c_str(), nullptr, 0); } } break; case RdfNode::URI: newNode = librdf_new_node_from_uri( m_World, librdf_new_uri(m_World, (const unsigned char*) node.GetValue().c_str()) ); break; default: break; } return newNode; }
librdf_node * node_create_from_string (const char *s) { librdf_node *node; node = librdf_new_node_from_literal (rdf_world::get_world (), (const unsigned char *) s, 0, 0); return node; }
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; }
int main(int argc, char *argv[]) { librdf_world* world; librdf_storage *storage; librdf_model* model; librdf_statement* statement; raptor_world *raptor_world_ptr; raptor_iostream* iostr; world=librdf_new_world(); librdf_world_open(world); raptor_world_ptr = librdf_world_get_raptor(world); model=librdf_new_model(world, storage=librdf_new_storage(world, "hashes", "test", "hash-type='bdb',dir='.'"), NULL); librdf_model_add_statement(model, statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://www.dajobe.org/"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator"), librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0) ) ); librdf_free_statement(statement); iostr = raptor_new_iostream_to_file_handle(raptor_world_ptr, stdout); librdf_model_write(model, iostr); raptor_free_iostream(iostr); librdf_free_model(model); librdf_free_storage(storage); librdf_free_world(world); #ifdef LIBRDF_MEMORY_DEBUG librdf_memory_report(stderr); #endif /* keep gcc -Wall happy */ return(0); }
bool RdfNode::dummy() { librdf_world* world; librdf_storage *storage; librdf_model* model; librdf_statement* statement; raptor_world *raptor_world_ptr; raptor_iostream* iostr; world=librdf_new_world(); librdf_world_open(world); raptor_world_ptr = librdf_world_get_raptor(world); model=librdf_new_model(world, storage=librdf_new_storage(world, "hashes", nullptr, "hash-type='memory'"), nullptr); librdf_model_add_statement(model, statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://www.dajobe.org/"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator"), librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", nullptr, 0) ) ); librdf_free_statement(statement); iostr = raptor_new_iostream_to_file_handle(raptor_world_ptr, stdout); librdf_model_write(model, iostr); raptor_free_iostream(iostr); librdf_free_model(model); librdf_free_storage(storage); librdf_free_world(world); #ifdef LIBRDF_MEMORY_DEBUG librdf_memory_report(stderr); #endif return true; }
int main(int argc, char *argv[]) { librdf_statement *statement, *statement2; int size, size2; const char *program=librdf_basename((const char*)argv[0]); char *s, *buffer; librdf_world *world; raptor_iostream *iostr; world=librdf_new_world(); librdf_world_open(world); iostr = raptor_new_iostream_to_file_handle(world->raptor_world_ptr, stdout); fprintf(stdout, "%s: Creating statement\n", program); statement=librdf_new_statement(world); fprintf(stdout, "%s: Empty statement: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/net/dajobe/")); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/#Creator")); librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0)); fprintf(stdout, "%s: Resulting statement: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); size = librdf_statement_encode2(world, statement, NULL, 0); fprintf(stdout, "%s: Encoding statement requires %d bytes\n", program, size); buffer=(char*)LIBRDF_MALLOC(cstring, size); fprintf(stdout, "%s: Encoding statement in buffer\n", program); size2 = librdf_statement_encode2(world, statement, (unsigned char*)buffer, size); if(size2 != size) { fprintf(stdout, "%s: Encoding statement used %d bytes, expected it to use %d\n", program, size2, size); return(1); } fprintf(stdout, "%s: Creating new statement\n", program); statement2=librdf_new_statement(world); fprintf(stdout, "%s: Decoding statement from buffer\n", program); if(!librdf_statement_decode2(world, statement2, NULL, (unsigned char*)buffer, size)) { fprintf(stdout, "%s: Decoding statement failed\n", program); return(1); } LIBRDF_FREE(cstring, buffer); fprintf(stdout, "%s: New statement is: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); fprintf(stdout, "%s: Freeing statements\n", program); librdf_free_statement(statement2); librdf_free_statement(statement); 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; }
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); }
/* * Convert the given Ruby +object+ (VALUE) to a librdf_node. */ librdf_node * rleaf_value_to_librdf_node( VALUE object ) { VALUE str, typeuristr, converted_pair; librdf_uri *typeuri; ID id; /* :TODO: how to set language? is_xml flag? */ // rleaf_log( "debug", "Converting %s to a librdf_node.", RSTRING_PTR(rb_inspect( object )) ); switch( TYPE(object) ) { /* nil -> bnode */ case T_NIL: return NULL; case T_SYMBOL: id = SYM2ID( object ); if ( id == rleaf_anon_bnodeid ) { return librdf_new_node_from_blank_identifier( rleaf_rdf_world, NULL ); } else { return librdf_new_node_from_blank_identifier( rleaf_rdf_world, (unsigned char *)rb_id2name(id) ); } /* String -> plain literal */ case T_STRING: return librdf_new_node_from_literal( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(object), NULL, 0 ); break; /* Float -> xsd:float */ case T_FLOAT: str = rb_obj_as_string( object ); typeuri = XSD_FLOAT_TYPE; break; /* Bignum -> xsd:decimal */ case T_BIGNUM: str = rb_obj_as_string( object ); typeuri = XSD_DECIMAL_TYPE; break; /* Fixnum -> xsd:integer */ case T_FIXNUM: str = rb_obj_as_string( object ); typeuri = XSD_INTEGER_TYPE; break; /* TrueClass/FalseClass -> xsd:boolean */ case T_TRUE: case T_FALSE: str = rb_obj_as_string( object ); typeuri = XSD_BOOLEAN_TYPE; break; /* URI -> librdf_uri */ case T_OBJECT: if ( IsURI(object) || IsNamespace(object) ) { // rleaf_log( "debug", "Converting %s object to librdf_uri node", // rb_obj_classname(object) ); str = rb_obj_as_string( object ); return librdf_new_node_from_uri_string( rleaf_rdf_world, (unsigned char*)RSTRING_PTR(str) ); } /* fallthrough */ /* Delegate anything else to Redleaf::NodeUtils.make_object_typed_literal */ default: converted_pair = rb_funcall( rleaf_mRedleafNodeUtils, rb_intern("make_object_typed_literal"), 1, object ); str = rb_obj_as_string( rb_ary_entry(converted_pair, 0) ); typeuristr = rb_obj_as_string( rb_ary_entry(converted_pair, 1) ); typeuri = librdf_new_uri( rleaf_rdf_world, (unsigned char*)RSTRING_PTR(typeuristr) ); } return librdf_new_node_from_typed_counted_literal( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str), NULL, 0, typeuri ); }