crdfjenaturtle-rdfredland

Redland RDF libraries: why does parsing model from Turtle without base URI cause error?


Why does the following test produce an error? Does Redland's turtle parser insist on a base URI even if all actual URIs are absolute? (Apache Jena apparently does not.) And how could I find out more about what actually went wrong (i.e. what API call would return an error description, or similar)?

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);

Solution

  • A base URI is required because the parser says so using RAPTOR_SYNTAX_NEED_BASE_URI flag. It produces the error before even looking at the content in raptor_parser_parse_start().

    If you know a real base URI is not needed, you can supply a dummy URI such as . instead:

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

    To enable better error reports, you should register a logger with librdf_world_set_logger() - the default logger just spits to stderr. Return non-0 from the logger function to signal you handler the message yourself. Example:

    #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);
    
    }
    

    Running this will result in

    mad custom logger: Missing base URI for turtle parser
    

    (For a real program, add some cleanup etc.)