/* add a namespace */ static int raptor_dot_serializer_declare_namespace_from_namespace(raptor_serializer* serializer, raptor_namespace *nspace) { raptor_dot_context * context = (raptor_dot_context *)serializer->context; int i; for( i = 0 ; i < raptor_sequence_size(context->namespaces) ; i++ ) { raptor_namespace * ns; ns = (raptor_namespace *)raptor_sequence_get_at(context->namespaces, i); /* If prefix is already declared, ignore it */ if((!ns->prefix && !nspace->prefix) || (ns->prefix && nspace->prefix && !strcmp((const char*)ns->prefix, (const char*)nspace->prefix)) || (ns->uri && nspace->uri && raptor_uri_equals(ns->uri, nspace->uri)) ) return 1; } nspace = raptor_new_namespace_from_uri(context->nstack, nspace->prefix, nspace->uri, 0); if(!nspace) return 1; raptor_sequence_push(context->namespaces, nspace); return 0; }
/** * librdf_uri_equals: * @first_uri: #librdf_uri object 1 * @second_uri: #librdf_uri object 2 * * Compare two librdf_uri objects for equality. * * Return value: non 0 if the objects are equal **/ int librdf_uri_equals(librdf_uri* first_uri, librdf_uri* second_uri) { LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(first_uri, librdf_uri, 0); LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(second_uri, librdf_uri, 0); return raptor_uri_equals(first_uri, second_uri); }
/** * librdf_uri_equals: * @first_uri: #librdf_uri object 1 * @second_uri: #librdf_uri object 2 * * Compare two librdf_uri objects for equality. * * Return value: non 0 if the objects are equal **/ int librdf_uri_equals(librdf_uri* first_uri, librdf_uri* second_uri) { #ifdef LIBRDF_USE_RAPTOR_URI return raptor_uri_equals(first_uri, second_uri); #else LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(first_uri, librdf_uri, 0); LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(second_uri, librdf_uri, 0); if(!first_uri || !second_uri) return 0; return (first_uri == second_uri); #endif }
static void rasqal_query_write_sparql_triple(sparql_writer_context *wc, raptor_iostream* iostr, rasqal_triple* triple) { rasqal_query_write_sparql_literal(wc, iostr, triple->subject); raptor_iostream_write_byte(' ', iostr); if(triple->predicate->type == RASQAL_LITERAL_URI && raptor_uri_equals(triple->predicate->value.uri, wc->type_uri)) raptor_iostream_write_byte('a', iostr); else rasqal_query_write_sparql_literal(wc, iostr, triple->predicate); raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_literal(wc, iostr, triple->object); raptor_iostream_counted_string_write(" .", 2, iostr); }
/* * rasqal_dataset_graph_present: * @world: rasqal world * @uri: graph URI * * INTERNAL - Check if a graph is in the dataset * * Return value: non-0 if the graph is present **/ int rasqal_dataset_graph_present(rasqal_world* world, raptor_uri* uri) { int offset = 0; while(1) { raptor_uri *graph_uri; graph_uri = rasqal_dataset_enumerate(world, offset++); if(!graph_uri) break; if(raptor_uri_equals(uri, graph_uri)) return 1; } return 0; }
rasqal_literal_type rasqal_xsd_datatype_uri_to_type(rasqal_world* world, raptor_uri* uri) { int i; rasqal_literal_type native_type = RASQAL_LITERAL_UNKNOWN; if(!uri || !world->xsd_datatype_uris) return native_type; for(i = (int)RASQAL_LITERAL_FIRST_XSD; i <= (int)XSD_INTEGER_DERIVED_LAST; i++) { if(raptor_uri_equals(uri, world->xsd_datatype_uris[i])) { if(i >= XSD_INTEGER_DERIVED_FIRST) native_type = RASQAL_LITERAL_INTEGER_SUBTYPE; else native_type = (rasqal_literal_type)i; break; } } return native_type; }
/* add a namespace */ static int raptor_turtle_serialize_declare_namespace_from_namespace(raptor_serializer* serializer, raptor_namespace *nspace) { raptor_turtle_context* context = (raptor_turtle_context*)serializer->context; int i; if(context->written_header) return 1; for(i = 0; i< raptor_sequence_size(context->namespaces); i++) { raptor_namespace* ns; ns = (raptor_namespace*)raptor_sequence_get_at(context->namespaces, i); /* If prefix is already declared, ignore it */ if(!ns->prefix && !nspace->prefix) return 1; if(ns->prefix && nspace->prefix && !strcmp((const char*)ns->prefix, (const char*)nspace->prefix)) return 1; if(ns->uri && nspace->uri && raptor_uri_equals(ns->uri, nspace->uri)) return 1; } nspace = raptor_new_namespace_from_uri(context->nstack, nspace->prefix, nspace->uri, TURTLE_NAMESPACE_DEPTH); if(!nspace) return 1; raptor_sequence_push(context->namespaces, nspace); return 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); }
/* * raptor_dot_serializer_node_matches: * @node: #raptor_node to compare * @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 * * Return value: non-zero if @node matches the node described by the rest of * the parameters. */ static int raptor_dot_serializer_node_matches(raptor_dot_serializer_node* node, raptor_identifier_type node_type, const void* node_data, raptor_uri* datatype, const unsigned char* language) { int rv = 0; if(node->type != node_type) return 0; switch (node->type) { case RAPTOR_IDENTIFIER_TYPE_RESOURCE: case RAPTOR_IDENTIFIER_TYPE_PREDICATE: rv = raptor_uri_equals(node->value.resource.uri, (raptor_uri*)node_data); break; case RAPTOR_IDENTIFIER_TYPE_ANONYMOUS: rv = !strcmp((const char*)node->value.blank.string, (const char*)node_data); break; case RAPTOR_IDENTIFIER_TYPE_LITERAL: case RAPTOR_IDENTIFIER_TYPE_XML_LITERAL: if((char*)node->value.literal.string != NULL && (char*)node_data != NULL) { /* string */ rv = (strcmp((char*)node->value.literal.string, (char*)node_data) == 0); /* language */ if((char*)node->value.literal.language != NULL && (char*)language != NULL) rv &= (strcmp((char*)node->value.literal.language, (char*)language) == 0); else if((char*)node->value.literal.language != NULL || (char*)language != NULL) rv= 0; /* datatype */ if(node->value.literal.datatype != NULL && datatype != NULL) rv &= (raptor_uri_equals(node->value.literal.datatype,datatype) !=0); else if(node->value.literal.datatype != NULL || datatype != NULL) rv = 0; } else { RAPTOR_FATAL1("string must be non-NULL for literal or xml literal\n"); rv = 0; } break; case RAPTOR_IDENTIFIER_TYPE_ORDINAL: case RAPTOR_IDENTIFIER_TYPE_UNKNOWN: default: /* Nothing to do */ break; } return rv; }
unsigned char* raptor_uri_to_relative_counted_uri_string(raptor_uri *base_uri, raptor_uri *reference_uri, size_t *length_p) { raptor_uri_detail *base_detail=NULL, *reference_detail; const unsigned char *base, *reference_str, *base_file, *reference_file; unsigned char *suffix, *cur_ptr; size_t base_len, reference_len, reference_file_len, suffix_len; unsigned char *result=NULL; if(!reference_uri) return NULL; if(length_p) *length_p=0; reference_str=raptor_uri_as_counted_string(reference_uri, &reference_len); reference_detail=raptor_new_uri_detail(reference_str); if(!reference_detail) goto err; if(!base_uri) goto buildresult; base=raptor_uri_as_counted_string(base_uri, &base_len); base_detail=raptor_new_uri_detail(base); if(!base_detail) goto err; /* Check if the whole URIs are equal */ if(raptor_uri_equals(base_uri, reference_uri)) { reference_len=0; goto buildresult; } /* Check if scheme and authority of the URIs are equal */ if(base_detail->scheme_len == reference_detail->scheme_len && base_detail->authority_len == reference_detail->authority_len && !strncmp((const char*)base_detail->scheme, (const char*)reference_detail->scheme, base_detail->scheme_len) && !strncmp((const char*)base_detail->authority, (const char*)reference_detail->authority, base_detail->authority_len)) { if(!base_detail->path) goto buildresult; /* Find the file name components */ base_file = (const unsigned char*)strrchr((const char*)base_detail->path, '/'); if(!base_file) goto buildresult; base_file++; if(!reference_detail->path) goto buildresult; reference_file=(const unsigned char*)strrchr((const char*)reference_detail->path, '/'); if(!reference_file) goto buildresult; reference_file++; reference_file_len=reference_detail->path_len - (reference_file - reference_detail->path); if(!strcmp((const char*)base_file, (const char*)reference_file)) { /* If the file names are equal, don't put them in the relative URI */ reference_file=NULL; reference_file_len=0; } else if(*base_file && !*reference_file) { /* If the base file is non-empty, but the reference file is * empty, use "." as the file name. */ reference_file=(const unsigned char*)"."; reference_file_len=1; } /* Calculate the length of the suffix (file name + query + fragment) */ suffix_len=reference_file_len + reference_detail->query_len + reference_detail->fragment_len; if (reference_detail->query) suffix_len++; /* add one char for the '?' */ if (reference_detail->fragment) suffix_len++; /* add one char for the '#' */ /* Assemble the suffix */ suffix=(unsigned char*)RAPTOR_MALLOC(cstring, suffix_len + sizeof(char*)); if(!suffix) goto err; cur_ptr=suffix; if(reference_file) { memcpy(suffix, reference_file, reference_file_len); cur_ptr+= reference_file_len; } if(reference_detail->query) { *cur_ptr++='?'; memcpy(cur_ptr, reference_detail->query, reference_detail->query_len); cur_ptr+= reference_detail->query_len; } if(reference_detail->fragment) { *cur_ptr++='#'; memcpy(cur_ptr, reference_detail->fragment, reference_detail->fragment_len); cur_ptr+= reference_detail->fragment_len; } *cur_ptr=0; /* Finally, create the full relative path */ result = raptor_uri_path_make_relative_path(base_detail->path, base_detail->path_len, reference_detail->path, reference_detail->path_len, suffix, suffix_len, length_p); RAPTOR_FREE(cstring, suffix); } buildresult: /* If result is NULL at this point, it means that we were unable to find a relative URI, so we'll return a full absolute URI instead. */ if(!result) { result=(unsigned char*)RAPTOR_MALLOC(cstring, reference_len + sizeof(char*)); if(result) { if(reference_len) memcpy(result, reference_str, reference_len); result[reference_len] = 0; if(length_p) *length_p=reference_len; } } err: if(base_detail) raptor_free_uri_detail(base_detail); raptor_free_uri_detail(reference_detail); return result; }
/* return 0 to abort visit */ static int raptor_json_serialize_avltree_visit(int depth, void* data, void *user_data) { raptor_serializer* serializer = (raptor_serializer*)user_data; raptor_json_context* context = (raptor_json_context*)serializer->context; raptor_statement* statement = (raptor_statement*)data; raptor_statement* s1 = statement; raptor_statement* s2 = context->last_statement; int new_subject = 0; int new_predicate = 0; if(s2) { new_subject = !raptor_term_equals(s1->subject, s2->subject); if(new_subject) { /* end last predicate */ raptor_json_writer_newline(context->json_writer); raptor_json_writer_end_block(context->json_writer, ']'); raptor_json_writer_newline(context->json_writer); /* end last statement */ raptor_json_writer_end_block(context->json_writer, '}'); raptor_json_writer_newline(context->json_writer); context->need_subject_comma = 1; context->need_object_comma = 0; } } else new_subject = 1; if(new_subject) { if(context->need_subject_comma) { raptor_iostream_write_byte(',', serializer->iostream); raptor_json_writer_newline(context->json_writer); } /* start triple */ /* subject */ switch(s1->subject->type) { case RAPTOR_TERM_TYPE_URI: raptor_json_writer_key_uri_value(context->json_writer, NULL, 0, s1->subject->value.uri); break; case RAPTOR_TERM_TYPE_BLANK: raptor_iostream_counted_string_write("\"_:", 3, serializer->iostream); raptor_string_python_write(s1->subject->value.blank.string, 0, '"', 2, serializer->iostream); raptor_iostream_write_byte('"', serializer->iostream); break; case RAPTOR_TERM_TYPE_LITERAL: case RAPTOR_TERM_TYPE_UNKNOWN: default: raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Triple has unsupported subject term type %d", s1->subject->type); break; } raptor_iostream_counted_string_write(" : ", 3, serializer->iostream); raptor_json_writer_start_block(context->json_writer, '{'); raptor_json_writer_newline(context->json_writer); } /* predicate */ if(context->last_statement) { if(new_subject) new_predicate = 1; else { new_predicate = !raptor_uri_equals(s1->predicate->value.uri, s2->predicate->value.uri); if(new_predicate) { raptor_json_writer_newline(context->json_writer); raptor_json_writer_end_block(context->json_writer, ']'); raptor_iostream_write_byte(',', serializer->iostream); raptor_json_writer_newline(context->json_writer); } } } else new_predicate = 1; if(new_predicate) { /* start predicate */ raptor_json_writer_key_uri_value(context->json_writer, NULL, 0, s1->predicate->value.uri); raptor_iostream_counted_string_write(" : ", 3, serializer->iostream); raptor_json_writer_start_block(context->json_writer, '['); raptor_iostream_write_byte(' ', serializer->iostream); context->need_object_comma = 0; } if(context->need_object_comma) { raptor_iostream_write_byte(',', serializer->iostream); raptor_json_writer_newline(context->json_writer); } /* object */ switch(s1->object->type) { case RAPTOR_TERM_TYPE_URI: raptor_json_writer_uri_object(context->json_writer, s1->object->value.uri); raptor_json_writer_newline(context->json_writer); break; case RAPTOR_TERM_TYPE_LITERAL: raptor_json_writer_literal_object(context->json_writer, s1->object->value.literal.string, s1->object->value.literal.language, s1->object->value.literal.datatype, "value", "type"); break; case RAPTOR_TERM_TYPE_BLANK: raptor_json_writer_blank_object(context->json_writer, s1->object->value.blank.string); raptor_json_writer_newline(context->json_writer); break; case RAPTOR_TERM_TYPE_UNKNOWN: default: raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Triple has unsupported object term type %d", s1->object->type); break; } /* end triple */ context->need_object_comma = 1; context->last_statement = statement; return 1; }
/** * raptor_term_equals: * @t1: first term * @t2: second term * * Compare a pair of #raptor_term for equality * * Return value: non-0 if the terms are equal */ int raptor_term_equals(raptor_term* t1, raptor_term* t2) { int d = 0; if(!t1 || !t2) return 0; if(t1->type != t2->type) return 0; if(t1 == t2) return 1; switch(t1->type) { case RAPTOR_TERM_TYPE_URI: d = raptor_uri_equals(t1->value.uri, t2->value.uri); break; case RAPTOR_TERM_TYPE_BLANK: if(t1->value.blank.string_len != t2->value.blank.string_len) /* different lengths */ break; d = !strcmp((const char*)t1->value.blank.string, (const char*)t2->value.blank.string); break; case RAPTOR_TERM_TYPE_LITERAL: if(t1->value.literal.string_len != t2->value.literal.string_len) /* different lengths */ break; d = !strcmp((const char*)t1->value.literal.string, (const char*)t2->value.literal.string); if(!d) break; if(t1->value.literal.language && t2->value.literal.language) { /* both have a language */ d = !strcmp((const char*)t1->value.literal.language, (const char*)t2->value.literal.language); if(!d) break; } else if(t1->value.literal.language || t2->value.literal.language) { /* only one has a language - different */ d = 0; break; } if(t1->value.literal.datatype && t2->value.literal.datatype) { /* both have a datatype */ d = raptor_uri_equals(t1->value.literal.datatype, t2->value.literal.datatype); } else if(t1->value.literal.datatype || t2->value.literal.datatype) { /* only one has a datatype - different */ d = 0; } break; case RAPTOR_TERM_TYPE_UNKNOWN: default: break; } return d; }
/* * raptor_turtle_emit_subject: * @serializer: #raptor_serializer object * @subject: subject node * @depth: depth into tree * * Emit a subject node * * Return value: non-0 on failure **/ static int raptor_turtle_emit_subject(raptor_serializer *serializer, raptor_abbrev_subject* subject, int depth) { raptor_turtle_context* context = (raptor_turtle_context*)serializer->context; raptor_turtle_writer* turtle_writer = context->turtle_writer; int blank = 1; int collection = 0; int rc = 0; if(!raptor_abbrev_subject_valid(subject)) return 0; RAPTOR_DEBUG5("Emitting subject node %p refcount %d subject %d object %d\n", subject->node, subject->node->ref_count, subject->node->count_as_subject, subject->node->count_as_object); if(!depth && subject->node->term->type == RAPTOR_TERM_TYPE_BLANK && subject->node->count_as_subject == 1 && subject->node->count_as_object == 1) { RAPTOR_DEBUG2("Skipping subject node %p\n", subject->node); return 0; } if(raptor_avltree_size(subject->properties) == 0) { RAPTOR_DEBUG2("Skipping subject node %p\n", subject->node); return 0; } /* check if we can do collection abbreviation */ if(raptor_avltree_size(subject->properties) >= 2) { raptor_avltree_iterator* iter = NULL; raptor_abbrev_node* pred1; raptor_abbrev_node* pred2; iter = raptor_new_avltree_iterator(subject->properties, NULL, NULL, 1); if(!iter) return 1; pred1 = ((raptor_abbrev_node**)raptor_avltree_iterator_get(iter))[0]; raptor_avltree_iterator_next(iter); pred2 = ((raptor_abbrev_node**)raptor_avltree_iterator_get(iter))[0]; raptor_free_avltree_iterator(iter); if(pred1->term->type == RAPTOR_TERM_TYPE_URI && pred2->term->type == RAPTOR_TERM_TYPE_URI && ( (raptor_uri_equals(pred1->term->value.uri, context->rdf_first_uri) && raptor_uri_equals(pred2->term->value.uri, context->rdf_rest_uri)) || (raptor_uri_equals(pred2->term->value.uri, context->rdf_first_uri) && raptor_uri_equals(pred1->term->value.uri, context->rdf_rest_uri)) ) ) { collection = 1; } } /* emit the subject node */ if(subject->node->term->type == RAPTOR_TERM_TYPE_URI) { rc= raptor_turtle_emit_resource(serializer, subject->node, depth+1); if(rc) return rc; blank = 0; collection = 0; } else if(subject->node->term->type == RAPTOR_TERM_TYPE_BLANK) { if((subject->node->count_as_subject == 1 && subject->node->count_as_object == 0) && depth > 1) { blank = 1; } else if(subject->node->count_as_object == 0) { raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"[]"); blank = 0; } else if(!collection && subject->node->count_as_object > 1) { /* Referred to (used as an object), so needs a nodeID */ const unsigned char* genid = subject->node->term->value.blank.string; size_t len = strlen((const char*)genid); unsigned char* subject_str; subject_str= RAPTOR_MALLOC(unsigned char*, len + 3); if(!subject_str) return 1; subject_str[0]='_'; subject_str[1]=':'; memcpy(&subject_str[2], genid, len + 1); raptor_turtle_writer_raw(turtle_writer, subject_str); RAPTOR_FREE(char*, subject_str); } } if(collection) { raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"("); raptor_turtle_writer_increase_indent(turtle_writer); rc = raptor_turtle_emit_subject_collection_items(serializer, subject, depth+1); raptor_turtle_writer_decrease_indent(turtle_writer); raptor_turtle_writer_newline(turtle_writer); raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)")"); } else { if(blank && depth > 1) raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"["); raptor_turtle_writer_increase_indent(turtle_writer); raptor_turtle_writer_newline(turtle_writer); raptor_turtle_emit_subject_properties(serializer, subject, depth+1); raptor_turtle_writer_decrease_indent(turtle_writer); if(blank && depth > 1) { raptor_turtle_writer_newline(turtle_writer); raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"]"); } } if(depth == 0) { /* NOTE: the space before the . here MUST be there or statements * that end in a numeric literal will be interpreted incorrectly * (the "." will be parsed as part of the literal and statement * left unterminated) */ raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)" ."); raptor_turtle_writer_newline(turtle_writer); raptor_turtle_writer_newline(turtle_writer); } return rc; }
/* * raptor_turtle_emit_subject_collection_items: * @serializer: #raptor_serializer object * @subject: subject node * @depth: depth into tree * * Emit an abbreviated rdf collection of items (rdf:first, rdf:rest) about a subject node. * * Return value: non-0 on failure **/ static int raptor_turtle_emit_subject_collection_items(raptor_serializer* serializer, raptor_abbrev_subject* subject, int depth) { raptor_turtle_context* context = (raptor_turtle_context*)serializer->context; int rv = 0; raptor_avltree_iterator* iter = NULL; int i; int is_new_subject = 0; RAPTOR_DEBUG5("Emitting subject collection items for node %p refcount %d subject %d object %d\n", subject->node, subject->node->ref_count, subject->node->count_as_subject, subject->node->count_as_object); /* if just saw a new subject (is_new_subject is true) then there is no need * to advance the iterator - it was just reset */ for(i = 0, (iter = raptor_new_avltree_iterator(subject->properties, NULL, NULL, 1)); iter && !rv; i++, (rv = is_new_subject ? 0 : raptor_avltree_iterator_next(iter))) { raptor_abbrev_node** nodes; raptor_abbrev_node* predicate; raptor_abbrev_node* object; is_new_subject = 0; nodes = (raptor_abbrev_node**)raptor_avltree_iterator_get(iter); if(!nodes) break; predicate= nodes[0]; object= nodes[1]; if(!raptor_uri_equals(predicate->term->value.uri, context->rdf_first_uri)) { raptor_log_error(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Malformed collection - first predicate is not rdf:first"); return 1; } if(!object) continue; if(i > 0) raptor_turtle_writer_newline(context->turtle_writer); switch(object->term->type) { case RAPTOR_TERM_TYPE_URI: rv = raptor_turtle_emit_resource(serializer, object, depth+1); break; case RAPTOR_TERM_TYPE_LITERAL: rv = raptor_turtle_emit_literal(serializer, object, depth+1); break; case RAPTOR_TERM_TYPE_BLANK: rv = raptor_turtle_emit_blank(serializer, object, depth+1); break; case RAPTOR_TERM_TYPE_UNKNOWN: default: raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Triple has unsupported term type %d", object->term->type); break; } /* Return error if emitting something failed above */ if(rv) return rv; /* last item */ rv = raptor_avltree_iterator_next(iter); if(rv) break; nodes = (raptor_abbrev_node**)raptor_avltree_iterator_get(iter); predicate = nodes[0]; object = nodes[1]; if(!raptor_uri_equals(predicate->term->value.uri, context->rdf_rest_uri)) { raptor_log_error(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Malformed collection - second predicate is not rdf:rest"); return 1; } if(object->term->type == RAPTOR_TERM_TYPE_BLANK) { subject = raptor_abbrev_subject_find(context->blanks, object->term); if(!subject) { raptor_log_error(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Malformed collection - could not find subject for rdf:rest"); return 1; } /* got a <(old)subject> rdf:rest <(new)subject> triple so know * subject has changed and should reset the properties iterator */ if(iter) raptor_free_avltree_iterator(iter); iter = raptor_new_avltree_iterator(subject->properties, NULL, NULL, 1); is_new_subject = 1; } else { if(object->term->type != RAPTOR_TERM_TYPE_URI || !raptor_uri_equals(object->term->value.uri, context->rdf_nil_uri)) { raptor_log_error(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Malformed collection - last rdf:rest resource is not rdf:nil"); return 1; } break; } } if(iter) raptor_free_avltree_iterator(iter); return rv; }
/** * raptor_turtle_writer_literal: * @turtle_writer: Turtle writer object * @nstack: Namespace stack for making a QName for datatype URI * @s: literal string to write (SHARED) * @lang: language tag (may be NULL) * @datatype: datatype URI (may be NULL) * * Write a literal (possibly with lang and datatype) to the Turtle writer. * * Return value: non-0 on failure **/ int raptor_turtle_writer_literal(raptor_turtle_writer* turtle_writer, raptor_namespace_stack *nstack, const unsigned char* s, const unsigned char* lang, raptor_uri* datatype) { /* DBL_MAX = 309 decimal digits */ #define INT_MAX_LEN 309 /* DBL_EPSILON = 52 digits */ #define FRAC_MAX_LEN 52 char* endptr = (char *)s; int written = 0; /* typed literal special cases */ if(datatype) { /* integer */ if(raptor_uri_equals(datatype, turtle_writer->xsd_integer_uri)) { /* FIXME. Work around that gcc < 4.5 cannot disable warn_unused_result */ long gcc_is_stupid = strtol((const char*)s, &endptr, 10); if(endptr != (char*)s && !*endptr) { raptor_iostream_string_write(s, turtle_writer->iostr); /* More gcc madness to 'use' the variable I didn't want */ written = 1 + 0 * (int)gcc_is_stupid; } else { raptor_log_error(turtle_writer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Illegal value for xsd:integer literal."); } /* double, decimal */ } else if(raptor_uri_equals(datatype, turtle_writer->xsd_double_uri) || raptor_uri_equals(datatype, turtle_writer->xsd_decimal_uri)) { /* FIXME. Work around that gcc < 4.5 cannot disable warn_unused_result */ double gcc_is_doubly_stupid = strtod((const char*)s, &endptr); if(endptr != (char*)s && !*endptr) { raptor_iostream_string_write(s, turtle_writer->iostr); /* More gcc madness to 'use' the variable I didn't want */ written = 1 + 0 * (int)gcc_is_doubly_stupid; } else { raptor_log_error(turtle_writer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Illegal value for xsd:double or xsd:decimal literal."); } /* boolean */ } else if(raptor_uri_equals(datatype, turtle_writer->xsd_boolean_uri)) { if(!strcmp((const char*)s, "0") || !strcmp((const char*)s, "false")) { raptor_iostream_string_write("false", turtle_writer->iostr); written = 1; } else if(!strcmp((const char*)s, "1") || !strcmp((const char*)s, "true")) { raptor_iostream_string_write("true", turtle_writer->iostr); written = 1; } else { raptor_log_error(turtle_writer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Illegal value for xsd:boolean literal."); } } } if(written) return 0; if(raptor_turtle_writer_quoted_counted_string(turtle_writer, s, strlen((const char*)s))) return 1; /* typed literal, not a special case */ if(datatype) { raptor_qname* qname; raptor_iostream_string_write("^^", turtle_writer->iostr); qname = raptor_new_qname_from_namespace_uri(nstack, datatype, 10); if(qname) { raptor_turtle_writer_qname(turtle_writer, qname); raptor_free_qname(qname); } else raptor_turtle_writer_reference(turtle_writer, datatype); } else if(lang) { /* literal with language tag */ raptor_iostream_write_byte('@', turtle_writer->iostr); raptor_iostream_string_write(lang, turtle_writer->iostr); } return 0; }