Exemple #1
0
static rasqal_data_graph*
rasqal_new_data_graph_common(rasqal_world* world,
                             raptor_uri* uri,
                             raptor_iostream* iostr, raptor_uri* base_uri,
                             raptor_uri* name_uri, int flags,
                             const char* format_type,
                             const char* format_name,
                             raptor_uri* format_uri)
{
  rasqal_data_graph* dg;

  dg = (rasqal_data_graph*)RASQAL_CALLOC(rasqal_data_graph, 1, sizeof(*dg));
  if(dg) {
    dg->world = world;

    dg->usage = 1;

    if(iostr)
      dg->iostr = iostr;
    else if(uri)
      dg->uri = raptor_uri_copy(uri);

    if(name_uri)
      dg->name_uri = raptor_uri_copy(name_uri);

    dg->flags = flags;

    if(format_type) {
      size_t len = strlen(format_type);
      dg->format_type = RASQAL_MALLOC(string, len + 1);
      if(!dg->format_type)
        goto error;

      memcpy(dg->format_type, format_type, len + 1);
    }

    if(format_name) {
      size_t len = strlen(format_name);
      dg->format_name = RASQAL_MALLOC(string, len + 1);
      if(!dg->format_name)
        goto error;

      memcpy(dg->format_name, format_name, len + 1);
    }

    if(format_uri)
      dg->format_uri = raptor_uri_copy(format_uri);

    if(base_uri)
      dg->base_uri = raptor_uri_copy(base_uri);
  }

  return dg;

  error:
  rasqal_free_data_graph(dg);
  return NULL;
}
Exemple #2
0
/*****
** Lee el archivo que contiene las tripletas RDF
** y hace el llamado a la funcion save_triple() para guardarlos
*****/
void rdf_database_read_file(rdf_database db, const char *file)
{
    raptor_world *world = NULL;
    unsigned char *uri_string;
    raptor_uri *uri, *base_uri;

    // parser
    world = raptor_new_world();
    rdf_parser = raptor_new_parser(world, "rdfxml");

    // seteo funcion handler para cada nodo
    raptor_parser_set_statement_handler(rdf_parser, (void*)db, save_triple);

    uri_string = raptor_uri_filename_to_uri_string(file);
    uri = raptor_new_uri(world, uri_string);
    base_uri = raptor_uri_copy(uri);

    // empieza parseo y guardado en memoria
    raptor_parser_parse_file(rdf_parser, uri, base_uri);

    // liberar ram
    raptor_free_parser(rdf_parser);
    raptor_free_uri(base_uri);
    raptor_free_uri(uri);
    raptor_free_memory(uri_string);
    raptor_free_world(world);
}
Exemple #3
0
/**
 * raptor_serializer_start_to_iostream:
 * @rdf_serializer:  the #raptor_serializer
 * @uri: base URI or NULL if no base URI is required
 * @iostream: #raptor_iostream to write serialization to
 * 
 * Start serialization to an iostream with given base URI
 *
 * The passed in @iostream does not become owned by the serializer
 * and can be used by the caller after serializing is done.  It
 * must be destroyed by the caller.
 *
 * Return value: non-0 on failure.
 **/
int
raptor_serializer_start_to_iostream(raptor_serializer *rdf_serializer,
                                    raptor_uri *uri, raptor_iostream *iostream) 
{
  if(rdf_serializer->base_uri)
    raptor_free_uri(rdf_serializer->base_uri);

  if(!iostream)
    return 1;
  
  if(uri)
    uri = raptor_uri_copy(uri);
  
  rdf_serializer->base_uri = uri;
  rdf_serializer->locator.uri = uri;
  rdf_serializer->locator.line = rdf_serializer->locator.column = 0;

  rdf_serializer->iostream = iostream;

  rdf_serializer->free_iostream_on_end = 0;

  if(rdf_serializer->factory->serialize_start)
    return rdf_serializer->factory->serialize_start(rdf_serializer);
  return 0;
}
DllExport int call_conv load_nquad_file(CTXTdecl)
{
	char *filename = p2c_string(reg_term(CTXTdecl 1));

	unsigned char *uri_string;
	raptor_uri *uri, *base_uri;

	world = raptor_new_world();

	rdf_parser = raptor_new_parser(world, "nquads");

	raptor_parser_set_statement_handler(rdf_parser, NULL, handle_term);

	uri_string = raptor_uri_filename_to_uri_string(filename);
	uri = raptor_new_uri(world, uri_string);
	base_uri = raptor_uri_copy(uri);

	raptor_parser_parse_file(rdf_parser, uri, base_uri);

	raptor_free_parser(rdf_parser);

	raptor_free_uri(base_uri);
	raptor_free_uri(uri);
	raptor_free_memory(uri_string);

	raptor_free_world(world);

	return 1;
}
Exemple #5
0
/**
 * raptor_serializer_start_to_string:
 * @rdf_serializer:  the #raptor_serializer
 * @uri: base URI or NULL if no base URI is required
 * @string_p: pointer to location to hold string
 * @length_p: pointer to location to hold length of string (or NULL)
 *
 * Start serializing to a string.
 * 
 * Return value: non-0 on failure.
 **/
int
raptor_serializer_start_to_string(raptor_serializer *rdf_serializer,
                                  raptor_uri *uri,
                                  void **string_p, size_t *length_p) 
{
  if(rdf_serializer->base_uri)
    raptor_free_uri(rdf_serializer->base_uri);

  if(uri)
    rdf_serializer->base_uri = raptor_uri_copy(uri);
  else
    rdf_serializer->base_uri = NULL;
  rdf_serializer->locator.uri = rdf_serializer->base_uri;
  rdf_serializer->locator.line = rdf_serializer->locator.column = 0;


  rdf_serializer->iostream = raptor_new_iostream_to_string(rdf_serializer->world,
                                                           string_p, length_p, 
                                                           NULL);
  if(!rdf_serializer->iostream)
    return 1;

  rdf_serializer->free_iostream_on_end = 1;

  if(rdf_serializer->factory->serialize_start)
    return rdf_serializer->factory->serialize_start(rdf_serializer);
  return 0;
}
Exemple #6
0
int
main(int argc, char *argv[])
{
#ifdef HAVE_RAPTOR
  raptor_world *world = NULL;
  raptor_parser* rdf_parser = NULL;
  unsigned char *uri_string;
  raptor_uri *uri, *base_uri;

  world = raptor_new_world();

  rdf_parser = raptor_new_parser(world, "ntriples");

  raptor_parser_set_statement_handler(rdf_parser, NULL, print_triple);

  uri_string = raptor_uri_filename_to_uri_string(argv[1]);
  uri = raptor_new_uri(world, uri_string);
  base_uri = raptor_uri_copy(uri);

  raptor_parser_parse_file(rdf_parser, uri, base_uri);

  raptor_free_parser(rdf_parser);

  raptor_free_uri(base_uri);
  raptor_free_uri(uri);
  raptor_free_memory(uri_string);

  raptor_free_world(world);
#endif

  return EXIT_SUCCESS;
}
Exemple #7
0
/**
 * librdf_new_uri_from_uri:
 * @old_uri: #librdf_uri object
 *
 * Copy constructor - create a new librdf_uri object from an existing librdf_uri object.
 * 
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_from_uri (librdf_uri* old_uri)
{

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(old_uri, librdf_uri, NULL);

  return raptor_uri_copy(old_uri);
}
/* 
 * rasqal_expression_evaluate_datatype:
 * @e: The expression to evaluate.
 * @eval_context: Evaluation context
 *
 * INTERNAL - Evaluate RASQAL_EXPR_DATATYPE (string literal) expression.
 *
 * Return value: A #rasqal_literal URI value or NULL on failure.
 */
static rasqal_literal*
rasqal_expression_evaluate_datatype(rasqal_expression *e,
                                    rasqal_evaluation_context *eval_context,
                                    int *error_p)
{
  rasqal_world* world = eval_context->world;
  rasqal_literal* l1;
  int free_literal = 1;
  rasqal_variable* v = NULL;
  raptor_uri* dt_uri = NULL;
      
  l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p);
  if(*error_p || !l1)
    goto failed;

  v = rasqal_literal_as_variable(l1);
  if(v) {
    rasqal_free_literal(l1);

    l1 = v->value; /* don't need v after this */

    free_literal = 0;
    if(!l1)
      goto failed;
  }
  
  if(rasqal_literal_get_rdf_term_type(l1) != RASQAL_LITERAL_STRING)
    goto failed;
  
  if(l1->language)
    goto failed;
  
  /* The datatype of a plain literal is xsd:string */
  dt_uri = l1->datatype;
  if(!dt_uri && l1->type == RASQAL_LITERAL_STRING)
    dt_uri = rasqal_xsd_datatype_type_to_uri(l1->world,
                                             RASQAL_LITERAL_XSD_STRING);
  
  if(!dt_uri)
    goto failed;
  
  dt_uri = raptor_uri_copy(dt_uri);

  if(free_literal)
    rasqal_free_literal(l1);

  /* after this dt_uri is owned by result */
  return rasqal_new_uri_literal(world, dt_uri);

  failed:
  if(error_p)
    *error_p = 1;
  
  if(free_literal)
    rasqal_free_literal(l1);

  return NULL;
}
Exemple #9
0
/**
 * rasqal_new_service:
 * @world: rasqal_world object
 * @service_uri: sparql protocol service URI
 * @query_string: query string (or NULL)
 * @data_graphs: sequence of #rasqal_data_graph graphs for service
 *
 * Constructor - create a new rasqal protocol service object.
 *
 * Create a structure to execute a sparql protocol service at
 * @service_uri running the query @query_string and returning
 * a sparql result set.
 *
 * All arguments are copied by the service object
 *
 * Return value: a new #rasqal_query object or NULL on failure
 */
