/** * 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; }
/** * raptor_term_to_counted_string: * @term: #raptor_term * @len_p: Pointer to location to store length of new string (if not NULL) * * Turns part of raptor term into a N-Triples format counted string. * * Turns the given @term into an N-Triples escaped string using all the * escapes as defined in http://www.w3.org/TR/rdf-testcases/#ntriples * * This function uses raptor_term_ntriples_write() to write to an * #raptor_iostream which is the prefered way to write formatted * output. * * 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_term_to_counted_string(raptor_term *term, size_t* len_p) { raptor_iostream *iostr; void *string = NULL; int rc; RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(term, raptor_term, NULL); iostr = raptor_new_iostream_to_string(term->world, &string, len_p, NULL); if(!iostr) return NULL; rc = raptor_term_ntriples_write(term, iostr); raptor_free_iostream(iostr); if(rc) { if(string) { RAPTOR_FREE(cstring, string); string = NULL; } } return (unsigned char *)string; }
/** * 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; }
static int test_write_to_string(raptor_world *world, const char* test_string, size_t test_string_len, const unsigned int expected_bytes_count) { raptor_iostream *iostr = NULL; unsigned long count; int rc = 0; void *string = NULL; size_t string_len; const char* const label="write iostream to a string"; #if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Testing %s\n", program, label); #endif iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL); if(!iostr) { fprintf(stderr, "%s: Failed to create write iostream to string\n", program); rc = 1; goto tidy; } raptor_iostream_write_bytes(test_string, 1, test_string_len, iostr); raptor_iostream_write_byte('\n', iostr); count = raptor_iostream_tell(iostr); if(count != expected_bytes_count) { fprintf(stderr, "%s: %s wrote %d bytes, expected %d\n", program, label, (int)count, expected_bytes_count); rc = 1; } raptor_free_iostream(iostr); iostr = NULL; if(!string) { fprintf(stderr, "%s: %s failed to create a string\n", program, label); return 1; } if(string_len != count) { fprintf(stderr, "%s: %s created a string length %d, expected %d\n", program, label, (int)string_len, (int)count); return 1; } tidy: if(string) raptor_free_memory(string); if(iostr) raptor_free_iostream(iostr); if(rc) fprintf(stderr, "%s: FAILED Testing %s\n", program, label); return rc; }
/** * librdf_query_results_to_counted_string2: * @query_results: #librdf_query_results object * @name: name of syntax to format to * @mime_type: mime type of syntax to format to (or NULL) * @format_uri: URI of syntax to format to (or NULL) * @base_uri: Base URI of output formatted syntax (or NULL) * @length_p: Pointer to where to store length of string (or NULL) * * Turn a query results into a string. * * A query results format can be named, have a mime type, or * identified by a URI, all of which are optional. The default * query results format will be used if @name, @mime_type and * @format_uri are all NULL. * * librdf_query_results_formats_enumerate() returns information on * the known query results names, labels and URIs. * * The @base_uri may be used for as the base URI the generated * syntax, depending on the format. * * The returned string must be freed by the caller using * librdf_free_memory(). * * Return value: new string value or NULL on failure **/ unsigned char* librdf_query_results_to_counted_string2(librdf_query_results *query_results, const char *name, const char *mime_type, librdf_uri *format_uri, librdf_uri *base_uri, size_t *length_p) { librdf_query_results_formatter *formatter; void *string=NULL; size_t string_length=0; raptor_iostream *iostr; int error=0; LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, librdf_query_results, NULL); iostr = raptor_new_iostream_to_string(query_results->query->world->raptor_world_ptr, &string, &string_length, malloc); if(!iostr) return NULL; formatter = librdf_new_query_results_formatter2(query_results, name, mime_type, format_uri); if(!formatter) { error=1; goto tidy; } if(librdf_query_results_formatter_write(iostr, formatter, query_results, base_uri)) error=1; librdf_free_query_results_formatter(formatter); tidy: raptor_free_iostream(iostr); /* string is available only after the iostream is finished * - clean it up here on error */ if(error) { if(string) { raptor_free_memory(string); string=NULL; } } else if(length_p) *length_p = string_length; return (unsigned char *)string; }
/* * Return the specified +node+ as an NTriples string. */ VALUE rleaf_librdf_node_to_string( librdf_node *node ) { raptor_iostream *stream = NULL; void *dumped_node = NULL; int ret; stream = raptor_new_iostream_to_string( node->world, &dumped_node, NULL, NULL ); if ( !stream ) { rb_sys_fail( "raptor_new_iostream_to_string" ); } ret = librdf_node_write( node, stream ); raptor_free_iostream( stream ); if ( ret != 0 ) rb_fatal( "librdf_node_write failed." ); return rb_str_new2( (char *)dumped_node ); }
/** * librdf_statement_to_string: * @statement: the statement * * Format the librdf_statement as a string. * * Formats the statement as a newly allocate string that must be freed by * the caller. * * @Deprecated: Use librdf_statement_write() to write to * #raptor_iostream which can be made to write to a string. Use a * #librdf_serializer to write proper syntax formats. * * Return value: the string or NULL on failure. **/ unsigned char * librdf_statement_to_string(librdf_statement *statement) { raptor_iostream* iostr; unsigned char *s; int rc; LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, NULL); iostr = raptor_new_iostream_to_string(statement->world->raptor_world_ptr, (void**)&s, NULL, malloc); if(!iostr) return NULL; rc = librdf_statement_write(statement, iostr); raptor_free_iostream(iostr); if(rc) { free(s); s = NULL; } return s; }
/** * raptor_term_to_turtle_counted_string: * @term: term * @nstack: namespace stack * @base_uri: base URI * @len_p: Pointer to location to store length of new string (if not NULL) * * Convert #raptor_term 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_term_to_turtle_counted_string(raptor_term* term, raptor_namespace_stack *nstack, raptor_uri *base_uri, size_t *len_p) { int rc; raptor_iostream* iostr; unsigned char *s; iostr = raptor_new_iostream_to_string(term->world, (void**)&s, len_p, malloc); if(!iostr) return NULL; rc = raptor_term_turtle_write(iostr, term, nstack, base_uri); raptor_free_iostream(iostr); if(rc) { free(s); s = NULL; } return s; }
/* * Return value: non-0 if equal */ static int compare_query_results_compare(compare_query_results* cqr) { int differences = 0; int i; int rowi; int size1; int size2; int row_differences_count = 0; size1 = rasqal_query_results_get_bindings_count(cqr->qr1); size2 = rasqal_query_results_get_bindings_count(cqr->qr2); if(size1 != size2) { cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = "Results have different numbers of bindings"; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); differences++; goto done; } /* check variables in each results project the same variables */ for(i = 0; 1; i++) { const unsigned char* v1; const unsigned char* v2; v1 = rasqal_query_results_get_binding_name(cqr->qr1, i); v2 = rasqal_query_results_get_binding_name(cqr->qr2, i); if(!v1 && !v2) break; if(v1 && v2) { if(strcmp((const char*)v1, (const char*)v2)) { /* different names */ differences++; } } else /* one is NULL, the other is a name */ differences++; } if(differences) { cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = "Results have different binding names"; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); goto done; } /* set results to be stored? */ /* sort rows by something ? As long as the sort is the same it * probably does not matter what the method is. */ /* what to do about blank nodes? */ /* for each row */ for(rowi = 0; 1; rowi++) { int bindingi; rasqal_row* row1 = rasqal_query_results_get_row_by_offset(cqr->qr1, rowi); rasqal_row* row2 = rasqal_query_results_get_row_by_offset(cqr->qr2, rowi); int this_row_different = 0; if(!row1 && !row2) break; /* for each variable in row1 (== same variables in row2) */ for(bindingi = 0; bindingi < size1; bindingi++) { /* we know the binding names are the same */ const unsigned char* name; rasqal_literal *value1; rasqal_literal *value2; int error = 0; name = rasqal_query_results_get_binding_name(cqr->qr1, bindingi); value1 = rasqal_query_results_get_binding_value(cqr->qr1, bindingi); value2 = rasqal_query_results_get_binding_value(cqr->qr2, bindingi); /* should have compare as native flag? * RASQAL_COMPARE_XQUERY doesn't compare all values */ if(!rasqal_literal_equals_flags(value1, value2, RASQAL_COMPARE_XQUERY, &error)) { /* if different report it */ raptor_world* raptor_world_ptr; void *string; size_t length; raptor_iostream* string_iostr; raptor_world_ptr = rasqal_world_get_raptor(cqr->world); string_iostr = raptor_new_iostream_to_string(raptor_world_ptr, &string, &length, (raptor_data_malloc_handler)malloc); raptor_iostream_counted_string_write("Difference in row ", 18, string_iostr); raptor_iostream_decimal_write(rowi + 1, string_iostr); raptor_iostream_counted_string_write(" binding '", 10, string_iostr); raptor_iostream_string_write(name, string_iostr); raptor_iostream_counted_string_write("' ", 2, string_iostr); raptor_iostream_string_write(cqr->qr1_label, string_iostr); raptor_iostream_counted_string_write(" value ", 7, string_iostr); rasqal_literal_write(value1, string_iostr); raptor_iostream_write_byte(' ', string_iostr); raptor_iostream_string_write(cqr->qr2_label, string_iostr); raptor_iostream_counted_string_write(" value ", 7, string_iostr); rasqal_literal_write(value2, string_iostr); raptor_iostream_write_byte(' ', string_iostr); /* this allocates and copies result into 'string' */ raptor_free_iostream(string_iostr); cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = (const char*)string; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); free(string); differences++; this_row_different = 1; } } /* end for each var */ if(this_row_different) row_differences_count++; rasqal_query_results_next(cqr->qr1); rasqal_query_results_next(cqr->qr2); } /* end for each row */ if(row_differences_count) { cqr->message.level = RAPTOR_LOG_LEVEL_ERROR; cqr->message.text = "Results have different values"; if(cqr->log_handler) cqr->log_handler(cqr->log_user_data, &cqr->message); } done: return (differences == 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); }
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); }