为什么下面的测试会产生错误?即使所有实际的URI都是绝对的,Redland的海龟解析器是否坚持使用基本URI?(Apache Jena显然没有。)我如何才能发现更多关于实际错误的信息(即API调用会返回错误描述或类似信息)?
librdf_world *world = librdf_new_world();
librdf_world_open(world);
librdf_storage *storage = librdf_new_storage(world, "memory", NULL, NULL);
librdf_model *model = librdf_new_model(world, storage, NULL);
librdf_parser* parser = librdf_new_parser(world, NULL, "text/turtle", NULL);
librdf_uri *baseUri = NULL;
const char *turtle = "<http://example.com/SomeSubject> <http://example.com/SomePredicate> <http://example.com/SomeObject> .";
int error = librdf_parser_parse_string_into_model(parser, (const unsigned char *)turtle, baseUri, model);
需要一个基本URI,因为解析器使用RAPTOR_SYNTAX_NEED_BASE_URI
标志这样说。它甚至在查看raptor_parser_parse_start()
中的内容之前就产生了错误。
如果您知道不需要真正的基本URI,那么可以提供一个伪URI,例如.
:
librdf_uri *baseUri = librdf_new_uri(world, (const unsigned char *)".");
为了实现更好的错误报告,您应该使用librdf_world_set_logger()
注册一个记录器——默认的记录器只是向stderr
吐出。从logger函数返回non-0,以指示您自己处理消息。示例:
#include <librdf.h>
int customlogger(void *user_data, librdf_log_message *message) {
fputs("mad custom logger: ", stderr);
fputs(message->message, stderr);
fputs("n", stderr);
return 1;
}
int main() {
librdf_world *world = librdf_new_world();
librdf_world_set_logger(world, /*user_data=*/ 0, customlogger);
librdf_world_open(world);
librdf_storage *storage = librdf_new_storage(world, "memory", NULL, NULL);
librdf_model *model = librdf_new_model(world, storage, NULL);
librdf_parser* parser = librdf_new_parser(world, NULL, "text/turtle", NULL);
librdf_uri *baseUri = NULL;
const char *turtle = "<http://example.com/SomeSubject> <http://example.com/SomePredicate> <http://example.com/SomeObject> .";
int error = librdf_parser_parse_string_into_model(parser, (const unsigned char *)turtle, baseUri, model);
}
运行此操作将导致
mad custom logger: Missing base URI for turtle parser
(对于真正的程序,添加一些清理等)