Exemple #1
0
  void RdfStorePrivate::Import(std::string url, std::string format)
  {
    std::string baseUri = m_BaseUri.ToString();

    if (baseUri.empty())
    {
      baseUri = url;
      SetBaseUri(RdfUri(baseUri));
    }

    if (format == "") format= "turtle";

    // Redland uses file paths like file:YOURPATH ( Example: file:D:/home/readme.txt )
    librdf_uri* uri = librdf_new_uri(m_World, (const unsigned char*) url.c_str());

    librdf_uri* libRdfBaseUri = librdf_new_uri(m_World, (const unsigned char*) baseUri.c_str());

    librdf_parser* p = librdf_new_parser(m_World, format.c_str(), nullptr, nullptr);

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

    if (librdf_parser_parse_into_model(p, uri, libRdfBaseUri, m_Model) != 0 )
    {
      librdf_free_parser(p);
      MITK_ERROR << "Parsing into Model failed.";
      return;
    }

    int namespaces = librdf_parser_get_namespaces_seen_count(p);

    for (int i = 0; i < namespaces; i++) {
      const char* prefixChar = librdf_parser_get_namespaces_seen_prefix(p, i);

      if ( !prefixChar ) continue;

      std::string prefix = prefixChar;
      RdfUri uri = LibRdfUriToRdfUri(librdf_parser_get_namespaces_seen_uri(p, i));

      if (uri == RdfUri()) return;

      RdfStorePrivate::PrefixMap::iterator it = m_Prefixes.find(prefix);

      // map iterator is equal to iterator-end so it is not already added
      if (it == m_Prefixes.end()) {
        AddPrefix(prefix, uri);
      }
    }
    librdf_free_parser(p);
  }
Exemple #2
0
/**
 * @brief Imports serialized data into storage.
 *
 *
 * @param input input data
 * @return true
 */
bool RDF_ImportFromFile(unsigned char *input)
{
    librdf_parser* parser;
    librdf_uri* uri;

    parser = librdf_new_parser(world, "rdfxml", "application/rdf+xml", NULL);

    uri = librdf_new_uri(world, input);
    librdf_parser_parse_into_model(parser, uri, uri, model);

    librdf_free_uri(uri);
    librdf_free_parser(parser);

    return true; 
}
Exemple #3
0
/**
 * @brief Imports serialized data into storage.
 *
 *
 * @param input input data
 * @return true
 */
bool RDF_ImportToStorage(unsigned char *input)
{
    librdf_parser* parser;
    parser = librdf_new_parser(world, "rdfxml", "application/rdf+xml", NULL);
    
    if (input != NULL)
    {
    	librdf_uri* uri = librdf_new_uri(world, input);
		librdf_parser_parse_string_into_model(parser, input, uri, model);

		librdf_free_uri(uri);
		librdf_free_parser(parser);

		free(input);
    }

    return true;
}
Exemple #4
0
/*
 * call-seq:
 *   graph.load( uri )   -> Fixnum
 *
 * Parse the RDF at the specified +uri+ into the receiving graph. Returns the number of statements
 * added to the graph (if the underlying store supports ).
 *
 *   graph = Redleaf::Graph.new
 *   graph.load( "http://bigasterisk.com/foaf.rdf" )
 *   graph.load( "http://www.w3.org/People/Berners-Lee/card.rdf" )
 *   graph.load( "http://danbri.livejournal.com/data/foaf" )
 *
 *   graph.size
 */
