Пример #1
0
/**
 * librdf_init_concepts:
 * @world: redland world object
 *
 * INTERNAL - Initialise the concepts module.
 *
 **/
void
librdf_init_concepts(librdf_world *world)
{
    int i;

    /* Create the Unique URI objects */
    world->concept_ms_namespace_uri=librdf_new_uri(world, librdf_concept_ms_namespace);
    world->concept_schema_namespace_uri=librdf_new_uri(world, librdf_concept_schema_namespace);
    if(!world->concept_ms_namespace_uri || !world->concept_schema_namespace_uri)
        LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Failed to create M&S or Schema URIs");

    /* Create arrays for the M&S and Schema resource nodes and uris */
    world->concept_uris=
        (librdf_uri**)LIBRDF_CALLOC(ptrarray, LIBRDF_CONCEPT_LAST+1, sizeof(librdf_uri*));
    world->concept_resources=
        (librdf_node**)LIBRDF_CALLOC(ptrarray, LIBRDF_CONCEPT_LAST+1, sizeof(librdf_node*));
    if(!world->concept_uris || !world->concept_resources)
        LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Out of memory creating node/uri arrays");

    /* Create the M&S and Schema resource nodes */
    for (i=0; i<= LIBRDF_CONCEPT_LAST; i++) {
        librdf_uri* ns_uri=(i < LIBRDF_CONCEPT_FIRST_S_ID) ? world->concept_ms_namespace_uri :
                           world->concept_schema_namespace_uri;
        const unsigned char * token=(const unsigned char *)librdf_concept_tokens[i];

        world->concept_resources[i]=librdf_new_node_from_uri_local_name(world, ns_uri, token);
        if(!world->concept_resources[i])
            LIBRDF_FATAL1(world, LIBRDF_FROM_CONCEPTS, "Failed to create Node from URI\n");

        /* keep shared copy of URI from node */
        world->concept_uris[i]=librdf_node_get_uri(world->concept_resources[i]);
    }
}
Пример #2
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;
}
Пример #3
0
/**
 * librdf_new_uri_normalised_to_base:
 * @uri_string: URI in string form
 * @source_uri: source URI to remove
 * @base_uri: base URI to add
 *
 * Constructor - create a new #librdf_uri object from a URI string stripped of the source URI, made relative to the base URI.
 * 
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_normalised_to_base(const unsigned char *uri_string,
                                  librdf_uri* source_uri,
                                  librdf_uri* base_uri) 
{
  int uri_string_len;
  int len;
  unsigned char *new_uri_string;
  librdf_uri *new_uri;
  librdf_world *world=source_uri->world;
                                    
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(source_uri, librdf_uri, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(base_uri, librdf_uri, NULL);
  
  if(!uri_string)
    return NULL;

  /* empty URI - easy, just make from base_uri */
  if(!*uri_string && base_uri)
    return librdf_new_uri_from_uri(base_uri);

  /* not a fragment, and no match - easy */
  if(*uri_string != '#' &&
     strncmp((const char*)uri_string, (const char*)source_uri->string, source_uri->string_length))
    return librdf_new_uri(world, uri_string);

  /* darn - is a fragment or matches, is a prefix of the source URI */

  /* move uri_string pointer to first non-matching char 
   * unless a fragment, when all of the uri_string will 
   * be appended
   */
  if(*uri_string != '#')
    uri_string += source_uri->string_length;

  /* size of remaining bytes to copy from uri_string */
  uri_string_len=strlen((const char*)uri_string);

  /* total bytes */
  len=uri_string_len + 1 + base_uri->string_length;

  new_uri_string=(unsigned char*)LIBRDF_MALLOC(cstring, len);
  if(!new_uri_string)
    return NULL;
  strncpy((char*)new_uri_string, (const char*)base_uri->string, base_uri->string_length);
  /* strcpy not strncpy since I want a \0 on the end */
  strcpy((char*)new_uri_string + base_uri->string_length, (const char*)uri_string);
  
  new_uri=librdf_new_uri(world, new_uri_string);
  LIBRDF_FREE(cstring, new_uri_string); /* always free this even on failure */

  return new_uri; /* new URI or NULL from librdf_new_uri failure */
}
Пример #4
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);
  }
