示例#1
0
文件: metadata.c 项目: garlik/4store
int fs_metadata_flush(fs_metadata *m)
{
    raptor_serializer *ser = raptor_new_serializer(m->rw, "turtle");
    if (!ser) {
        fs_error(LOG_CRIT, "cannot create turtle serialiser for metadata");

        return 1;
    }
    raptor_serializer_start_to_filename(ser, m->uri+7);

    raptor_statement st;
    for (int e=0; e < m->length; e++) {
        st.subject = raptor_new_term_from_uri_string(m->rw, (unsigned char *)m->uri);
        st.predicate = raptor_new_term_from_uri_string(m->rw, (unsigned char *)m->entries[e].key);
        st.object = raptor_new_term_from_literal(m->rw,
            (unsigned char *)m->entries[e].val, NULL, NULL);
        raptor_serializer_serialize_statement(ser, &st);
        raptor_free_term(st.subject);
        raptor_free_term(st.predicate);
        raptor_free_term(st.object);
    }

    raptor_serializer_serialize_end(ser);
    raptor_free_serializer(ser);

    return 0;
}
示例#2
0
文件: rdf_term.c 项目: herc/librdf
/**
 * librdf_new_node_from_typed_literal:
 * @world: redland world object
 * @value: literal string value
 * @xml_language: literal XML language (or NULL, empty string)
 * @datatype_uri: URI of typed literal datatype or NULL
 *
 * Constructor - create a new typed literal #librdf_node object.
 * 
 * Only one of @xml_language or @datatype_uri may be given.  If both
 * are given, NULL is returned.  If @xml_language is the empty string,
 * it is the equivalent to NULL.
 *
 * Return value: new #librdf_node object or NULL on failure
 **/
librdf_node*
librdf_new_node_from_typed_literal(librdf_world *world,
                                   const unsigned char *value,
                                   const char *xml_language,
                                   librdf_uri *datatype_uri)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, librdf_world, NULL);
  
  librdf_world_open(world);

  return raptor_new_term_from_literal(world->raptor_world_ptr,
                                      value, datatype_uri,
                                      (const unsigned char*)xml_language);
}
示例#3
0
static void
ser_emit_triple(void* user_data,
                const char* subject, int subject_type,
                const char* predicate_nspace, const char* predicate_name,
                const char *object, int object_type,
                const char *datatype_uri)
{
  raptor_serializer* serializer = (raptor_serializer*)user_data;
  raptor_statement s; /* static */
  raptor_uri* predicate_ns_uri;
  raptor_uri* predicate_uri;
  
  raptor_statement_init(&s, rworld);
  
  if(subject_type == FLICKCURL_TERM_TYPE_RESOURCE)
    s.subject = raptor_new_term_from_uri_string(rworld, (const unsigned char*)subject);
  else /* blank node */
    s.subject = raptor_new_term_from_blank(rworld, (const unsigned char*)subject);

  predicate_ns_uri = raptor_new_uri(rworld, (const unsigned char*)predicate_nspace);
  predicate_uri = raptor_new_uri_from_uri_local_name(rworld, predicate_ns_uri,
                                                     (const unsigned char*)predicate_name);
  s.predicate = raptor_new_term_from_uri(rworld, predicate_uri);
  raptor_free_uri(predicate_uri);
  raptor_free_uri(predicate_ns_uri);

  if(object_type == FLICKCURL_TERM_TYPE_RESOURCE)
    s.object = (void*)raptor_new_term_from_uri_string(rworld, (const unsigned char*)object);
  else if(object_type == FLICKCURL_TERM_TYPE_BLANK)
    s.object = raptor_new_term_from_blank(rworld, (const unsigned char*)subject);
  else {
    /* literal */
    raptor_uri* raptor_datatype_uri = NULL;

    if(datatype_uri)
      raptor_datatype_uri = raptor_new_uri(rworld, (const unsigned char*)datatype_uri);

    s.object = raptor_new_term_from_literal(rworld, (const unsigned char*)object, raptor_datatype_uri, NULL /* language */);

    raptor_free_uri(raptor_datatype_uri);
  }

  raptor_serializer_serialize_statement(serializer, &s);

raptor_statement_clear(&s);

}
示例#4
0
文件: rdf_term.c 项目: herc/librdf
/**
 * librdf_new_node_from_literal:
 * @world: redland world object
 * @string: literal string value
 * @xml_language: literal XML language (or NULL, empty string)
 * @is_wf_xml: non 0 if literal is XML
 *
 * Constructor - create a new literal #librdf_node object.
 * 
 * 0.9.12: xml_space argument deleted
 *
 * An @xml_language cannot be used when @is_wf_xml is non-0. If both
 * are given, NULL is returned.  If @xml_language is the empty string,
 * it is the equivalent to NULL.
 *
 * Return value: new #librdf_node object or NULL on failure
 **/
