Esempio n. 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);
  }
Esempio n. 2
0
File: RDF.c Progetto: Webo/DEFOREST
/**
 * @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; 
}
Esempio n. 3
0
File: graph.c Progetto: ged/redleaf
/*
 * 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 );
}
Esempio n. 4
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;
}
Esempio n. 5
0
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;
}