Пример #5
0
/**
 * librdf_new_uri_relative_to_base:
 * @base_uri: absolute base URI
 * @uri_string: relative URI string
 *
 * Constructor - create a new #librdf_uri object from a URI string relative to a base URI.
 *
 * An empty uri_string or NULL is equivalent to 
 * librdf_new_uri_from_uri(base_uri)
 * 
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_relative_to_base(librdf_uri* base_uri,
                                const unsigned char *uri_string) {
  unsigned char *buffer;
  int buffer_length;
  librdf_uri* new_uri;
  librdf_world *world=base_uri->world;
                                  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(base_uri, librdf_uri, NULL);

  if(!uri_string)
    return NULL;
  
  /* If URI string is empty, just copy base URI */
  if(!*uri_string)
    return librdf_new_uri_from_uri(base_uri);
  
  /* +2 is for \0 plus an extra 1 for adding any missing URI path '/' */
  buffer_length=base_uri->string_length + strlen((const char*)uri_string) +2;
  buffer=(unsigned char*)LIBRDF_MALLOC(cstring, buffer_length);
  if(!buffer)
    return NULL;
  
  raptor_uri_resolve_uri_reference(base_uri->string, uri_string,
                                   buffer, buffer_length);

  new_uri=librdf_new_uri(world, buffer);
  LIBRDF_FREE(cstring, buffer);
  return new_uri;
}
Пример #6
0
/**
 * librdf_new_uri_from_uri_local_name:
 * @old_uri: #librdf_uri object
 * @local_name: local name to append to URI
 *
 * Copy constructor - create a new librdf_uri object from an existing librdf_uri object and a local name.
 * 
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_from_uri_local_name (librdf_uri* old_uri, 
                                    const unsigned char *local_name) {
  int len;
  unsigned char *new_string;
  librdf_uri* new_uri;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(old_uri, librdf_uri, NULL);
  
  if(!old_uri)
    return NULL;
  
  len=old_uri->string_length + strlen((const char*)local_name) +1 ; /* +1 for \0 */

  new_string=(unsigned char*)LIBRDF_MALLOC(cstring, len);
  if(!new_string)
    return NULL;

  strcpy((char*)new_string, (const char*)old_uri->string);
  strcat((char*)new_string, (const char*)local_name);

  new_uri=librdf_new_uri (old_uri->world, new_string);
  LIBRDF_FREE(cstring, new_string);

  return new_uri;
}
Пример #7
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;
}
Пример #8
0
  librdf_node* RdfStorePrivate::RdfNodeToLibRdfNode(RdfNode node) const
  {
    librdf_node* newNode = nullptr;

    switch (node.GetType())
    {
    case RdfNode::NOTHING:
      break;
    case RdfNode::BLANK:
      newNode = librdf_new_node_from_blank_identifier(m_World, (const unsigned char*) node.GetValue().c_str());
      break;
    case RdfNode::LITERAL:
      {
        if (node.GetDatatype() != RdfUri())
        {
          librdf_uri* typeUri = RdfUriToLibRdfUri(node.GetDatatype());
          newNode = librdf_new_node_from_typed_literal(m_World, (const unsigned char*) node.GetValue().c_str(), nullptr, typeUri);
        }
        else
        {
          newNode = librdf_new_node_from_literal(m_World, (const unsigned char*) node.GetValue().c_str(), nullptr, 0);
        }
      }
      break;
    case RdfNode::URI:
      newNode = librdf_new_node_from_uri( m_World, librdf_new_uri(m_World, (const unsigned char*) node.GetValue().c_str()) );
      break;
    default:
      break;
    }
    return newNode;
  }
Пример #9
0
/**
 * librdf_new_uri_from_uri_local_name:
 * @old_uri: #librdf_uri object
 * @local_name: local name to append to URI
 *
 * Copy constructor - create a new librdf_uri object from an existing librdf_uri object and a local name.
 * 
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_from_uri_local_name (librdf_uri* old_uri, 
                                    const unsigned char *local_name) {
#ifdef LIBRDF_USE_RAPTOR_URI
  return raptor_new_uri_from_uri_local_name(raptor_uri_get_world(old_uri), old_uri, local_name);
#else
  int len;
  unsigned char *new_string;
  librdf_uri* new_uri;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(old_uri, librdf_uri, NULL);
  
  if(!old_uri)
    return NULL;
  
  len=old_uri->string_length + strlen((const char*)local_name) +1 ; /* +1 for \0 */

  new_string=(unsigned char*)LIBRDF_MALLOC(cstring, len);
  if(!new_string)
    return NULL;

  strcpy((char*)new_string, (const char*)old_uri->string);
  strcat((char*)new_string, (const char*)local_name);

  new_uri=librdf_new_uri (old_uri->world, new_string);
  LIBRDF_FREE(cstring, new_string);

  return new_uri;
#endif /* !LIBRDF_USE_RAPTOR_URI */
}
Пример #10
0
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
}
Пример #11
0
Файл: node.c Проект: ged/redleaf
/*
 * Convert the given Ruby URI object to a librdf_uri and return it. The caller must
 * free the returned pointer.
 *
 * The caller is responsible for catching the ArgumentError that results if the
 * +uriobj+ can't be converted.
 */