static VALUE
rleaf_redleaf_graph_load( VALUE self, VALUE uri ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );
	librdf_parser *parser = NULL;
	librdf_uri *rdfuri = NULL;
	int statement_count = librdf_model_size( ptr->model );

	if ( (parser = librdf_new_parser( rleaf_rdf_world, NULL, NULL, NULL )) == NULL )
		rb_raise( rleaf_eRedleafError, "failed to create a parser." );

	rdfuri = rleaf_object_to_librdf_uri( uri );

	if ( librdf_parser_parse_into_model(parser, rdfuri, NULL, ptr->model) != 0 )
		rb_raise( rleaf_eRedleafError, "failed to load %s into %s",
		librdf_uri_as_string(rdfuri), RSTRING_PTR(rb_inspect( self )) );

	return INT2FIX( librdf_model_size(ptr->model) - statement_count );
}
Exemple #5
0
/* functions implementing storage api */
static int
librdf_storage_file_init(librdf_storage* storage, const char *name,
                         librdf_hash* options)
{
    char *name_copy;
    char *contexts;
    int rc = 1;
    int is_uri = !strcmp(storage->factory->name, "uri");
    const char *format_name = (is_uri ? "guess" : "rdfxml");
    librdf_storage_file_instance* context;

    context = (librdf_storage_file_instance*)LIBRDF_CALLOC(librdf_storage_file_instance, 1, sizeof(librdf_storage_file_instance));
    if(!context)
        goto done;

    librdf_storage_set_instance(storage, context);

    /* Cannot save contexts in a file; pass everything else on */
    contexts = librdf_hash_get_del(options, "contexts");
    if(contexts)
        LIBRDF_FREE(cstring, contexts);

    context->format_name = librdf_hash_get_del(options, "format");
    if(context->format_name) {
        /* for 'file' and 'uri' storage, check this is a valid parser
         * for 'file' storage, also check this is a valid serializer
         */
        if(!librdf_parser_check_name(storage->world, context->format_name) ||
                (!is_uri && !librdf_serializer_check_name(storage->world, context->format_name))) {
            librdf_log(storage->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_STORAGE, NULL,
                       "Ignoring storage %s format option '%s' - using default format '%s'",
                       storage->factory->name, context->format_name, format_name);
            LIBRDF_FREE(cstring, context->format_name);
            context->format_name = NULL;
        }

        if(context->format_name)
            format_name = context->format_name;
    }


    if(is_uri)
        context->uri = librdf_new_uri(storage->world, (const unsigned char*)name);
    else {
        context->name_len = strlen(name);
        name_copy = (char*)LIBRDF_MALLOC(cstring, context->name_len+1);
        if(!name_copy)
            goto done;
        strcpy(name_copy,name);
        context->name = name_copy;
        context->uri = librdf_new_uri_from_filename(storage->world, context->name);
    }

    context->storage = librdf_new_storage_with_options(storage->world,
                       NULL, NULL,
                       options);
    if(!context->storage)
        goto done;

    context->model = librdf_new_model(storage->world, context->storage, NULL);
    if(!context->model)
        goto done;

    if(is_uri || !access((const char*)context->name, F_OK)) {
        librdf_parser *parser;

        parser = librdf_new_parser(storage->world, format_name, NULL, NULL);
        if(!parser) {
            rc = 1;
            goto done;
        }
        librdf_parser_parse_into_model(parser, context->uri, NULL, context->model);
        librdf_free_parser(parser);
    }

    context->changed = 0;

    rc = 0;

done:

    /* no more options, might as well free them now */
    if(options)
        librdf_free_hash(options);

    return rc;
}
static int
check_request_acl(request_rec *r, int req_access) {
    char *dir_path, *acl_path;
    apr_finfo_t acl_finfo;

    const char *req_uri, *dir_uri, *acl_uri, *access;
    const char *port, *par_uri, *req_file;

    librdf_world *rdf_world = NULL;
    librdf_storage *rdf_storage = NULL;
    librdf_model *rdf_model = NULL;
    librdf_parser *rdf_parser = NULL;
    librdf_uri *rdf_uri_acl = NULL,
               *rdf_uri_base = NULL;

    int ret = HTTP_FORBIDDEN;

    // dir_path: parent directory of request filename
    // acl_path: absolute path to request ACL
    dir_path = ap_make_dirstr_parent(r->pool, r->filename);
    acl_path = ap_make_full_path(r->pool, dir_path, WEBID_ACL_FNAME);

    if (apr_filepath_merge(&acl_path, NULL, acl_path, APR_FILEPATH_NOTRELATIVE, r->pool) != APR_SUCCESS) {
        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
                      "Module bug?  Request filename path %s is invalid or "
                      "or not absolute for uri %s",
                      r->filename, r->uri);
        return HTTP_FORBIDDEN;
    }

    // acl_path: 403 if missing
    if ((apr_stat(&acl_finfo, acl_path, APR_FINFO_TYPE, r->pool) != APR_SUCCESS) ||
        (acl_finfo.filetype != APR_REG)) {
        return HTTP_FORBIDDEN;
    }

    // req_uri: fully qualified URI of request filename
    // dir_uri: fully qualified URI of request filename parent
    // acl_uri: fully qualified URI of request filename ACL
    // access: ACL URI of requested access
    port = ap_is_default_port(ap_get_server_port(r), r)
           ? "" : apr_psprintf(r->pool, ":%u", ap_get_server_port(r));
    req_uri = apr_psprintf(r->pool, "%s://%s%s%s%s",
                           ap_http_scheme(r), ap_get_server_name(r), port,
                           (*r->uri == '/') ? "" : "/",
                           r->uri);
    par_uri = ap_make_dirstr_parent(r->pool, r->uri);
    dir_uri = apr_psprintf(r->pool, "%s://%s%s%s%s",
                           ap_http_scheme(r), ap_get_server_name(r), port,
                           (*par_uri == '/') ? "" : "/",
                           par_uri);
    acl_uri = ap_make_full_path(r->pool, dir_uri, WEBID_ACL_FNAME);

    if (req_access == WEBID_ACCESS_READ) {
        access = "Read";
    } else if (req_access == WEBID_ACCESS_WRITE) {
        if ((req_file = strrchr(r->filename, '/')) != NULL &&
            strcmp(++req_file, WEBID_ACL_FNAME) == 0)
            access = "Control";
        else
            access = "Write";
    } else {
        access = "Control";
    }

    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
                  "[ACL] %s (%s) %s | URI: %s | DIR: %s (%s) | ACL: %s (%s) | status: %d",
                  r->method, access, r->uri, req_uri, dir_uri, dir_path, acl_uri, acl_path, r->status);

    if ((rdf_world = librdf_new_world()) != NULL) {
        librdf_world_open(rdf_world);
        if ((rdf_storage = librdf_new_storage(rdf_world, "memory", NULL, NULL)) != NULL) {
            if ((rdf_model = librdf_new_model(rdf_world, rdf_storage, NULL)) != NULL) {
                if ((rdf_parser = librdf_new_parser(rdf_world, "turtle", NULL, NULL)) != NULL) {
                    if ((rdf_uri_base = librdf_new_uri(rdf_world, (unsigned char*)acl_uri)) != NULL) {
                        if ((rdf_uri_acl = librdf_new_uri_from_filename(rdf_world, acl_path)) != NULL) {
                            if (!librdf_parser_parse_into_model(rdf_parser, rdf_uri_acl, rdf_uri_base, rdf_model)) {
                                //log_stream_prefix(r, librdf_model_as_stream(rdf_model), "[ACL] [model]");
                                if (query_results(r, rdf_world, rdf_model,
                                    apr_psprintf(r->pool, SPARQL_URI_MODE_AGENT, "accessTo", req_uri, access, r->user)) > 0 || \
                                    query_results(r, rdf_world, rdf_model,
                                    apr_psprintf(r->pool, SPARQL_URI_MODE_AGENTCLASS, "accessTo", req_uri, access, r->user)) > 0 || \
                                    query_results(r, rdf_world, rdf_model,
                                    apr_psprintf(r->pool, SPARQL_URI_MODE_WORLD, "accessTo", req_uri, access)) > 0 || \
                                    ( ( query_results(r, rdf_world, rdf_model,
                                        apr_psprintf(r->pool, SPARQL_URI_ACL_EXISTS, "accessTo", req_uri )) == 0 ) &&
                                      ( query_results(r, rdf_world, rdf_model,
                                        apr_psprintf(r->pool, SPARQL_URI_MODE_AGENT, "defaultForNew", dir_uri, access, r->user)) > 0 || \
                                        query_results(r, rdf_world, rdf_model,
                                        apr_psprintf(r->pool, SPARQL_URI_MODE_AGENTCLASS, "defaultForNew", dir_uri, access, r->user)) > 0 || \
                                        query_results(r, rdf_world, rdf_model,
                                        apr_psprintf(r->pool, SPARQL_URI_MODE_WORLD, "defaultForNew", dir_uri, access)) > 0 ) ) ) {
                                    apr_table_set(r->headers_out, "Link", apr_psprintf(r->pool, "%s; rel=meta", acl_uri));
                                    ret = OK;
                                }
                            } else
                                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_parser_parse_into_model failed");
                            librdf_free_uri(rdf_uri_acl);
                        } else
                            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_new_uri_from_filename returned NULL");
                        librdf_free_uri(rdf_uri_base);
                    } else
                        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_new_uri returned NULL");
                    librdf_free_parser(rdf_parser);
                } else
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_new_parser returned NULL");
                librdf_free_model(rdf_model);
            } else
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_new_model returned NULL");
            librdf_free_storage(rdf_storage);
        } else
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_new_storage returned NULL");
        librdf_free_world(rdf_world);
    } else
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_new_world returned NULL");

    return ret;
}
Exemple #7
0
int
main(int argc, char *argv[]) 
{
  const char *program=librdf_basename((const char*)argv[0]);
  const char *test_serializer_types[]={"rdfxml", "ntriples", NULL};
  int i;
  const char *type;
  unsigned char *string;
  size_t string_length;
  librdf_world *world;
  librdf_storage *storage;
  librdf_model* model;
  librdf_uri* base_uri;
  librdf_statement* statement;
  librdf_serializer* serializer;
  librdf_parser* parser;
  librdf_stream* stream;
  FILE *fh;
  struct stat st_buf;

  world=librdf_new_world();
  librdf_world_open(world);

  librdf_world_set_logger(world, &LogData, log_handler);

  for(i=0; (type=test_serializer_types[i]); i++) {
    fprintf(stderr, "%s: Trying to create new %s serializer\n", program, type);
    serializer=librdf_new_serializer(world, type, NULL, NULL);
    if(!serializer) {
      fprintf(stderr, "%s: Failed to create new serializer type %s\n", program, type);
      continue;
    }
    
    fprintf(stderr, "%s: Freeing serializer\n", program);
    librdf_free_serializer(serializer);
  }
  

  storage=librdf_new_storage(world, NULL, NULL, NULL);
  model=librdf_new_model(world, storage, NULL);

  /* ERROR: Subject URI is bad UTF-8 */
  statement=librdf_new_statement_from_nodes(world,
    librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo\xfc"),
    librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/bar"),
    librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0));

  librdf_model_add_statement(model, statement);
  librdf_free_statement(statement);

  /* ERROR: Predicate URI is not serializable */
  statement=librdf_new_statement_from_nodes(world,
    librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"),
    librdf_new_node_from_uri_string(world, (const unsigned char*)"http://bad.example.org/"),
    librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0));

  librdf_model_add_statement(model, statement);
  librdf_free_statement(statement);

  /* ERROR: Object literal is bad UTF-8 */
  statement=librdf_new_statement_from_nodes(world,
    librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"),
    librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/abc"),
    librdf_new_node_from_literal(world, (const unsigned char*)"\xfc", NULL, 0));

  librdf_model_add_statement(model, statement);
  librdf_free_statement(statement);

  serializer=librdf_new_serializer(world, "rdfxml", NULL, NULL);
  base_uri=librdf_new_uri(world, (const unsigned char*)"http://example.org/base#");

  string=librdf_serializer_serialize_model_to_counted_string(serializer,
                                                             base_uri, model,
                                                             &string_length);
