Exemplo n.º 1
0
  void RdfStorePrivate::Save(std::string filename, std::string format)
  {
    if (format == "") format = "turtle";

    librdf_uri* baseUri = RdfUriToLibRdfUri(m_BaseUri);

    librdf_serializer* s = librdf_new_serializer(m_World, format.c_str(), nullptr, nullptr);

    if(!s)
    {
      mitkThrow() << "RDF Library Error";
    }

    for (PrefixMap::const_iterator i = m_Prefixes.begin(); i != m_Prefixes.end(); i++)
    {
      librdf_serializer_set_namespace(s, RdfUriToLibRdfUri(i->second), i->first.c_str());
    }

    FILE* f = fopen(filename.c_str(), "w+");

    librdf_serializer_serialize_model_to_file_handle(s, f, baseUri, m_Model);

    librdf_free_serializer(s);
    librdf_free_uri(baseUri);
    fclose(f);
  }
Exemplo n.º 2
0
/**
 * librdf_serializer_serialize_model_to_file:
 * @serializer: the serializer
 * @name: filename to serialize to
 * @base_uri: the base URI to use (or NULL)
 * @model: the #librdf_model model to use
 *
 * Write a serialized #librdf_model to a file.
 * 
 * Return value: non 0 on failure
 **/
int
librdf_serializer_serialize_model_to_file(librdf_serializer* serializer,
                                          const char *name, 
                                          librdf_uri* base_uri,
                                          librdf_model* model) 
{
  FILE* fh;
  int status;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(serializer, librdf_serializer, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(name, string, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);

  fh=fopen(name, "w+");
  if(!fh) {
    librdf_log(serializer->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_SERIALIZER,
               NULL, "failed to open file '%s' for writing - %s",
               name, strerror(errno));
    return 1;
  }
  
  status=librdf_serializer_serialize_model_to_file_handle(serializer, fh, 
                                                          base_uri, model);
  fclose(fh);
  return status;
}
Exemplo n.º 3
0
/**
 * librdf_serializer_serialize_model:
 * @serializer: the serializer
 * @handle: file handle to serialize to
 * @base_uri: the base URI to use (or NULL)
 * @model: the #librdf_model model to use
 *
 * @Deprecated: Use librdf_serializer_serialize_model_to_file_handle()
 *
 * Write a serialized #librdf_model to a FILE*.
 *
 * Return value: non 0 on failure
 **/
int
librdf_serializer_serialize_model(librdf_serializer* serializer,
                                  FILE *handle, librdf_uri* base_uri,
                                  librdf_model* model) 
{
  return librdf_serializer_serialize_model_to_file_handle(serializer,
                                                          handle, base_uri,
                                                          model);
}
Exemplo n.º 4
0
static int
librdf_storage_file_sync(librdf_storage *storage)
{
    librdf_storage_file_instance* context=(librdf_storage_file_instance*)storage->instance;
    char *backup_name;
    char *new_name;
    librdf_serializer* serializer;
    FILE *fh;
    int rc=0;

    if(!context->changed)
        return 0;

    if(!context->name) {
        /* FIXME - URI cannot be written */
        context->changed=0;
        return 0;
    }

    backup_name=NULL;

    if(!access((const char*)context->name, F_OK)) {
        /* name"~\0" */
        backup_name=(char*)LIBRDF_MALLOC(cstring, context->name_len+2);
        if(!backup_name)
            return 1;
        strcpy(backup_name, (const char*)context->name);
        backup_name[context->name_len]='~';
        backup_name[context->name_len+1]='\0';

        if(rename(context->name, backup_name) < 0) {
            librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                       "rename of '%s' to '%s' failed - %s",
                       context->name, backup_name, strerror(errno));
            LIBRDF_FREE(cstring, backup_name);
            return 1;
        }
    }

    /* name".new\0" */
    new_name=(char*)LIBRDF_MALLOC(cstring, context->name_len+5);
    if(!new_name)
        return 1;
    strcpy(new_name, (const char*)context->name);
    strcpy(new_name+context->name_len, ".new");

    serializer = librdf_new_serializer(storage->world, context->format_name,
                                       NULL, NULL);
    if(!serializer) {
        LIBRDF_FREE(cstring, new_name);
        if(backup_name)
            LIBRDF_FREE(cstring, backup_name);
        return 1;
    }

    fh=fopen(new_name, "w+");
    if(!fh) {
        librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                   "failed to open file '%s' for writing - %s",
                   new_name, strerror(errno));
        rc=1;
    } else {
        librdf_serializer_serialize_model_to_file_handle(serializer, fh,
                context->uri,
                context->model);
        fclose(fh);
    }
    librdf_free_serializer(serializer);

    if(fh && rename(new_name, context->name) < 0) {
        librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                   "rename of '%s' to '%s' failed - %s (%d)",
                   new_name, context->name, strerror(errno), errno);
        fh=NULL;
        rc=1;
    }

    LIBRDF_FREE(cstring, new_name);

    /* restore backup on failure (fh=NULL) */
    if(!fh && backup_name && rename(backup_name, context->name) < 0) {
        librdf_log(storage->world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_STORAGE, NULL,
                   "rename of '%s' to '%s' failed - %s",
                   backup_name, context->name, strerror(errno));
        rc=1;
    }

    if(backup_name)
        LIBRDF_FREE(cstring, backup_name);

    context->changed=0;

    return rc;
}
Exemplo n.º 5
0
int 
print_query_results(librdf_world* world, librdf_model* model, librdf_query_results *results)
{
  int i;
  char *name;
  librdf_stream* stream;
  librdf_serializer* serializer;
  const char *query_graph_serializer_syntax_name="rdfxml";

  if(librdf_query_results_is_bindings(results)) {
    fprintf(stdout, ": Query returned bindings results:\n");

    while(!librdf_query_results_finished(results)) {
      fputs("result: [", stdout);
      for(i=0; i<librdf_query_results_get_bindings_count(results); i++) {
	librdf_node *value=librdf_query_results_get_binding_value(results, i);
	name=(char*)librdf_query_results_get_binding_name(results, i);

	if(i>0)
	fputs(", ", stdout);
	fprintf(stdout, "%s=", name);
	if(value) {
	  librdf_node_print(value, stdout);
	  librdf_free_node(value);
	} else
	fputs("NULL", stdout);
      }
      fputs("]\n", stdout);

      librdf_query_results_next(results);
    }
    fprintf(stdout, ": Query returned %d results\n", librdf_query_results_get_count(results));
  } else if(librdf_query_results_is_boolean(results)) {
    fprintf(stdout, ": Query returned boolean result: %s\n", librdf_query_results_get_boolean(results) ? "true" : "false");
  } else if(librdf_query_results_is_graph(results)) {
    librdf_storage* tmp_storage;
    librdf_model* tmp_model;

    tmp_storage=librdf_new_storage(world, NULL, NULL, NULL);
    tmp_model=librdf_new_model(world, tmp_storage, NULL);

    fprintf(stdout, ": Query returned graph result:\n");

    stream=librdf_query_results_as_stream(results);
    if(!stream) {
      fprintf(stderr, ": Failed to get query results graph\n");
      return -1;
    }
    librdf_model_add_statements(tmp_model, stream);
    librdf_free_stream(stream);

    fprintf(stdout, ": Total %d triples\n", librdf_model_size(model));

    serializer=librdf_new_serializer(world, query_graph_serializer_syntax_name, NULL, NULL);
    if(!serializer) {
      fprintf(stderr, ": Failed to create serializer type %s\n", query_graph_serializer_syntax_name);
      return -1;
    }

    librdf_serializer_serialize_model_to_file_handle(serializer, stdout, NULL, tmp_model);
    librdf_free_serializer(serializer);
    librdf_free_model(tmp_model);
    librdf_free_storage(tmp_storage);
  } else {
    fprintf(stdout, ": Query returned unknown result format\n");
    return -1;
  }
  return 0;
}