Пример #1
0
static
librdf_node* node_constructor_helper(librdf_world* world, const char* t,
				     size_t len)
{

    librdf_node* o;

    if ((strlen(t) < 2) || (t[1] != ':')) {
	fprintf(stderr, "node_constructor_helper called on invalid term\n");
	return 0;
    }

    if (t[0] == 'u') {
 	o = librdf_new_node_from_counted_uri_string(world,
						    (unsigned char*) t + 2,
						    len - 2);
	return o;
    }

    if (t[0] == 's') {
	o = librdf_new_node_from_typed_counted_literal(world,
						       (unsigned char*) t + 2,
						       len - 2, 0, 0, 0);
	return o;
    }


    if (t[0] == 'i') {
	librdf_uri* dt =
	    librdf_new_uri(world,
			   "http://www.w3.org/2001/XMLSchema#integer");
	if (dt == 0)
	    return 0;

	o = librdf_new_node_from_typed_counted_literal(world, t + 2, len - 2,
						       0, 0, dt);
	librdf_free_uri(dt);
	return o;
    }
    
    if (t[0] == 'f') {
	librdf_uri* dt =
	    librdf_new_uri(world,
			   "http://www.w3.org/2001/XMLSchema#float");
	if (dt == 0)
	    return 0;

	o = librdf_new_node_from_typed_counted_literal(world, t + 2, len - 2,
						       0, 0, dt);
	librdf_free_uri(dt);
	return o;
    }

    if (t[0] == 'd') {
	librdf_uri* dt =
	    librdf_new_uri(world,
			   "http://www.w3.org/2001/XMLSchema#dateTime");
	if (dt == 0)
	    return 0;

	o = librdf_new_node_from_typed_counted_literal(world, t + 2, len - 2,
						       0, 0, dt);
	librdf_free_uri(dt);
	return o;
    }    

    return librdf_new_node_from_typed_counted_literal(world,
						      (unsigned char*) t + 2,
						      len - 2, 0, 0, 0);

}
Пример #2
0
Файл: node.c Проект: ged/redleaf
/*
 * 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 );
}