Ejemplo n.º 1
0
static void
raptor_dot_serializer_write_uri(raptor_serializer* serializer,
				raptor_uri* uri)
{
  raptor_dot_context* context = (raptor_dot_context*)serializer->context;
  unsigned char* full = raptor_uri_as_string(uri);
  int i;

  for( i = 0 ; i < raptor_sequence_size(context->namespaces) ; i++ ) {
    raptor_namespace* ns =
      (raptor_namespace*)raptor_sequence_get_at(context->namespaces, i);
    const unsigned char* ns_uri_string;
    size_t ns_uri_string_len;
    ns_uri_string=raptor_uri_as_counted_string(ns->uri, &ns_uri_string_len);

    if(!strncmp((char*)full, (char*)ns_uri_string, ns_uri_string_len) ) {
      const unsigned char* prefix = raptor_namespace_get_prefix(ns);
      
      if(prefix) {	
        raptor_iostream_write_string(serializer->iostream, prefix);
        raptor_iostream_write_byte(serializer->iostream, ':');
      }

      raptor_iostream_write_string(serializer->iostream,
                                   full + ns_uri_string_len);

      return;
    }
  }

  raptor_iostream_write_string(serializer->iostream, full);
}
Ejemplo n.º 2
0
/**
 * librdf_uri_as_counted_string:
 * @uri: #librdf_uri object
 * @len_p: pointer to location to store length
 *
 * Get a pointer to the string representation of the URI with length.
 * 
 * Returns a shared pointer to the URI string representation. 
 * Note: does not allocate a new string so the caller must not free it.
 * 
 * Return value: string representation of URI
 **/
unsigned char*
librdf_uri_as_counted_string(librdf_uri *uri, size_t* len_p) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(uri, librdf_uri, NULL);

  return raptor_uri_as_counted_string(uri, len_p);
}
Ejemplo n.º 3
0
/**
 * raptor_iostream_write_uri:
 * @iostr: raptor iostream
 * @uri: URI
 *
 * Write a raptor URI to the iostream.
 *
 * Return value: non-0 on failure
 **/
int
raptor_iostream_write_uri(raptor_iostream* iostr,raptor_uri* uri)
{
  size_t len;
  const void *string=raptor_uri_as_counted_string(uri, &len);
  return (raptor_iostream_write_bytes(iostr, string, 1, len) != (int)len);
}
Ejemplo n.º 4
0
/**
 * raptor_uri_write:
 * @uri: URI
 * @iostr: raptor iostream
 *
 * Write a raptor URI to the iostream.
 *
 * Return value: non-0 on failure
 **/
int
raptor_uri_write(raptor_uri* uri, raptor_iostream* iostr)
{
  size_t len;
  const void *string = raptor_uri_as_counted_string(uri, &len);
  return (raptor_iostream_write_bytes(string, 1, len, iostr) != RAPTOR_BAD_CAST(int, len));
}
Ejemplo n.º 5
0
static void
rasqal_query_write_sparql_uri(sparql_writer_context *wc,
                              raptor_iostream* iostr, raptor_uri* uri)
{
  size_t len;
  unsigned char* string;
  raptor_qname* qname;

  qname = raptor_new_qname_from_namespace_uri(wc->nstack, uri, 10);
  if(qname) {
    const raptor_namespace* nspace = raptor_qname_get_namespace(qname);
    if(!raptor_namespace_get_prefix(nspace))
      raptor_iostream_write_byte(':', iostr);
    raptor_qname_write(qname, iostr);
    raptor_free_qname(qname);
    return;
  }
  
  if(wc->base_uri)
    string = raptor_uri_to_relative_counted_uri_string(wc->base_uri, uri, &len);
  else
    string = raptor_uri_as_counted_string(uri, &len);

  raptor_iostream_write_byte('<', iostr);
  raptor_string_ntriples_write(string, len, '>', iostr);
  raptor_iostream_write_byte('>', iostr);

  if(wc->base_uri)
    raptor_free_memory(string);
}
Ejemplo n.º 6
0
/**
 * raptor_uri_print:
 * @uri: URI to print
 * @stream: The file handle to print to
 *
 * Print a URI to a file handle.
 *
 **/
void
raptor_uri_print(const raptor_uri* uri, FILE *stream) {
  if(uri) {
    size_t len;
    unsigned char *string=raptor_uri_as_counted_string((raptor_uri*)uri, &len);
    (void)fwrite(string, len, 1, stream);
  } else 
    (void)fwrite("(NULL URI)", 10, 1, stream);
}
Ejemplo n.º 7
0
Archivo: rdf_uri.c Proyecto: njh/librdf
/**
 * librdf_uri_as_counted_string:
 * @uri: #librdf_uri object
 * @len_p: pointer to location to store length
 *
 * Get a pointer to the string representation of the URI with length.
 * 
 * Returns a shared pointer to the URI string representation. 
 * Note: does not allocate a new string so the caller must not free it.
 * 
 * Return value: string representation of URI
 **/
