Exemple #1
0
static librdf_node* sd_get_endpoint_node(const char * request_url_str)
{
  librdf_uri *request_uri = NULL, *endpoint_uri = NULL;
  librdf_node *endpoint_node = NULL;

  request_uri = librdf_new_uri(world, (unsigned char*)request_url_str);
  if (!request_uri) {
    redstore_error("Failed to create request_uri");
    goto CLEANUP;
  }

  endpoint_uri = librdf_new_uri_relative_to_base(request_uri, (unsigned char*)"sparql");
  if (!endpoint_uri) {
    redstore_error("Failed to create endpoint_uri");
    goto CLEANUP;
  }

  endpoint_node = librdf_new_node_from_uri(world, endpoint_uri);
  if (!endpoint_uri) {
    redstore_error("Failed to create endpoint_node");
    goto CLEANUP;
  }

CLEANUP:
  if (request_uri)
    librdf_free_uri(request_uri);
  if (endpoint_uri)
    librdf_free_uri(endpoint_uri);

  return endpoint_node;
}
Exemple #2
0
/**
 * librdf_finish_concepts:
 * @world: redland world object
 *
 * INTERNAL - Terminate the concepts module.
 *
 **/
void
librdf_finish_concepts(librdf_world *world)
{
    int i;

    /* Free resources and set pointers to NULL so that they are cleared
     * in case the concepts module is initialised again in the same process. */

    if(world->concept_ms_namespace_uri) {
        librdf_free_uri(world->concept_ms_namespace_uri);
        world->concept_ms_namespace_uri=NULL;
    }

    if(world->concept_schema_namespace_uri) {
        librdf_free_uri(world->concept_schema_namespace_uri);
        world->concept_schema_namespace_uri=NULL;
    }

    if(world->concept_resources) {
        for (i=0; i<= LIBRDF_CONCEPT_LAST; i++) {
            /* deletes associated URI too */
            if(world->concept_resources[i])
                librdf_free_node(world->concept_resources[i]);
        }
        LIBRDF_FREE(ptrarray, world->concept_resources);
        world->concept_resources=NULL;
    }

    if(world->concept_uris) {
        /* uris were freed above, now just free the array */
        LIBRDF_FREE(ptrarray, world->concept_uris);
        world->concept_uris=NULL;
    }
}
UT_Error IE_Imp_OpenDocument::_loadRDFFromFile ( GsfInput* pInput,
                                                 const char * pStream,
                                                 RDFArguments* args )
{
    UT_return_val_if_fail(pInput, UT_ERROR);
#ifndef WITH_REDLAND
    UT_UNUSED(pStream);
    UT_UNUSED(args);

    return UT_OK;
#else
    
    int sz = gsf_input_size (pInput);
    if (sz > 0)
    {
        // I would have liked to pass 0 to input_read() and
        // get a shared buffer back, but doing so seems to
        // return a non-null terminated buffer, so we make a
        // smart_ptr to an array an explicitly nul-terminate it.
        boost::shared_array<char> data( new char[sz+1] );
        data[sz] = '\0';
        gsf_input_read ( pInput, sz, (guint8*)data.get() );
        if( sz && !data )
        {
            return UT_ERROR;
        }

        // Note that although the API docs say you can use NULL for base_uri
        // you will likely find it an error to try to call that way.
        librdf_uri* base_uri = librdf_new_uri( args->world,
                                               (const unsigned char*)pStream );
        if( !base_uri )
        {
            UT_DEBUGMSG(("Failed to create a base URI to parse RDF into model. stream:%s sz:%d\n",
                         pStream, sz ));
            return UT_ERROR;
        }

        UT_DEBUGMSG(("_handleRDFStreams() stream:%s RDF/XML:::%s:::\n", pStream, data.get() ));
        if( librdf_parser_parse_string_into_model( args->parser,
                                                   (const unsigned char*)data.get(),
                                                   base_uri, args->model ))
        {
            UT_DEBUGMSG(("Failed to parse RDF into model. stream:%s sz:%d\n",
                         pStream, sz ));
            librdf_free_uri( base_uri );
            return UT_ERROR;
        }
        librdf_free_uri( base_uri );
    }

    return UT_OK;
#endif
}
Exemple #4
0
void description_free()
{
  if (format_ns_uri)
    librdf_free_uri(format_ns_uri);

  if (sd_ns_uri)
    librdf_free_uri(sd_ns_uri);

  if (void_ns_uri)
    librdf_free_uri(void_ns_uri);
}
Exemple #5
0
/**
 * librdf_world_set_feature:
 * @world: #librdf_world object
 * @feature: #librdf_uri feature property
 * @value: #librdf_node feature property value
 *
 * Set the value of a world feature.
 * 
 * Return value: non 0 on failure (negative if no such feature)
 **/
int
librdf_world_set_feature(librdf_world* world, librdf_uri* feature,
                         librdf_node* value) 
{
  librdf_uri* genid_base;
  librdf_uri* genid_counter;
  int rc= -1;

  genid_counter = librdf_new_uri(world,
                                 (const unsigned char*)LIBRDF_WORLD_FEATURE_GENID_COUNTER);
  genid_base = librdf_new_uri(world,
                              (const unsigned char*)LIBRDF_WORLD_FEATURE_GENID_BASE);

  if(librdf_uri_equals(feature, genid_base)) {
    if(!librdf_node_is_resource(value))
      rc=1;
    else {
      int i = atoi((const char*)librdf_node_get_literal_value(value));
      if(i < 1)
        i = 1;

#ifdef WITH_THREADS
      pthread_mutex_lock(world->mutex);
#endif
      world->genid_base = 1;
#ifdef WITH_THREADS
      pthread_mutex_unlock(world->mutex);
#endif
      rc = 0;
    }
  } else if(librdf_uri_equals(feature, genid_counter)) {
    if(!librdf_node_is_resource(value))
      rc = 1;
    else {
      int i = atoi((const char*)librdf_node_get_literal_value(value));
      if(i < 1)
        i = 1;

#ifdef WITH_THREADS
      pthread_mutex_lock(world->mutex);
#endif
      world->genid_counter = 1;
#ifdef WITH_THREADS
      pthread_mutex_unlock(world->mutex);
#endif
      rc = 0;
    }
  }

  librdf_free_uri(genid_base);
  librdf_free_uri(genid_counter);

  return rc;
}
Exemple #6
0
/**
 * librdf_finish_concepts - Terminate the librdf_concepts module
 * @world: redland world object
 **/