librdf_uri *
rleaf_object_to_librdf_uri( VALUE uriobj ) {
	librdf_uri *uri;
	VALUE uristring = rb_obj_as_string( uriobj );

	uri = librdf_new_uri( rleaf_rdf_world, (const unsigned char *)StringValuePtr(uristring) );
	if ( !uri )
		rb_raise( rleaf_eRedleafError, "Couldn't make a librdf_uri out of %s",
			RSTRING_PTR(rb_inspect( uriobj )) );

	return uri;
}
Пример #12
0
int description_init()
{
  // Create namespace URIs
  format_ns_uri = librdf_new_uri(world, (unsigned char *) "http://www.w3.org/ns/formats/");
  if (!format_ns_uri) {
    redstore_error("Failed to initialise format_ns_uri");
    return 1;
  }

  sd_ns_uri = librdf_new_uri(world, (unsigned char *) "http://www.w3.org/ns/sparql-service-description#");
  if (!sd_ns_uri) {
    redstore_error("Failed to initialise sd_ns_uri");
    return 1;
  }

  void_ns_uri = librdf_new_uri(world, (unsigned char *) "http://rdfs.org/ns/void#");
  if (!void_ns_uri) {
    redstore_error("Failed to initialise void_ns_uri");
    return 1;
  }

  // Success
  return 0;
}
Пример #13
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; 
}
Пример #14
0
/**
 * librdf_init_concepts - Initialise the concepts module.
 * @world: redland world object
 * 
 **/
void
librdf_init_concepts(librdf_world *world)
{
  int i;


  /* Create the Unique URI objects */
  librdf_concept_ms_namespace_uri=librdf_new_uri(world, librdf_concept_ms_namespace);
  librdf_concept_schema_namespace_uri=librdf_new_uri(world, librdf_concept_schema_namespace);

  /* Create the M&S and Schema resource nodes */
  for (i=0; i< LIBRDF_CONCEPT_LAST; i++) {
    librdf_uri* ns_uri=(i < LIBRDF_CONCEPT_FIRST_S_ID) ? librdf_concept_ms_namespace_uri :
      librdf_concept_schema_namespace_uri;
    const unsigned char * token=(const unsigned char *)librdf_concept_tokens[i];

    librdf_concept_resources[i]=librdf_new_node_from_uri_local_name(world, ns_uri, token);
    if(!librdf_concept_resources[i])
      LIBRDF_FATAL1(world, "Failed to create Node from URI\n");

    /* keep shared copy of URI from node */
    librdf_concept_uris[i]=librdf_node_get_uri(librdf_concept_resources[i]);
  }
}
Пример #15
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;
}
Пример #16
0
/**
 * librdf_new_uri_from_filename:
 * @world: Redland #librdf_world object
 * @filename: filename
 *
 * Constructor - create a new #librdf_uri object from a filename.
 *
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_from_filename(librdf_world* world, const char *filename) {
  librdf_uri* new_uri;
  unsigned char *uri_string;

  librdf_world_open(world);

  if(!filename)
    return NULL;
  
  uri_string=raptor_uri_filename_to_uri_string(filename);
  if(!uri_string)
    return NULL;
  
  new_uri=librdf_new_uri(world, uri_string);
  raptor_free_memory(uri_string);
  return new_uri;
}
Пример #17
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);
}
Пример #18
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;
}
Пример #19
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);
    
}
Пример #20
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);
}
Пример #21
0
/**
 * librdf_serializer_register_factory:
 * @world: redland world object
 * @name: the name of the serializer
 * @label: the label of the serializer (optional)
 * @mime_type: MIME type of the syntax (optional)
 * @uri_string: URI of the syntax (optional)
 * @factory: function to be called to register the factor parameters
 *
 * Register a serializer factory .
 * 
 **/