rasqal_service*
rasqal_new_service(rasqal_world* world, raptor_uri* service_uri,
                   const char* query_string,
                   raptor_sequence* data_graphs)
{
  rasqal_service* svc;
  size_t len = 0;
  
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(service_uri, raptor_uri, NULL);
  
  svc = (rasqal_service*)RASQAL_CALLOC(rasqal_service, 1, sizeof(*svc));
  if(!svc)
    return NULL;
  
  svc->world = world;
  svc->service_uri = raptor_uri_copy(service_uri);

  if(query_string) {
    len = strlen(query_string);
    svc->query_string = RASQAL_MALLOC(cstring, len + 1);
    if(!svc->query_string) {
      rasqal_free_service(svc);
      return NULL;
    }
  
    memcpy(svc->query_string, query_string, len + 1);
  }
  svc->query_string_len = len;

  if(data_graphs) {
    int i;
    rasqal_data_graph* dg;
    
#ifdef HAVE_RAPTOR2_API
    svc->data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph,
                                           NULL);

#else
    svc->data_graphs = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_data_graph,
                                           NULL);
#endif
    if(!svc->data_graphs) {
      rasqal_free_service(svc);
      return NULL;
    }

    for(i = 0;
        (dg = (rasqal_data_graph*)raptor_sequence_get_at(data_graphs, i));
        i++) {
      raptor_sequence_push(svc->data_graphs,
                           rasqal_new_data_graph_from_data_graph(dg));
    }
  }
  
  return svc;
}
Exemple #10
0
/**
 * librdf_new_uri_from_uri:
 * @old_uri: #librdf_uri object
 *
 * Copy constructor - create a new librdf_uri object from an existing librdf_uri object.
 * 
 * Return value: a new #librdf_uri object or NULL on failure
 **/
librdf_uri*
librdf_new_uri_from_uri (librdf_uri* old_uri) {

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(old_uri, librdf_uri, NULL);

#ifdef LIBRDF_USE_RAPTOR_URI
  return raptor_uri_copy(old_uri);
#else
  old_uri->usage++;
  return old_uri;
#endif
}
Exemple #11
0
int
raptor_rss_item_set_uri(raptor_rss_item *item, raptor_uri* uri)
{
  RAPTOR_DEBUG3("Set node %p to URI <%s>\n", item,
                raptor_uri_as_string(uri));
  
  item->uri = raptor_uri_copy(uri);
  if(!item->uri)
    return 1;
  
  item->term = raptor_new_term_from_uri(item->world, item->uri);
  return 0;
}
Exemple #12
0
/*
 * rasqal_query_results_getrowsource_sparql_xml:
 * @world: rasqal world object
 * @iostr: #raptor_iostream to read the query results from
 * @base_uri: #raptor_uri base URI of the input format
 *
 * Read the fourth version of the SPARQL XML query results format from an
 * iostream in a format returning a rwosurce - INTERNAL.
 * 
 * Return value: a new rasqal_rowsource or NULL on failure
 **/
static rasqal_rowsource*
rasqal_query_results_get_rowsource_sparql_xml(rasqal_query_results_formatter* formatter,
                                              rasqal_world *world,
                                              rasqal_variables_table* vars_table,
                                              raptor_iostream *iostr,
                                              raptor_uri *base_uri)
{
  rasqal_rowsource_sparql_xml_context* con;
  
  con=(rasqal_rowsource_sparql_xml_context*)RASQAL_CALLOC(rasqal_rowsource_sparql_xml_context, 1, sizeof(rasqal_rowsource_sparql_xml_context));
  if(!con)
    return NULL;

  con->world=world;
  con->base_uri = base_uri ? raptor_uri_copy(base_uri) : NULL;
  con->iostr=iostr;

  con->locator.uri=base_uri;

  con->sax2 = raptor_new_sax2(world->raptor_world_ptr, &con->locator, con);
  if(!con->sax2)
    return NULL;
  
  raptor_sax2_set_start_element_handler(con->sax2,
                                        rasqal_sparql_xml_sax2_start_element_handler);
  raptor_sax2_set_characters_handler(con->sax2,
                                     rasqal_sparql_xml_sax2_characters_handler);

  raptor_sax2_set_end_element_handler(con->sax2,
                                      rasqal_sparql_xml_sax2_end_element_handler);

  con->results_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row, (raptor_data_print_handler)rasqal_row_print);

  con->vars_table = rasqal_new_variables_table_from_variables_table(vars_table);
  
  return rasqal_new_rowsource_from_handler(world, NULL,
                                           con,
                                           &rasqal_rowsource_sparql_xml_handler,
                                           con->vars_table,
                                           0);
}
Exemple #13
0
static int
rasqal_graph_next_dg(rasqal_graph_rowsource_context *con) 
{
  rasqal_query *query = con->rowsource->query;
  rasqal_data_graph *dg;

  con->finished = 0;

  while(1) {
    rasqal_literal *o;

    con->dg_offset++;
    dg = rasqal_query_get_data_graph(query, con->dg_offset);
    if(!dg) {
      con->finished = 1;
      break;
    }
    
    if(!dg->name_uri)
      continue;
    
    o = rasqal_new_uri_literal(query->world, raptor_uri_copy(dg->name_uri));
    if(!o) {
      RASQAL_DEBUG1("Failed to create new URI literal\n");
      con->finished = 1;
      break;
    }

    RASQAL_DEBUG2("Using data graph URI literal <%s>\n",
                  rasqal_literal_as_string(o));
    
    rasqal_rowsource_set_origin(con->rowsource, o);

    /* this passes ownership of o to con->var */
    rasqal_variable_set_value(con->var, o);
      
    break;
  }

  return con->finished;
}
Exemple #14
0
/**
 * raptor_new_xml_element_from_namespace_local_name:
 * @ns: namespace
 * @name: the XML element local name
 * @xml_language: the in-scope XML language (or NULL)
 * @xml_base: base uri (or NULL)
 *
 * Constructor - create a new XML element from an XML namespace and a local name
 *
 * Added in 1.4.16.
 *
 * Return value: a new #raptor_xml_element or NULL on failure
 */
raptor_xml_element*
raptor_new_xml_element_from_namespace_local_name(raptor_namespace *ns,
                                                 const unsigned char *name,
                                                 const unsigned char *xml_language, 
                                                 raptor_uri *xml_base)
{
  raptor_uri *base_uri_copy;
  raptor_qname *qname;
  raptor_xml_element *element = NULL;

  qname = raptor_new_qname_from_namespace_local_name(ns->nstack->world, ns,
                                                     name, NULL);
  if(qname) {
    base_uri_copy = xml_base ? raptor_uri_copy(xml_base) : NULL;
    element = raptor_new_xml_element(qname, xml_language, base_uri_copy);
    if(!element) {
      raptor_free_qname(qname);
      if(base_uri_copy)
        raptor_free_uri(base_uri_copy);
    }
  }
  return element;
}
Exemple #15
0
/**
 * raptor_new_term_from_uri:
 * @world: raptor world
 * @uri: uri
 *
 * Constructor - create a new URI statement term
 *
 * Takes a copy (reference) of the passed in @uri
 *
 * Return value: new term or NULL on failure
*/
raptor_term*
raptor_new_term_from_uri(raptor_world* world, raptor_uri* uri)
{
  raptor_term *t;

  RAPTOR_CHECK_CONSTRUCTOR_WORLD(world);

  if(!uri)
    return NULL;
  
  raptor_world_open(world);

  t = (raptor_term*)RAPTOR_CALLOC(raptor_term, 1, sizeof(*t));
  if(!t)
    return NULL;

  t->usage = 1;
  t->world = world;
  t->type = RAPTOR_TERM_TYPE_URI;
  t->value.uri = raptor_uri_copy(uri);

  return t;
}
Exemple #16
0
/**
 * raptor_sax2_parse_start:
 * @sax2: sax2 object
 * @base_uri: base URI
 *
 * Start an XML SAX2 parse.
 */
void
raptor_sax2_parse_start(raptor_sax2* sax2, raptor_uri *base_uri)
{
  sax2->depth = 0;
  sax2->root_element = NULL;
  sax2->current_element = NULL;

  if(sax2->base_uri)
    raptor_free_uri(sax2->base_uri);
  if(base_uri)
    sax2->base_uri = raptor_uri_copy(base_uri);
  else
    sax2->base_uri = NULL;

#ifdef RAPTOR_XML_LIBXML
  raptor_libxml_sax_init(sax2);

#if LIBXML_VERSION < 20425
  sax2->first_read = 1;
#endif

  if(sax2->xc) {
    raptor_libxml_free(sax2->xc);
    sax2->xc = NULL;
  }
#endif

  raptor_namespaces_clear(&sax2->namespaces);

  if(raptor_namespaces_init(sax2->world, &sax2->namespaces, 1)) {
    /* log a fatal error and set sax2 to failed state
       since the function signature does not currently support returning an error */
    raptor_log_error(sax2->world, RAPTOR_LOG_LEVEL_FATAL, sax2->locator,
                     "raptor_namespaces_init() failed");
    sax2->failed = 1;
  }
}
turtle_input::turtle_input(const std::string& path) {
    raptor_world* world = raptor_new_world();
    raptor_parser* parser = raptor_new_parser(world, "turtle");

    unsigned char* uri_string;
    raptor_uri *uri, *base_uri;
    uri_string = raptor_uri_filename_to_uri_string(path.c_str());
    uri = raptor_new_uri(world, uri_string);
    base_uri = raptor_uri_copy(uri);

    std::pair<triples_t*, raptor_uri*> user_data(&triples_, base_uri);
    raptor_parser_set_statement_handler(parser, (void*)&user_data,
                                        &statement_handler);

    triples_.clear();
    raptor_parser_parse_file(parser, uri, base_uri);

    raptor_free_uri(base_uri);
    raptor_free_uri(uri);
    raptor_free_memory(uri_string);

    raptor_free_parser(parser);
    raptor_free_world(world);
}
Exemple #18
0
/**
 * raptor_serializer_start_to_file_handle:
 * @rdf_serializer:  the #raptor_serializer
 * @uri: base URI or NULL if no base URI is required
 * @fh:  FILE* to serialize to
 *
 * Start serializing to a FILE*.
 * 
 * NOTE: This does not fclose the handle when it is finished.
 *
 * Return value: non-0 on failure.
 **/
