2

为什么以下测试会产生错误?即使所有实际的 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);
4

1 回答 1

1

需要一个基本 URI,因为解析器使用RAPTOR_SYNTAX_NEED_BASE_URIflag这么说。它甚至在查看raptor_parser_parse_start().

如果您知道不需要真正的基本 URI,则可以提供一个虚拟 URI,例如.

librdf_uri *baseUri = librdf_new_uri(world, (const unsigned char *)".");

为了启用更好的错误报告,您应该注册一个记录器librdf_world_set_logger()- 默认记录器只是吐到stderr. 从 logger 函数返回非 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

(对于一个真正的程序,添加一些清理等)

于 2014-01-08T12:55:31.883 回答