librdf_node*
librdf_new_node_from_literal(librdf_world *world,
                             const unsigned char *string,
                             const char *xml_language,
                             int is_wf_xml)
{
  librdf_uri* datatype_uri;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, librdf_world, NULL);
  
  librdf_world_open(world);

  datatype_uri = (is_wf_xml ?  LIBRDF_RS_XMLLiteral_URI(world) : NULL);

  return raptor_new_term_from_literal(world->raptor_world_ptr,
                                      string, datatype_uri,
                                      (const unsigned char*)xml_language);
}
示例#5
0
/**
 * rasqal_query_results_get_triple:
 * @query_results: #rasqal_query_results query_results
 *
 * Get the current triple in the result.
 *
 * The return value is a shared #raptor_statement.
 * 
 * Return value: #raptor_statement or NULL if failed or results exhausted
 **/
raptor_statement*
rasqal_query_results_get_triple(rasqal_query_results* query_results)
{
  rasqal_query* query;
  int rc;
  rasqal_triple *t;
  rasqal_literal *s, *p, *o;
  raptor_statement *rs = NULL;
  unsigned char *nodeid;
  int skipped;
  
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, NULL);

 if(query_results->failed || query_results->finished)
    return NULL;
  
  if(!rasqal_query_results_is_graph(query_results))
    return NULL;
  
  query = query_results->query;
  if(!query)
    return NULL;
  
  if(query->verb == RASQAL_QUERY_VERB_DESCRIBE)
    return NULL;

 
  /* ensure we have a row to work on */
  if(rasqal_query_results_ensure_have_row_internal(query_results))
    return NULL;

  skipped = 0;
  while(1) {
    if(skipped) {
      rc = rasqal_query_results_next(query_results);
      if(rc) {
        rs = NULL;
        break;
      }
      query_results->current_triple_result = -1;
    }
    
    if(query_results->current_triple_result < 0)
      query_results->current_triple_result = 0;

    t = (rasqal_triple*)raptor_sequence_get_at(query->constructs,
                                               query_results->current_triple_result);

    rs = &query_results->result_triple;

    s = rasqal_literal_as_node(t->subject);
    if(!s) {
      rasqal_log_error_simple(query_results->world,
                              RAPTOR_LOG_LEVEL_WARN,
                              &query->locator,
                              "Triple with unbound subject skipped");
      skipped = 1;
      continue;
    }

    /* raptor v2 terms are copied, not shared */
    if(rs->subject) {
      raptor_free_term(rs->subject);
      rs->subject = NULL;
    }

    switch(s->type) {
      case RASQAL_LITERAL_URI:
        rs->subject = raptor_new_term_from_uri(query_results->world->raptor_world_ptr,
                                               s->value.uri);
        break;

      case RASQAL_LITERAL_BLANK:
        nodeid = rasqal_prefix_id(query_results->result_count,
                                  (unsigned char*)s->string);
        rasqal_free_literal(s);
        if(!nodeid) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not prefix subject blank identifier");
          return NULL;
        }
        s = rasqal_new_simple_literal(query_results->world, RASQAL_LITERAL_BLANK,
                                      nodeid);
        if(!s) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not create a new subject blank literal");
          return NULL;
        }
        rs->subject = raptor_new_term_from_blank(query_results->world->raptor_world_ptr,
                                                 nodeid);
        break;

      case RASQAL_LITERAL_QNAME:
      case RASQAL_LITERAL_PATTERN:
      case RASQAL_LITERAL_XSD_STRING:
      case RASQAL_LITERAL_BOOLEAN:
      case RASQAL_LITERAL_INTEGER:
      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:
        /* QNames should be gone by the time expression eval happens
         * Everything else is removed by rasqal_literal_as_node() above. 
         */

      case RASQAL_LITERAL_STRING:
        /* string [literal] subjects are not RDF */

      case RASQAL_LITERAL_UNKNOWN:
      default:
        /* case RASQAL_LITERAL_STRING: */
        rasqal_log_error_simple(query_results->world,
                                RAPTOR_LOG_LEVEL_WARN,
                                &query->locator,
                                "Triple with non-URI/blank node subject skipped");
        skipped = 1;
        break;
    }
    if(skipped) {
      if(s)
        rasqal_free_literal(s);
      continue;
    }
    

    p = rasqal_literal_as_node(t->predicate);
    if(!p) {
      rasqal_log_error_simple(query_results->world,
                              RAPTOR_LOG_LEVEL_WARN,
                              &query->locator,
                              "Triple with unbound predicate skipped");
      rasqal_free_literal(s);
      skipped = 1;
      continue;
    }
    switch(p->type) {
      case RASQAL_LITERAL_URI:
        /* raptor v2 terms are copied, not shared */
        if(rs->predicate) {
          raptor_free_term(rs->predicate);
          rs->predicate = NULL;
        }
        rs->predicate = raptor_new_term_from_uri(query_results->world->raptor_world_ptr,
                                                 p->value.uri);
        break;

      case RASQAL_LITERAL_QNAME:
      case RASQAL_LITERAL_PATTERN:
      case RASQAL_LITERAL_XSD_STRING:
      case RASQAL_LITERAL_BOOLEAN:
      case RASQAL_LITERAL_INTEGER:
      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:
        /* QNames should be gone by the time expression eval happens
         * Everything else is removed by rasqal_literal_as_node() above. 
         */

      case RASQAL_LITERAL_BLANK:
      case RASQAL_LITERAL_STRING:
        /* blank node or string [literal] predicates are not RDF */

      case RASQAL_LITERAL_UNKNOWN:
      default:
        rasqal_log_error_simple(query_results->world,
                                RAPTOR_LOG_LEVEL_WARN,
                                &query->locator,
                                "Triple with non-URI predicate skipped");
        skipped = 1;
        break;
    }
    if(skipped) {
      rasqal_free_literal(s);
      if(p)
        rasqal_free_literal(p);
      continue;
    }

    o = rasqal_literal_as_node(t->object);
    if(!o) {
      rasqal_log_error_simple(query_results->world,
                              RAPTOR_LOG_LEVEL_WARN,
                              &query->locator,
                              "Triple with unbound object skipped");
      rasqal_free_literal(s);
      rasqal_free_literal(p);
      skipped = 1;
      continue;
    }

    /* raptor v2 terms are copied, not shared */
    if(rs->object) {
      raptor_free_term(rs->object);
      rs->object = NULL;
    }

    switch(o->type) {
      case RASQAL_LITERAL_URI:
        rs->object = raptor_new_term_from_uri(query_results->world->raptor_world_ptr,
                                              o->value.uri);
        break;

      case RASQAL_LITERAL_BLANK:
        nodeid = rasqal_prefix_id(query_results->result_count,
                                  (unsigned char*)o->string);
        rasqal_free_literal(o);
        if(!nodeid) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not prefix blank identifier");
          rasqal_free_literal(s);
          rasqal_free_literal(p);
          return NULL;
        }
        o = rasqal_new_simple_literal(query_results->world, RASQAL_LITERAL_BLANK,
                                      nodeid);
        if(!o) {
          rasqal_log_error_simple(query_results->world, RAPTOR_LOG_LEVEL_FATAL,
                                  &query->locator,
                                  "Could not create a new subject blank literal");
          rasqal_free_literal(s);
          rasqal_free_literal(p);
          return NULL;
        }
        rs->object = raptor_new_term_from_blank(query_results->world->raptor_world_ptr,
                                                nodeid);
        break;

      case RASQAL_LITERAL_STRING:
        rs->object = raptor_new_term_from_literal(query_results->world->raptor_world_ptr,
                                                  o->string,
                                                  o->datatype,
                                                  (const unsigned char*)o->language);
        break;

      case RASQAL_LITERAL_QNAME:
      case RASQAL_LITERAL_PATTERN:
      case RASQAL_LITERAL_XSD_STRING:
      case RASQAL_LITERAL_BOOLEAN:
      case RASQAL_LITERAL_INTEGER:
      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:
        /* QNames should be gone by the time expression eval happens
         * Everything else is removed by rasqal_literal_as_node() above. 
         */

      case RASQAL_LITERAL_UNKNOWN:
      default:
        rasqal_log_error_simple(query_results->world,
                                RAPTOR_LOG_LEVEL_WARN,
                                &query->locator,
                                "Triple with unknown object skipped");
        skipped = 1;
        break;
    }
    if(skipped) {
      rasqal_free_literal(s);
      rasqal_free_literal(p);
      if(o)
        rasqal_free_literal(o);
      continue;
    }
    
    /* dispose previous triple if any */
    if(query_results->triple) {
      rasqal_free_triple(query_results->triple);
      query_results->triple = NULL;
    }

    /* for saving s, p, o for later disposal */
    query_results->triple = rasqal_new_triple(s, p, o);

    /* got triple, return it */
    break;
  }
  
  return rs;
}
示例#6
0
static void
raptor_librdfa_generate_statement(rdftriple* triple, void* callback_data)
{
  raptor_parser* parser = (raptor_parser*)callback_data;
  raptor_statement *s = &parser->statement;
  raptor_term *subject_term = NULL;
  raptor_term *predicate_term = NULL;
  raptor_uri *predicate_uri = NULL;
  raptor_term *object_term = NULL;

  if(!parser->emitted_default_graph) {
    raptor_parser_start_graph(parser, NULL, 0);
    parser->emitted_default_graph++;
  }

  if(!parser->statement_handler)
    goto cleanup;

  if(!triple->subject || !triple->predicate || !triple->object) {
#ifdef RAPTOR_DEBUG
    RAPTOR_FATAL1("Triple has NULL parts\n");
#else
    rdfa_free_triple(triple);
    return;
#endif
  }
  
  if(triple->predicate[0] == '_') {
    raptor_parser_warning(parser, 
                          "Ignoring RDFa triple with blank node predicate %s.",
                          triple->predicate);
    rdfa_free_triple(triple);
    return;
  }
  
  if(triple->object_type == RDF_TYPE_NAMESPACE_PREFIX) {
#ifdef RAPTOR_DEBUG
    RAPTOR_FATAL1("Triple has namespace object type\n");
#else
    rdfa_free_triple(triple);
    return;
#endif
  }
  
  if((triple->subject[0] == '_') && (triple->subject[1] == ':')) {
    subject_term = raptor_new_term_from_blank(parser->world,
                                              (const unsigned char*)triple->subject + 2);
  } else {
    raptor_uri* subject_uri;
    
    subject_uri = raptor_new_uri(parser->world,
                                 (const unsigned char*)triple->subject);
    subject_term = raptor_new_term_from_uri(parser->world, subject_uri);
    raptor_free_uri(subject_uri);
    subject_uri = NULL;
  }
  s->subject = subject_term;
  

  predicate_uri = raptor_new_uri(parser->world,
                                 (const unsigned char*)triple->predicate);
  if(!predicate_uri)
    goto cleanup;

  predicate_term = raptor_new_term_from_uri(parser->world, predicate_uri);
  raptor_free_uri(predicate_uri);
  predicate_uri = NULL;
  s->predicate = predicate_term;
 

  if(triple->object_type == RDF_TYPE_IRI) {
    if((triple->object[0] == '_') && (triple->object[1] == ':')) {
      object_term = raptor_new_term_from_blank(parser->world,
                                               (const unsigned char*)triple->object + 2);
    } else {
      raptor_uri* object_uri;
      object_uri = raptor_new_uri(parser->world,
                                  (const unsigned char*)triple->object);
      if(!object_uri)
        goto cleanup;

      object_term = raptor_new_term_from_uri(parser->world, object_uri);
      raptor_free_uri(object_uri);
    }
  } else if(triple->object_type == RDF_TYPE_PLAIN_LITERAL) {
    object_term = raptor_new_term_from_literal(parser->world,
                                               (const unsigned char*)triple->object,
                                               NULL,
                                               (const unsigned char*)triple->language);
    
  } else if(triple->object_type == RDF_TYPE_XML_LITERAL) {
    raptor_uri* datatype_uri;
    datatype_uri = raptor_new_uri_from_counted_string(parser->world,
                                                      (const unsigned char*)raptor_xml_literal_datatype_uri_string,
                                                      raptor_xml_literal_datatype_uri_string_len);
    object_term = raptor_new_term_from_literal(parser->world,
                                               (const unsigned char*)triple->object,
                                               datatype_uri,
                                               NULL);
    raptor_free_uri(datatype_uri);
  } else if(triple->object_type == RDF_TYPE_TYPED_LITERAL) {
    raptor_uri *datatype_uri = NULL;
    const unsigned char* language = (const unsigned char*)triple->language;
    
    if(triple->datatype) {
      /* If datatype, no language allowed */
      language = NULL;
      datatype_uri = raptor_new_uri(parser->world,
                                    (const unsigned char*)triple->datatype);
      if(!datatype_uri)
        goto cleanup;
    }
    
    object_term = raptor_new_term_from_literal(parser->world,
                                               (const unsigned char*)triple->object,
                                               datatype_uri,
                                               language);
    raptor_free_uri(datatype_uri);
  } else {
    raptor_log_error_formatted(parser->world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                               "Triple has unknown object term type %u",
                               s->object->type);
    goto cleanup;
  }
  s->object = object_term;
  
  /* Generate statement */
  (*parser->statement_handler)(parser->user_data, s);

  cleanup:
  rdfa_free_triple(triple);
  
  if(subject_term)
    raptor_free_term(subject_term);
  if(predicate_term)
    raptor_free_term(predicate_term);
  if(object_term)
    raptor_free_term(object_term);
}