int
raptor_serializer_start_to_file_handle(raptor_serializer *rdf_serializer,
                                       raptor_uri *uri, FILE *fh) 
{
  if(rdf_serializer->base_uri)
    raptor_free_uri(rdf_serializer->base_uri);

  if(uri)
    rdf_serializer->base_uri = raptor_uri_copy(uri);
  else
    rdf_serializer->base_uri = NULL;
  rdf_serializer->locator.uri = rdf_serializer->base_uri;
  rdf_serializer->locator.line = rdf_serializer->locator.column = 0;

  rdf_serializer->iostream = raptor_new_iostream_to_file_handle(rdf_serializer->world, fh);
  if(!rdf_serializer->iostream)
    return 1;

  rdf_serializer->free_iostream_on_end = 1;

  if(rdf_serializer->factory->serialize_start)
    return rdf_serializer->factory->serialize_start(rdf_serializer);
  return 0;
}
Exemple #19
0
/**
 * raptor_id_set_add:
 * @set: #raptor_id_set
 * @base_uri: base #raptor_uri of identifier
 * @id: identifier name
 * @id_len: length of identifier
 *
 * INTERNAL - Add an item to the set.
 * 
 * Return value: <0 on failure, 0 on success, 1 if already present
 **/
int
raptor_id_set_add(raptor_id_set* set, raptor_uri *base_uri,
                  const unsigned char *id, size_t id_len)
{
  raptor_base_id_set *base;
  char* item;
  
  if(!base_uri || !id || !id_len)
    return -1;

  base = set->first;
  while(base) {
    if(raptor_uri_equals(base->uri, base_uri))
      break;
    base = base->next;
  }

  if(!base) {
    /* a set for this base_uri not found */
    base = (raptor_base_id_set*)RAPTOR_CALLOC(raptor_base_id_set, 1, 
                                              sizeof(*base));
    if(!base)
      return -1;

    base->world = set->world;

    base->uri = raptor_uri_copy(base_uri);

    base->tree = raptor_new_avltree((raptor_data_compare_handler)strcmp,
                                    free, 0);
  
    /* Add to the start of the list */
    if(set->first)
      set->first->prev = base;
    /* base->prev = NULL; */
    base->next = set->first;

    set->first = base;
  } else {
    /* If not at the start of the list, move there */
    if(base != set->first) {
      /* remove from the list */
      base->prev->next = base->next;
      if(base->next)
        base->next->prev = base->prev;
      /* add at the start of the list */
      set->first->prev = base;
      base->prev = NULL;
      base->next = set->first;
    }
  }
  
  item = (char*)raptor_avltree_search(base->tree, id);

  /* if already there, error */
  if(item) {
#if RAPTOR_DEBUG > 1
    set->misses++;
#endif
    return 1;
  }
  
#if RAPTOR_DEBUG > 1
  set->hits++;
#endif
  
  item = (char*)RAPTOR_MALLOC(cstring, id_len+1);
  if(!item)
    return 1;

  memcpy(item, id, id_len + 1);

  return raptor_avltree_add(base->tree, item);
}
Exemple #20
0
int
main(int argc, char *argv[]) 
{
  raptor_world *world;
  const char *program = raptor_basename(argv[0]);
  raptor_iostream *iostr;
  raptor_namespace_stack *nstack;
  raptor_namespace* foo_ns;
  raptor_xml_writer* xml_writer;
  raptor_uri* base_uri;
  raptor_qname* el_name;
  raptor_xml_element *element;
  unsigned long offset;
  raptor_qname **attrs;
  raptor_uri* base_uri_copy = NULL;

  /* for raptor_new_iostream_to_string */
  void *string = NULL;
  size_t string_len = 0;

  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);
  
  iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL);
  if(!iostr) {
    fprintf(stderr, "%s: Failed to create iostream to string\n", program);
    exit(1);
  }

  nstack = raptor_new_namespaces(world, 1);

  xml_writer = raptor_new_xml_writer(world, nstack, iostr);
  if(!xml_writer) {
    fprintf(stderr, "%s: Failed to create xml_writer to iostream\n", program);
    exit(1);
  }

  base_uri = raptor_new_uri(world, base_uri_string);

  foo_ns = raptor_new_namespace(nstack,
                              (const unsigned char*)"foo",
                              (const unsigned char*)"http://example.org/foo-ns#",
                              0);


  el_name = raptor_new_qname_from_namespace_local_name(world,
                                                       foo_ns,
                                                       (const unsigned char*)"bar", 
                                                       NULL);
  base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL;
  element = raptor_new_xml_element(el_name,
                                  NULL, /* language */
                                  base_uri_copy);

  raptor_xml_writer_start_element(xml_writer, element);
  raptor_xml_writer_cdata_counted(xml_writer, (const unsigned char*)"hello\n", 6);
  raptor_xml_writer_comment_counted(xml_writer, (const unsigned char*)"comment", 7);
  raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n");
  raptor_xml_writer_end_element(xml_writer, element);

  raptor_free_xml_element(element);

  raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n");

  el_name = raptor_new_qname(nstack, 
                             (const unsigned char*)"blah", 
                             NULL /* no attribute value - element */);
  base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL;
  element = raptor_new_xml_element(el_name,
                                   NULL, /* language */
                                   base_uri_copy);

  attrs = (raptor_qname **)RAPTOR_CALLOC(qnamearray, 1, sizeof(raptor_qname*));
  attrs[0] = raptor_new_qname(nstack, 
                              (const unsigned char*)"a",
                              (const unsigned char*)"b" /* attribute value */);
  raptor_xml_element_set_attributes(element, attrs, 1);

  raptor_xml_writer_empty_element(xml_writer, element);

  raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n");

  raptor_free_xml_writer(xml_writer);

  raptor_free_xml_element(element);

  raptor_free_namespace(foo_ns);

  raptor_free_namespaces(nstack);

  raptor_free_uri(base_uri);

  
  offset = raptor_iostream_tell(iostr);

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Freeing iostream\n", program);
#endif
  raptor_free_iostream(iostr);

  if(offset != OUT_BYTES_COUNT) {
    fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program,
            (int)offset, (int)OUT_BYTES_COUNT);
    fputs("[[", stderr);
    (void)fwrite(string, 1, string_len, stderr);
    fputs("]]\n", stderr);
    return 1;
  }
  
  if(!string) {
    fprintf(stderr, "%s: I/O stream failed to create a string\n", program);
    return 1;
  }
  string_len = strlen((const char*)string);
  if(string_len != offset) {
    fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)offset);
    return 1;
  }

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Made XML string of %d bytes\n", program, (int)string_len);
  fputs("[[", stderr);
  (void)fwrite(string, 1, string_len, stderr);
  fputs("]]\n", stderr);
