/* start a serialize */ static int raptor_turtle_serialize_start(raptor_serializer* serializer) { raptor_turtle_context* context=(raptor_turtle_context*)serializer->context; raptor_turtle_writer* turtle_writer; if(context->turtle_writer) raptor_free_turtle_writer(context->turtle_writer); turtle_writer=raptor_new_turtle_writer(serializer->world, serializer->base_uri, serializer->feature_write_base_uri, context->nstack, serializer->iostream, (raptor_simple_message_handler)raptor_serializer_simple_error, serializer); if(!turtle_writer) return 1; raptor_turtle_writer_set_feature(turtle_writer, RAPTOR_FEATURE_WRITER_AUTO_INDENT,1); raptor_turtle_writer_set_feature(turtle_writer, RAPTOR_FEATURE_WRITER_INDENT_WIDTH,2); context->turtle_writer=turtle_writer; return 0; }
/** * raptor_uri_to_turtle_counted_string: * @world: world * @uri: uri * @nstack: namespace stack * @base_uri: base URI * @len_p: Pointer to location to store length of new string (if not NULL) * * Convert #raptor_uri to a string. * Caller has responsibility to free the string. * * Note: This creates and destroys several internal objects for each * call so for more efficient writing, create a turtle serializer. * * Return value: the new string or NULL on failure. The length of * the new string is returned in *@len_p if len_p is not NULL. */ unsigned char* raptor_uri_to_turtle_counted_string(raptor_world *world, raptor_uri* uri, raptor_namespace_stack *nstack, raptor_uri *base_uri, size_t *len_p) { int rc = 1; raptor_iostream* iostr; unsigned char *s = NULL; raptor_turtle_writer* turtle_writer; iostr = raptor_new_iostream_to_string(world, (void**)&s, len_p, malloc); if(!iostr) return NULL; turtle_writer = raptor_new_turtle_writer(world, base_uri, 0, nstack, iostr); if(!turtle_writer) goto tidy; rc = raptor_turtle_writer_uri(turtle_writer, uri); raptor_free_turtle_writer(turtle_writer); tidy: raptor_free_iostream(iostr); if(rc) { free(s); s = NULL; } return s; }
/* start a serialize */ static int raptor_turtle_serialize_start(raptor_serializer* serializer) { raptor_turtle_context* context = (raptor_turtle_context*)serializer->context; raptor_turtle_writer* turtle_writer; int flag; if(context->turtle_writer) raptor_free_turtle_writer(context->turtle_writer); flag = RAPTOR_OPTIONS_GET_NUMERIC(serializer, RAPTOR_OPTION_WRITE_BASE_URI); turtle_writer = raptor_new_turtle_writer(serializer->world, serializer->base_uri, flag, context->nstack, serializer->iostream); if(!turtle_writer) return 1; raptor_turtle_writer_set_option(turtle_writer, RAPTOR_OPTION_WRITER_AUTO_INDENT, 1); raptor_turtle_writer_set_option(turtle_writer, RAPTOR_OPTION_WRITER_INDENT_WIDTH, 2); context->turtle_writer = turtle_writer; return 0; }
/** * raptor_term_turtle_write: * @iostr: iostream for writing * @term: term * @nstack: namespace stack * @base_uri: base URI * * Write #raptor_term to a stream in turtle syntax (using QNames). * * Return value: non-0 on failure */ int raptor_term_turtle_write(raptor_iostream* iostr, raptor_term* term, raptor_namespace_stack *nstack, raptor_uri *base_uri) { int rc = 0; raptor_turtle_writer* turtle_writer; turtle_writer = raptor_new_turtle_writer(term->world, base_uri, 0, nstack, iostr); if(!turtle_writer) { return 1; } if(term->type == RAPTOR_TERM_TYPE_URI) { rc = raptor_uri_turtle_write(term->world, iostr, term->value.uri, nstack, base_uri); } else if(term->type == RAPTOR_TERM_TYPE_LITERAL) { raptor_turtle_writer_literal(turtle_writer, nstack, term->value.literal.string, term->value.literal.language, term->value.literal.datatype); } else if(term->type == RAPTOR_TERM_TYPE_BLANK) { raptor_bnodeid_ntriples_write(term->value.blank.string, term->value.blank.string_len, iostr); } else { rc = 2; } raptor_free_turtle_writer(turtle_writer); return rc; }
/** * raptor_uri_turtle_write: * @world: world * @iostr: iostream for writing * @uri: uri * @nstack: namespace stack * @base_uri: base URI * * Write #raptor_uri to a stream in turtle syntax (using QNames). * * Return value: non-0 on failure */ int raptor_uri_turtle_write(raptor_world *world, raptor_iostream* iostr, raptor_uri* uri, raptor_namespace_stack *nstack, raptor_uri *base_uri) { raptor_qname* qname; raptor_turtle_writer* turtle_writer; turtle_writer = raptor_new_turtle_writer(world, base_uri, 0, nstack, iostr); if(!turtle_writer) { return 1; } qname = raptor_new_qname_from_namespace_uri(nstack, uri, 10); /* XML Names allow leading '_' and '.' anywhere but Turtle does not */ if(qname && !raptor_turtle_is_legal_turtle_qname(qname)) { raptor_free_qname(qname); qname = NULL; } if(qname) { raptor_turtle_writer_qname(turtle_writer, qname); raptor_free_qname(qname); } else { raptor_turtle_writer_reference(turtle_writer, uri); } raptor_free_turtle_writer(turtle_writer); return 0; }
/** * raptor_term_turtle_write: * @iostr: iostream for writing * @term: term * @nstack: namespace stack * @base_uri: base URI * * Write #raptor_term to a stream in turtle syntax (using QNames). * * Note: This creates and destroys several internal objects for each * call so for more efficient writing, create a turtle serializer. * * Return value: non-0 on failure */ int raptor_term_turtle_write(raptor_iostream* iostr, raptor_term* term, raptor_namespace_stack *nstack, raptor_uri *base_uri) { int rc; raptor_turtle_writer* turtle_writer; turtle_writer = raptor_new_turtle_writer(term->world, base_uri, 0, nstack, iostr); if(!turtle_writer) return 1; rc = raptor_turtle_writer_term(turtle_writer, term); raptor_free_turtle_writer(turtle_writer); return rc; }
/** * raptor_uri_turtle_write: * @world: world * @iostr: iostream for writing * @uri: uri * @nstack: namespace stack * @base_uri: base URI * * Write #raptor_uri to a stream in turtle syntax (using QNames). * * Note: This creates and destroys several internal objects for each * call so for more efficient writing, create a turtle serializer. * * Return value: non-0 on failure */ int raptor_uri_turtle_write(raptor_world *world, raptor_iostream* iostr, raptor_uri* uri, raptor_namespace_stack *nstack, raptor_uri *base_uri) { int rc; raptor_turtle_writer* turtle_writer; turtle_writer = raptor_new_turtle_writer(world, base_uri, 0, nstack, iostr); if(!turtle_writer) return 1; rc = raptor_turtle_writer_uri(turtle_writer, uri); raptor_free_turtle_writer(turtle_writer); return rc; }
/* destroy a serializer */ static void raptor_turtle_serialize_terminate(raptor_serializer* serializer) { raptor_turtle_context* context=(raptor_turtle_context*)serializer->context; if(context->turtle_writer) { raptor_free_turtle_writer(context->turtle_writer); context->turtle_writer=NULL; } if(context->rdf_nspace) { raptor_free_namespace(context->rdf_nspace); context->rdf_nspace=NULL; } if(context->namespaces) { int i; /* Note: item 0 in the list is rdf:RDF's namespace and freed above */ for(i=1; i< raptor_sequence_size(context->namespaces); i++) { raptor_namespace* ns; ns =(raptor_namespace*)raptor_sequence_get_at(context->namespaces, i); if(ns) raptor_free_namespace(ns); } raptor_free_sequence(context->namespaces); context->namespaces=NULL; } if(context->subjects) { raptor_free_avltree(context->subjects); context->subjects=NULL; } if(context->blanks) { raptor_free_avltree(context->blanks); context->blanks=NULL; } if(context->nodes) { raptor_free_avltree(context->nodes); context->nodes=NULL; } if(context->nstack) { raptor_free_namespaces(context->nstack); context->nstack=NULL; } if(context->rdf_type) { raptor_free_abbrev_node(context->rdf_type); context->rdf_type=NULL; } if(context->rdf_xml_literal_uri) { raptor_free_uri_v2(serializer->world, context->rdf_xml_literal_uri); context->rdf_xml_literal_uri=NULL; } if(context->rdf_first_uri) { raptor_free_uri_v2(serializer->world, context->rdf_first_uri); context->rdf_first_uri=NULL; } if(context->rdf_rest_uri) { raptor_free_uri_v2(serializer->world, context->rdf_rest_uri); context->rdf_rest_uri=NULL; } if(context->rdf_nil_uri) { raptor_free_uri_v2(serializer->world, context->rdf_nil_uri); context->rdf_nil_uri=NULL; } }
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* ex_ns; raptor_turtle_writer* turtle_writer; raptor_uri* base_uri; raptor_qname* el_name; unsigned long count; raptor_uri* datatype; /* 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); base_uri = raptor_new_uri(world, base_uri_string); turtle_writer = raptor_new_turtle_writer(world, base_uri, 1, nstack, iostr); if(!turtle_writer) { fprintf(stderr, "%s: Failed to create turtle_writer to iostream\n", program); exit(1); } raptor_turtle_writer_set_option(turtle_writer, RAPTOR_OPTION_WRITER_AUTO_INDENT, 1); ex_ns = raptor_new_namespace(nstack, (const unsigned char*)"ex", (const unsigned char*)"http://example.org/ns#", 0); raptor_turtle_writer_namespace_prefix(turtle_writer, ex_ns); raptor_turtle_writer_reference(turtle_writer, base_uri); raptor_turtle_writer_increase_indent(turtle_writer); raptor_turtle_writer_newline(turtle_writer); raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"ex:foo "); raptor_turtle_writer_quoted_counted_string(turtle_writer, longstr, strlen((const char*)longstr)); raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)" ;", 2); raptor_turtle_writer_newline(turtle_writer); el_name = raptor_new_qname_from_namespace_local_name(world, ex_ns, (const unsigned char*)"bar", NULL); raptor_turtle_writer_qname(turtle_writer, el_name); raptor_free_qname(el_name); raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)" ", 1); datatype = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/2001/XMLSchema#decimal"); raptor_turtle_writer_literal(turtle_writer, nstack, (const unsigned char*)"10.0", NULL, datatype); raptor_free_uri(datatype); raptor_turtle_writer_newline(turtle_writer); raptor_turtle_writer_decrease_indent(turtle_writer); raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)".", 1); raptor_turtle_writer_newline(turtle_writer); raptor_free_turtle_writer(turtle_writer); raptor_free_namespace(ex_ns); raptor_free_namespaces(nstack); raptor_free_uri(base_uri); count = raptor_iostream_tell(iostr); #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Freeing iostream\n", program); #endif raptor_free_iostream(iostr); if(count != OUT_BYTES_COUNT) { fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program, (int)count, (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 != count) { fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)count); return 1; } #if RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Made Turtle 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); }