/** * 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_turtle_writer_bnodeid: * @turtle_writer: Turtle writer object * @bnodeid: blank node ID to write * @len: length of @bnodeid * * Write a blank node ID with leading _: to the Turtle writer. * **/ void raptor_turtle_writer_bnodeid(raptor_turtle_writer* turtle_writer, const unsigned char *bnodeid, size_t len) { raptor_bnodeid_ntriples_write(bnodeid, len, turtle_writer->iostr); }
/* * rasqal_query_results_write_sv: * @iostr: #raptor_iostream to write the query to * @results: #rasqal_query_results query results format * @base_uri: #raptor_uri base URI of the output format * @label: name of this format for errors * @sep: column sep character * @csv_escape: non-0 if values are written escaped with CSV rules, else turtle * @variable_prefix: char to print before a variable name or NUL * @eol_str: end of line string * @eol_str_len: length of @eol_str * * INTERNAL - Write a @sep-separated values version of the query results format to an iostream. * * If the writing succeeds, the query results will be exhausted. * * Return value: non-0 on failure **/ static int rasqal_query_results_write_sv(raptor_iostream *iostr, rasqal_query_results* results, raptor_uri *base_uri, const char* label, const char sep, int csv_escape, const char variable_prefix, const char* eol_str, size_t eol_str_len) { rasqal_query* query = rasqal_query_results_get_query(results); int i; int vars_count; if(!rasqal_query_results_is_bindings(results)) { rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR, &query->locator, "Can only write %s format for variable binding results", label); return 1; } /* Header */ for(i = 0; 1; i++) { const unsigned char *name; name = rasqal_query_results_get_binding_name(results, i); if(!name) break; if(i > 0) raptor_iostream_write_byte(sep, iostr); if(variable_prefix) raptor_iostream_write_byte(variable_prefix, iostr); raptor_iostream_string_write(name, iostr); } raptor_iostream_counted_string_write(eol_str, eol_str_len, iostr); /* Variable Binding Results */ vars_count = rasqal_query_results_get_bindings_count(results); while(!rasqal_query_results_finished(results)) { /* Result row */ for(i = 0; i < vars_count; i++) { rasqal_literal *l = rasqal_query_results_get_binding_value(results, i); if(i > 0) raptor_iostream_write_byte(sep, iostr); if(l) { const unsigned char* str; size_t len; switch(l->type) { case RASQAL_LITERAL_URI: str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->value.uri, &len)); if(csv_escape) rasqal_iostream_write_csv_string(str, len, iostr); else { raptor_iostream_write_byte('<', iostr); if(str && len > 0) raptor_string_ntriples_write(str, len, '"', iostr); raptor_iostream_write_byte('>', iostr); } break; case RASQAL_LITERAL_BLANK: raptor_bnodeid_ntriples_write(l->string, l->string_len, iostr); break; case RASQAL_LITERAL_STRING: if(csv_escape) { rasqal_iostream_write_csv_string(l->string, l->string_len, iostr); } else { if(l->datatype && l->valid) { rasqal_literal_type ltype; ltype = rasqal_xsd_datatype_uri_to_type(l->world, l->datatype); if(ltype >= RASQAL_LITERAL_INTEGER && ltype <= RASQAL_LITERAL_DECIMAL) { /* write integer, float, double and decimal XSD typed * data without quotes, datatype or language */ raptor_string_ntriples_write(l->string, l->string_len, '\0', iostr); break; } } raptor_iostream_write_byte('"', iostr); raptor_string_ntriples_write(l->string, l->string_len, '"', iostr); raptor_iostream_write_byte('"', iostr); if(l->language) { raptor_iostream_write_byte('@', iostr); raptor_iostream_string_write(RASQAL_GOOD_CAST(const unsigned char*, l->language), iostr); } if(l->datatype) { raptor_iostream_string_write("^^<", iostr); str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->datatype, &len)); raptor_string_ntriples_write(str, len, '"', iostr); raptor_iostream_write_byte('>', iostr); } } break; case RASQAL_LITERAL_PATTERN: case RASQAL_LITERAL_QNAME: case RASQAL_LITERAL_INTEGER: case RASQAL_LITERAL_XSD_STRING: case RASQAL_LITERAL_BOOLEAN: case RASQAL_LITERAL_DOUBLE: case RASQAL_LITERAL_FLOAT: case RASQAL_LITERAL_VARIABLE: case RASQAL_LITERAL_DECIMAL: case RASQAL_LITERAL_DATE: case RASQAL_LITERAL_DATETIME: case RASQAL_LITERAL_UDT: case RASQAL_LITERAL_INTEGER_SUBTYPE: case RASQAL_LITERAL_UNKNOWN: default: rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR, &query->locator, "Cannot turn literal type %d into %s", l->type, label); } } /* End Binding */ }