#endif

  raptor_free_memory(string);
  
  raptor_free_world(world);
  
  /* keep gcc -Wall happy */
  return(0);
}
Exemple #21
0
manifest_world*
manifest_new_world(rasqal_world* world)
{
  manifest_world* mw;
  raptor_world* raptor_world_ptr = rasqal_world_get_raptor(world);
  
  mw = (manifest_world*)calloc(sizeof(*mw), 1);
  if(!mw)
    return NULL;
  
  mw->world = world;
  mw->raptor_world_ptr = raptor_world_ptr;

  /* Create Namespace URIs, concept URIs and rasqal literal concepts  */
  mw->rdfs_namespace_uri = raptor_new_uri(raptor_world_ptr, raptor_rdf_schema_namespace_uri);
  mw->mf_namespace_uri = raptor_new_uri(raptor_world_ptr, (const unsigned char*)"http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#");
  mw->t_namespace_uri = raptor_new_uri(raptor_world_ptr, (const unsigned char*)"http://ns.librdf.org/2009/test-manifest#");
  mw->qt_namespace_uri = raptor_new_uri(raptor_world_ptr, (const unsigned char*)"http://www.w3.org/2001/sw/DataAccess/tests/test-query#");
  mw->dawgt_namespace_uri = raptor_new_uri(raptor_world_ptr, (const unsigned char*)"http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#");
  mw->sd_namespace_uri = raptor_new_uri(raptor_world_ptr, (const unsigned char*)"http://www.w3.org/ns/sparql-service-description#");

  mw->mf_Manifest_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->mf_namespace_uri, (const unsigned char*)"Manifest");
  mw->mf_entries_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->mf_namespace_uri, (const unsigned char*)"entries");
  mw->mf_name_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->mf_namespace_uri, (const unsigned char*)"name");
  mw->mf_action_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->mf_namespace_uri, (const unsigned char*)"action");
  mw->mf_result_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->mf_namespace_uri, (const unsigned char*)"result");
  mw->mf_resultCardinality_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->mf_namespace_uri, (const unsigned char*)"resultCardinality");
  mw->rdf_type_uri = raptor_new_uri_for_rdf_concept(raptor_world_ptr, (const unsigned char*)"type");
  mw->rdf_first_uri = raptor_new_uri_for_rdf_concept(raptor_world_ptr, (const unsigned char*)"first");
  mw->rdf_rest_uri = raptor_new_uri_for_rdf_concept(raptor_world_ptr, (const unsigned char*)"rest");
  mw->rdf_nil_uri = raptor_new_uri_for_rdf_concept(raptor_world_ptr, (const unsigned char*)"nil");
  mw->rdfs_comment_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->rdfs_namespace_uri, (const unsigned char*)"comment");
  mw->t_path_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->t_namespace_uri, (const unsigned char*)"path");
  mw->qt_data_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->qt_namespace_uri, (const unsigned char*)"data");
  mw->qt_graphData_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->qt_namespace_uri, (const unsigned char*)"graphData");
  mw->qt_query_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->qt_namespace_uri, (const unsigned char*)"query");
  mw->dawgt_approval_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->dawgt_namespace_uri, (const unsigned char*)"approval");
  mw->sd_entailmentRegime_uri = raptor_new_uri_from_uri_local_name(raptor_world_ptr, mw->sd_namespace_uri, (const unsigned char*)"entailmentRegime");

  mw->mf_Manifest_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->mf_Manifest_uri));
  mw->mf_entries_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->mf_entries_uri));
  mw->mf_name_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->mf_name_uri));
  mw->mf_action_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->mf_action_uri));
  mw->mf_result_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->mf_result_uri));
  mw->mf_resultCardinality_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->mf_resultCardinality_uri));
  mw->rdf_type_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->rdf_type_uri));
  mw->rdf_first_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->rdf_first_uri));
  mw->rdf_rest_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->rdf_rest_uri));
  mw->rdfs_comment_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->rdfs_comment_uri));
  mw->t_path_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->t_path_uri));
  mw->qt_data_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->qt_data_uri));
  mw->qt_graphData_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->qt_graphData_uri));
  mw->qt_query_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->qt_query_uri));
  mw->dawgt_approval_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->dawgt_approval_uri));
  mw->sd_entailmentRegime_literal = rasqal_new_uri_literal(world, raptor_uri_copy(mw->sd_entailmentRegime_uri));

  return mw;
}
/*
 * raptor_dot_serializer_new_node implementation:
 * @node_type: Raptor identifier type
 * @node_data: For node_type RAPTOR_IDENTIFIER_TYPE_ORDINAL, int* to the
 *             ordinal.
 * @datatype: Literal datatype or NULL
 * @language: Literal language or NULL
 *
 * Parts of this is taken from redland librdf_node.h and librdf_node.c
 *
 * Return value: a new raptor_dot_serializer_node
 *
 **/
static raptor_dot_serializer_node *
raptor_dot_serializer_new_node(raptor_identifier_type node_type,
                               const void* node_data,
			       raptor_uri* datatype,
                               const unsigned char *language)
{
  unsigned char *string;
  raptor_dot_serializer_node* node;
  
  if(node_type == RAPTOR_IDENTIFIER_TYPE_UNKNOWN)
    return 0;

  node = (raptor_dot_serializer_node *)RAPTOR_CALLOC(raptor_dot_serializer_node, 1, sizeof(raptor_dot_serializer_node));

  if(node) {
    node->type = node_type;
    
    switch (node_type) {
      case RAPTOR_IDENTIFIER_TYPE_PREDICATE:
        node->type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
        /* intentional fall through */

      case RAPTOR_IDENTIFIER_TYPE_RESOURCE:
        node->value.resource.uri = raptor_uri_copy((raptor_uri*)node_data);
        break;
        
      case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS:
        string=(unsigned char*)RAPTOR_MALLOC(blank,
                                             strlen((char*)node_data)+1);
        strcpy((char*)string, (const char*) node_data);
        node->value.blank.string = string;
        break;
        
      case RAPTOR_IDENTIFIER_TYPE_LITERAL:
      case RAPTOR_IDENTIFIER_TYPE_XML_LITERAL:
        string = (unsigned char*)RAPTOR_MALLOC(literal,
                                               strlen((char*)node_data)+1);
        strcpy((char*)string, (const char*)node_data);
        node->value.literal.string = string;
        
        if(datatype)
          node->value.literal.datatype = raptor_uri_copy(datatype);
        
        if(language) {
          unsigned char *lang;
          lang = (unsigned char*)RAPTOR_MALLOC(language,
                                               strlen((const char*)language)+1);
          strcpy((char*)lang, (const char*)language);
          node->value.literal.language = lang;
        }
        break;
        
      case RAPTOR_IDENTIFIER_TYPE_ORDINAL:
      case RAPTOR_IDENTIFIER_TYPE_UNKNOWN: 
      default:
        RAPTOR_FREE(raptor_dot_serializer_node, node);
    }
    
  }

  return node;
}
Exemple #23
0
int
main(int argc, char *argv[])
{

#if 0
  raptor_uri *xsd_uri;
  raptor_uri *dateTime_uri;
  rasqal_literal *l1, *l2;
  int fn_i;
  raptor_uri* fn_uri;
  const unsigned char *fn_name;
  rasqal_extension_fn fn;
  raptor_sequence *fn_args;
  char *error;
  rasqal_literal *fn_result;
  rasqal_world *world;
  
  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }

  xsd_uri = raptor_new_uri(raptor_xmlschema_datatypes_namespace_uri);
  dateTime_uri = raptor_new_uri_from_uri_local_name(xsd_uri, (const unsigned char*)"dateTime");

  rasqal_init_datatypes();

  fn_args = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_literal, (raptor_sequence_print_handler*)rasqal_literal_print);
  l1 = rasqal_new_string_literal((unsigned char*)strdup("2004-05-04"), NULL, raptor_uri_copy(dateTime_uri), NULL);
  raptor_sequence_push(fn_args, l1);
  l2 = rasqal_new_string_literal((unsigned char*)strdup("2003-01-02"), NULL, raptor_uri_copy(dateTime_uri), NULL);
  raptor_sequence_push(fn_args, l2);
  
  fn_i = 0;
  fn_name = rasqal_xsd_datatype_fns[fn_i].name;
  fn = rasqal_xsd_datatype_fns[fn_i].fn;
  fn_uri = rasqal_xsd_datatype_fns[fn_i].uri;

  error = NULL;
  fn_result = fn(fn_uri, fn_args, &error);
  raptor_free_sequence(fn_args);

  if(!fn_result) {
    if(error)
      fprintf(stderr, "function %s failed with error %s\n", fn_name, error);
    else
      fprintf(stderr, "function %s unknown error\n", fn_name);
  } else {
    fprintf(stderr, "function %s returned result: ", fn_name);
    rasqal_literal_print(fn_result, stderr);
    fputc('\n', stderr);
  }
  

  if(fn_result) 
    rasqal_free_literal(fn_result);

  rasqal_finish_datatypes();
  
  raptor_free_uri(xsd_uri);
  raptor_free_uri(dateTime_uri);

  rasqal_free_world(world);
