/** 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; }
/* * 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; }
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; }
/** * librdf_statement_to_string: * @statement: the statement * * Format the librdf_statement as a string. * * Formats the statement as a newly allocate string that must be freed by * the caller. * * Return value: the string or NULL on failure. **/ unsigned char * librdf_statement_to_string(librdf_statement *statement) { unsigned char *subject_string, *predicate_string, *object_string; unsigned char *s; int statement_string_len=0; const char *format; #define NULL_STRING_LENGTH 6 static const unsigned char * const null_string=(const unsigned char *)"(null)"; size_t len; LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, NULL); if(statement->subject) { subject_string=librdf_node_to_counted_string(statement->subject, &len); if(!subject_string) return NULL; statement_string_len += len; } else { subject_string=(unsigned char*)null_string; statement_string_len += NULL_STRING_LENGTH; } if(statement->predicate) { predicate_string=librdf_node_to_counted_string(statement->predicate, &len); if(!predicate_string) { if(subject_string != null_string) LIBRDF_FREE(cstring, subject_string); return NULL; } statement_string_len += len; } else { predicate_string=(unsigned char*)null_string; statement_string_len += NULL_STRING_LENGTH; } if(statement->object) { object_string=librdf_node_to_counted_string(statement->object, &len); if(!object_string) { if(subject_string != null_string) LIBRDF_FREE(cstring, subject_string); if(predicate_string != null_string) LIBRDF_FREE(cstring, predicate_string); return NULL; } statement_string_len += len; } else { object_string=(unsigned char*)null_string; statement_string_len += NULL_STRING_LENGTH; } #define LIBRDF_STATEMENT_FORMAT_STRING_LITERAL "{%s, %s, \"%s\"}" #define LIBRDF_STATEMENT_FORMAT_RESOURCE_LITERAL "{%s, %s, %s}" statement_string_len += + 1 + /* "{" %s */ 2 + /* ", " %s */ 2 + /* ", " %s */ 1; /* "}" */ if(statement->object && librdf_node_get_type(statement->object) == LIBRDF_NODE_TYPE_LITERAL) { format=LIBRDF_STATEMENT_FORMAT_STRING_LITERAL; statement_string_len+=2; /* Extra "" around literal */ } else { format=LIBRDF_STATEMENT_FORMAT_RESOURCE_LITERAL; } s=(unsigned char*)LIBRDF_MALLOC(cstring, statement_string_len+1); if(s) sprintf((char*)s, format, subject_string, predicate_string, object_string); /* always free allocated intermediate strings */ if(subject_string != null_string) LIBRDF_FREE(cstring, subject_string); if(predicate_string != null_string) LIBRDF_FREE(cstring, predicate_string); if(object_string != null_string) LIBRDF_FREE(cstring, object_string); return s; }
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); }
UT_Error IE_Imp_OpenDocument::_handleRDFStreams () { #ifndef WITH_REDLAND return UT_OK; #else UT_Error error = UT_OK; UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams()\n")); // // DEBUG. // { // PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF(); // PD_DocumentRDFMutationHandle m = rdf->createMutation(); // m->add( PD_URI("http://www.example.com/foo#bar" ), // PD_URI("http://www.example.com/foo#bar" ), // PD_Object("http://www.example.com/foo#bar" ) ); // m->commit(); // rdf->dumpModel("added foo"); // } RDFArguments args; librdf_model* model = args.model; // check if we can load a manifest.rdf file GsfInput* pRdfManifest = gsf_infile_child_by_name(m_pGsfInfile, "manifest.rdf"); if (pRdfManifest) { UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() have manifest.rdf\n")); error = _loadRDFFromFile( pRdfManifest, "manifest.rdf", &args ); g_object_unref (G_OBJECT (pRdfManifest)); if (error != UT_OK) return error; } // find other RDF/XML files referenced in the manifest const char* query_string = "" "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" "prefix odf: <http://docs.oasis-open.org/opendocument/meta/package/odf#> \n" "prefix odfcommon: <http://docs.oasis-open.org/opendocument/meta/package/common#> \n" "select ?subj ?fileName \n" " where { \n" " ?subj rdf:type odf:MetaDataFile . \n" " ?subj odfcommon:path ?fileName \n" " } \n"; librdf_uri* base_uri = 0; librdf_query* query = librdf_new_query( args.world, "sparql", 0, (unsigned char*)query_string, base_uri ); librdf_query_results* results = librdf_query_execute( query, model ); if( !results ) { // Query failed is a failure to execute the SPARQL, // in which case there might be results but we couldn't find them UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() SPARQL query to find auxillary RDF/XML files failed! q:%p\n", query )); error = UT_ERROR; } else { UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() aux RDF/XML file count:%d\n", librdf_query_results_get_count( results ))); // parse auxillary RDF/XML files too for( ; !librdf_query_results_finished( results ) ; librdf_query_results_next( results )) { librdf_node* fnNode = librdf_query_results_get_binding_value_by_name ( results, "fileName" ); UT_DEBUGMSG(("_handleRDFStreams() fnNode:%p\n", fnNode )); std::string fn = toString(fnNode); UT_DEBUGMSG(("_handleRDFStreams() loading auxilary RDF/XML file from:%s\n", fn.c_str())); GsfInput* pAuxRDF = gsf_infile_child_by_name(m_pGsfInfile, fn.c_str()); if (pAuxRDF) { error = _loadRDFFromFile( pAuxRDF, fn.c_str(), &args ); g_object_unref (G_OBJECT (pAuxRDF)); if( error != UT_OK ) break; } else { UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); return UT_ERROR; } } librdf_free_query_results( results ); } librdf_free_query( query ); UT_DEBUGMSG(("_handleRDFStreams() error:%d model.sz:%d\n", error, librdf_model_size( model ))); if( error != UT_OK ) { return error; } // convert the redland model into native AbiWord RDF triples { PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF(); PD_DocumentRDFMutationHandle m = rdf->createMutation(); librdf_statement* statement = librdf_new_statement( args.world ); librdf_stream* stream = librdf_model_find_statements( model, statement ); while (!librdf_stream_end(stream)) { librdf_statement* current = librdf_stream_get_object( stream ); int objectType = PD_Object::OBJECT_TYPE_URI; std::string xsdType = ""; if( librdf_node_is_blank( librdf_statement_get_object( current ))) { objectType = PD_Object::OBJECT_TYPE_BNODE; } if( librdf_node_is_literal( librdf_statement_get_object( current ))) { objectType = PD_Object::OBJECT_TYPE_LITERAL; if( librdf_uri* u = librdf_node_get_literal_value_datatype_uri( librdf_statement_get_object( current ))) { xsdType = toString(u); } } if( DEBUG_RDF_IO ) { UT_DEBUGMSG(("_handleRDFStreams() adding s:%s p:%s o:%s rotv:%d otv:%d ots:%s\n", toString( librdf_statement_get_subject( current )).c_str(), toString( librdf_statement_get_predicate( current )).c_str(), toString( librdf_statement_get_object( current )).c_str(), librdf_node_get_type(librdf_statement_get_object( current )), objectType, xsdType.c_str() )); } m->add( PD_URI( toString( librdf_statement_get_subject( current ))), PD_URI( toString( librdf_statement_get_predicate( current ))), PD_Object( toString( librdf_statement_get_object( current )), objectType, xsdType )); // m->add( PD_URI( toString( librdf_statement_get_subject( current ))), // PD_URI( toString( librdf_statement_get_predicate( current ))), // PD_Object( toString( librdf_statement_get_object( current )), // objectType, // xsdType )); librdf_stream_next(stream); } librdf_free_stream( stream ); librdf_free_statement( statement ); // m->add( PD_URI("http://www.example.com/foo#bar" ), // PD_URI("http://www.example.com/foo#bar" ), // PD_Object("http://www.example.com/foo#bar" ) ); m->commit(); } if( DEBUG_RDF_IO ) { getDoc()->getDocumentRDF()->dumpModel("Loaded RDF from ODF file"); } return error; #endif }
static int validate_webid(request_rec *request, const char *subjAltName, char *pkey_n, unsigned int pkey_e_i) { int r = HTTP_UNAUTHORIZED; librdf_world *rdf_world = NULL; librdf_storage *rdf_storage = NULL; librdf_model *rdf_model = NULL; librdf_query *rdf_query = NULL; librdf_query_results *rdf_query_results = NULL; rdf_world = librdf_new_world(); if (rdf_world != NULL) { librdf_world_open(rdf_world); librdf_world_set_logger(rdf_world, (void *)request, ap_rdf_log_proxy); rdf_storage = librdf_new_storage(rdf_world, "uri", subjAltName, NULL); if (rdf_storage != NULL) { rdf_model = librdf_new_model(rdf_world, rdf_storage, NULL); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_storage returned NULL"); } if (rdf_model != NULL) { char *c_query = apr_psprintf(request->pool, SPARQL_WEBID, subjAltName); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: SPARQL query = %s", c_query); rdf_query = librdf_new_query(rdf_world, "sparql", NULL, (unsigned char*)c_query, NULL); } else { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_query returned NULL"); } if (rdf_query != NULL) { rdf_query_results = librdf_query_execute(rdf_query, rdf_model); if (rdf_query_results != NULL) { for (; r != OK && librdf_query_results_finished(rdf_query_results)==0; librdf_query_results_next(rdf_query_results)) { librdf_node *m_node, *e_node; unsigned char *rdf_mod; unsigned char *rdf_exp; if (r != OK && NULL != (m_node = librdf_query_results_get_binding_value_by_name(rdf_query_results, "m")) && NULL != (e_node = librdf_query_results_get_binding_value_by_name(rdf_query_results, "e"))) { if (librdf_node_get_type(m_node) == LIBRDF_NODE_TYPE_LITERAL && librdf_node_get_type(e_node) == LIBRDF_NODE_TYPE_LITERAL) { rdf_mod = librdf_node_get_literal_value(m_node); rdf_exp = librdf_node_get_literal_value(e_node); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: modulus = %s", rdf_mod); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: exponent = %s", rdf_exp); if (rdf_exp != NULL && apr_strtoi64((char*)rdf_exp, NULL, 10) == pkey_e_i && matches_pkey(rdf_mod, pkey_n)) r = OK; librdf_free_node(m_node); librdf_free_node(e_node); } } } librdf_free_query_results(rdf_query_results); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_query_execute returned NULL"); librdf_free_query(rdf_query); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_query returned NULL"); if (rdf_model) librdf_free_model(rdf_model); if (rdf_storage) librdf_free_storage(rdf_storage); if (rdf_world) librdf_free_world(rdf_world); return r; }