void
librdf_finish_concepts(librdf_world *world)
{
  int i;

  if(librdf_concept_ms_namespace_uri)
    librdf_free_uri(librdf_concept_ms_namespace_uri);
  if(librdf_concept_schema_namespace_uri)
    librdf_free_uri(librdf_concept_schema_namespace_uri);

  for (i=0; i< LIBRDF_CONCEPT_LAST; i++)
    /* deleted associated URI too */
    librdf_free_node(librdf_concept_resources[i]);
}
Exemple #7
0
static void
librdf_storage_file_terminate(librdf_storage* storage)
{
    librdf_storage_file_instance* context=(librdf_storage_file_instance*)storage->instance;

    if (context == NULL)
        return;

    librdf_storage_file_sync(storage);

    if(context->format_name)
        LIBRDF_FREE(cstring, context->format_name);

    if(context->name)
        LIBRDF_FREE(cstring, context->name);

    if(context->uri)
        librdf_free_uri(context->uri);

    if(context->model)
        librdf_free_model(context->model);

    if(context->storage)
        librdf_free_storage(context->storage);

    LIBRDF_FREE(librdf_storage_file_instance, context);
}
Exemple #8
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);
  }
Exemple #9
0
/* helper functions */
static void
librdf_free_serializer_factory(librdf_serializer_factory *factory) 
{
  if(factory->name)
    LIBRDF_FREE(cstring, factory->name);
  if(factory->label)
    LIBRDF_FREE(cstring, factory->label);
  if(factory->mime_type)
    LIBRDF_FREE(cstring, factory->mime_type);
  if(factory->type_uri)
    librdf_free_uri(factory->type_uri);
  LIBRDF_FREE(librdf_serializer_factory, factory);
}
Exemple #10
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 #11
0
static void
librdf_query_rasqal_terminate(librdf_query* query)
{
  librdf_query_rasqal_context *context=(librdf_query_rasqal_context*)query->context;

  if(context->rq)
    rasqal_free_query(context->rq);

  if(context->query_string)
    LIBRDF_FREE(char*, context->query_string);

  if(context->uri)
    librdf_free_uri(context->uri);

  if(context->model)
    librdf_free_model(context->model);
}
Exemple #12
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 #13
0
int
main(int argc, char *argv[]) 
{
  librdf_world* world;
  librdf_storage *storage;
  librdf_model* model;
  librdf_uri* uri;
  unsigned char *string;
  
  librdf_world_open(world=librdf_new_world());

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

  uri=librdf_new_uri(world, (const unsigned char*)"http://planetrdf.com/index.rdf");
  librdf_model_load(model, uri, NULL, NULL, NULL);

  string=librdf_model_to_string(model, uri, "ntriples", NULL, NULL);
  if(!string)
    printf("Failed to serialize model\n");
  else {
    printf("Made a %d byte string\n", (int)strlen((char*)string));
    free(string);
  }

  librdf_free_uri(uri);
  
  librdf_free_model(model);
  librdf_free_storage(storage);

  librdf_free_world(world);

#ifdef LIBRDF_MEMORY_DEBUG
  librdf_memory_report(stderr);
#endif
	
  /* keep gcc -Wall happy */
  return(0);
}
Exemple #14
0
static librdf_node *new_node_from_integer(librdf_world * world, int i)
{
  librdf_uri *xsd_integer_uri = NULL;
  librdf_node *node = NULL;
  unsigned char *string = NULL;

#define INTEGER_BUFFER_SIZE 20
  string = (unsigned char *) malloc(INTEGER_BUFFER_SIZE + 1);
  if (!string)
    return NULL;

  // snprintf() takes as length the buffer size including NULL
  snprintf((char *) string, INTEGER_BUFFER_SIZE + 1, "%d", i);

  xsd_integer_uri =
      librdf_new_uri(world, (unsigned char *) "http://www.w3.org/2001/XMLSchema#integer");

  node = librdf_new_node_from_typed_literal(world, string, NULL, xsd_integer_uri);

  if (xsd_integer_uri)
    librdf_free_uri(xsd_integer_uri);

  return node;
}
Exemple #15
0
/*
 * call-seq:
 *    graph.execute_query( qstring, language=:sparql, limit=nil, offset=nil ) -> queryresult
 *
 * Run the query in the given query string (+qstring+) against the graph. The query +language+
 * specifies the query language, and +limit+, and +offset+ can be used to limit the results. The
 * #query method is the public interface to this method.
 *
 */