#endif

  return 0;
}
Exemple #24
0
int
main(int argc, char *argv[]) 
{
  raptor_parser* rdf_parser=NULL;
  unsigned char *uri_string=NULL;
  int free_uri_string=0;
  unsigned char *base_uri_string=NULL;
  int rc;
  int scanning=0;
  const char *syntax_name="rdfxml";
  int strict_mode=0;
  int usage=0;
  int help=0;
  raptor_uri *base_uri;
  raptor_uri *uri;
  char *p;
  char *filename=NULL;

  program=argv[0];
  if((p=strrchr(program, '/')))
    program=p+1;
  else if((p=strrchr(program, '\\')))
    program=p+1;
  argv[0]=program;

  raptor_init();
  
  while (!usage && !help)
  {
    int c;
#ifdef HAVE_GETOPT_LONG
    int option_index = 0;

    c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
    c = getopt (argc, argv, GETOPT_STRING);
#endif
    if (c == -1)
      break;

    switch (c) {
      case 0:
      case '?': /* getopt() - unknown option */
        usage=1;
        break;
        
      case 'a':
        break;

      case 'c':
        count=1;
        break;

      case 'h':
        help=1;
        break;

      case 'n':
        syntax_name="ntriples";
        break;

      case 's':
        scanning=1;
        break;

      case 'q':
        quiet=1;
        break;

      case 'r':
        replace_newlines=1;
        break;

      case 'm':
        if(optarg) {
          if(!strcmp(optarg, "strict"))
            strict_mode=1;
          else if (!strcmp(optarg, "lax"))
            strict_mode=0;
          else {
            fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(m, mode) "'\n",
                    program, optarg);
            fprintf(stderr, "Valid arguments are:\n  - `lax'\n  - `strict'\n");
            usage=1;
          }
        }
        break;

      case 'o':
        if(optarg) {
          if(!strcmp(optarg, "simple"))
            output_format=OUTPUT_FORMAT_SIMPLE;
          else if (!strcmp(optarg, "ntriples"))
            output_format=OUTPUT_FORMAT_NTRIPLES;
          else {
            fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(o, output) "'\n",
                    program, optarg);
            fprintf(stderr, "Valid arguments are:\n  `simple'   for a simple format (default)\n  `ntriples' for N-Triples\n");
            usage=1;
          }
        }
        break;

      case 'i':
        if(optarg) {
          if(raptor_syntax_name_check(optarg))
            syntax_name=optarg;
          else {
            int i;
            
            fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(i, input) "'\n",
                    program, optarg);
            fprintf(stderr, "Valid arguments are:\n");
            for(i=0; 1; i++) {
              const char *help_name;
              const char *help_label;
              if(raptor_syntaxes_enumerate(i, &help_name, &help_label, NULL, NULL))
                break;
              printf("  %-12s for %s\n", help_name, help_label);
            }
            usage=1;
            break;
            
          }
        }
        break;

      case 'w':
        ignore_warnings=1;
        break;
        
      case 'e':
        ignore_errors=1;
        break;

      case 'v':
        fputs(raptor_version_string, stdout);
        fputc('\n', stdout);
        exit(0);
    }
    
  }

  if(optind != argc-1 && optind != argc-2 && !help && !usage) {
    usage=2; /* Title and usage */
  }

  
  if(usage) {
    if(usage>1) {
      fprintf(stderr, title_format_string, raptor_version_string);
      fputs(raptor_short_copyright_string, stderr);
      fputc('\n', stderr);
    }
    fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
                    program);
    exit(1);
  }

  if(help) {
    int i;
    
    printf("Usage: %s [OPTIONS] <source URI> [base URI]\n", program);
    printf(title_format_string, raptor_version_string);
    puts(raptor_short_copyright_string);
    puts("Parse RDF content at the source URI into RDF triples.");
    puts("\nMain options:");
    puts(HELP_TEXT(h, "help            ", "Print this help, then exit"));
    puts(HELP_TEXT(i, "input FORMAT    ", "Set input format to one of:"));
    for(i=0; 1; i++) {
      const char *help_name;
      const char *help_label;
      if(raptor_syntaxes_enumerate(i, &help_name, &help_label, NULL, NULL))
        break;
      printf("    %-12s            %s", help_name, help_label);
      if(!i)
        puts(" (default)");
      else
        putchar('\n');
    }
    puts(HELP_TEXT(o, "output FORMAT   ", "Set output format to one of:"));
    puts("    'simple'                A simple format (default)\n    'ntriples'              N-Triples");
    puts(HELP_TEXT(m, "mode MODE       ", "Set parser mode - 'lax' (default) or 'strict'"));
    puts("\nAdditional options:");
    puts(HELP_TEXT(c, "count           ", "Count triples - no output"));
    puts(HELP_TEXT(e, "ignore-errors   ", "Ignore error messages"));
    puts(HELP_TEXT(q, "quiet           ", "No extra information messages"));
    puts(HELP_TEXT(r, "replace-newlines", "Replace newlines with spaces in literals"));
    puts(HELP_TEXT(s, "scan            ", "Scan for <rdf:RDF> element in source"));
    puts(HELP_TEXT(w, "ignore-warnings ", "Ignore warning messages"));
    puts(HELP_TEXT(v, "version         ", "Print the Raptor version"));
    puts("\nReport bugs to <*****@*****.**>.");
    puts("Raptor home page: http://www.redland.opensource.ac.uk/raptor/");
    exit(0);
  }


  if(optind == argc-1)
    uri_string=(unsigned char*)argv[optind];
  else {
    uri_string=(unsigned char*)argv[optind++];
    base_uri_string=(unsigned char*)argv[optind];
  }

  /* If uri_string is "path-to-file", turn it into a file: URI */
  if(!strcmp((const char*)uri_string, "-")) {
    if(!base_uri_string) {
      fprintf(stderr, "%s: A Base URI is required when reading from standard input.\n",
              program);
      return(1);
    }
    uri_string=NULL;
  } else if(!access((const char*)uri_string, R_OK)) {
    filename=(char*)uri_string;
    uri_string=raptor_uri_filename_to_uri_string(filename);
    free_uri_string=1;
  }

  if(uri_string) {
    uri=raptor_new_uri(uri_string);
    if(!uri) {
      fprintf(stderr, "%s: Failed to create URI for %s\n",
              program, uri_string);
      return(1);
    }
  } else
    uri=NULL; /* stdin */


  if(!base_uri_string) {
    base_uri=raptor_uri_copy(uri);
  } else {
    base_uri=raptor_new_uri(base_uri_string);
    if(!base_uri) {
      fprintf(stderr, "%s: Failed to create URI for %s\n",
              program, base_uri_string);
      return(1);
    }
  }

  rdf_parser=raptor_new_parser(syntax_name);
  if(!rdf_parser) {
    fprintf(stderr, "%s: Failed to create raptor parser type %s\n", program,
            syntax_name);
    return(1);
  }
  
  raptor_set_error_handler(rdf_parser, rdf_parser, rdfdump_error_handler);
  raptor_set_warning_handler(rdf_parser, rdf_parser, rdfdump_warning_handler);
  
  raptor_set_parser_strict(rdf_parser, strict_mode);
  
  if(scanning)
    raptor_set_feature(rdf_parser, RAPTOR_FEATURE_SCANNING, 1);

  if(!quiet) {
    if (filename) {
      if(base_uri_string)
        fprintf(stdout, "%s: Parsing file %s with base URI %s\n", program,
                filename, base_uri_string);
      else
        fprintf(stdout, "%s: Parsing file %s\n", program, filename);
    } else {
      if(base_uri_string)
        fprintf(stdout, "%s: Parsing URI %s with base URI %s\n", program,
                uri_string, base_uri_string);
      else
        fprintf(stdout, "%s: Parsing URI %s\n", program, uri_string);
    }
  }
  
  raptor_set_statement_handler(rdf_parser, NULL, print_statements);


  /* PARSE the URI as RDF/XML */
  rc=0;
  if(!uri || filename) {
    if(raptor_parse_file(rdf_parser, uri, base_uri)) {
      fprintf(stderr, "%s: Failed to parse file %s %s content\n", program, 
              filename, syntax_name);
      rc=1;
    }
  } else {
    if(raptor_parse_uri(rdf_parser, uri, base_uri)) {
      fprintf(stderr, "%s: Failed to parse URI %s %s content\n", program, 
              uri_string, syntax_name);
      rc=1;
    }
  }

  raptor_free_parser(rdf_parser);

  if(!quiet)
    fprintf(stdout, "%s: Parsing returned %d statements\n", program,
            statement_count);

  raptor_free_uri(base_uri);
  if(uri)
    raptor_free_uri(uri);
  if(free_uri_string)
    free(uri_string);

  raptor_finish();

  if(error_count && !ignore_errors)
    return 1;

  if(warning_count && !ignore_warnings)
    return 2;

  return(rc);
}
Exemple #25
0
int
main(int argc, char *argv[])
{
    int query_from_string = 0;
    unsigned char *query_string = NULL;
    unsigned char *uri_string = NULL;
    int free_uri_string = 0;
    unsigned char *base_uri_string = NULL;
    rasqal_query *rq = NULL;
    rasqal_query_results *results;
    const char *ql_name = "sparql";
    char *ql_uri = NULL;
    int rc = 0;
    raptor_uri *uri = NULL;
    raptor_uri *base_uri = NULL;
    char *filename = NULL;
    char *p;
    int usage = 0;
    int help = 0;
    int quiet = 0;
    int count = 0;
    int dryrun = 0;
    raptor_sequence* data_graphs = NULL;
    const char *result_format = NULL;
    query_output_format output_format = QUERY_OUTPUT_UNKNOWN;
    rasqal_feature query_feature = (rasqal_feature)-1;
    int query_feature_value= -1;
    unsigned char* query_feature_string_value = NULL;
    rasqal_world *world;
    raptor_world* raptor_world_ptr = NULL;
#ifdef RASQAL_INTERNAL
    int store_results = -1;
#endif
    char* data_graph_parser_name = NULL;
    raptor_iostream* iostr = NULL;
    const unsigned char* service_uri_string = 0;
    raptor_uri* service_uri = NULL;

    program = argv[0];
    if((p = strrchr(program, '/')))
        program = p + 1;
    else if((p = strrchr(program, '\\')))
        program = p + 1;
    argv[0] = program;

    world = rasqal_new_world();
    if(!world || rasqal_world_open(world)) {
        fprintf(stderr, "%s: rasqal_world init failed\n", program);
        return(1);
    }

    raptor_world_ptr = rasqal_world_get_raptor(world);
    rasqal_world_set_log_handler(world, world, roqet_log_handler);

#ifdef STORE_RESULTS_FLAG
    /* This is for debugging only */
    if(1) {
        char* sr = getenv("RASQAL_DEBUG_STORE_RESULTS");
        if(sr)
            store_results = atoi(sr);
    }
#endif

    while (!usage && !help)
    {
        int c;

#ifdef HAVE_GETOPT_LONG
        int option_index = 0;

        c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
        c = getopt (argc, argv, GETOPT_STRING);
#endif
        if (c == -1)
            break;

        switch (c) {
        case 0:
        case '?': /* getopt() - unknown option */
            usage = 1;
            break;

        case 'c':
            count = 1;
            break;

        case 'd':
            output_format = QUERY_OUTPUT_UNKNOWN;
            if(optarg) {
                int i;

                for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
                    if(!strcmp(optarg, query_output_format_labels[i][0])) {
                        output_format = (query_output_format)i;
                        break;
                    }

            }
            if(output_format == QUERY_OUTPUT_UNKNOWN) {
                int i;
                fprintf(stderr,
                        "%s: invalid argument `%s' for `" HELP_ARG(d, dump-query) "'\n",
                        program, optarg);
                for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
                    fprintf(stderr,
                            "  %-12s for %s\n", query_output_format_labels[i][0],
                            query_output_format_labels[i][1]);
                usage = 1;
            }
            break;


        case 'e':
            if(optarg) {
                query_string = (unsigned char*)optarg;
                query_from_string = 1;
            }
            break;

        case 'f':
            if(optarg) {
                if(!strcmp(optarg, "help")) {
                    int i;

                    fprintf(stderr, "%s: Valid query features are:\n", program);
                    for(i = 0; i < (int)rasqal_get_feature_count(); i++) {
                        const char *feature_name;
                        const char *feature_label;
                        if(!rasqal_features_enumerate(world, (rasqal_feature)i,
                                                      &feature_name, NULL,
                                                      &feature_label)) {
                            const char *feature_type;
                            feature_type = (rasqal_feature_value_type((rasqal_feature)i) == 0) ? "" : " (string)";
                            fprintf(stderr, "  %-20s  %s%s\n", feature_name, feature_label,
                                    feature_type);
                        }
                    }
                    fputs("Features are set with `" HELP_ARG(f, feature) " FEATURE=VALUE or `-f FEATURE'\nand take a decimal integer VALUE except where noted, defaulting to 1 if omitted.\n", stderr);

                    rasqal_free_world(world);
                    exit(0);
                } else {
                    int i;
                    size_t arg_len = strlen(optarg);

                    for(i = 0; i < (int)rasqal_get_feature_count(); i++) {
                        const char *feature_name;
                        size_t len;

                        if(rasqal_features_enumerate(world, (rasqal_feature)i,
                                                     &feature_name, NULL, NULL))
                            continue;

                        len = strlen(feature_name);

                        if(!strncmp(optarg, feature_name, len)) {
                            query_feature = (rasqal_feature)i;
                            if(rasqal_feature_value_type(query_feature) == 0) {
                                if(len < arg_len && optarg[len] == '=')
                                    query_feature_value=atoi(&optarg[len + 1]);
                                else if(len == arg_len)
                                    query_feature_value = 1;
                            } else {
                                if(len < arg_len && optarg[len] == '=')
                                    query_feature_string_value = (unsigned char*)&optarg[len + 1];
                                else if(len == arg_len)
                                    query_feature_string_value = (unsigned char*)"";
                            }
                            break;
                        }
                    }

                    if(query_feature_value < 0 && !query_feature_string_value) {
                        fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(f, feature) "'\nTry '%s " HELP_ARG(f, feature) " help' for a list of valid features\n",
                                program, optarg, program);
                        usage = 1;
                    }
                }
            }
            break;

        case 'F':
            if(optarg) {
                if(!raptor_world_is_parser_name(raptor_world_ptr, optarg)) {
                    fprintf(stderr,
                            "%s: invalid parser name `%s' for `" HELP_ARG(F, format) "'\n\n",
                            program, optarg);
                    usage = 1;
                } else {
                    data_graph_parser_name = optarg;
                }
            }
            break;

        case 'h':
            help = 1;
            break;

        case 'n':
            dryrun = 1;
            break;

        case 'p':
            if(optarg)
                service_uri_string = (const unsigned char*)optarg;
            break;

        case 'r':
            if(optarg)
                result_format = optarg;
            break;

        case 'i':
            if(rasqal_language_name_check(world, optarg))
                ql_name = optarg;
            else {
                int i;

                fprintf(stderr,
                        "%s: invalid argument `%s' for `" HELP_ARG(i, input) "'\n",
                        program, optarg);
                fprintf(stderr, "Valid arguments are:\n");
                for(i = 0; 1; i++) {
                    const raptor_syntax_description* desc;
                    desc = rasqal_world_get_query_language_description(world, i);
                    if(desc == NULL)
                        break;

                    fprintf(stderr, "  %-18s for %s\n", desc->names[0], desc->label);
                }
                usage = 1;
            }
            break;

        case 'q':
            quiet = 1;
            break;

        case 's':
        case 'D':
        case 'G':
            if(optarg) {
                rasqal_data_graph *dg = NULL;
                rasqal_data_graph_flags type;

                type = (c == 's' || c == 'G') ? RASQAL_DATA_GRAPH_NAMED :
                       RASQAL_DATA_GRAPH_BACKGROUND;

                if(!strcmp((const char*)optarg, "-")) {
                    /* stdin: use an iostream not a URI data graph */
                    unsigned char* source_uri_string;
                    raptor_uri* iostr_base_uri = NULL;
                    raptor_uri* graph_name = NULL;

                    /* FIXME - get base URI from somewhere else */
                    source_uri_string = (unsigned char*)"file:///dev/stdin";

                    iostr_base_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
                    if(iostr_base_uri) {
                        iostr = raptor_new_iostream_from_file_handle(raptor_world_ptr,
                                stdin);
                        if(iostr)
                            dg = rasqal_new_data_graph_from_iostream(world,
                                    iostr, iostr_base_uri,
                                    graph_name,
                                    type,
                                    NULL,
                                    data_graph_parser_name,
                                    NULL);
                    }

                    if(base_uri)
                        raptor_free_uri(base_uri);
                } else if(!access((const char*)optarg, R_OK)) {
                    /* file: use URI */
                    unsigned char* source_uri_string;
                    raptor_uri* source_uri;
                    raptor_uri* graph_name = NULL;

                    source_uri_string = raptor_uri_filename_to_uri_string((const char*)optarg);
                    source_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
                    raptor_free_memory(source_uri_string);

                    if(type == RASQAL_DATA_GRAPH_NAMED)
                        graph_name = source_uri;

                    if(source_uri)
                        dg = rasqal_new_data_graph_from_uri(world,
                                                            source_uri,
                                                            graph_name,
                                                            type,
                                                            NULL, data_graph_parser_name,
                                                            NULL);

                    if(source_uri)
                        raptor_free_uri(source_uri);
                } else {
                    raptor_uri* source_uri;
                    raptor_uri* graph_name = NULL;

                    /* URI: use URI */
                    source_uri = raptor_new_uri(raptor_world_ptr,
                                                (const unsigned char*)optarg);
                    if(type == RASQAL_DATA_GRAPH_NAMED)
                        graph_name = source_uri;

                    if(source_uri)
                        dg = rasqal_new_data_graph_from_uri(world,
                                                            source_uri,
                                                            graph_name,
                                                            type,
                                                            NULL, data_graph_parser_name,
                                                            NULL);

                    if(source_uri)
                        raptor_free_uri(source_uri);
                }

                if(!dg) {
                    fprintf(stderr, "%s: Failed to create data graph for `%s'\n",
                            program, optarg);
                    return(1);
                }

                if(!data_graphs) {
                    data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph,
                                                      NULL);

                    if(!data_graphs) {
                        fprintf(stderr, "%s: Failed to create data graphs sequence\n",
                                program);
                        return(1);
                    }
                }

                raptor_sequence_push(data_graphs, dg);
            }
            break;

        case 'W':
            if(optarg)
                warning_level = atoi(optarg);
            else
                warning_level = 0;
            rasqal_world_set_warning_level(world, warning_level);
            break;

        case 'E':
            ignore_errors = 1;
            break;

        case 'v':
            fputs(rasqal_version_string, stdout);
            fputc('\n', stdout);
            rasqal_free_world(world);
            exit(0);

#ifdef STORE_RESULTS_FLAG
        case STORE_RESULTS_FLAG:
            store_results = (!strcmp(optarg, "yes") || !strcmp(optarg, "YES"));
            break;
#endif

        }

    }

    if(!help && !usage) {
        if(service_uri_string) {
            if(optind != argc && optind != argc-1)
                usage = 2; /* Title and usage */
        } else if(query_string) {
            if(optind != argc && optind != argc-1)
                usage = 2; /* Title and usage */
        } else {
            if(optind != argc-1 && optind != argc-2)
                usage = 2; /* Title and usage */
        }
    }


    if(usage) {
        if(usage > 1) {
            fprintf(stderr, title_format_string, rasqal_version_string);
            fputs("Rasqal home page: ", stderr);
            fputs(rasqal_home_url_string, stderr);
            fputc('\n', stderr);
            fputs(rasqal_copyright_string, stderr);
            fputs("\nLicense: ", stderr);
            fputs(rasqal_license_string, stderr);
            fputs("\n\n", stderr);
        }
        fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
                program);
        rasqal_free_world(world);

        exit(1);
    }

    if(help) {
        int i;

        printf(title_format_string, rasqal_version_string);
        puts("Run an RDF query giving variable bindings or RDF triples.");
        printf("Usage: %s [OPTIONS] <query URI> [base URI]\n", program);
        printf("       %s [OPTIONS] -e <query string> [base URI]\n", program);
        printf("       %s [OPTIONS] -p <SPARQL protocol service URI> -e <query string> [base URI]\n\n", program);

        fputs(rasqal_copyright_string, stdout);
        fputs("\nLicense: ", stdout);
        puts(rasqal_license_string);
        fputs("Rasqal home page: ", stdout);
        puts(rasqal_home_url_string);

        puts("\nNormal operation is to execute the query retrieved from URI <query URI>");
        puts("and print the results in a simple text format.");
        puts("\nMain options:");
        puts(HELP_TEXT("e", "exec QUERY      ", "Execute QUERY string instead of <query URI>"));
        puts(HELP_TEXT("p", "protocol URI    ", "Execute QUERY against a SPARQL protocol service URI"));
        puts(HELP_TEXT("i", "input LANGUAGE  ", "Set query language name to one of:"));
        for(i = 0; 1; i++) {
            const raptor_syntax_description* desc;

            desc = rasqal_world_get_query_language_description(world, i);
            if(!desc)
                break;

            printf("    %-15s         %s", desc->names[0], desc->label);
            if(!i)
                puts(" (default)");
            else
                putchar('\n');
        }

        puts(HELP_TEXT("r", "results FORMAT  ", "Set query results output format to one of:"));
        puts("    For variable bindings and boolean results:");
        puts("      simple                A simple text format (default)");

        for(i = 0; 1; i++) {
            const raptor_syntax_description* desc;

            desc = rasqal_world_get_query_results_format_description(world, i);
            if(!desc)
                break;

            if(desc->flags & RASQAL_QUERY_RESULTS_FORMAT_FLAG_WRITER)
                printf("      %-10s            %s\n", desc->names[0], desc->label);
        }

        puts("    For RDF graph results:");

        for(i = 0; 1; i++) {
            const raptor_syntax_description *desc;
            desc = raptor_world_get_parser_description(raptor_world_ptr, i);
            if(!desc)
                break;

            printf("      %-15s       %s", desc->names[0], desc->label);
            if(!i)
                puts(" (default)");
            else
                putchar('\n');
        }
        puts("\nAdditional options:");
        puts(HELP_TEXT("c", "count             ", "Count triples - no output"));
        puts(HELP_TEXT("d FORMAT", "dump-query FORMAT", HELP_PAD "Print the parsed query out in FORMAT:"));
        puts(HELP_TEXT("D URI", "data URI      ", "RDF data source URI"));
        for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
            printf("      %-15s         %s\n", query_output_format_labels[i][0],
                   query_output_format_labels[i][1]);
        puts(HELP_TEXT("E", "ignore-errors     ", "Ignore error messages"));
        puts(HELP_TEXT("f FEATURE(=VALUE)", "feature FEATURE(=VALUE)", HELP_PAD "Set query features" HELP_PAD "Use `-f help' for a list of valid features"));
        puts(HELP_TEXT("F NAME", "format NAME  ", "Set data source format name (default: guess)"));
        puts(HELP_TEXT("G URI", "named URI     ", "RDF named graph data source URI"));
        puts(HELP_TEXT("h", "help              ", "Print this help, then exit"));
        puts(HELP_TEXT("n", "dryrun            ", "Prepare but do not run the query"));
        puts(HELP_TEXT("q", "quiet             ", "No extra information messages"));
        puts(HELP_TEXT("s URI", "source URI    ", "Same as `-G URI'"));
        puts(HELP_TEXT("v", "version           ", "Print the Rasqal version"));
        puts(HELP_TEXT("w", "walk-query        ", "Print query.  Same as '-d structure'"));
        puts(HELP_TEXT("W LEVEL", "warnings LEVEL", HELP_PAD "Set warning message LEVEL from 0: none to 100: all"));
#ifdef STORE_RESULTS_FLAG
        puts(HELP_TEXT_LONG("store-results BOOL", "DEBUG: Set store results yes/no BOOL"));
#endif
        puts("\nReport bugs to http://bugs.librdf.org/");

        rasqal_free_world(world);

        exit(0);
    }


    if(service_uri_string) {
        service_uri = raptor_new_uri(raptor_world_ptr, service_uri_string);
        if(optind == argc-1) {
            base_uri_string = (unsigned char*)argv[optind];
        }
    } else if(query_string) {
        if(optind == argc-1)
            base_uri_string = (unsigned char*)argv[optind];
    } else {
        if(optind == argc-1)
            uri_string = (unsigned char*)argv[optind];
        else {
            uri_string = (unsigned char*)argv[optind++];
            base_uri_string = (unsigned char*)argv[optind];
        }

        /* If uri_string is "path-to-file", turn it into a file: URI */
        if(!strcmp((const char*)uri_string, "-")) {
            if(!base_uri_string) {
                fprintf(stderr, "%s: A Base URI is required when reading from standard input.\n",
                        program);
                return(1);
            }

            uri_string = NULL;
        } else if(!access((const char*)uri_string, R_OK)) {
            filename = (char*)uri_string;
            uri_string = raptor_uri_filename_to_uri_string(filename);
            free_uri_string = 1;
        }

        if(uri_string) {
            uri = raptor_new_uri(raptor_world_ptr, uri_string);
            if(!uri) {
                fprintf(stderr, "%s: Failed to create URI for %s\n",
                        program, uri_string);
                return(1);
            }
        } else
            uri = NULL; /* stdin */
    }

    if(!base_uri_string) {
        if(uri)
            base_uri = raptor_uri_copy(uri);
    } else
        base_uri = raptor_new_uri(raptor_world_ptr, base_uri_string);

    if(base_uri_string && !base_uri) {
        fprintf(stderr, "%s: Failed to create URI for %s\n",
                program, base_uri_string);
        return(1);
    }


    if(service_uri_string) {
        /* NOP - nothing to do here */
    } else if(query_string) {
        /* NOP - already got it */
    } else if(!uri_string) {
        size_t read_len;

        query_string = (unsigned char*)calloc(FILE_READ_BUF_SIZE, 1);
        read_len = fread(query_string, FILE_READ_BUF_SIZE, 1, stdin);
        if(read_len < FILE_READ_BUF_SIZE) {
            if(ferror(stdin))
                fprintf(stderr, "%s: query string stdin read failed - %s\n",
                        program, strerror(errno));
            else
                fprintf(stderr, "%s: query string stdin read failed - no error\n",
                        program);
            rc = 1;
            goto tidy_setup;
        }

        query_from_string = 0;
    } else if(filename) {
        raptor_stringbuffer *sb = raptor_new_stringbuffer();
        size_t len;
        FILE *fh;
        unsigned char* buffer;

        fh = fopen(filename, "r");
        if(!fh) {
            fprintf(stderr, "%s: file '%s' open failed - %s",
                    program, filename, strerror(errno));
            rc = 1;
            goto tidy_setup;
        }

        buffer = (unsigned char*)malloc(FILE_READ_BUF_SIZE);

        while(!feof(fh)) {
            size_t read_len;

            read_len = fread((char*)buffer, 1, FILE_READ_BUF_SIZE, fh);
            if(read_len > 0)
                raptor_stringbuffer_append_counted_string(sb, buffer, read_len, 1);

            if(read_len < FILE_READ_BUF_SIZE) {
                if(ferror(fh)) {
                    fprintf(stderr, "%s: file '%s' read failed - %s\n",
                            program, filename, strerror(errno));
                    free(buffer);
                    fclose(fh);
                    rc = 1;
                    goto tidy_setup;
                }

                break;
            }
        }
        free(buffer);
        fclose(fh);

        len = raptor_stringbuffer_length(sb);
        query_string = (unsigned char*)malloc(len + 1);
        raptor_stringbuffer_copy_to_string(sb, query_string, len);
        raptor_free_stringbuffer(sb);
        query_from_string = 0;
    } else {
        raptor_www *www;

        www = raptor_new_www(raptor_world_ptr);
        if(www) {
            raptor_www_fetch_to_string(www, uri, (void**)&query_string, NULL, malloc);
            raptor_free_www(www);
        }

        if(!query_string || error_count) {
            fprintf(stderr, "%s: Retrieving query at URI '%s' failed\n",
                    program, uri_string);
            rc = 1;
            goto tidy_setup;
        }

        query_from_string = 0;
    }


    if(!quiet) {
        if(service_uri) {
            fprintf(stderr, "%s: Calling SPARQL service at URI %s", program,
                    service_uri_string);
            if(query_string)
                fprintf(stderr, " with query '%s'", query_string);

            if(base_uri_string)
                fprintf(stderr, " with base URI %s\n", base_uri_string);

            fputc('\n', stderr);
        } else if(query_from_string) {
            if(base_uri_string)
                fprintf(stderr, "%s: Running query '%s' with base URI %s\n", program,
                        query_string, base_uri_string);
            else
                fprintf(stderr, "%s: Running query '%s'\n", program,
                        query_string);
        } else if(filename) {
            if(base_uri_string)
                fprintf(stderr, "%s: Querying from file %s with base URI %s\n", program,
                        filename, base_uri_string);
            else
                fprintf(stderr, "%s: Querying from file %s\n", program, filename);
        } else if(uri_string) {
            if(base_uri_string)
                fprintf(stderr, "%s: Querying URI %s with base URI %s\n", program,
                        uri_string, base_uri_string);
            else
                fprintf(stderr, "%s: Querying URI %s\n", program, uri_string);
        }
    }



    if(service_uri) {
        /* Execute query remotely */
        if(!dryrun)
            results = roqet_call_sparql_service(world, service_uri, query_string,
                                                data_graphs,
                                                /* service_format */ NULL);
    } else {
        /* Execute query in this query engine (from URI or from -e QUERY) */
        rq = roqet_init_query(world,
                              ql_name, ql_uri, query_string,
                              base_uri,
                              query_feature, query_feature_value,
                              query_feature_string_value,
                              store_results,
                              data_graphs);

        if(!rq) {
            rc = 1;
            goto tidy_query;
        }

        if(output_format != QUERY_OUTPUT_UNKNOWN && !quiet)
            roqet_print_query(rq, raptor_world_ptr, output_format, base_uri);

        if(!dryrun) {
            results = rasqal_query_execute(rq);
        }

    }


    /* No results from dryrun */
    if(dryrun)
        goto tidy_query;

    if(!results) {
        fprintf(stderr, "%s: Query execution failed\n", program);
        rc = 1;
        goto tidy_query;
    }

    if(rasqal_query_results_is_bindings(results)) {
        if(result_format)
            rc = print_formatted_query_results(world, results,
                                               raptor_world_ptr, stdout,
                                               result_format, base_uri, quiet);
        else
            print_bindings_result_simple(results, stdout, quiet, count);
    } else if(rasqal_query_results_is_boolean(results)) {
        if(result_format)
            rc = print_formatted_query_results(world, results,
                                               raptor_world_ptr, stdout,
                                               result_format, base_uri, quiet);
        else
            print_boolean_result_simple(results, stdout, quiet);
    } else if(rasqal_query_results_is_graph(results)) {
        if(!result_format)
            result_format = DEFAULT_GRAPH_FORMAT;

        rc = print_graph_result(rq, results, raptor_world_ptr,
                                stdout, result_format, base_uri, quiet);
    } else {
        fprintf(stderr, "%s: Query returned unknown result format\n", program);
        rc = 1;
    }

    rasqal_free_query_results(results);

