示例#1
0
static int
librdf_storage_trees_node_compare(librdf_node* n1, librdf_node* n2)
{
  if (n1 == n2) {
    return 0;
  } else if (n1->type != n2->type) {
    return n2->type - n1->type;
  } else {
    switch (n1->type) {
      case LIBRDF_NODE_TYPE_RESOURCE:
        return librdf_uri_compare(librdf_node_get_uri(n1), librdf_node_get_uri(n2));
      case LIBRDF_NODE_TYPE_LITERAL:
        if (1) {
          const char *s;
          size_t l1;
          size_t l2;
          size_t l;
          int r;

          s = librdf_node_get_literal_value_language(n1);
          l1 = s ? strlen(s) : 0;
          s = librdf_node_get_literal_value_language(n2);
          l2 = s ? strlen(s) : 0;

          l = (l1 < l2) ? l1 : l2;

          /* compare first by data type */
          r = librdf_uri_compare(librdf_node_get_literal_value_datatype_uri(n1),
                                 librdf_node_get_literal_value_datatype_uri(n2));
          if (r)
            return r;

          /* if data type is equal, compare by value */
          r = strcmp((const char*)librdf_node_get_literal_value(n1),
                     (const char*)librdf_node_get_literal_value(n2));
          if (r)
            return r;

          /* if both data type and value are equal, compare by language */
          if (l) {
            return strncmp(librdf_node_get_literal_value_language(n1),
                           librdf_node_get_literal_value_language(n2),
                           (size_t)l);
          } else {
            /* if l == 0 strncmp will always return 0; in that case
             * consider the node with no language to be lesser. */
            return l1 - l2;
          }
        }
      case LIBRDF_NODE_TYPE_BLANK:
        return strcmp((char*)n1->value.blank.identifier,
                      (char*)n2->value.blank.identifier);
      case LIBRDF_NODE_TYPE_UNKNOWN:
      default:
        return (char*)n2-(char*)n1; /* ? */
    }
  }
}
示例#2
0
文件: rdf_init.c 项目: njh/librdf
/**
 * librdf_world_set_feature:
 * @world: #librdf_world object
 * @feature: #librdf_uri feature property
 * @value: #librdf_node feature property value
 *
 * Set the value of a world feature.
 * 
 * Return value: non 0 on failure (negative if no such feature)
 **/
int
librdf_world_set_feature(librdf_world* world, librdf_uri* feature,
                         librdf_node* value) 
{
  librdf_uri* genid_base;
  librdf_uri* genid_counter;
  int rc= -1;

  genid_counter = librdf_new_uri(world,
                                 (const unsigned char*)LIBRDF_WORLD_FEATURE_GENID_COUNTER);
  genid_base = librdf_new_uri(world,
                              (const unsigned char*)LIBRDF_WORLD_FEATURE_GENID_BASE);

  if(librdf_uri_equals(feature, genid_base)) {
    if(!librdf_node_is_resource(value))
      rc=1;
    else {
      int i = atoi((const char*)librdf_node_get_literal_value(value));
      if(i < 1)
        i = 1;

#ifdef WITH_THREADS
      pthread_mutex_lock(world->mutex);
#endif
      world->genid_base = 1;
#ifdef WITH_THREADS
      pthread_mutex_unlock(world->mutex);
#endif
      rc = 0;
    }
  } else if(librdf_uri_equals(feature, genid_counter)) {
    if(!librdf_node_is_resource(value))
      rc = 1;
    else {
      int i = atoi((const char*)librdf_node_get_literal_value(value));
      if(i < 1)
        i = 1;

#ifdef WITH_THREADS
      pthread_mutex_lock(world->mutex);
#endif
      world->genid_counter = 1;
#ifdef WITH_THREADS
      pthread_mutex_unlock(world->mutex);
#endif
      rc = 0;
    }
  }

  librdf_free_uri(genid_base);
  librdf_free_uri(genid_counter);

  return rc;
}
示例#3
0
  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;
  }
示例#4
0
文件: utilities.c 项目: h0st/m3sec
void statement_in_tl(GSList ** list_, librdf_statement * statement)
{
	GSList * list=*list_;

	ssTriple_t * triple;

	librdf_node* sub_node ;
	librdf_node* pred_node;
	librdf_node* obj_node ;

	sub_node  = librdf_statement_get_subject(statement);
	pred_node = librdf_statement_get_predicate(statement);
	obj_node  = librdf_statement_get_object(statement);

	triple  =(ssTriple_t *) g_new0(ssTriple_t,1);

	triple->subject=	g_strdup(librdf_uri_as_string( librdf_node_get_uri(sub_node)));
	triple->predicate =	g_strdup(librdf_uri_as_string( librdf_node_get_uri(pred_node)));

	if (librdf_node_is_literal(obj_node))
	{	triple->object =  g_strdup(librdf_node_get_literal_value(obj_node));
		triple->objType = ssElement_TYPE_LIT;
	}
	else
	{	triple->object =  g_strdup(librdf_uri_as_string( librdf_node_get_uri(obj_node)));
		triple->objType = ssElement_TYPE_URI;
	}

	//printf ("statement to TL %s_%s_%s_%d\n", (char *)triple->subject, (char *)triple->predicate, (char *)triple->object, (bool *)triple->objType);
	list=g_slist_prepend(list, triple);
	///////////////////////////////////////////////////////////

	*list_=list;

}
示例#5
0
void output_node(librdf_node* n)
{

    if (librdf_node_is_literal(n)) {
	std::cout << librdf_node_get_literal_value(n);
    }

    if (librdf_node_is_resource(n)) {
	librdf_uri* uri = librdf_node_get_uri(n);
	std::cout << librdf_uri_as_string(uri);
    }

}
示例#6
0
文件: graph.c 项目: ged/redleaf
/*
 * call-seq:
 *   graph.supports_contexts?   -> true or false
 *
 * Returns +true+ if the receiving model supports contexts.
 *
 */
static VALUE
rleaf_redleaf_graph_supports_contexts_p( VALUE self ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );
	librdf_node *rval;
	const char *literal_rval;

	rval = librdf_model_get_feature( ptr->model, rleaf_contexts_feature );
	if ( rval == NULL ) return Qfalse;

	literal_rval = (const char *)librdf_node_get_literal_value( rval );
	if ( strncmp(literal_rval, "1", 1) == 0 )
		return Qtrue;
	else
		return Qfalse;
}
示例#7
0
/**
 * 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;
}
示例#8
0
/** 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;
}
示例#9
0
/* 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;
}
示例#10
0
文件: node.c 项目: ged/redleaf
/*
 * Convert the given literal librdf_node to a Ruby object and return it.
 */
VALUE
rleaf_librdf_literal_node_to_object( librdf_node *node ) {
	VALUE node_object = Qnil;
	librdf_uri *uri;
	VALUE literalstring, uristring;

	uri = librdf_node_get_literal_value_datatype_uri( node );
	literalstring = rb_str_new2( (char *)librdf_node_get_literal_value(node) );

	/* Plain literal -> String */
	if ( uri == NULL ) {
		// rleaf_log( "debug", "Converting plain literal %s to a String.", literalstring );
		node_object = literalstring;
	}

	/* Typed literal */
	else {
		uristring = rb_str_new2( (char *)librdf_uri_to_string(uri) );
		node_object = rb_funcall( rleaf_mRedleafNodeUtils,
			rb_intern("make_typed_literal_object"), 2, uristring, literalstring );
	}

	return node_object;
}
示例#11
0
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;

}
示例#12
0
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);
}
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;
}