#define EXPECTED_BAD_STRING_LENGTH 382
  if(string_length != EXPECTED_BAD_STRING_LENGTH) {
    fprintf(stderr, "%s: Serialising model to RDF/XML returned string '%s' size %d, expected %d\n", program, string,
            (int)string_length, EXPECTED_BAD_STRING_LENGTH);
    return 1;
  }

  if(string)
    free(string);

  librdf_free_uri(base_uri); base_uri=NULL;
  librdf_free_model(model); model=NULL;
  librdf_free_storage(storage); storage=NULL;
  

  if(LogData.errors != EXPECTED_ERRORS) {
    fprintf(stderr, "%s: Serialising to RDF/XML returned %d errors, expected %d\n", program,
            LogData.errors, EXPECTED_ERRORS);
    return 1;
  }

  if(LogData.warnings != EXPECTED_WARNINGS) {
    fprintf(stderr, "%s: Serialising to RDF/XML returned %d warnings, expected %d\n", program,
            LogData.warnings, EXPECTED_WARNINGS);
    return 1;
  }
  

  /* Good model to serialize */
  storage=librdf_new_storage(world, NULL, NULL, NULL);
  model=librdf_new_model(world, storage, NULL);

  parser=librdf_new_parser(world, SYNTAX_TYPE, NULL, NULL);
  if(!parser) {
    fprintf(stderr, "%s: Failed to create new parser type %s\n", program, 
            SYNTAX_TYPE);
    return 1;
  }

  fprintf(stderr, "%s: Adding %s string content\n", program, SYNTAX_TYPE);
  if(librdf_parser_parse_string_into_model(parser, 
                                           (const unsigned char*)SYNTAX_CONTENT,
                                           NULL /* no base URI*/, 
                                           model)) {
    fprintf(stderr, "%s: Failed to parse RDF from %s string into model\n", 
            SYNTAX_TYPE, program);
    return 1;
  }
  librdf_free_parser(parser);
  

  fprintf(stderr, "%s: Serializing stream to a string\n", program);

  stream=librdf_model_as_stream(model);
  string_length=0;
  string=librdf_serializer_serialize_stream_to_counted_string(serializer,
                                                              NULL, stream,
                                                              &string_length);
