Ejemplo n.º 1
0
rasqal_literal*
redland_node_to_rasqal_literal(librdf_world* world, librdf_node *node)
{
  rasqal_literal* l;
  
  if(librdf_node_is_resource(node)) {
    raptor_uri* uri=(raptor_uri*)librdf_new_uri_from_uri(librdf_node_get_uri(node));
    l=rasqal_new_uri_literal(world->rasqal_world_ptr, uri); /* transfer uri ownership to literal */
  } else if(librdf_node_is_literal(node)) {
    unsigned char *string;
    librdf_uri *uri;
    unsigned char *new_string;
    char *new_language=NULL;
    raptor_uri *new_datatype=NULL;
    size_t len;

    string=librdf_node_get_literal_value_as_counted_string(node, &len);
    new_string=(unsigned char*)rasqal_alloc_memory(len+1);
    if(!new_string)
      return NULL;
    strcpy((char*)new_string, (const char*)string);

    string=(unsigned char*)librdf_node_get_literal_value_language(node);
    if(string) {
      new_language=(char*)rasqal_alloc_memory(strlen((const char*)string)+1);
      if(!new_language) {
        rasqal_free_memory((void*)new_string);
        return NULL;
      }
      strcpy((char*)new_language, (const char*)string);
    }
    uri=librdf_node_get_literal_value_datatype_uri(node);
    if(uri)
      new_datatype=(raptor_uri*)librdf_new_uri_from_uri(uri);
    /* transfer new_string,new_language,new_datatype ownership to literal */
    l = rasqal_new_string_literal(world->rasqal_world_ptr, (const unsigned char*)new_string, new_language, new_datatype, NULL);
  } else {
    unsigned char *blank=librdf_node_get_blank_identifier(node);
    unsigned char *new_blank;
    if(!blank)
      return NULL;
    new_blank=(unsigned char*)rasqal_alloc_memory(strlen((const char*)blank)+1);
    if(!new_blank)
      return NULL;
    strcpy((char*)new_blank, (const char*)blank);
    /* transfer new_blank ownership to literal */
    l = rasqal_new_simple_literal(world->rasqal_world_ptr, RASQAL_LITERAL_BLANK, (const unsigned char*)new_blank);
  }

  return l;
}
Ejemplo n.º 2
0
/*
 * librdf_parser_raptor_namespace_handler - helper callback function for raptor RDF when a namespace is seen
 * @context: context for callback
 * @statement: raptor_statement
 *
 * Adds the statement to the list of statements.
 */
static void
librdf_parser_raptor_namespace_handler(void* user_data,
                                       raptor_namespace *nspace)
{
  librdf_parser_raptor_context* pcontext=(librdf_parser_raptor_context*)user_data;
  const unsigned char* prefix;
  unsigned char* nprefix;
  size_t prefix_length;
  librdf_uri* uri;
  int i;

  uri=(librdf_uri*)raptor_namespace_get_uri(nspace);
  if(!uri)
    return;

  for(i=0; i < raptor_sequence_size(pcontext->nspace_uris); i++) {
    librdf_uri* u=(librdf_uri*)raptor_sequence_get_at(pcontext->nspace_uris, i);
    if(librdf_uri_equals(uri, u))
      return;
  }

  /* new namespace */
  uri=librdf_new_uri_from_uri(uri);
  raptor_sequence_push(pcontext->nspace_uris, uri);

  prefix=raptor_namespace_get_counted_prefix(nspace, &prefix_length);
  if(prefix) {
    nprefix = LIBRDF_MALLOC(unsigned char*, prefix_length + 1);
    /* FIXME: what if nprefix alloc failed? now just pushes NULL to sequence */
    if(nprefix)
      strncpy((char*)nprefix, (const char*)prefix, prefix_length+1);
  } else
Ejemplo n.º 3
0
static int
librdf_query_rasqal_init(librdf_query* query, 
                         const char *name, librdf_uri* uri,
                         const unsigned char* query_string,
                         librdf_uri *base_uri)
{
  librdf_query_rasqal_context *context=(librdf_query_rasqal_context*)query->context;
  size_t len;
  unsigned char *query_string_copy;
  
  context->query = query;
  context->language=context->query->factory->name;

  context->rq=rasqal_new_query(query->world->rasqal_world_ptr, context->language, NULL);
  if(!context->rq)
    return 1;

  rasqal_query_set_user_data(context->rq, query);

  rasqal_world_set_log_handler(query->world->rasqal_world_ptr, query->world,
                               librdf_query_rasqal_log_handler);

  len = strlen((const char*)query_string);
  query_string_copy = LIBRDF_MALLOC(unsigned char*, len + 1);
  if(!query_string_copy)
    return 1;
  strcpy((char*)query_string_copy, (const char*)query_string);

  context->query_string=query_string_copy;
  if(base_uri)
    context->uri=librdf_new_uri_from_uri(base_uri);

  return 0;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
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 */
}
Ejemplo n.º 6
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);
}