tidy_query:
    if(!query_from_string)
        free(query_string);

    if(rq)
        rasqal_free_query(rq);

tidy_setup:

    if(data_graphs)
        raptor_free_sequence(data_graphs);
    if(base_uri)
        raptor_free_uri(base_uri);
    if(uri)
        raptor_free_uri(uri);
    if(free_uri_string)
        raptor_free_memory(uri_string);
    if(iostr)
        raptor_free_iostream(iostr);
    if(service_uri)
        raptor_free_uri(service_uri);

    rasqal_free_world(world);

    if(error_count && !ignore_errors)
        return 1;

    if(warning_count && warning_level != 0)
        return 2;

    return (rc);
}
Exemple #26
0
/**
 * raptor_www_get_final_uri:
 * @www: #raptor_www object 
 *
 * Get the WWW final resolved URI.
 * 
 * This returns the URI used after any protocol redirection.
 *
 * Return value: a new URI or NULL if not known.
 **/
raptor_uri*
raptor_www_get_final_uri(raptor_www* www) 
{
  return www->final_uri ? raptor_uri_copy(www->final_uri) : NULL;
}
Exemple #27
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) 
{
  size_t uri_string_len;
  size_t len;
  unsigned char *new_uri_string;
  librdf_uri *new_uri;
  unsigned char* source_uri_string;
  size_t source_uri_string_length;
  unsigned char* base_uri_string;
  size_t base_uri_string_length;

                                    
  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 raptor_uri_copy(base_uri);
  }
  
  source_uri_string = librdf_uri_as_counted_string(source_uri,
                                                   &source_uri_string_length);
  base_uri_string = librdf_uri_as_counted_string(base_uri,
                                                 &base_uri_string_length);

  /* not a fragment, and no match - easy */
  if(*uri_string != '#' &&
     strncmp((const char*)uri_string, (const char*)source_uri_string,
             source_uri_string_length)) {
    raptor_world* rworld = raptor_uri_get_world(base_uri);
    return raptor_new_uri(rworld, 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 = LIBRDF_MALLOC(unsigned char*, 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 = raptor_new_uri(raptor_uri_get_world(source_uri), new_uri_string);
  LIBRDF_FREE(char*, new_uri_string); /* always free this even on failure */

  return new_uri; /* new URI or NULL from librdf_new_uri failure */
}
Exemple #28
0
raptor_abbrev_node* 
raptor_new_abbrev_node(raptor_identifier_type node_type, const void *node_data,
                       raptor_uri *datatype, const unsigned char *language)
{
  unsigned char *string;
  raptor_abbrev_node* node=NULL;
  
  if(node_type == RAPTOR_IDENTIFIER_TYPE_UNKNOWN)
    return 0;

  node = (raptor_abbrev_node*)RAPTOR_CALLOC(raptor_abbrev_node, 1,
                                            sizeof(raptor_abbrev_node));

  if(node) {
    node->ref_count = 1;
    node->type = node_type;
    
    switch (node_type) {
        case RAPTOR_IDENTIFIER_TYPE_PREDICATE:
          node->type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
          /* intentional fall through */
        case RAPTOR_IDENTIFIER_TYPE_RESOURCE:
          node->value.resource.uri = raptor_uri_copy((raptor_uri*)node_data);
          break;
          
        case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS:
          string=(unsigned char*)RAPTOR_MALLOC(blank,
                                               strlen((char*)node_data)+1);
          if(!string)
            goto oom;
          strcpy((char*)string, (const char*) node_data);
          node->value.blank.string = string;
          break;
          
        case RAPTOR_IDENTIFIER_TYPE_ORDINAL:
          node->value.ordinal.ordinal = *(int *)node_data;
          break;
          
        case RAPTOR_IDENTIFIER_TYPE_LITERAL:
        case RAPTOR_IDENTIFIER_TYPE_XML_LITERAL:
          string = (unsigned char*)RAPTOR_MALLOC(literal,
                                                 strlen((char*)node_data)+1);
          if(!string)
            goto oom;
          strcpy((char*)string, (const char*)node_data);
          node->value.literal.string = string;

          if(datatype) {
            node->value.literal.datatype = raptor_uri_copy(datatype);
          }

          if(language) {
            unsigned char *lang;
            lang=(unsigned char*)RAPTOR_MALLOC(language,
                                               strlen((const char*)language)+1);
            if(!lang) {
              RAPTOR_FREE(literal, string);
              goto oom;
            }
            strcpy((char*)lang, (const char*)language);
            node->value.literal.language = lang;
          }
          break;
          
        case RAPTOR_IDENTIFIER_TYPE_UNKNOWN: 
        default:
          RAPTOR_FREE(raptor_abbrev_node, node);
    }
    
  }

  return node;

  /* out of memory - clean up and return NULL */
  oom:
  RAPTOR_FREE(raptor_abbrev_node, node);
  return NULL;
}
Exemple #29
0
int
main(int argc, char *argv[])
{
  raptor_world *world = NULL;
  raptor_parser* rdf_parser = NULL;
  unsigned char *uri_string;
  raptor_uri *uri;
  raptor_uri *base_uri;
  int rc = 0;
  int free_uri_string = 0;

  rdf_serializer = NULL;

  if(argc < 2 || argc > 3) {
    fprintf(stderr, "USAGE: %s RDF-FILE [BASE-URI]\n", program);
    rc = 1;
    goto tidy;
  }

  world = raptor_new_world();

  uri_string = (unsigned char*)argv[1];
  if(!access((const char*)uri_string, R_OK)) {
    uri_string = raptor_uri_filename_to_uri_string((char*)uri_string);
    uri = raptor_new_uri(world, uri_string);
    free_uri_string = 1;
  } else {
    uri = raptor_new_uri(world, (const unsigned char*)uri_string);
  }

  if(argc == 3) {
    char* base_uri_string = argv[2];
    base_uri = raptor_new_uri(world, (unsigned char*)(base_uri_string));
  } else {
    base_uri = raptor_uri_copy(uri);
  }

  rdf_parser = raptor_new_parser(world, "guess");

  raptor_world_set_log_handler(world, rdf_parser, to_ntriples_log_handler);

  raptor_parser_set_statement_handler(rdf_parser, NULL,
                                      to_ntriples_write_triple);

  rdf_serializer = raptor_new_serializer(world, "ntriples");

  raptor_serializer_start_to_file_handle(rdf_serializer, base_uri, stdout);
  raptor_parser_parse_file(rdf_parser, uri, base_uri);
  raptor_serializer_serialize_end(rdf_serializer);

  raptor_free_serializer(rdf_serializer);
  raptor_free_parser(rdf_parser);

  raptor_free_uri(base_uri);
  raptor_free_uri(uri);
  if(free_uri_string)
    raptor_free_memory(uri_string);

  raptor_free_world(world);

  tidy:
  if(warning_count)
    rc = 2;
  else if(error_count)
    rc = 1;

  return rc;
}
Exemple #30
0
/* 
 * rasqal_expression_evaluate_strdt:
 * @e: The expression to evaluate.
 * @eval_context: Evaluation context
 *
 * INTERNAL - Evaluate RASQAL_EXPR_STRDT(expr) expression.
 *
 * Return value: A #rasqal_literal string value or NULL on failure.
 */
static rasqal_literal*
rasqal_expression_evaluate_strdt(rasqal_expression *e,
                                 rasqal_evaluation_context *eval_context,
                                 int *error_p)
{
  rasqal_world* world = eval_context->world;
  rasqal_literal *l1 = NULL;
  rasqal_literal *l2 = NULL;
  const unsigned char* s = NULL;
  unsigned char* new_s = NULL;
  size_t len;
  raptor_uri* dt_uri = NULL;
  
  l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p);
  if(*error_p || !l1)
    goto failed;

  if(l1->language || l1->datatype) {
    /* not a simple literal so return NULL success */
    rasqal_free_literal(l1);
    return NULL;
  }
  
  s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p);
  if(*error_p)
    goto failed;
  
  l2 = rasqal_expression_evaluate2(e->arg2, eval_context, error_p);
  if(*error_p || !l2)
    goto failed;
  
  dt_uri = rasqal_literal_as_uri(l2);
  if(dt_uri) {
    dt_uri = raptor_uri_copy(dt_uri);
  } else {
    const unsigned char *uri_string;
    
    uri_string = rasqal_literal_as_string_flags(l2, eval_context->flags,
                                                error_p);
    if(*error_p)
      goto failed;

    dt_uri = raptor_new_uri(world->raptor_world_ptr, uri_string);
    if(!dt_uri)
      goto failed;
  }
  
  new_s =(unsigned char*)RASQAL_MALLOC(cstring, len + 1);
  if(!new_s)
    goto failed;
  memcpy(new_s, s, len + 1);

  rasqal_free_literal(l1);
  rasqal_free_literal(l2);
  
  /* after this new_s and dt_uri become owned by result */
  return rasqal_new_string_literal(world, new_s, /* language */ NULL,
                                   dt_uri, /* qname */ NULL);
  
  
  failed:
  if(error_p)
    *error_p = 1;
  
  if(new_s)
    RASQAL_FREE(cstring, new_s);
  if(l1)
    rasqal_free_literal(l1);
  if(l2)
    rasqal_free_literal(l2);

  return NULL;
}