RdfNode RdfStorePrivate::LibRdfNodeToRdfNode(librdf_node* node) const { RdfNode mitkNode; if (!node) return mitkNode; if (librdf_node_is_resource(node)) { mitkNode.SetType(RdfNode::URI); librdf_uri *uri = librdf_node_get_uri(node); mitkNode.SetValue(LibRdfUriToRdfUri(uri).ToString()); } else if (librdf_node_is_literal(node)) { mitkNode.SetType(RdfNode::LITERAL); std::string value = (const char*) librdf_node_get_literal_value(node); if (!value.empty()) mitkNode.SetValue(value); librdf_uri* typeUri = librdf_node_get_literal_value_datatype_uri(node); if (typeUri) mitkNode.SetDatatype(LibRdfUriToRdfUri(typeUri)); } else if (librdf_node_is_blank(node)) { mitkNode.SetType(RdfNode::BLANK); std::string str = (const char*) librdf_node_get_blank_identifier(node); if (!str.empty()) mitkNode.SetValue(str); } return mitkNode; }
/** * librdf_storage_tstore_context_add_statement: * @storage: #librdf_storage object * @context_node: #librdf_node object * @statement: #librdf_statement statement to add * * Add a statement to a storage context. * * Return value: non 0 on failure **/ static int librdf_storage_tstore_context_add_statement(librdf_storage* storage, librdf_node* context_node, librdf_statement* statement) { librdf_storage_tstore_instance* context=(librdf_storage_tstore_instance*)storage->instance; librdf_node *subject_node=statement->subject; librdf_node *predicate_node=statement->predicate; librdf_node *object_node=statement->object; char *subject; char *predicate; char *object; rs_obj_type type; if(librdf_node_is_blank(subject_node)) { subject=(char*)librdf_node_get_blank_identifier(subject_node); } else subject=(char*)librdf_uri_as_string(librdf_node_get_uri(subject_node)); predicate=(char*)librdf_uri_as_string(librdf_node_get_uri(predicate_node)); /* Assumptions - FIXME */ if(librdf_node_is_literal(object_node)) { object=(char*)librdf_node_get_literal_value(object_node); type = ObjLiteral; } else if(librdf_node_is_blank(object_node)) { object=(char*)librdf_node_get_blank_identifier(object_node); type = ObjURI; } else { object=(char*)librdf_uri_as_string(librdf_node_get_uri(object_node)); type = ObjURI; } if(rs_assert_triple(context->rdfsql, subject, predicate, object, type)) return 1; return 0; }
rasqal_literal* redland_node_to_rasqal_literal(librdf_world* world, librdf_node *node) { rasqal_literal* l; if(librdf_node_is_resource(node)) { raptor_uri* uri=(raptor_uri*)librdf_new_uri_from_uri(librdf_node_get_uri(node)); l=rasqal_new_uri_literal(world->rasqal_world_ptr, uri); /* transfer uri ownership to literal */ } else if(librdf_node_is_literal(node)) { unsigned char *string; librdf_uri *uri; unsigned char *new_string; char *new_language=NULL; raptor_uri *new_datatype=NULL; size_t len; string=librdf_node_get_literal_value_as_counted_string(node, &len); new_string=(unsigned char*)rasqal_alloc_memory(len+1); if(!new_string) return NULL; strcpy((char*)new_string, (const char*)string); string=(unsigned char*)librdf_node_get_literal_value_language(node); if(string) { new_language=(char*)rasqal_alloc_memory(strlen((const char*)string)+1); if(!new_language) { rasqal_free_memory((void*)new_string); return NULL; } strcpy((char*)new_language, (const char*)string); } uri=librdf_node_get_literal_value_datatype_uri(node); if(uri) new_datatype=(raptor_uri*)librdf_new_uri_from_uri(uri); /* transfer new_string,new_language,new_datatype ownership to literal */ l = rasqal_new_string_literal(world->rasqal_world_ptr, (const unsigned char*)new_string, new_language, new_datatype, NULL); } else { unsigned char *blank=librdf_node_get_blank_identifier(node); unsigned char *new_blank; if(!blank) return NULL; new_blank=(unsigned char*)rasqal_alloc_memory(strlen((const char*)blank)+1); if(!new_blank) return NULL; strcpy((char*)new_blank, (const char*)blank); /* transfer new_blank ownership to literal */ l = rasqal_new_simple_literal(world->rasqal_world_ptr, RASQAL_LITERAL_BLANK, (const unsigned char*)new_blank); } return l; }
/* FIXME returns an alloced triple pointing to shared strings */ static rs_triple* librdf_storage_tstore_statement_as_rs_triple(librdf_statement *statement) { librdf_node *subject_node=statement->subject; librdf_node *predicate_node=statement->predicate; librdf_node *object_node=statement->object; rs_triple* triple=LIBRDF_MALLOC(rs_triple, sizeof(rs_triple)); if(subject_node) { if(librdf_node_is_blank(subject_node)) triple->subject=(char*)librdf_node_get_blank_identifier(subject_node); else triple->subject=(char*)librdf_uri_as_string(librdf_node_get_uri(subject_node)); } else triple->subject=NULL; if(predicate_node) triple->predicate=(char*)librdf_uri_as_string(librdf_node_get_uri(predicate_node)); else triple->predicate=NULL; /* Assumptions - FIXME */ triple->literal = 0; if(object_node) { if(librdf_node_is_literal(object_node)) { triple->object=(char*)librdf_node_get_literal_value(object_node); triple->literal = 1; } else if(librdf_node_is_blank(object_node)) { triple->object=(char*)librdf_node_get_blank_identifier(object_node); } else { triple->object=(char*)librdf_uri_as_string(librdf_node_get_uri(object_node)); } } else triple->object=NULL; return triple; }
/* * Convert the given librdf_node to a Ruby object and return it as a VALUE. */ VALUE rleaf_librdf_node_to_value( librdf_node *node ) { VALUE node_object = Qnil, node_string = Qnil; librdf_node_type nodetype = LIBRDF_NODE_TYPE_UNKNOWN; unsigned char *bnode_idname = NULL; ID bnode_id; if ( !node ) rb_fatal( "NULL pointer given to rleaf_librdf_node_to_value()" ); nodetype = librdf_node_get_type( node ); node_string = rleaf_librdf_node_to_string( node ); rleaf_log( "debug", "Converting node %s to a Ruby VALUE", RSTRING_PTR(node_string) ); switch( nodetype ) { /* URI node => URI */ case LIBRDF_NODE_TYPE_RESOURCE: node_object = rleaf_librdf_uri_node_to_object( node ); break; /* Blank node => Symbol */ case LIBRDF_NODE_TYPE_BLANK: bnode_idname = librdf_node_get_blank_identifier( node ); if ( bnode_idname ) { rleaf_log( "debug", "Converted a bnode to %s", bnode_idname ); bnode_id = rb_intern( (char *)bnode_idname ); node_object = ID2SYM( bnode_id ); } else { rleaf_log( "error", "Failed to get a blank identifier!" ); node_object = Qnil; } break; /* Literal => <ruby object> */ case LIBRDF_NODE_TYPE_LITERAL: node_object = rleaf_librdf_literal_node_to_object( node ); break; default: rb_fatal( "Unknown node type %d encountered when converting a node.", nodetype ); } return node_object; }
/** Create a new SLV2Value from a librdf_node, or return NULL if impossible */ SLV2Value slv2_value_from_librdf_node(SLV2World world, librdf_node* node) { SLV2Value result = NULL; librdf_uri* datatype_uri = NULL; SLV2ValueType type = SLV2_VALUE_STRING; switch (librdf_node_get_type(node)) { case LIBRDF_NODE_TYPE_RESOURCE: type = SLV2_VALUE_URI; result = slv2_value_new_librdf_uri(world, librdf_node_get_uri(node)); break; case LIBRDF_NODE_TYPE_LITERAL: datatype_uri = librdf_node_get_literal_value_datatype_uri(node); if (datatype_uri) { if (!strcmp((const char*)librdf_uri_as_string(datatype_uri), "http://www.w3.org/2001/XMLSchema#integer")) type = SLV2_VALUE_INT; else if (!strcmp((const char*)librdf_uri_as_string(datatype_uri), "http://www.w3.org/2001/XMLSchema#decimal")) type = SLV2_VALUE_FLOAT; else fprintf(stderr, "Unknown datatype %s\n", librdf_uri_as_string(datatype_uri)); } result = slv2_value_new(world, type, (const char*)librdf_node_get_literal_value(node)); break; case LIBRDF_NODE_TYPE_BLANK: type = SLV2_VALUE_STRING; result = slv2_value_new(world, type, (const char*)librdf_node_get_blank_identifier(node)); break; case LIBRDF_NODE_TYPE_UNKNOWN: default: fprintf(stderr, "Unknown RDF node type %d\n", librdf_node_get_type(node)); break; } return result; }
static char* node_helper(librdf_storage* storage, librdf_node* node) { librdf_uri* uri; librdf_uri* dt_uri; const char* integer_type = "http://www.w3.org/2001/XMLSchema#integer"; const char* float_type = "http://www.w3.org/2001/XMLSchema#float"; const char* datetime_type = "http://www.w3.org/2001/XMLSchema#dateTime"; char* name; char data_type; switch(librdf_node_get_type(node)) { case LIBRDF_NODE_TYPE_RESOURCE: uri = librdf_node_get_uri(node); name = librdf_uri_as_string(uri); data_type = 'u'; break; case LIBRDF_NODE_TYPE_LITERAL: dt_uri = librdf_node_get_literal_value_datatype_uri(node); if (dt_uri == 0) data_type = 's'; else { const char* type_uri = librdf_uri_as_string(dt_uri); if (strcmp(type_uri, integer_type) == 0) data_type = 'i'; else if (strcmp(type_uri, float_type) == 0) data_type = 'f'; else if (strcmp(type_uri, datetime_type) == 0) data_type = 'd'; else data_type = 's'; } name = librdf_node_get_literal_value(node); break; case LIBRDF_NODE_TYPE_BLANK: name = librdf_node_get_blank_identifier(node); data_type = 'b'; break; case LIBRDF_NODE_TYPE_UNKNOWN: break; } char* term = malloc(5 + strlen(name)); if (term == 0) { fprintf(stderr, "malloc failed"); return 0; } sprintf(term, "%c:%s", data_type, name); return term; }
static void librdf_serializer_print_statement_as_ntriple(librdf_statement * statement, FILE *stream) { librdf_node *subject=librdf_statement_get_subject(statement); librdf_node *predicate=librdf_statement_get_predicate(statement); librdf_node *object=librdf_statement_get_object(statement); char *lang=NULL; librdf_uri *dt_uri=NULL; if(librdf_node_is_blank(subject)) fprintf(stream, "_:%s", librdf_node_get_blank_identifier(subject)); else if(librdf_node_is_resource(subject)) { /* Must be a URI */ fputc('<', stream); raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(subject)), '\0'); fputc('>', stream); } else { LIBRDF_ERROR2(statement->world, "Do not know how to print triple subject type %d\n", librdf_node_get_type(subject)); return; } if(!librdf_node_is_resource(predicate)) { LIBRDF_ERROR2(statement->world, "Do not know how to print triple predicate type %d\n", librdf_node_get_type(predicate)); return; } fputc(' ', stream); fputc('<', stream); raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(predicate)), '\0'); fputc('>', stream); fputc(' ', stream); switch(librdf_node_get_type(object)) { case LIBRDF_NODE_TYPE_LITERAL: fputc('"', stream); raptor_print_ntriples_string(stream, librdf_node_get_literal_value(object), '"'); fputc('"', stream); lang=librdf_node_get_literal_value_language(object); dt_uri=librdf_node_get_literal_value_datatype_uri(object); if(lang) { fputc('@', stream); fputs(lang, stream); } if(dt_uri) { fputs("^^<", stream); raptor_print_ntriples_string(stream, librdf_uri_as_string(dt_uri), '\0'); fputc('>', stream); } break; case LIBRDF_NODE_TYPE_BLANK: fputs("_:", stream); fputs((const char*)librdf_node_get_blank_identifier(object), stream); break; case LIBRDF_NODE_TYPE_RESOURCE: fputc('<', stream); raptor_print_ntriples_string(stream, librdf_uri_as_string(librdf_node_get_uri(object)), '\0'); fputc('>', stream); break; default: LIBRDF_ERROR2(statement->world, "Do not know how to print triple object type %d\n", librdf_node_get_type(object)); return; } fputs(" .", stream); }