unsigned char*
librdf_uri_as_counted_string(librdf_uri *uri, size_t* len_p) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(uri, librdf_uri, NULL);
#ifdef LIBRDF_USE_RAPTOR_URI
  return raptor_uri_as_counted_string(uri, len_p);
#else
  if(len_p)
    *len_p=uri->string_length;
  return uri->string;
#endif
}
Ejemplo n.º 8
0
/**
 * raptor_uri_to_counted_string:
 * @uri: #raptor_uri object
 * @len_p: Pointer to length (or NULL)
 *
 * Get a new counted string for a URI.
 *
 * If @len_p is not NULL, the length of the string is stored in it.
 *
 * The memory allocated must be freed by the caller and
 * raptor_free_memory() should be used for best portability.
 *
 * Return value: new string or NULL on failure
 **/
unsigned char*
raptor_uri_to_counted_string(raptor_uri *uri, size_t *len_p)
{
  size_t len;
  unsigned char *string;
  unsigned char *new_string;

  if(!uri)
    return NULL;
  
  string=raptor_uri_as_counted_string(uri, &len);
  if(!string)
    return NULL;
  
  new_string=(unsigned char*)RAPTOR_MALLOC(cstring, len + 1); /* +1 for NUL termination */
  if(!new_string)
    return NULL;
  
  memcpy(new_string, string, len+1);

  if(len_p)
    *len_p=len;
  return new_string;
}
Ejemplo n.º 9
0
/**
 * rasqal_service_execute:
 * @svc: rasqal service
 *
 * Execute a rasqal sparql protocol service
 *
 * Return value: query results or NULL on failure
 */
rasqal_query_results*
rasqal_service_execute(rasqal_service* svc)
{
  rasqal_query_results* results = NULL;
  unsigned char* result_string = NULL;
  size_t result_length;
  raptor_iostream* read_iostr = NULL;
  raptor_uri* read_base_uri = NULL;
  rasqal_variables_table* vars_table = NULL;
  rasqal_query_results_formatter* read_formatter = NULL;
  raptor_uri* retrieval_uri = NULL;
  raptor_stringbuffer* uri_sb = NULL;
  size_t len;
  unsigned char* str;
  raptor_world* raptor_world_ptr = rasqal_world_get_raptor(svc->world);
  
  if(!svc->www) {
    svc->www = raptor_new_www(raptor_world_ptr);

    if(!svc->www) {
      rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                              "Failed to create WWW");
      goto error;
    }
  }
    
  svc->started = 0;
  svc->final_uri = NULL;
  svc->sb = raptor_new_stringbuffer();
  svc->content_type = NULL;
  
  if(svc->format)
    raptor_www_set_http_accept(svc->www, svc->format);
  else
    raptor_www_set_http_accept(svc->www, DEFAULT_FORMAT);

  raptor_www_set_write_bytes_handler(svc->www,
                                     rasqal_service_write_bytes, svc);
  raptor_www_set_content_type_handler(svc->www,
                                      rasqal_service_content_type_handler, svc);


  /* Construct a URI to retrieve following SPARQL protocol HTTP
   *  binding from concatenation of
   *
   * 1. service_uri
   * 2. '?'
   * 3. "query=" query_string
   * 4. "&default-graph-uri=" background graph URI if any
   * 5. "&named-graph-uri=" named graph URI for all named graphs
   * with URI-escaping of the values
   */

  uri_sb = raptor_new_stringbuffer();
  if(!uri_sb) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to create stringbuffer");
    goto error;
  }

  str = raptor_uri_as_counted_string(svc->service_uri, &len);
  raptor_stringbuffer_append_counted_string(uri_sb, str, len, 1);

  raptor_stringbuffer_append_counted_string(uri_sb,
                                            (const unsigned char*)"?", 1, 1);

  if(svc->query_string) {
    raptor_stringbuffer_append_counted_string(uri_sb,
                                              (const unsigned char*)"query=", 6, 1);
    raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb,
                                                          svc->query_string,
                                                          svc->query_string_len,
                                                          1);
  }


  if(svc->data_graphs) {
    rasqal_data_graph* dg;
    int i;
    int bg_graph_count;
    
    for(i = 0, bg_graph_count = 0;
        (dg = (rasqal_data_graph*)raptor_sequence_get_at(svc->data_graphs, i));
        i++) {
      unsigned char* graph_str;
      size_t graph_len;
      raptor_uri* graph_uri;
      
      if(dg->flags & RASQAL_DATA_GRAPH_BACKGROUND) {

        if(bg_graph_count++) {
          if(bg_graph_count == 2) {
            /* Warn once, only when the second BG is seen */
            rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_WARN, NULL,
                                    "Attempted to add use background graphs");
          }
          /* always skip after first BG */
          continue;
        }
        
        raptor_stringbuffer_append_counted_string(uri_sb,
                                                  (const unsigned char*)"&default-graph-uri=", 19, 1);
        graph_uri = dg->uri;
      } else {
        raptor_stringbuffer_append_counted_string(uri_sb,
                                                  (const unsigned char*)"&named-graph-uri=", 17, 1);
        graph_uri = dg->name_uri;
      }
      
      graph_str = raptor_uri_as_counted_string(graph_uri, &graph_len);
      raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb,
                                                            (const char*)graph_str, graph_len, 1);
    }
  }
  

  str = raptor_stringbuffer_as_string(uri_sb);

  retrieval_uri = raptor_new_uri(raptor_world_ptr, str);
  if(!retrieval_uri) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to create retrieval URI %s",
                            raptor_uri_as_string(retrieval_uri));
    goto error;
  }

  raptor_free_stringbuffer(uri_sb); uri_sb = NULL;
  
  if(raptor_www_fetch(svc->www, retrieval_uri)) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to fetch retrieval URI %s",
                            raptor_uri_as_string(retrieval_uri));
    goto error;
  }

  vars_table = rasqal_new_variables_table(svc->world);
  if(!vars_table) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to create variables table");
    goto error;
  }
  
  results = rasqal_new_query_results(svc->world, NULL, 
                                     RASQAL_QUERY_RESULTS_BINDINGS, 
                                     vars_table);
  /* (results takes a reference/copy to vars_table) */
  rasqal_free_variables_table(vars_table); vars_table = NULL;

  if(!results) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to create query results");
    goto error;
  }
  
  result_length = raptor_stringbuffer_length(svc->sb);  
  result_string = raptor_stringbuffer_as_string(svc->sb);
  read_iostr = raptor_new_iostream_from_string(raptor_world_ptr,
                                               result_string, result_length);
  if(!read_iostr) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to create iostream from string");
    rasqal_free_query_results(results);
    results = NULL;
    goto error;
  }
    
  read_base_uri = svc->final_uri ? svc->final_uri : svc->service_uri;
  read_formatter = rasqal_new_query_results_formatter(svc->world,
                                                      /* format name */ NULL,
                                                      svc->content_type,
                                                      /* format URI */ NULL);
  if(!read_formatter) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to create query formatter for type %s",
                            svc->content_type);
    rasqal_free_query_results(results);
    results = NULL;
    goto error;
  }

  if(rasqal_query_results_formatter_read(svc->world,
                                         read_iostr, read_formatter,
                                         results, read_base_uri)) {
    rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Failed to read from query formatter");
    rasqal_free_query_results(results);
    results = NULL;
    goto error;
  }


  error:
  if(retrieval_uri)
    raptor_free_uri(retrieval_uri);

  if(uri_sb)
    raptor_free_stringbuffer(uri_sb);

  if(read_formatter)
    rasqal_free_query_results_formatter(read_formatter);
  
  if(read_iostr)
    raptor_free_iostream(read_iostr);
  
  if(vars_table)
    rasqal_free_variables_table(vars_table);

  if(svc->final_uri) {
    raptor_free_uri(svc->final_uri);
    svc->final_uri = NULL;
  }

  if(svc->content_type) {
    RASQAL_FREE(cstring, svc->content_type);
    svc->content_type = NULL;
  }

  if(svc->sb) {
    raptor_free_stringbuffer(svc->sb);
    svc->sb = NULL;
  }
  
  return results;
}
Ejemplo n.º 10
0
/*
 * raptor_new_qname_from_resource:
 * @namespaces: sequence of namespaces (corresponding to nstack)
 * @nstack: #raptor_namespace_stack to use/update
 * @namespace_count: size of nstack (may be modified)
 * @node: #raptor_abbrev_node to use 
 * 
 * Make an XML QName from the URI associated with the node.
 * 
 * Return value: the QName or NULL on failure
 **/
