Example #1
0
int add_triple_typed(librdf_world *world, librdf_node *context, librdf_model* model, const char *s, const char *p, const char *o)
{
  librdf_node *subject, *predicate, *object;
  librdf_statement* statement=NULL;
  int rc;

  if(librdf_heuristic_is_blank_node(s))
    subject=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(s));
  else
    subject=librdf_new_node_from_uri_string(world, (const unsigned char *)s);

  predicate=librdf_new_node_from_uri_string(world, (const unsigned char *)p);

  if(librdf_heuristic_is_blank_node(o))
    object=librdf_new_node_from_blank_identifier(world, (const unsigned char *)librdf_heuristic_get_blank_node(o));
  else
    object=librdf_new_node_from_literal(world, (const unsigned char *)o, NULL, 0);

  statement=librdf_new_statement(world);
  librdf_statement_set_subject(statement, subject);
  librdf_statement_set_predicate(statement, predicate);
  librdf_statement_set_object(statement, object);

  rc=librdf_model_context_add_statement(model, context, statement);

  librdf_free_statement(statement);
  return rc;
}
Example #2
0
/*
 * librdf_parser_raptor_new_statement_handler - helper callback function for raptor RDF when a new triple is asserted
 * @context: context for callback
 * @statement: raptor_statement
 *
 * Adds the statement to the list of statements.
 */
static void
librdf_parser_raptor_new_statement_handler(void *context,
                                           raptor_statement *rstatement)
{
  librdf_parser_raptor_stream_context* scontext=(librdf_parser_raptor_stream_context*)context;
  librdf_node* node;
  librdf_statement* statement;
  librdf_world* world=scontext->pcontext->parser->world;
  int rc;

  statement=librdf_new_statement(world);
  if(!statement)
    return;

  if(rstatement->subject->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, (const unsigned char*)rstatement->subject->value.blank.string);
  } else if (rstatement->subject->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->subject->value.uri);
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor subject identifier type %d",
               rstatement->subject->type);
    librdf_free_statement(statement);
    return;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot create subject node");
    librdf_free_statement(statement);
    return;
  }

  librdf_statement_set_subject(statement, node);


  if(rstatement->predicate->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->predicate->value.uri);
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor predicate identifier type %d",
               rstatement->predicate->type);
    librdf_free_statement(statement);
    return;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot create predicate node");
    librdf_free_statement(statement);
    return;
  }

  librdf_statement_set_predicate(statement, node);

  if(rstatement->object->type == RAPTOR_TERM_TYPE_LITERAL) {
    node = librdf_new_node_from_typed_literal(world,
                                              rstatement->object->value.literal.string,
                                              (const char *)rstatement->object->value.literal.language,
                                              (librdf_uri*)rstatement->object->value.literal.datatype);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, rstatement->object->value.blank.string);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->object->value.uri);
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor object identifier type %d",
               rstatement->object->type);
    librdf_free_statement(statement);
    return;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot create object node");
    librdf_free_statement(statement);
    return;
  }

  librdf_statement_set_object(statement, node);

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  if(1) {
    raptor_iostream *iostr;
    iostr = raptor_new_iostream_to_file_handle(world->raptor_world_ptr, stderr);
    librdf_statement_write(statement, iostr);
    raptor_free_iostream(iostr);
  }
#endif

  if(scontext->model) {
    if(librdf_model_supports_contexts(scontext->model) &&
       rstatement->graph &&
       (rstatement->graph->type == RAPTOR_TERM_TYPE_URI ||
        rstatement->graph->type == RAPTOR_TERM_TYPE_BLANK)) {
      node = librdf_new_node_from_uri(world, (librdf_uri*)rstatement->graph->value.uri);
      rc = librdf_model_context_add_statement(scontext->model, node, statement);
      librdf_free_node(node);
    } else {
      rc = librdf_model_add_statement(scontext->model, statement);
    }
    librdf_free_statement(statement);
  } else {
    rc=librdf_list_add(scontext->statements, statement);
    if(rc)
      librdf_free_statement(statement);
  }
  if(rc) {
    librdf_log(world,
               0, LIBRDF_LOG_FATAL, LIBRDF_FROM_PARSER, NULL,
               "Cannot add statement to model");
  }
}