static VALUE
rleaf_redleaf_graph_execute_query( int argc, VALUE *argv, VALUE self ) {
	rleaf_GRAPH *ptr = rleaf_get_graph( self );

	VALUE qstring, language, limit, offset, base;
	const char *qlang_name = NULL;
	librdf_uri *qlang_uri = NULL, *base_uri = NULL;
	librdf_query *query;
	librdf_query_results *res;

	rb_scan_args( argc, argv, "14", &qstring, &language, &limit, &offset, &base );
	rleaf_log_with_context(
		self,
		"debug",
		"Executing query=%s (%s) with offset=%s, limit=%s, and base=%s",
		RSTRING_PTR(rb_inspect( qstring )),
		RSTRING_PTR(rb_inspect( language )),
		RSTRING_PTR(rb_inspect( limit )),
		RSTRING_PTR(rb_inspect( offset )),
		RSTRING_PTR(rb_inspect( base ))
	  );

	/* Set the query language, from a URI or a language name string */
	if ( RTEST(language) && IsURI(language) )
		qlang_uri = rleaf_object_to_librdf_uri( language );

	else if ( language != Qnil ) {
		VALUE langstring = rb_obj_as_string( language );
		qlang_name = (const char *)(RSTRING_PTR(langstring));
	}

	/* Set the baseuri if one is specified */
	if ( RTEST(base) ) {
		VALUE basestr = rb_obj_as_string( base );
		base_uri = librdf_new_uri( rleaf_rdf_world, (const unsigned char *)(RSTRING_PTR(basestr)) );
		if ( !base_uri ) {
			if ( qlang_uri ) librdf_free_uri( qlang_uri );
			rb_raise( rleaf_eRedleafError, "Couldn't make a librdf_uri out of %s",
				RSTRING_PTR(basestr) );
		}
	}

	/* Make the query object */
	rleaf_log_with_context( self, "debug", "  creating a new '%s' query: %s",
		qlang_name, RSTRING_PTR(qstring) );
	query = librdf_new_query( rleaf_rdf_world, qlang_name, qlang_uri,
		(unsigned char *)(StringValuePtr(qstring)), base_uri );
	if ( !query ) {
		if ( qlang_uri ) librdf_free_uri( qlang_uri );
		if ( base_uri ) librdf_free_uri( base_uri );
		rb_raise( rleaf_eRedleafError, "Failed to create query %s", RSTRING_PTR(qstring) );
	}

	/* Check for a non-nil limit and offset, setting them in the query object if they exist. */
	if ( RTEST(limit) ) {
		rleaf_log_with_context( self, "debug", "  setting limit to %d", FIX2INT(limit) );
		librdf_query_set_limit( query, FIX2INT(limit) );
	}
	if ( RTEST(offset) ) {
		rleaf_log_with_context( self, "debug", "  setting offset to %d", FIX2INT(offset) );
		librdf_query_set_offset( query, FIX2INT(offset) );
	}

	/* Run the query against the model */
	rleaf_log_with_context( self, "debug", "  executing query <%p> against model <%p>", query, ptr->model );
	res = librdf_model_query_execute( ptr->model, query );

	if ( qlang_uri ) librdf_free_uri( qlang_uri );
	if ( base_uri ) librdf_free_uri( base_uri );

	if ( !res )
		rb_raise( rleaf_eRedleafError, "Execution of query failed." );

	rleaf_log_with_context( self, "debug", "  creating result" );
	return rleaf_new_queryresult( self, res );
}
Exemple #16
0
int
main(int argc, char *argv[]) 
{
  const unsigned char *hp_string=(const unsigned char*)"http://purl.org/net/dajobe/";
  librdf_uri *uri1, *uri2, *uri3, *uri4, *uri5, *uri6, *uri7, *uri8, *uri9;
  librdf_digest *d;
  const char *program=librdf_basename((const char*)argv[0]);
  const char *file_string="/big/long/directory/file";
  const unsigned char *file_uri_string=(const unsigned char*)"file:///big/long/directory/file";
  const unsigned char *uri_string=(const unsigned char*)"http://example.com/big/long/directory/blah#frag";
  const unsigned char *relative_uri_string1=(const unsigned char*)"#foo";
  const unsigned char *relative_uri_string2=(const unsigned char*)"bar";
  librdf_world *world;
  
  world=librdf_new_world();
  librdf_world_open(world);

  fprintf(stderr, "%s: Creating new URI from string\n", program);
  uri1=librdf_new_uri(world, hp_string);
  if(!uri1) {
    fprintf(stderr, "%s: Failed to create URI from string '%s'\n", program, 
	    hp_string);
    return(1);
  }
  
  fprintf(stderr, "%s: Home page URI is ", program);
  librdf_uri_print(uri1, stderr);
  fputs("\n", stderr);
  
  fprintf(stderr, "%s: Creating URI from URI\n", program);
  uri2=librdf_new_uri_from_uri(uri1);
  if(!uri2) {
    fprintf(stderr, "%s: Failed to create new URI from old one\n", program);
    return(1);
  }

  fprintf(stderr, "%s: New URI is ", program);
  librdf_uri_print(uri2, stderr);
  fputs("\n", stderr);

  
  fprintf(stderr, "%s: Getting digest for URI\n", program);
  d = librdf_uri_get_digest(world, uri2);
  if(!d) {
    fprintf(stderr, "%s: Failed to get digest for URI %s\n", program, 
	    librdf_uri_as_string(uri2));
    return(1);
  }
  fprintf(stderr, "%s: Digest is: ", program);
  librdf_digest_print(d, stderr);
  fputs("\n", stderr);
  librdf_free_digest(d);

  uri3=librdf_new_uri(world, (const unsigned char*)"file:/big/long/directory/");
  uri4=librdf_new_uri(world, (const unsigned char*)"http://somewhere/dir/");
  fprintf(stderr, "%s: Source URI is ", program);
  librdf_uri_print(uri3, stderr);
  fputs("\n", stderr);
  fprintf(stderr, "%s: Base URI is ", program);
  librdf_uri_print(uri4, stderr);
  fputs("\n", stderr);
  fprintf(stderr, "%s: URI string is '%s'\n", program, uri_string);

  uri5=librdf_new_uri_normalised_to_base(uri_string, uri3, uri4);
  fprintf(stderr, "%s: Normalised URI is ", program);
  librdf_uri_print(uri5, stderr);
  fputs("\n", stderr);


  uri6=librdf_new_uri_relative_to_base(uri5, relative_uri_string1);
  fprintf(stderr, "%s: URI + Relative URI %s gives ", program, 
          relative_uri_string1);
  librdf_uri_print(uri6, stderr);
  fputs("\n", stderr);

  uri7=librdf_new_uri_relative_to_base(uri5, relative_uri_string2);
  fprintf(stderr, "%s: URI + Relative URI %s gives ", program, 
          relative_uri_string2);
  librdf_uri_print(uri7, stderr);
  fputs("\n", stderr);

  uri8=librdf_new_uri_from_filename(world, file_string);
  uri9=librdf_new_uri(world, file_uri_string);
  if(!librdf_uri_equals(uri8, uri9)) {
    fprintf(stderr, "%s: URI string from filename %s returned %s, expected %s\n", program, file_string, librdf_uri_as_string(uri8), file_uri_string);
    return(1);
  }

  fprintf(stderr, "%s: Freeing URIs\n", program);
  librdf_free_uri(uri1);
  librdf_free_uri(uri2);
  librdf_free_uri(uri3);
  librdf_free_uri(uri4);
  librdf_free_uri(uri5);
  librdf_free_uri(uri6);
  librdf_free_uri(uri7);
  librdf_free_uri(uri8);
  librdf_free_uri(uri9);
  
  librdf_free_world(world);

  /* keep gcc -Wall happy */
  return(0);
}
void run_query2(librdf_world* world, librdf_model* model, const std::string& q)
{

    librdf_stream* strm;

    std::cout << "** Query: " << q << std::endl;

    librdf_uri* uri1 =
	librdf_new_uri(world, (const unsigned char*) "http://bunchy.org");

    if (uri1 == 0)
	throw std::runtime_error("Couldn't parse URI");
    
    librdf_uri* uri2 =
	librdf_new_uri(world, (const unsigned char*) "http://bunchy.org");
    if (uri2 == 0)
	throw std::runtime_error("Couldn't parse URI");
    
    librdf_query* qry =
	librdf_new_query(world, "sparql", uri1,
			 (const unsigned char*) q.c_str(),
			 uri2);
    
    librdf_free_uri(uri1);
    librdf_free_uri(uri2);
    
    if (qry == 0)
	throw std::runtime_error("Couldn't parse query.");
    
    librdf_query_results* results = librdf_query_execute(qry, model);
    if (results == 0)
	throw std::runtime_error("Couldn't execute query");
    
    librdf_free_query(qry);

    std::cout << "--------------------------------------------------------"
	      << std::endl;

    while (true) {
      
	if (librdf_query_results_finished(results)) break;
      
	int bcount = librdf_query_results_get_bindings_count(results);
	if (bcount < 0)
	    throw std::runtime_error("Couldn't get query bindings count");
      
	for(int i = 0; i < bcount; i++) {
	
	    if (i != 0) std::cout << " ";
	
	    librdf_node* value =
		librdf_query_results_get_binding_value(results, i);
	
	    if (value == 0)
		throw std::runtime_error("Couldn't get results binding");
	
	    output_node(value);

	    librdf_free_node(value);

	}

	std::cout << std::endl;

	librdf_query_results_next(results);

    }

    std::cout << "--------------------------------------------------------"
	      << std::endl;

    librdf_free_query_results(results);
    
}
Exemple #18
0
int
main(int argc, char *argv[]) 
{
  librdf_world *world;
  librdf_uri* prefix_uri;
  librdf_node* nodes[ITERATOR_NODES_COUNT];
  int i;
  librdf_iterator* iterator;
  int count;
  
  char *program=argv[0];
	
  world=librdf_new_world();
  librdf_world_init_mutex(world);

  librdf_init_hash(world);
  librdf_init_uri(world);
  librdf_init_node(world);

  prefix_uri=librdf_new_uri(world, (const unsigned char*)NODE_URI_PREFIX);
  if(!prefix_uri) {
    fprintf(stderr, "%s: Failed to create prefix URI\n", program);
    return(1);
  }

  for(i=0; i < ITERATOR_NODES_COUNT; i++) {
    unsigned char buf[2];
    buf[0]='a'+i;
    buf[1]='\0';
    nodes[i]=librdf_new_node_from_uri_local_name(world, prefix_uri, buf);
    if(!nodes[i]) {
      fprintf(stderr, "%s: Failed to create node %i (%s)\n", program, i, buf);
      return(1);
    }
  }
  
  fprintf(stdout, "%s: Creating static node iterator\n", program);
  iterator=librdf_node_static_iterator_create(nodes, ITERATOR_NODES_COUNT);
  if(!iterator) {
    fprintf(stderr, "%s: Failed to createstatic  node iterator\n", program);
    return(1);
  }
  
  fprintf(stdout, "%s: Listing static node iterator\n", program);
  count=0;
  while(!librdf_iterator_end(iterator)) {
    librdf_node* i_node=(librdf_node*)librdf_iterator_get_object(iterator);
    if(!i_node) {
      fprintf(stderr, "%s: librdf_iterator_current return NULL when not end o fiterator\n", program);
      return(1);
    }

    fprintf(stdout, "%s: node %d is: ", program, count);
    librdf_node_print(i_node, stdout);
    fputc('\n', stdout);

    if(!librdf_node_equals(i_node, nodes[count])) {
      fprintf(stderr, "%s: static node iterator node %i returned unexpected node\n", program, count);
      librdf_node_print(i_node, stderr);
      fputs(" rather than ", stdout);
      librdf_node_print(nodes[count], stderr);
      fputc('\n', stdout);
      return(1);
    }
    
    librdf_iterator_next(iterator);
    count++;
  }

  librdf_free_iterator(iterator);

  if(count != ITERATOR_NODES_COUNT) {
    fprintf(stderr, "%s: Iterator returned %d nodes, expected %d\n", program,
            count, ITERATOR_NODES_COUNT);
    return(1);
  }

  fprintf(stdout, "%s: Static node iterator worked ok\n", program);


  fprintf(stdout, "%s: Freeing nodes\n", program);
  for (i=0; i<ITERATOR_NODES_COUNT; i++) {
    librdf_free_node(nodes[i]);
  }

  librdf_free_uri(prefix_uri);
  
  librdf_finish_node(world);
  librdf_finish_uri(world);
  librdf_finish_hash(world);

  LIBRDF_FREE(librdf_world, world);

  /* keep gcc -Wall happy */
  return(0);
}
Exemple #19
0
int
main(int argc, char *argv[]) 
{
  librdf_statement *statement;
  librdf_stream* stream;
  const char *program=librdf_basename((const char*)argv[0]);
  librdf_world *world;
  librdf_uri* prefix_uri;
  librdf_node* nodes[STREAM_NODES_COUNT];
  int i;
  librdf_iterator* iterator;
  int count;
  
  world=librdf_new_world();
  librdf_world_open(world);

  prefix_uri=librdf_new_uri(world, (const unsigned char*)NODE_URI_PREFIX);
  if(!prefix_uri) {
    fprintf(stderr, "%s: Failed to create prefix URI\n", program);
    return(1);
  }

  for(i=0; i < STREAM_NODES_COUNT; i++) {
    unsigned char buf[2];
    buf[0]='a'+i;
    buf[1]='\0';
    nodes[i]=librdf_new_node_from_uri_local_name(world, prefix_uri, buf);
    if(!nodes[i]) {
      fprintf(stderr, "%s: Failed to create node %i (%s)\n", program, i, buf);
      return(1);
    }
  }
  
  fprintf(stdout, "%s: Creating static node iterator\n", program);
  iterator = librdf_node_new_static_node_iterator(world, nodes, STREAM_NODES_COUNT);
  if(!iterator) {
    fprintf(stderr, "%s: Failed to create static node iterator\n", program);
    return(1);
  }

  statement=librdf_new_statement_from_nodes(world,
                                            librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/resource"),
                                            librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/property"),
                                            NULL);
  if(!statement) {
    fprintf(stderr, "%s: Failed to create statement\n", program);
    return(1);
  }

  fprintf(stdout, "%s: Creating stream from node iterator\n", program);
  stream=librdf_new_stream_from_node_iterator(iterator, statement, LIBRDF_STATEMENT_OBJECT);
  if(!stream) {
    fprintf(stderr, "%s: Failed to createstatic  node stream\n", program);
    return(1);
  }
  

  /* This is to check that the stream_from_node_iterator code
   * *really* takes a copy of what it needs from statement 
   */
  fprintf(stdout, "%s: Freeing statement\n", program);
  librdf_free_statement(statement);


  fprintf(stdout, "%s: Listing static node stream\n", program);
  count=0;
  while(!librdf_stream_end(stream)) {
    librdf_statement* s_statement=librdf_stream_get_object(stream);
    if(!s_statement) {
      fprintf(stderr, "%s: librdf_stream_current returned NULL when not end of stream\n", program);
      return(1);
    }

    fprintf(stdout, "%s: statement %d is: ", program, count);
    librdf_statement_print(s_statement, stdout);
    fputc('\n', stdout);
    
    librdf_stream_next(stream);
    count++;
  }

  if(count != STREAM_NODES_COUNT) {
    fprintf(stderr, "%s: Stream returned %d statements, expected %d\n", program,
            count, STREAM_NODES_COUNT);
    return(1);
  }

  fprintf(stdout, "%s: stream from node iterator worked ok\n", program);


  fprintf(stdout, "%s: Freeing stream\n", program);
  librdf_free_stream(stream);


  fprintf(stdout, "%s: Freeing nodes\n", program);
  for (i=0; i<STREAM_NODES_COUNT; i++) {
    librdf_free_node(nodes[i]);
  }

  librdf_free_uri(prefix_uri);
  
  librdf_free_world(world);
  
  /* keep gcc -Wall happy */
  return(0);
}
void run_query(librdf_world* world, librdf_model* model, const std::string& q)
{
    librdf_stream* strm;

    std::cout << "** Query: " << q << std::endl;

    librdf_uri* uri1 =
	librdf_new_uri(world, (const unsigned char*) "http://bunchy.org");

    if (uri1 == 0)
	throw std::runtime_error("Couldn't parse URI");
    
    librdf_uri* uri2 =
	librdf_new_uri(world, (const unsigned char*) "http://bunchy.org");
    if (uri2 == 0)
	throw std::runtime_error("Couldn't parse URI");
    
    librdf_query* qry =
	librdf_new_query(world, "sparql", uri1,
			 (const unsigned char*) q.c_str(),
			 uri2);
    
    librdf_free_uri(uri1);
    librdf_free_uri(uri2);
    
    if (qry == 0)
	throw std::runtime_error("Couldn't parse query.");
    
    librdf_query_results* results = librdf_query_execute(qry, model);
    if (results == 0)
	throw std::runtime_error("Couldn't execute query");
    
    librdf_free_query(qry);
    
    strm = librdf_query_results_as_stream(results);
    if (strm == 0)
	throw std::runtime_error("Couldn't stream results");
    
    librdf_serializer* srl = librdf_new_serializer(world, "ntriples",
						   0, 0);
    if (srl == 0)
	throw std::runtime_error("Couldn't create serialiser");
    
    size_t len;
    
    unsigned char* out =
	librdf_serializer_serialize_stream_to_counted_string(srl, 0,
							     strm, &len);
    
    std::cout << "--------------------------------------------------------"
	      << std::endl;
    write(1, out, len);
    std::cout << "--------------------------------------------------------"
	      << std::endl;
    
    free(out);
    
    librdf_free_serializer(srl);

    librdf_free_stream(strm);
    
    librdf_free_query_results(results);
    
}
Exemple #21
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);
}
static
librdf_node* node_constructor_helper(librdf_world* world, const char* t,
				     size_t len)
{

    librdf_node* o;

    if ((strlen(t) < 2) || (t[1] != ':')) {
	fprintf(stderr, "node_constructor_helper called on invalid term\n");
	return 0;
    }

    if (t[0] == 'u') {
 	o = librdf_new_node_from_counted_uri_string(world,
						    (unsigned char*) t + 2,
						    len - 2);
	return o;
    }

    if (t[0] == 's') {
	o = librdf_new_node_from_typed_counted_literal(world,
						       (unsigned char*) t + 2,
						       len - 2, 0, 0, 0);
	return o;
    }


    if (t[0] == 'i') {
	librdf_uri* dt =
	    librdf_new_uri(world,
			   "http://www.w3.org/2001/XMLSchema#integer");
	if (dt == 0)
	    return 0;

	o = librdf_new_node_from_typed_counted_literal(world, t + 2, len - 2,
						       0, 0, dt);
	librdf_free_uri(dt);
	return o;
    }
    
    if (t[0] == 'f') {
	librdf_uri* dt =
	    librdf_new_uri(world,
			   "http://www.w3.org/2001/XMLSchema#float");
	if (dt == 0)
	    return 0;

	o = librdf_new_node_from_typed_counted_literal(world, t + 2, len - 2,
						       0, 0, dt);
	librdf_free_uri(dt);
	return o;
    }

    if (t[0] == 'd') {
	librdf_uri* dt =
	    librdf_new_uri(world,
			   "http://www.w3.org/2001/XMLSchema#dateTime");
	if (dt == 0)
	    return 0;

	o = librdf_new_node_from_typed_counted_literal(world, t + 2, len - 2,
						       0, 0, dt);
	librdf_free_uri(dt);
	return o;
    }    

    return librdf_new_node_from_typed_counted_literal(world,
						      (unsigned char*) t + 2,
						      len - 2, 0, 0, 0);

}
Exemple #23
0
int
main(int argc, char *argv[])
{
  librdf_world* world;
  librdf_parser* parser;
  librdf_serializer* serializer;
  librdf_storage *storage;
  librdf_model* model;
  librdf_node *source, *arc, *target, *node;
  librdf_node *subject, *predicate, *object;
  librdf_node* context_node=NULL;
  librdf_stream* stream;
  librdf_iterator* iterator;
  librdf_uri *uri;
  librdf_uri *base_uri=NULL;
  librdf_query *query;
  librdf_query_results *results;
  librdf_hash *options;
  int count;
  int rc;
  int transactions=0;
  const char* storage_name;
  const char* storage_options;
  const char* context;
  const char* identifier;
  const char* results_format;
  librdf_statement* statement=NULL;
  char* query_cmd=NULL;
  char* s;


  /*
   * Initialize
   */
  storage_name="virtuoso";
  results_format="xml";
  context=DEFAULT_CONTEXT;
  identifier=DEFAULT_IDENTIFIER;


  /*
   * Get connection options
   */
  if(argc == 2 && argv[1][0] != '\0')
    storage_options=argv[1];
  else if((s=getenv ("VIRTUOSO_STORAGE_OPTIONS")) != NULL)
    storage_options=s;
  else
    storage_options=DEFAULT_STORAGE_OPTIONS;


  world=librdf_new_world();

  librdf_world_set_logger(world, world, log_handler);

  librdf_world_open(world);

  options=librdf_new_hash(world, NULL);
  librdf_hash_open(options, NULL, 0, 1, 1, NULL);

  librdf_hash_put_strings(options, "contexts", "yes");
  transactions=1;

  librdf_hash_from_string(options, storage_options);

  storage=librdf_new_storage_with_options(world, storage_name, identifier, options);

  if(!storage) {
    fprintf(stderr, ": Failed to open %s storage '%s'\n", storage_name, identifier);
    return(1);
  }

  model=librdf_new_model(world, storage, NULL);
  if(!model) {
    fprintf(stderr, ": Failed to create model\n");
    return(1);
  }

  if(transactions)
    librdf_model_transaction_start(model);

  /* Do this or gcc moans */
  stream=NULL;
  iterator=NULL;
  parser=NULL;
  serializer=NULL;
  source=NULL;
  arc=NULL;
  target=NULL;
  subject=NULL;
  predicate=NULL;
  object=NULL;
  uri=NULL;
  node=NULL;
  query=NULL;
  results=NULL;
  context_node=librdf_new_node_from_uri_string(world, (const unsigned char *)context);


  /**** Test 1 *******/
  startTest(1, " Remove all triples in <%s> context\n", context);
  {
    rc=librdf_model_context_remove_statements(model, context_node);

    if(rc)
      endTest(0, " failed to remove context triples from the graph\n");
    else
      endTest(1, " removed context triples from the graph\n");
  }


  /**** Test 2 *******/
  startTest(2, " Add triples to <%s> context\n", context);
  {
    rc=0;
    rc |= add_triple(world, context_node, model, "aa", "bb", "cc");
    rc |= add_triple(world, context_node, model, "aa", "bb1", "cc");
    rc |= add_triple(world, context_node, model, "aa", "a2", "_:cc");
    rc |= add_triple_typed(world, context_node, model, "aa", "a2", "cc");
    rc |= add_triple_typed(world, context_node, model, "mm", "nn", "Some long literal with language@en");
    rc |= add_triple_typed(world, context_node, model, "oo", "pp", "12345^^<http://www.w3.org/2001/XMLSchema#int>");
    if(rc)
      endTest(0, " failed add triple\n");
    else
      endTest(1, " add triple to context\n");
  }


  /**** Test 3 *******/
  startTest(3, " Print all triples in <%s> context\n", context);
  {
    raptor_iostream* iostr = raptor_new_iostream_to_file_handle(librdf_world_get_raptor(world), stdout);
    librdf_model_write(model, iostr);
    raptor_free_iostream(iostr);
    endTest(1, "\n");
  }


  /***** Test 4 *****/
  startTest(4, " Count of triples in <%s> context\n", context);
  {
    count=librdf_model_size(model);
    if(count >= 0)
      endTest(1, " graph has %d triples\n", count);
    else
      endTest(0, " graph has unknown number of triples\n");
  }


  /***** Test 5 *****/
  startTest(5, " Exec:  ARC  aa bb \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb");
    node=librdf_model_get_target(model, subject, predicate);
    librdf_free_node(subject);
    librdf_free_node(predicate);
    if(!node) {
      endTest(0, " Failed to get arc\n");
    } else {
      print_node(stdout, node);
      librdf_free_node(node);
      endTest(1, "\n");
    }
  }


  /***** Test 6 *****/
  startTest(6, " Exec:  ARCS  aa cc \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc");
    iterator=librdf_model_get_arcs(model, subject, object);
    librdf_free_node(subject);
    librdf_free_node(object);
    if(!iterator) {
      endTest(0, " Failed to get arcs\n");
    } else {
      print_nodes(stdout, iterator);
      endTest(1, "\n");
    }
  }


  /***** Test 7 *****/
  startTest(7, " Exec:  ARCS-IN  cc \n");
  {
    object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc");
    iterator=librdf_model_get_arcs_in(model, object);
    librdf_free_node(object);
    if(!iterator) {
      endTest(0, " Failed to get arcs in\n");
    } else {
      int ok=1;
      count=0;
      while(!librdf_iterator_end(iterator)) {
        context_node=(librdf_node*)librdf_iterator_get_context(iterator);
        node=(librdf_node*)librdf_iterator_get_object(iterator); /*returns SHARED pointer */
        if(!node) {
          ok=0;
          endTest(ok, " librdf_iterator_get_next returned NULL\n");
          break;
        }

        fputs("Matched arc: ", stdout);
        librdf_node_print(node, stdout);
        if(context_node) {
          fputs(" with context ", stdout);
          librdf_node_print(context_node, stdout);
        }
        fputc('\n', stdout);

        count++;
        librdf_iterator_next(iterator);
      }
      librdf_free_iterator(iterator);
      endTest(ok, " matching arcs: %d\n", count);
    }
  }


  /***** Test 8 *****/
  startTest(8, " Exec:  ARCS-OUT  aa \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    iterator=librdf_model_get_arcs_out(model, subject);
    librdf_free_node(subject);
    if(!iterator)
      endTest(0, " Failed to get arcs out\n");
    else {
      int ok=1;
      count=0;
      while(!librdf_iterator_end(iterator)) {
        context_node=(librdf_node*)librdf_iterator_get_context(iterator);
        node=(librdf_node*)librdf_iterator_get_object(iterator);
        if(!node) {
          ok=0;
          endTest(ok, " librdf_iterator_get_next returned NULL\n");
          break;
        }

        fputs("Matched arc: ", stdout);
        librdf_node_print(node, stdout);
        if(context_node) {
          fputs(" with context ", stdout);
          librdf_node_print(context_node, stdout);
        }
        fputc('\n', stdout);

        count++;
        librdf_iterator_next(iterator);
      }
      librdf_free_iterator(iterator);
      endTest(ok, " matching arcs: %d\n", count);
    }
  }


  /***** Test 9 *****/
  startTest(9, " Exec:  CONTAINS aa bb1 cc \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb1");
    object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc");
    statement=librdf_new_statement(world);
    librdf_statement_set_subject(statement, subject);
    librdf_statement_set_predicate(statement, predicate);
    librdf_statement_set_object(statement, object);

    if(librdf_model_contains_statement(model, statement))
       endTest(1, " the graph contains the triple\n");
    else
       endTest(0, " the graph does not contain the triple\n");

    librdf_free_statement(statement);
  }


  /***** Test 10 *****/
  startTest(10, " Exec:  FIND aa - - \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    statement=librdf_new_statement(world);
    librdf_statement_set_subject(statement, subject);

    stream=librdf_model_find_statements_in_context(model, statement, context_node);

    if(!stream) {
        endTest(0, " FIND returned no results (NULL stream)\n");
    } else {
        librdf_node* ctxt_node=NULL;
        int ok=1;
        count=0;
        while(!librdf_stream_end(stream)) {
          librdf_statement *stmt=librdf_stream_get_object(stream);
          ctxt_node=(librdf_node*)librdf_stream_get_context(stream);
          if(!stmt) {
              ok=0;
              endTest(ok, " librdf_stream_next returned NULL\n");
              break;
          }

          fputs("Matched triple: ", stdout);
          librdf_statement_print(stmt, stdout);
          if(context) {
              fputs(" with context ", stdout);
              librdf_node_print(ctxt_node, stdout);
          }
          fputc('\n', stdout);

          count++;
          librdf_stream_next(stream);
        }
        librdf_free_stream(stream);
        endTest(ok, " matching triples: %d\n", count);
    }

    librdf_free_statement(statement);
  }


  /***** Test 11 *****/
  startTest(11, " Exec:  HAS-ARC-IN cc bb \n");
  {
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb");
    object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc");

    if(librdf_model_has_arc_in(model, object, predicate))
        endTest(1, " the graph contains the arc\n");
      else
        endTest(0, " the graph does not contain the arc\n");

    librdf_free_node(predicate);
    librdf_free_node(object);
  }


  /***** Test 12 *****/
  startTest(12, " Exec:  HAS-ARC-OUT aa bb \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb");

    if(librdf_model_has_arc_out(model, subject, predicate))
        endTest(1, " the graph contains the arc\n");
      else
        endTest(0, " the graph does not contain the arc\n");

    librdf_free_node(predicate);
    librdf_free_node(subject);
  }


  /***** Test 13 *****/
  startTest(13, " Exec:  SOURCE  aa cc \n");
  {
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb");
    object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc");

    node=librdf_model_get_source(model, predicate, object);
    librdf_free_node(predicate);
    librdf_free_node(object);
    if(!node) {
      endTest(0, " Failed to get source\n");
    } else {
      print_node(stdout, node);
      librdf_free_node(node);
      endTest(1, "\n");
    }
  }


  /***** Test 14 *****/
  startTest(14, " Exec:  SOURCES  bb cc \n");
  {
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb");
    object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc");

    iterator=librdf_model_get_sources(model, predicate, object);
    librdf_free_node(predicate);
    librdf_free_node(object);
    if(!iterator) {
      endTest(0, " Failed to get sources\n");
    } else {
      print_nodes(stdout, iterator);
      endTest(1, "\n");
    }
  }


  /***** Test 15 *****/
  startTest(15, " Exec:  TARGET  aa bb \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb");

    node=librdf_model_get_target(model, subject, predicate);
    librdf_free_node(subject);
    librdf_free_node(predicate);
    if(!node) {
      endTest(0, " Failed to get target\n");
    } else {
      print_node(stdout, node);
      librdf_free_node(node);
      endTest(1, "\n");
    }
  }


  /***** Test 16 *****/
  startTest(16, " Exec:  TARGETS  aa bb \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb");

    iterator=librdf_model_get_targets(model, subject, predicate);
    librdf_free_node(subject);
    librdf_free_node(predicate);
    if(!iterator) {
      endTest(0, " Failed to get targets\n");
    } else {
      print_nodes(stdout, iterator);
      endTest(1, "\n");
    }
  }


  /***** Test 17 *****/
  startTest(17, " Exec:  REMOVE aa bb1 cc \n");
  {
    subject=librdf_new_node_from_uri_string(world, (const unsigned char*)"aa");
    predicate=librdf_new_node_from_uri_string(world, (const unsigned char*)"bb1");
    object=librdf_new_node_from_uri_string(world, (const unsigned char*)"cc");
    statement=librdf_new_statement(world);
    librdf_statement_set_subject(statement, subject);
    librdf_statement_set_predicate(statement, predicate);
    librdf_statement_set_object(statement, object);

    if(librdf_model_context_remove_statement(model, context_node, statement))
       endTest(0, " failed to remove triple from the graph\n");
    else
       endTest(1, " removed triple from the graph\n");

    librdf_free_statement(statement);
  }


  /***** Test 18 *****/
  query_cmd=(char *)"CONSTRUCT {?s ?p ?o} FROM <http://red> WHERE {?s ?p ?o}";
  startTest(18, " Exec:  QUERY \"%s\" \n", query_cmd);
  {
    query=librdf_new_query(world, (char *)"vsparql", NULL, (const unsigned char *)query_cmd, NULL);

    if(!(results=librdf_model_query_execute(model, query))) {
      endTest(0, " Query of model with '%s' failed\n", query_cmd);
      librdf_free_query(query);
      query=NULL;
    } else {
      stream=librdf_query_results_as_stream(results);

      if(!stream) {
        endTest(0, " QUERY returned no results (NULL stream)\n");
      } else {
        librdf_node* ctxt=NULL;
        count=0;
        while(!librdf_stream_end(stream)) {
          librdf_statement *stmt=librdf_stream_get_object(stream);  /*returns SHARED pointer */
          ctxt=(librdf_node*)librdf_stream_get_context(stream);
          if(!stmt) {
              endTest(0, " librdf_stream_next returned NULL\n");
              break;
          }

          fputs("Matched triple: ", stdout);
          librdf_statement_print(stmt, stdout);
          if(ctxt) {
              fputs(" with context ", stdout);
              librdf_node_print(ctxt, stdout);
          }
          fputc('\n', stdout);

          count++;
          librdf_stream_next(stream);
        }
       librdf_free_stream(stream);

        endTest(1, " matching triples: %d\n", count);
        librdf_free_query_results(results);
      }
    }
    librdf_free_query(query);
  }


  /***** Test 19 *****/
  query_cmd=(char *)"SELECT * WHERE {graph <http://red> { ?s ?p ?o }}";
  startTest(19, " Exec1:  QUERY_AS_BINDINGS \"%s\" \n", query_cmd);
  {
    query=librdf_new_query(world, (char *)"vsparql", NULL, (const unsigned char *)query_cmd, NULL);

    if(!(results=librdf_model_query_execute(model, query))) {
        endTest(0, " Query of model with '%s' failed\n", query_cmd);
        librdf_free_query(query);
        query=NULL;
    } else {

        raptor_iostream *iostr;
        librdf_query_results_formatter *formatter;

        fprintf(stderr, "**: Formatting query result as '%s':\n", results_format);

        iostr = raptor_new_iostream_to_file_handle(librdf_world_get_raptor(world), stdout);
        formatter = librdf_new_query_results_formatter2(results, results_format,
                                                        NULL /* mime type */,
                                                        NULL /* format_uri */);

        base_uri = librdf_new_uri(world, (const unsigned char*)"http://example.org/");
        
        librdf_query_results_formatter_write(iostr, formatter, results, base_uri);
        librdf_free_query_results_formatter(formatter);
        raptor_free_iostream(iostr);
        librdf_free_uri(base_uri);

        endTest(1, "\n");
        librdf_free_query_results(results);
    }
    librdf_free_query(query);
  }


  /***** Test 20 *****/
  query_cmd=(char *)"SELECT * WHERE {graph <http://red> { ?s ?p ?o }}";
  startTest(20, " Exec2:  QUERY_AS_BINDINGS \"%s\" \n", query_cmd);
  {
    query=librdf_new_query(world, (char *)"vsparql", NULL, (const unsigned char *)query_cmd, NULL);

    if(!(results=librdf_model_query_execute(model, query))) {
       endTest(0, " Query of model with '%s' failed\n", query_cmd);
       librdf_free_query(query);
       query=NULL;
    } else {
       if(print_query_results(world, model, results))
         endTest(0, "\n");
       else
         endTest(1, "\n");

       librdf_free_query_results(results);
    }
    librdf_free_query(query);
  }

  getTotal();


  if(transactions)
    librdf_model_transaction_commit(model);

  librdf_free_node(context_node);
  librdf_free_node(context_node);
  librdf_free_hash(options);
  librdf_free_model(model);
  librdf_free_storage(storage);

  librdf_free_world(world);

#ifdef LIBRDF_MEMORY_DEBUG
  librdf_memory_report(stderr);
#endif
	
  /* keep gcc -Wall happy */
  return(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;
}