/* * raptor_turtle_emit_subject_list_items: * @serializer: #raptor_serializer object * @subject: subject node * @depth: depth into tree * * Emit an rdf list of items (rdf:li) about a subject node. * * Return value: non-0 on failure **/ static int raptor_turtle_emit_subject_list_items(raptor_serializer* serializer, raptor_abbrev_subject* subject, int depth) { int rv = 0; int i = 0; RAPTOR_DEBUG5("Emitting subject list 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); while(!rv && i < raptor_sequence_size(subject->list_items)) { raptor_abbrev_node* object; object = (raptor_abbrev_node*)raptor_sequence_get_at(subject->list_items, i++); if(!object) continue; 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 rv; }
static void raptor_dot_serializer_write_term(raptor_serializer * serializer, raptor_term* term) { switch(term->type) { case RAPTOR_TERM_TYPE_LITERAL: raptor_dot_iostream_write_string(serializer->iostream, term->value.literal.string); if(term->value.literal.language) { raptor_iostream_write_byte('|', serializer->iostream); raptor_iostream_string_write("Language: ", serializer->iostream); raptor_iostream_string_write(term->value.literal.language, serializer->iostream); } if(term->value.literal.datatype) { raptor_iostream_write_byte('|', serializer->iostream); raptor_iostream_string_write("Datatype: ", serializer->iostream); raptor_dot_serializer_write_uri(serializer, term->value.literal.datatype); } break; case RAPTOR_TERM_TYPE_BLANK: raptor_iostream_counted_string_write("_:", 2, serializer->iostream); raptor_iostream_string_write(term->value.blank.string, serializer->iostream); break; case RAPTOR_TERM_TYPE_URI: raptor_dot_serializer_write_uri(serializer, term->value.uri); break; case RAPTOR_TERM_TYPE_UNKNOWN: default: raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Triple has unsupported term type %d", term->type); } }
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); }
/* * raptor_ntriples_parse_term_internal: * @world: raptor world * @locator: locator object (in/out) (or NULL) * @start: pointer to starting character of string (in) * @dest: destination of string (in) * @lenp: pointer to length of string (in/out) * @dest_lenp: pointer to length of destination string (out) * @end_char: string ending character * @class: string class * * INTERNAL - Parse an N-Triples term with escapes. * * Relies that @dest is long enough; it need only be as large as the * input string @start since when UTF-8 encoding, the escapes are * removed and the result is always less than or equal to length of * input. * * N-Triples strings / URIs are written in ASCII at present; * characters outside the printable ASCII range are discarded with a * warning. See the grammar for full details of the allowed ranges. * * UTF-8 and the \u and \U esapes are both allowed. * * Return value: Non 0 on failure **/ static int raptor_ntriples_parse_term_internal(raptor_world* world, raptor_locator* locator, const unsigned char **start, unsigned char *dest, size_t *lenp, size_t *dest_lenp, char end_char, raptor_ntriples_term_class term_class) { const unsigned char *p = *start; unsigned char c = '\0'; size_t ulen = 0; unsigned long unichar = 0; unsigned int position = 0; int end_char_seen = 0; /* find end of string, fixing backslashed characters on the way */ while(*lenp > 0) { int unichar_width; c = *p; p++; (*lenp)--; if(locator) { locator->column++; locator->byte++; } if(c > 0x7f) { /* just copy the UTF-8 bytes through */ int unichar_len; unichar_len = raptor_unicode_utf8_string_get_char(p - 1, 1 + *lenp, NULL); if(unichar_len < 0 || RAPTOR_GOOD_CAST(size_t, unichar_len) > *lenp) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "UTF-8 encoding error at character %d (0x%02X) found.", c, c); /* UTF-8 encoding had an error or ended in the middle of a string */ return 1; } memmove(dest, p-1, unichar_len); dest += unichar_len; unichar_len--; /* p, *lenp were moved on by 1 earlier */ p += unichar_len; (*lenp) -= unichar_len; if(locator) { locator->column += unichar_len; locator->byte += unichar_len; } continue; } if(c != '\\') { /* finish at non-backslashed end_char */ if(end_char && c == end_char) { end_char_seen = 1; break; } if(!raptor_ntriples_term_valid(c, position, term_class)) { if(end_char) { /* end char was expected, so finding an invalid thing is an error */ raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "Missing terminating '%c' (found '%c')", end_char, c); return 0; } else { /* it's the end - so rewind 1 to save next char */ p--; (*lenp)++; if(locator) { locator->column--; locator->byte--; } if(term_class == RAPTOR_TERM_CLASS_BNODEID && dest[-1] == '.') { /* If bnode id ended on '.' move back one */ dest--; p--; (*lenp)++; if(locator) { locator->column--; locator->byte--; } } break; } } /* otherwise store and move on */ *dest++ = c; position++; continue; } if(!*lenp) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "\\ at end of input."); return 0; } c = *p; p++; (*lenp)--; if(locator) { locator->column++; locator->byte++; } switch(c) { case '"': case '\\': *dest++ = c; break; case 'b': *dest++ = '\b'; break; case 'f': *dest++ = '\f'; break; case 'n': *dest++ = '\n'; break; case 'r': *dest++ = '\r'; break; case 't': *dest++ = '\t'; break; case '<': case '>': case '{': case '}': case '|': case '^': case '`': /* Turtle 2013 allows these in URIs (as well as \" and \\) */ *dest++ = c; break; case 'u': case 'U': ulen = (c == 'u') ? 4 : 8; if(*lenp < ulen) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "%c over end of input.", c); return 0; } if(1) { unsigned int ii; int n = 0; for(ii = 0; ii < ulen; ii++) { char cc = p[ii]; if(!isxdigit(RAPTOR_GOOD_CAST(char, cc))) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "N-Triples string error - illegal hex digit %c in Unicode escape '%c%s...'", cc, c, p); n = 1; break; } } if(n) break; n = sscanf((const char*)p, ((ulen == 4) ? "%04lx" : "%08lx"), &unichar); if(n != 1) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "Illegal Uncode escape '%c%s...'", c, p); break; } } p += ulen; (*lenp) -= ulen; if(locator) { locator->column += RAPTOR_GOOD_CAST(int, ulen); locator->byte += RAPTOR_GOOD_CAST(int, ulen); } if(unichar > raptor_unicode_max_codepoint) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "Illegal Unicode character with code point #x%lX (max #x%lX).", unichar, raptor_unicode_max_codepoint); break; } unichar_width = raptor_unicode_utf8_string_put_char(unichar, dest, 4); if(unichar_width < 0) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "Illegal Unicode character with code point #x%lX.", unichar); break; } /* The destination length is set here to 4 since we know that in * all cases, the UTF-8 encoded output sequence is always shorter * than the input sequence, and the buffer is edited in place. * \uXXXX: 6 bytes input - UTF-8 max 3 bytes output * \uXXXXXXXX: 10 bytes input - UTF-8 max 4 bytes output */ dest += (int)unichar_width; break; default: raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, locator, "Illegal string escape \\%c in \"%s\"", c, (char*)start); return 0; } position++; } /* end while */
/* 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; }
static int raptor_json_serialize_statement(raptor_serializer* serializer, raptor_statement *statement) { raptor_json_context* context = (raptor_json_context*)serializer->context; if(context->is_resource) { raptor_statement* s = raptor_statement_copy(statement); if(!s) return 1; return raptor_avltree_add(context->avltree, s); } if(context->need_subject_comma) { raptor_iostream_write_byte(',', serializer->iostream); raptor_json_writer_newline(context->json_writer); } /* start triple */ raptor_json_writer_start_block(context->json_writer, '{'); raptor_json_writer_newline(context->json_writer); /* subject */ raptor_iostream_string_write((const unsigned char*)"\"subject\" : ", serializer->iostream); switch(statement->subject->type) { case RAPTOR_TERM_TYPE_URI: raptor_json_writer_uri_object(context->json_writer, statement->subject->value.uri); break; case RAPTOR_TERM_TYPE_BLANK: raptor_json_writer_blank_object(context->json_writer, statement->subject->value.blank.string); 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", statement->subject->type); break; } raptor_iostream_write_byte(',', serializer->iostream); raptor_json_writer_newline(context->json_writer); /* predicate */ raptor_iostream_string_write((const unsigned char*)"\"predicate\" : ", serializer->iostream); raptor_json_writer_uri_object(context->json_writer, statement->predicate->value.uri); raptor_iostream_write_byte(',', serializer->iostream); raptor_json_writer_newline(context->json_writer); /* object */ raptor_iostream_string_write((const unsigned char*)"\"object\" : ", serializer->iostream); switch(statement->object->type) { case RAPTOR_TERM_TYPE_URI: raptor_json_writer_uri_object(context->json_writer, statement->object->value.uri); break; case RAPTOR_TERM_TYPE_LITERAL: raptor_json_writer_literal_object(context->json_writer, statement->object->value.literal.string, statement->object->value.literal.language, statement->object->value.literal.datatype, "value", "type"); break; case RAPTOR_TERM_TYPE_BLANK: raptor_json_writer_blank_object(context->json_writer, statement->object->value.blank.string); 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", statement->object->type); break; } raptor_json_writer_newline(context->json_writer); /* end triple */ raptor_json_writer_end_block(context->json_writer, '}'); context->need_subject_comma = 1; return 0; }
/* * raptor_turtle_emit_subject_properties: * @serializer: #raptor_serializer object * @subject: subject node * @depth: depth into tree * * Emit the properties about a subject node. * * Return value: non-0 on failure **/ static int raptor_turtle_emit_subject_properties(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; raptor_abbrev_node* last_predicate = NULL; int rv = 0; raptor_avltree_iterator* iter = NULL; int i; RAPTOR_DEBUG5("Emitting subject properties 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); /* Emit any rdf:_n properties collected */ if(raptor_sequence_size(subject->list_items) > 0) rv = raptor_turtle_emit_subject_list_items(serializer, subject, depth+1); for(i = 0, (iter = raptor_new_avltree_iterator(subject->properties, NULL, NULL, 1)); iter && !rv; i++, (rv = raptor_avltree_iterator_next(iter))) { raptor_abbrev_node** nodes; raptor_abbrev_node* predicate; raptor_abbrev_node* object; raptor_qname *qname; nodes = (raptor_abbrev_node**)raptor_avltree_iterator_get(iter); if(!nodes) break; predicate= nodes[0]; object= nodes[1]; if(!(last_predicate && raptor_abbrev_node_equals(predicate, last_predicate))) { /* no object list abbreviation possible, terminate last object */ if(last_predicate) { raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)" ;"); raptor_turtle_writer_newline(turtle_writer); } qname = raptor_new_qname_from_namespace_uri(context->nstack, predicate->term->value.uri, 10); if(raptor_abbrev_node_equals(predicate, context->rdf_type)) raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"a"); else if(qname) raptor_turtle_writer_qname(turtle_writer, qname); else raptor_turtle_writer_reference(turtle_writer, predicate->term->value.uri); raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)" "); if(qname) raptor_free_qname(qname); } else raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)", "); 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_predicate = predicate; } if(iter) raptor_free_avltree_iterator(iter); return rv; }
/* * 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; }
/* serialize a statement */ static int raptor_turtle_serialize_statement(raptor_serializer* serializer, raptor_statement *statement) { raptor_turtle_context* context = (raptor_turtle_context*)serializer->context; raptor_abbrev_subject* subject = NULL; raptor_abbrev_node* predicate = NULL; raptor_abbrev_node* object = NULL; int rv; raptor_term_type object_type; if(!(statement->subject->type == RAPTOR_TERM_TYPE_URI || statement->subject->type == RAPTOR_TERM_TYPE_BLANK)) { raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Do not know how to serialize node type %d", statement->subject->type); return 1; } subject = raptor_abbrev_subject_lookup(context->nodes, context->subjects, context->blanks, statement->subject); if(!subject) { return 1; } object_type = statement->object->type; if(!(object_type == RAPTOR_TERM_TYPE_URI || object_type == RAPTOR_TERM_TYPE_BLANK || object_type == RAPTOR_TERM_TYPE_LITERAL)) { raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Cannot serialize a triple with object node type %d", object_type); return 1; } object = raptor_abbrev_node_lookup(context->nodes, statement->object); if(!object) return 1; if(statement->predicate->type == RAPTOR_TERM_TYPE_URI) { predicate = raptor_abbrev_node_lookup(context->nodes, statement->predicate); if(!predicate) return 1; rv = raptor_abbrev_subject_add_property(subject, predicate, object); if(rv < 0) { raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Unable to add properties to subject %p", subject); return rv; } } else { raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Do not know how to serialize node type %d", statement->predicate->type); return 1; } if(object_type == RAPTOR_TERM_TYPE_URI || object_type == RAPTOR_TERM_TYPE_BLANK) object->count_as_object++; return 0; }
/** * raptor_xml_escape_string_any: * @world: raptor world * @string: string to XML escape (UTF-8) * @len: length of string * @buffer: the buffer to use for new string (UTF-8) or NULL to just calculate expected length * @length: buffer size * @quote: optional quote character to escape for attribute content, or 0 * @xml_version: XML 1.0 (10) or XML 1.1 (11) * * Return an XML-escaped version a string. * * Follows * <ulink url="http://www.w3.org/TR/xml-c14n#ProcessingModel">Canonical XML rules on Text Nodes and Attribute Nodes</ulink> * * Both: * Replaces <literal>&</literal> and <literal><</literal> * with <literal>&amp;</literal> and <literal>&lt;</literal> * respectively, preserving other characters. * * Text Nodes: * <literal>></literal> is turned into <literal>&gt;</literal> * ##xD is turned into <literal>&##xD;</literal> * * Attribute Nodes: * <literal>></literal> is generated not <literal>&gt</literal>. * ##x9, ##xA and ##xD are turned into * <literal>&##x9;</literal>, * <literal>&##xA;</literal> and * <literal>&##xD;</literal> * entities. * * If @quote is given it can be either of '\'' or '\"' * which will be turned into <literal>&apos;</literal> or * <literal>&quot;</literal> respectively. * ASCII NUL ('\0') or any other character will not be escaped. * * If @buffer is NULL, no work is done but the size of buffer * required is returned. The output in buffer remains in UTF-8. * * If the input @string is empty, a single NUL will be written to the * buffer. * * Return value: the number of bytes required / used or <0 on failure. **/ int raptor_xml_escape_string_any(raptor_world *world, const unsigned char *string, size_t len, unsigned char *buffer, size_t length, char quote, int xml_version) { size_t l; size_t new_len = 0; const unsigned char *p; unsigned char *q; int unichar_len; raptor_unichar unichar; if(!string) return -1; RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, raptor_world, -1); raptor_world_open(world); if(quote != '\"' && quote != '\'') quote='\0'; for(l = len, p = string; l; p++, l--) { if(*p > 0x7f) { unichar_len = raptor_unicode_utf8_string_get_char(p, l, &unichar); if(unichar_len < 0 || RAPTOR_GOOD_CAST(size_t, unichar_len) > l) { raptor_log_error(world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Bad UTF-8 encoding."); return -1; } } else { unichar=*p; unichar_len = 1; } if(unichar == '&') /* & */ new_len+= 5; else if(unichar == '<' || (!quote && unichar == '>')) /* < or > */ new_len+= 4; else if(quote && unichar == (unsigned long)quote) /* ' or " */ new_len+= 6; else if(unichar == 0x0d || (quote && (unichar == 0x09 || unichar == 0x0a))) /* 
 or 	 or &xA; */ new_len+= 5; else if(unichar == 0x7f || (unichar < 0x20 && unichar != 0x09 && unichar != 0x0a)) { if(!unichar || xml_version < 11) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Cannot write illegal XML 1.0 character U+%6lX.", unichar); } else { /* &#xX; */ new_len+= 5; if(unichar > 0x0f) new_len++; } } else new_len+= unichar_len; unichar_len--; /* since loop does len-- */ p += unichar_len; l -= unichar_len; } if(length && new_len > length) return 0; if(!buffer) return RAPTOR_BAD_CAST(int, new_len); for(l = len, p = string, q = buffer; l; p++, l--) { if(*p > 0x7f) { unichar_len = raptor_unicode_utf8_string_get_char(p, l, &unichar); /* if the UTF-8 encoding is bad, we already did return -1 above */ } else { unichar=*p; unichar_len = 1; } if(unichar == '&') { memcpy(q, "&", 5); q+= 5; } else if(unichar == '<') { memcpy(q, "<", 4); q+= 4; } else if(!quote && unichar == '>') { memcpy(q, ">", 4); q+= 4; } else if(quote && unichar == (unsigned long)quote) { if(quote == '\'') memcpy(q, "'", 6); else memcpy(q, """, 6); q+= 6; } else if(unichar == 0x0d || (quote && (unichar == 0x09 || unichar == 0x0a))) { /* &#xX; */ *q++='&'; *q++='#'; *q++='x'; if(unichar == 0x09) *q++ = '9'; else *q++ = 'A'+ ((char)unichar-0x0a); *q++= ';'; } else if(unichar == 0x7f || (unichar < 0x20 && unichar != 0x09 && unichar != 0x0a)) { if(!unichar || xml_version < 11) { raptor_log_error_formatted(world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Cannot write illegal XML 1.0 character U+%6lX.", unichar); } else { /* &#xX; */ *q++ = '&'; *q++ = '#'; *q++ = 'x'; q += raptor_format_integer((char*)q, 3, RAPTOR_GOOD_CAST(unsigned int, unichar), /* base */ 16, -1, '\0'); *q++ = ';'; } } else { /* coverity[negative_returns] * negative unichar_len values are checked and cause return -1 above */ memcpy(q, p, unichar_len); q+= unichar_len; } unichar_len--; /* since loop does len-- */ p += unichar_len; l -= unichar_len; }