#define EXPECTED_GOOD_STRING_LENGTH 668
  if(string_length != EXPECTED_GOOD_STRING_LENGTH) {
    fprintf(stderr, "%s: Serialising stream to RDF/XML returned string '%s' size %d, expected %d\n", program, string,
            (int)string_length, EXPECTED_GOOD_STRING_LENGTH);
    return 1;
  }
  librdf_free_stream(stream);

  if(string)
    free(string);


  fprintf(stderr, "%s: Serializing stream to a file handle\n", program);

  stream=librdf_model_as_stream(model);

#define FILENAME "test.rdf"
  fh=fopen(FILENAME, "w");
  if(!fh) {
    fprintf(stderr, "%s: Failed to fopen for writing '%s' - %s\n",
            program, FILENAME, strerror(errno));
    return 1;
  }
  librdf_serializer_serialize_stream_to_file_handle(serializer, fh, NULL, 
                                                    stream);
  fclose(fh);
  stat(FILENAME, &st_buf);
  
  if((int)st_buf.st_size != EXPECTED_GOOD_STRING_LENGTH) {
    fprintf(stderr, "%s: Serialising stream to file handle returned file '%s' of size %d bytes, expected %d\n", program, FILENAME, (int)st_buf.st_size, 
            EXPECTED_GOOD_STRING_LENGTH);
    return 1;
  }
  unlink(FILENAME);
  
  librdf_free_stream(stream);


  librdf_free_serializer(serializer); serializer=NULL;
  librdf_free_model(model); model=NULL;
  librdf_free_storage(storage); storage=NULL;


  librdf_free_world(world);
  
  /* keep gcc -Wall happy */
  return(0);
}