REDLAND_EXTERN_C
void
librdf_serializer_register_factory(librdf_world *world,
                                   const char *name, const char *label,
                                   const char *mime_type,
                                   const unsigned char *uri_string,
                                   void (*factory) (librdf_serializer_factory*))
{
  librdf_serializer_factory *serializer;

  librdf_world_open(world);

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  LIBRDF_DEBUG2("Received registration for serializer %s\n", name);
#endif

  if(!world->serializers) {
    world->serializers = raptor_new_sequence((raptor_data_free_handler)librdf_free_serializer_factory, NULL);

    if(!world->serializers)
      goto oom;
  }

  serializer=(librdf_serializer_factory*)LIBRDF_CALLOC(librdf_serializer_factory, 1,
                                                       sizeof(librdf_serializer_factory));
  if(!serializer)
    goto oom;

  serializer->name=(char*)LIBRDF_MALLOC(cstring, strlen(name)+1);
  if(!serializer->name)
    goto oom_tidy;
  strcpy(serializer->name, name);

  if(label) {
    serializer->label=(char*)LIBRDF_MALLOC(cstring, strlen(label)+1);
    if(!serializer->label)
      goto oom_tidy;
    strcpy(serializer->label, label);
  }

  /* register mime type if any */
  if(mime_type) {
    serializer->mime_type=(char*)LIBRDF_MALLOC(cstring, strlen(mime_type)+1);
    if(!serializer->mime_type)
      goto oom_tidy;
    strcpy(serializer->mime_type, mime_type);
  }

  /* register URI if any */
  if(uri_string) {
    serializer->type_uri=librdf_new_uri(world, uri_string);
    if(!serializer->type_uri)
      goto oom_tidy;
  }

  if(raptor_sequence_push(world->serializers, serializer)) 
    goto oom;

  /* Call the serializer registration function on the new object */
  (*factory)(serializer);

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  LIBRDF_DEBUG3("%s has context size %d\n", name, serializer->context_length);
#endif

  return;

  oom_tidy:
  librdf_free_serializer_factory(serializer);
  oom:
  LIBRDF_FATAL1(world, LIBRDF_FROM_SERIALIZER, "Out of memory");
}
Пример #22
0
Файл: node.c Проект: ged/redleaf
/*
 * Convert the given Ruby +object+ (VALUE) to a librdf_node.
 */
librdf_node *
rleaf_value_to_librdf_node( VALUE object ) {
	VALUE str, typeuristr, converted_pair;
	librdf_uri *typeuri;
	ID id;

	/* :TODO: how to set language? is_xml flag? */

	// rleaf_log( "debug", "Converting %s to a librdf_node.", RSTRING_PTR(rb_inspect( object )) );
	switch( TYPE(object) ) {

		/* nil -> bnode */
		case T_NIL:
		return NULL;

		case T_SYMBOL:
		id = SYM2ID( object );
		if ( id == rleaf_anon_bnodeid ) {
			return librdf_new_node_from_blank_identifier( rleaf_rdf_world, NULL );
		} else {
			return librdf_new_node_from_blank_identifier( rleaf_rdf_world, (unsigned char *)rb_id2name(id) );
		}

		/* String -> plain literal */
		case T_STRING:
		return librdf_new_node_from_literal( rleaf_rdf_world, (unsigned char *)RSTRING_PTR(object), NULL, 0 );
		break;

		/* Float -> xsd:float */
		case T_FLOAT:
		str = rb_obj_as_string( object );
		typeuri = XSD_FLOAT_TYPE;
		break;

		/* Bignum -> xsd:decimal */
		case T_BIGNUM:
		str = rb_obj_as_string( object );
		typeuri = XSD_DECIMAL_TYPE;
		break;

		/* Fixnum -> xsd:integer */
		case T_FIXNUM:
		str = rb_obj_as_string( object );
		typeuri = XSD_INTEGER_TYPE;
		break;

		/* TrueClass/FalseClass -> xsd:boolean */
		case T_TRUE:
		case T_FALSE:
		str = rb_obj_as_string( object );
		typeuri = XSD_BOOLEAN_TYPE;
		break;

		/* URI -> librdf_uri */
		case T_OBJECT:
		if ( IsURI(object) || IsNamespace(object) ) {
			// rleaf_log( "debug", "Converting %s object to librdf_uri node",
			//            rb_obj_classname(object) );
			str = rb_obj_as_string( object );
			return librdf_new_node_from_uri_string( rleaf_rdf_world,
				(unsigned char*)RSTRING_PTR(str) );
		}
		/* fallthrough */

		/* Delegate anything else to Redleaf::NodeUtils.make_object_typed_literal */
		default:
		converted_pair = rb_funcall( rleaf_mRedleafNodeUtils,
			rb_intern("make_object_typed_literal"), 1, object );
		str = rb_obj_as_string( rb_ary_entry(converted_pair, 0) );
		typeuristr = rb_obj_as_string( rb_ary_entry(converted_pair, 1) );
		typeuri = librdf_new_uri( rleaf_rdf_world, (unsigned char*)RSTRING_PTR(typeuristr) );
	}

	return librdf_new_node_from_typed_counted_literal(
		rleaf_rdf_world,
		(unsigned char *)RSTRING_PTR(str),
		RSTRING_LEN(str),
		NULL,
		0,
		typeuri );
}
Пример #23
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);
}
Пример #24
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);
}
Пример #25
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);
}
Пример #26
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;
}
Пример #27
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;
}
Пример #28
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);
    
}
Пример #29
0
 librdf_uri* RdfStorePrivate::RdfUriToLibRdfUri(RdfUri uri) const
 {
   librdf_uri* libUri = librdf_new_uri(m_World, (const unsigned char*) uri.ToString().c_str());
   if (!libUri) return nullptr;
   return libUri;
 }
Пример #30
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);

}