raptor_qname*
raptor_new_qname_from_resource(raptor_sequence* namespaces,
                               raptor_namespace_stack* nstack,
                               int* namespace_count,
                               raptor_abbrev_node* node)
{
  unsigned char* name=NULL;  /* where to split predicate name */
  size_t name_len=1;
  unsigned char *uri_string;
  size_t uri_len;
  unsigned char c;
  unsigned char *p;
  raptor_uri *ns_uri;
  raptor_namespace *ns;
  raptor_qname *qname;
  
  if(node->type != RAPTOR_IDENTIFIER_TYPE_RESOURCE) {
    RAPTOR_FATAL1("Node must be a resource\n");
    return NULL;
  }

  qname=raptor_namespaces_qname_from_uri(nstack, 
                                         node->value.resource.uri, 10);
  if(qname)
    return qname;
  
  uri_string = raptor_uri_as_counted_string(node->value.resource.uri, &uri_len);

  p= uri_string;
  name_len=uri_len;
  while(name_len >0) {
    if(raptor_xml_name_check(p, name_len, 10)) {
      name=p;
      break;
    }
    p++; name_len--;
  }
      
  if(!name || (name == uri_string))
    return NULL;

  c=*name; *name='\0';
  ns_uri=raptor_new_uri(uri_string);
  if(!ns_uri)
    return NULL;
  
  *name=c;
  
  ns = raptor_namespaces_find_namespace_by_uri(nstack, ns_uri);
  if(!ns) {
    /* The namespace was not declared, so create one */
    unsigned char prefix[2 + MAX_ASCII_INT_SIZE + 1];
      *namespace_count = *namespace_count + 1;
    sprintf((char *)prefix, "ns%d", *namespace_count);

    ns = raptor_new_namespace_from_uri(nstack, prefix, ns_uri, 0);

    /* We'll most likely need this namespace again. Push it on our
     * stack.  It will be deleted in
     * raptor_rdfxmla_serialize_terminate
     */
    if(raptor_sequence_push(namespaces, ns)) {
      /* namespaces sequence has no free handler so we have to free the ns ourselves on error */
      raptor_free_namespace(ns);
      raptor_free_uri(ns_uri);
      return NULL;
    }
  }

  qname = raptor_new_qname_from_namespace_local_name(ns, name,  NULL);
  
  raptor_free_uri(ns_uri);

  return qname;
}
Ejemplo n.º 11
0
/*
 * rasqal_query_results_write_sv:
 * @iostr: #raptor_iostream to write the query to
 * @results: #rasqal_query_results query results format
 * @base_uri: #raptor_uri base URI of the output format
 * @label: name of this format for errors
 * @sep: column sep character
 *
 * INTERNAL - Write a @sep-separated values version of the query results format to an iostream.
 *
 * If the writing succeeds, the query results will be exhausted.
 *
 * Return value: non-0 on failure
 **/
static int
rasqal_query_results_write_sv(raptor_iostream *iostr,
                              rasqal_query_results* results,
                              raptor_uri *base_uri,
                              const char* label,
                              char sep)
{
    rasqal_query* query = rasqal_query_results_get_query(results);
    int i;
    int count = 1;
#define empty_value_str_len 0
    static const char empty_value_str[empty_value_str_len+1] = "";
#define nl_str_len 1
    static const char nl_str[nl_str_len+1] = "\n";
    int vars_count;

    if(!rasqal_query_results_is_bindings(results)) {
        rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                                &query->locator,
                                "Can only write %s format for variable binding results",
                                label);
        return 1;
    }

    /* Header */
    raptor_iostream_counted_string_write("Result", 6, iostr);

    for(i = 0; 1; i++) {
        const unsigned char *name;

        name = rasqal_query_results_get_binding_name(results, i);
        if(!name)
            break;

        raptor_iostream_write_byte(sep, iostr);
        raptor_iostream_string_write(name, iostr);
    }
    raptor_iostream_counted_string_write(nl_str, nl_str_len, iostr);


    /* Variable Binding Results */
    vars_count = rasqal_query_results_get_bindings_count(results);
    while(!rasqal_query_results_finished(results)) {
        /* Result row */
        raptor_iostream_decimal_write(count++, iostr);

        for(i = 0; i < vars_count; i++) {
            rasqal_literal *l = rasqal_query_results_get_binding_value(results, i);

            raptor_iostream_write_byte(sep, iostr);

            if(!l) {
                if(empty_value_str_len)
                    raptor_iostream_counted_string_write(empty_value_str,
                                                         empty_value_str_len, iostr);
            } else switch(l->type) {
                    const unsigned char* str;
                    size_t len;

                case RASQAL_LITERAL_URI:
                    raptor_iostream_string_write("uri(", iostr);
                    str = (const unsigned char*)raptor_uri_as_counted_string(l->value.uri, &len);
                    raptor_string_ntriples_write(str, len, '"', iostr);
                    raptor_iostream_write_byte(')', iostr);
                    break;

                case RASQAL_LITERAL_BLANK:
                    raptor_iostream_string_write("blank(", iostr);
                    raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
                    raptor_iostream_write_byte(')', iostr);
                    break;

                case RASQAL_LITERAL_STRING:
                    if(l->datatype && l->valid) {
                        rasqal_literal_type ltype;
                        ltype = rasqal_xsd_datatype_uri_to_type(l->world, l->datatype);

                        if(ltype >= RASQAL_LITERAL_INTEGER &&
                                ltype <= RASQAL_LITERAL_DECIMAL) {
                            /* write integer, float, double and decimal XSD typed
                             * data without quotes, datatype or language
                             */
                            raptor_string_ntriples_write(l->string, l->string_len, '\0', iostr);
                            break;
                        }
                    }

                    raptor_iostream_write_byte('"', iostr);
                    raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
                    raptor_iostream_write_byte('"', iostr);

                    if(l->language) {
                        raptor_iostream_write_byte('@', iostr);
                        raptor_iostream_string_write((const unsigned char*)l->language,
                                                     iostr);
                    }

                    if(l->datatype) {
                        raptor_iostream_string_write("^^uri(", iostr);
                        str = (const unsigned char*)raptor_uri_as_counted_string(l->datatype, &len);
                        raptor_string_ntriples_write(str, len, '"', iostr);
                        raptor_iostream_write_byte(')', iostr);
                    }

                    break;

                case RASQAL_LITERAL_PATTERN:
                case RASQAL_LITERAL_QNAME:
                case RASQAL_LITERAL_INTEGER:
                case RASQAL_LITERAL_XSD_STRING:
                case RASQAL_LITERAL_BOOLEAN:
                case RASQAL_LITERAL_DOUBLE:
                case RASQAL_LITERAL_FLOAT:
                case RASQAL_LITERAL_VARIABLE:
                case RASQAL_LITERAL_DECIMAL:
                case RASQAL_LITERAL_DATETIME:
                case RASQAL_LITERAL_UDT:
                case RASQAL_LITERAL_INTEGER_SUBTYPE:

                case RASQAL_LITERAL_UNKNOWN:
                default:
                    rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                                            &query->locator,
                                            "Cannot turn literal type %d into %s",
                                            l->type, label);
                }

            /* End Binding */
        }

        /* End Result Row */
        raptor_iostream_counted_string_write(nl_str, nl_str_len, iostr);

        rasqal_query_results_next(results);
    }

    /* end sparql */
    return 0;
}
Ejemplo n.º 12
0
unsigned char*
raptor_uri_to_relative_counted_uri_string(raptor_uri *base_uri, 
                                          raptor_uri *reference_uri,
                                          size_t *length_p) {
  raptor_uri_detail *base_detail=NULL, *reference_detail;
  const unsigned char *base, *reference_str, *base_file, *reference_file;
  unsigned char *suffix, *cur_ptr;
  size_t base_len, reference_len, reference_file_len, suffix_len;
  unsigned char *result=NULL;

  if(!reference_uri)
    return NULL;
    
  if(length_p)
    *length_p=0;

  reference_str=raptor_uri_as_counted_string(reference_uri, &reference_len);
  reference_detail=raptor_new_uri_detail(reference_str);
  if(!reference_detail)
    goto err;
  
  if(!base_uri)
    goto buildresult;
  
  base=raptor_uri_as_counted_string(base_uri, &base_len);
  base_detail=raptor_new_uri_detail(base);
  if(!base_detail)
    goto err;
  
  /* Check if the whole URIs are equal */
  if(raptor_uri_equals(base_uri, reference_uri)) {
    reference_len=0;
    goto buildresult;
  }
  
  /* Check if scheme and authority of the URIs are equal */
  if(base_detail->scheme_len == reference_detail->scheme_len &&
     base_detail->authority_len == reference_detail->authority_len &&
     !strncmp((const char*)base_detail->scheme, 
              (const char*)reference_detail->scheme,
              base_detail->scheme_len) &&
     !strncmp((const char*)base_detail->authority, 
              (const char*)reference_detail->authority,
              base_detail->authority_len)) {
    
    if(!base_detail->path)
      goto buildresult;
    
    /* Find the file name components */
    base_file = (const unsigned char*)strrchr((const char*)base_detail->path, '/');
    if(!base_file)
      goto buildresult;
    base_file++;

    if(!reference_detail->path)
      goto buildresult;
    reference_file=(const unsigned char*)strrchr((const char*)reference_detail->path, '/');
    if(!reference_file)
      goto buildresult;
    reference_file++;
    
    reference_file_len=reference_detail->path_len -
                       (reference_file - reference_detail->path);
    
    if(!strcmp((const char*)base_file, (const char*)reference_file)) {
      /* If the file names are equal, don't put them in the relative URI */
      reference_file=NULL;
      reference_file_len=0;
    } else if(*base_file && !*reference_file) {
      /* If the base file is non-empty, but the reference file is
       * empty, use "."  as the file name.
       */
      reference_file=(const unsigned char*)".";
      reference_file_len=1;
    }
    
    /* Calculate the length of the suffix (file name + query + fragment) */
    suffix_len=reference_file_len + reference_detail->query_len + 
               reference_detail->fragment_len;
    
    if (reference_detail->query)
      suffix_len++; /* add one char for the '?' */
    if (reference_detail->fragment)
      suffix_len++; /* add one char for the '#' */
    
    /* Assemble the suffix */
    suffix=(unsigned char*)RAPTOR_MALLOC(cstring, suffix_len + sizeof(char*));
    if(!suffix)
      goto err;
    cur_ptr=suffix;
    if(reference_file) {
      memcpy(suffix, reference_file, reference_file_len);
      cur_ptr+= reference_file_len;
    }

    if(reference_detail->query) {
      *cur_ptr++='?';
      memcpy(cur_ptr, reference_detail->query, reference_detail->query_len);
      cur_ptr+= reference_detail->query_len;
    }

    if(reference_detail->fragment) {
      *cur_ptr++='#';
      memcpy(cur_ptr, reference_detail->fragment, reference_detail->fragment_len);
      cur_ptr+= reference_detail->fragment_len;
    }
    *cur_ptr=0;
    
    /* Finally, create the full relative path */
    result = raptor_uri_path_make_relative_path(base_detail->path,
                                                base_detail->path_len,
                                                reference_detail->path,
                                                reference_detail->path_len,
                                                suffix,
                                                suffix_len,
                                                length_p);
    RAPTOR_FREE(cstring, suffix);
  }

  
 buildresult:
  /* If result is NULL at this point, it means that we were unable to find a
     relative URI, so we'll return a full absolute URI instead. */
  if(!result) {
    result=(unsigned char*)RAPTOR_MALLOC(cstring, reference_len + sizeof(char*));
    if(result) {
      if(reference_len)
        memcpy(result, reference_str, reference_len);
      result[reference_len] = 0;
      if(length_p)
        *length_p=reference_len;
    }
  }
  
  err:
  if(base_detail)
    raptor_free_uri_detail(base_detail);
  raptor_free_uri_detail(reference_detail);
  
  return result;
}
Ejemplo n.º 13
0
int
main(int argc, char *argv[])
{
  raptor_world *world;
  const char *program = raptor_basename(argv[0]);
  int rc = 0;
  raptor_term* term1 = NULL; /* URI string 1 */
  raptor_term* term2 = NULL; /* literal string1 */
  raptor_term* term3 = NULL; /* blank node 1 */
  raptor_term* term4 = NULL; /* URI string 2 */
  raptor_term* term5 = NULL; /* URI string 1 again */
  raptor_uri* uri1;
  unsigned char* uri_str;
  size_t uri_len;
  
  
  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);


  /* check a term for NULL URI fails */
  term1 = raptor_new_term_from_uri(world, NULL);
  if(term1) {
    fprintf(stderr, "%s: raptor_new_uri(NULL) returned object rather than failing\n", program);
    rc = 1;
    goto tidy;
  }

  /* check a term for non-NULL URI succeeds */
  uri1 = raptor_new_uri(world, uri_string1);
  if(!uri1) {
    fprintf(stderr, "%s: raptor_new_uri(%s) failed\n", program, uri_string1);
    rc = 1;
    goto tidy;
  }
  term1 = raptor_new_term_from_uri(world, uri1);
  if(!term1) {
    fprintf(stderr, "%s: raptor_new_term_from_uri_string(URI %s) failed\n",
            program, uri_string1);
    rc = 1;
    goto tidy;
  }
  raptor_free_uri(uri1); uri1 = NULL;
  if(term1->type != uri_string1_type) {
    fprintf(stderr, "%s: raptor term 1 is of type %d expected %d\n",
            program, term1->type, uri_string1_type);
    rc = 1;
    goto tidy;
  }


  /* returns a pointer to shared string */
  uri_str = raptor_uri_as_counted_string(term1->value.uri, &uri_len);
  if(!uri_str) {
    fprintf(stderr, "%s: raptor_uri_as_counted_string term 1 failed\n",
            program);
    rc = 1;
    goto tidy;
  }

  if(uri_len != uri_string1_len) {
    fprintf(stderr, "%s: raptor term 1 URI is of length %d expected %d\n",
            program, (int)uri_len, (int)uri_string1_len);
    rc = 1;
    goto tidy;
  }

  
  /* check an empty literal is created from a NULL literal pointer succeeds */
  term2 = raptor_new_term_from_counted_literal(world, NULL, 0, NULL, NULL, 0);
  if(!term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal() with all NULLs failed\n", program);
    rc = 1;
    goto tidy;
  }
  raptor_free_term(term2);


  /* check an empty literal from an empty language literal pointer succeeds */
  term2 = raptor_new_term_from_counted_literal(world, NULL, 0, NULL,
                                               (const unsigned char*)"", 0);
  if(!term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal() with empty language failed\n", program);
    rc = 1;
    goto tidy;
  }
  raptor_free_term(term2);

  /* check a literal with language and datatype fails */
  uri1 = raptor_new_uri(world, uri_string1);
  if(!uri1) {
    fprintf(stderr, "%s: raptor_new_uri(%s) failed\n", program, uri_string1);
    rc = 1;
    goto tidy;
  }
  term2 = raptor_new_term_from_counted_literal(world, literal_string1,
                                               literal_string1_len, 
                                               uri1, language1, 0);
  raptor_free_uri(uri1); uri1 = NULL;
  if(term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal() with language and datatype returned object rather than failing\n", program);
    rc = 1;
    goto tidy;
  }
  
  /* check a literal with no language and no datatype succeeds */
  term2 = raptor_new_term_from_counted_literal(world, literal_string1,
                                               literal_string1_len, NULL, NULL, 0);
  if(!term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal(%s) failed\n",
            program, literal_string1);
    rc = 1;
    goto tidy;
  }
  if(term2->type != literal_string1_type) {
    fprintf(stderr, "%s: raptor term 2 is of type %d expected %d\n",
            program, term2->type, literal_string1_type);
    rc = 1;
    goto tidy;
  }
  

  /* check a blank node term with NULL id generates a new identifier */
  term3 = raptor_new_term_from_counted_blank(world, NULL, 0);
  if(!term3) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_blank(NULL) failed\n",
            program);
    rc = 1;
    goto tidy;
  }
  if(term3->type != bnodeid1_type) {
    fprintf(stderr, "%s: raptor term 3 is of type %d expected %d\n",
            program, term3->type, bnodeid1_type);
    rc = 1;
    goto tidy;
  }
  raptor_free_term(term3);

  /* check a blank node term with an identifier succeeds */
  term3 = raptor_new_term_from_counted_blank(world, bnodeid1, bnodeid1_len);
  if(!term3) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_blank(%s) failed\n",
            program, bnodeid1);
    rc = 1;
    goto tidy;
  }
  if(term3->type != bnodeid1_type) {
    fprintf(stderr, "%s: raptor term 3 is of type %d expected %d\n",
            program, term3->type, bnodeid1_type);
    rc = 1;
    goto tidy;
  }


  /* check a different URI term succeeds */
  term4 = raptor_new_term_from_counted_uri_string(world, uri_string2,
                                                  uri_string2_len);
  if(!term4) {
    fprintf(stderr,
            "%s: raptor_new_term_from_counted_uri_string(URI %s) failed\n",
            program, uri_string2);
    rc = 1;
    goto tidy;
  }
  if(term4->type != uri_string2_type) {
    fprintf(stderr, "%s: raptor term 4 is of type %d expected %d\n",
            program, term4->type, uri_string2_type);
    rc = 1;
    goto tidy;
  }
  /* returns a pointer to shared string */
  uri_str = raptor_uri_as_counted_string(term4->value.uri, &uri_len);
  if(!uri_str) {
    fprintf(stderr, "%s: raptor_uri_as_counted_string term 4 failed\n",
            program);
    rc = 1;
    goto tidy;
  }

  if(uri_len != uri_string2_len) {
    fprintf(stderr, "%s: raptor term 4 URI is of length %d expected %d\n",
            program, (int)uri_len, (int)uri_string2_len);
    rc = 1;
    goto tidy;
  }


  /* check the same URI term as term1 succeeds */
  term5 = raptor_new_term_from_uri_string(world, uri_string1);
  if(!term5) {
    fprintf(stderr, "%s: raptor_new_term_from_uri_string(URI %s) failed\n",
            program, uri_string1);
    rc = 1;
    goto tidy;
  }


  if(raptor_term_equals(term1, term2)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, literal %s) returned equal, expected not-equal\n",
            program, uri_string1, literal_string1);
    rc = 1;
    goto tidy;
  }

  if(raptor_term_equals(term1, term3)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, bnode %s) returned equal, expected not-equal\n",
            program, uri_string1, bnodeid1);
    rc = 1;
    goto tidy;
  }
  
  if(raptor_term_equals(term1, term4)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, URI %s) returned equal, expected not-equal\n",
            program, uri_string1, uri_string2);
    rc = 1;
    goto tidy;
  }
  
  if(!raptor_term_equals(term1, term5)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, URI %s) returned not-equal, expected equal\n",
            program, uri_string1, uri_string1);
    rc = 1;
    goto tidy;
  }

  if(term1->value.uri != term5->value.uri) {
    fprintf(stderr, "%s: term1 and term5 URI objects returned not-equal pointers, expected equal\n",
            program);
    /* This is not necessarily a failure if the raptor_uri module has had
     * the URI interning disabled with
     *   raptor_world_set_flag(world, RAPTOR_WORLD_FLAG_URI_INTERNING, 0) 
     * however this test suite does not do that, so it is a failure here.
     */
    rc = 1;
    goto tidy;
  }
  

  tidy:
  if(term1)
    raptor_free_term(term1);
  if(term2)
    raptor_free_term(term2);
  if(term3)
    raptor_free_term(term3);
  if(term4)
    raptor_free_term(term4);
  if(term5)
    raptor_free_term(term5);
  
  raptor_free_world(world);

  return rc;
}
Ejemplo n.º 14
0
/*
 * rasqal_query_results_write_json1:
 * @iostr: #raptor_iostream to write the query to
 * @results: #rasqal_query_results query results format
 * @base_uri: #raptor_uri base URI of the output format
 *
 * Write a JSON version of the query results format to an
 * iostream in a format - INTERNAL.
 * 
 * If the writing succeeds, the query results will be exhausted.
 * 
 * Return value: non-0 on failure
 **/
static int
rasqal_query_results_write_json1(raptor_iostream *iostr,
                                 rasqal_query_results* results,
                                 raptor_uri *base_uri)
{
  rasqal_world* world = rasqal_query_results_get_world(results);
  rasqal_query* query = rasqal_query_results_get_query(results);
  int i;
  int row_comma;
  int column_comma = 0;
  
  if(!rasqal_query_results_is_bindings(results) &&
     !rasqal_query_results_is_boolean(results)) {
    rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Can only write JSON format for variable binding and boolean results");
    return 1;
  }
  
  
  raptor_iostream_counted_string_write("{\n", 2, iostr);
  
  /* Header */
  raptor_iostream_counted_string_write("  \"head\": {\n", 12, iostr);
  
  if(rasqal_query_results_is_bindings(results)) {
    raptor_iostream_counted_string_write("    \"vars\": [ ", 14, iostr);
    for(i = 0; 1; i++) {
      const unsigned char *name;
      
      name = rasqal_query_results_get_binding_name(results, i);
      if(!name)
        break;
      
      /*     'x', */
      if(i > 0)
        raptor_iostream_counted_string_write(", ", 2, iostr);
      raptor_iostream_write_byte('\"', iostr);
      raptor_iostream_string_write(name, iostr);
      raptor_iostream_write_byte('\"', iostr);
    }
    raptor_iostream_counted_string_write(" ]\n", 3, iostr);
  }

  /* FIXME - could add link inside 'head': */
    
  /*   End Header */
  raptor_iostream_counted_string_write("  },\n", 5, iostr);


  /* Boolean Results */
  if(rasqal_query_results_is_boolean(results)) {
    raptor_iostream_counted_string_write("  ", 2, iostr);
    rasqal_iostream_write_json_boolean(iostr, "boolean", 
                                       rasqal_query_results_get_boolean(results));
    goto results3done;
  }

  /* Variable Binding Results */
  raptor_iostream_counted_string_write("  \"results\": {\n", 15, iostr);

  if(query) {
    raptor_iostream_counted_string_write("    ", 4, iostr);
    rasqal_iostream_write_json_boolean(iostr, "ordered", 
                                       (rasqal_query_get_order_condition(query, 0) != NULL));
    raptor_iostream_counted_string_write(",\n", 2, iostr);

    raptor_iostream_counted_string_write("    ", 4, iostr);
    rasqal_iostream_write_json_boolean(iostr, "distinct", 
                                       rasqal_query_get_distinct(query));
    raptor_iostream_counted_string_write(",\n", 2, iostr);
  }
  
  raptor_iostream_counted_string_write("    \"bindings\" : [\n", 19, iostr);

  row_comma = 0;
  while(!rasqal_query_results_finished(results)) {
    if(row_comma)
      raptor_iostream_counted_string_write(",\n", 2, iostr);

    /* Result row */
    raptor_iostream_counted_string_write("      {\n", 8, iostr);

    column_comma = 0;
    for(i = 0; i<rasqal_query_results_get_bindings_count(results); i++) {
      const unsigned char *name = rasqal_query_results_get_binding_name(results, i);
      rasqal_literal *l = rasqal_query_results_get_binding_value(results, i);

      if(column_comma)
        raptor_iostream_counted_string_write(",\n", 2, iostr);

      /*       <binding> */
      raptor_iostream_counted_string_write("        \"", 9, iostr);
      raptor_iostream_string_write(name, iostr);
      raptor_iostream_counted_string_write("\" : { ", 6, iostr);

      if(!l) {
        raptor_iostream_string_write("\"type\": \"unbound\", \"value\": null", iostr);
      } else switch(l->type) {
        const unsigned char* str;
        size_t len;
        
        case RASQAL_LITERAL_URI:
          raptor_iostream_string_write("\"type\": \"uri\", \"value\": \"", iostr);
          str = (const unsigned char*)raptor_uri_as_counted_string(l->value.uri, &len);
          raptor_string_ntriples_write(str, len, '"', iostr);
          raptor_iostream_write_byte('"', iostr);
          break;

        case RASQAL_LITERAL_BLANK:
          raptor_iostream_string_write("\"type\": \"bnode\", \"value\": \"", iostr);
          raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
          raptor_iostream_write_byte('"', iostr);
          break;

        case RASQAL_LITERAL_STRING:
          raptor_iostream_string_write("\"type\": \"literal\", \"value\": \"", iostr);
          raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
          raptor_iostream_write_byte('"', iostr);

          if(l->language) {
            raptor_iostream_string_write(",\n      \"xml:lang\" : \"", iostr);
            raptor_iostream_string_write((const unsigned char*)l->language, iostr);
            raptor_iostream_write_byte('"', iostr);
          }
          
          if(l->datatype) {
            raptor_iostream_string_write(",\n      \"datatype\" : \"", iostr);
            str = (const unsigned char*)raptor_uri_as_counted_string(l->datatype, &len);
            raptor_string_ntriples_write(str, len, '"', iostr);
            raptor_iostream_write_byte('"', iostr);
          }
          
          break;

        case RASQAL_LITERAL_PATTERN:
        case RASQAL_LITERAL_QNAME:
        case RASQAL_LITERAL_INTEGER:
        case RASQAL_LITERAL_XSD_STRING:
        case RASQAL_LITERAL_BOOLEAN:
        case RASQAL_LITERAL_DOUBLE:
        case RASQAL_LITERAL_FLOAT:
        case RASQAL_LITERAL_VARIABLE:
        case RASQAL_LITERAL_DECIMAL:
        case RASQAL_LITERAL_DATETIME:
        case RASQAL_LITERAL_UDT:
        case RASQAL_LITERAL_INTEGER_SUBTYPE:

        case RASQAL_LITERAL_UNKNOWN:
        default:
          rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                                  "Cannot turn literal type %d into XML", 
                                  l->type);
      }

      /* End Binding */
      raptor_iostream_counted_string_write(" }", 2, iostr);
      column_comma = 1;
    }

    /* End Result Row */
    raptor_iostream_counted_string_write("\n      }", 8, iostr);
    row_comma = 1;
    
    rasqal_query_results_next(results);
  }

  raptor_iostream_counted_string_write("\n    ]\n  }", 10, iostr);

  results3done:
  
  /* end sparql */
  raptor_iostream_counted_string_write("\n}\n", 3, iostr);

  return 0;
}