/** * raptor_free_xml_writer: * @xml_writer: XML writer object * * Destructor - Free XML Writer * **/ void raptor_free_xml_writer(raptor_xml_writer* xml_writer) { if(!xml_writer) return; if(xml_writer->nstack && xml_writer->my_nstack) raptor_free_namespaces(xml_writer->nstack); raptor_object_options_clear(&xml_writer->options); RAPTOR_FREE(raptor_xml_writer, xml_writer); }
/** * raptor_free_turtle_writer: * @turtle_writer: Turtle writer object * * Destructor - Free Turtle Writer * **/ void raptor_free_turtle_writer(raptor_turtle_writer* turtle_writer) { if(!turtle_writer) return; if(turtle_writer->nstack && turtle_writer->my_nstack) raptor_free_namespaces(turtle_writer->nstack); if(turtle_writer->xsd_boolean_uri) raptor_free_uri(turtle_writer->xsd_boolean_uri); if(turtle_writer->xsd_decimal_uri) raptor_free_uri(turtle_writer->xsd_decimal_uri); if(turtle_writer->xsd_double_uri) raptor_free_uri(turtle_writer->xsd_double_uri); if(turtle_writer->xsd_integer_uri) raptor_free_uri(turtle_writer->xsd_integer_uri); RAPTOR_FREE(raptor_turtle_writer, turtle_writer); }
/* 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* 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); }
/* * rasqal_query_results_write_sparql_xml: * @iostr: #raptor_iostream to write the query results to * @results: #rasqal_query_results query results input * @base_uri: #raptor_uri base URI of the output format * * Write the fourth version of the SPARQL XML query results format to an * iostream in a format - INTERNAL. * * If the writing succeeds, the query results will be exhausted. * * Return value: non-0 on failure **/ static int rasqal_query_results_write_sparql_xml(rasqal_query_results_formatter* formatter, raptor_iostream *iostr, rasqal_query_results* results, raptor_uri *base_uri) { int rc=1; rasqal_world* world = rasqal_query_results_get_world(results); raptor_xml_writer* xml_writer=NULL; raptor_namespace *res_ns=NULL; raptor_namespace_stack *nstack=NULL; raptor_xml_element *sparql_element=NULL; raptor_xml_element *results_element=NULL; raptor_xml_element *result_element=NULL; raptor_xml_element *element1=NULL; raptor_xml_element *binding_element=NULL; raptor_xml_element *variable_element=NULL; raptor_qname **attrs=NULL; int i; if(!rasqal_query_results_is_bindings(results) && !rasqal_query_results_is_boolean(results)) { rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Can only write XML format v3 for variable binding and boolean results"); return 1; } nstack = raptor_new_namespaces(world->raptor_world_ptr, 1); if(!nstack) return 1; xml_writer = raptor_new_xml_writer(world->raptor_world_ptr, nstack, iostr); if(!xml_writer) goto tidy; res_ns=raptor_new_namespace(nstack, NULL, (const unsigned char*)"http://www.w3.org/2005/sparql-results#", 0); if(!res_ns) goto tidy; sparql_element=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"sparql", NULL, base_uri); if(!sparql_element) goto tidy; raptor_xml_writer_start_element(xml_writer, sparql_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); /* <head> */ element1=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"head", NULL, base_uri); if(!element1) goto tidy; raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 2); raptor_xml_writer_start_element(xml_writer, element1); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); if(rasqal_query_results_is_bindings(results)) { for(i=0; 1; i++) { const unsigned char *name; name=rasqal_query_results_get_binding_name(results, i); if(!name) break; /* <variable name="x"/> */ variable_element=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"variable", NULL, base_uri); if(!variable_element) goto tidy; attrs=(raptor_qname **)raptor_alloc_memory(sizeof(raptor_qname*)); if(!attrs) goto tidy; attrs[0] = raptor_new_qname_from_namespace_local_name(world->raptor_world_ptr, res_ns, (const unsigned char*)"name", (const unsigned char*)name); /* attribute value */ if(!attrs[0]) { raptor_free_memory((void*)attrs); goto tidy; } raptor_xml_element_set_attributes(variable_element, attrs, 1); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 4); raptor_xml_writer_empty_element(xml_writer, variable_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); raptor_free_xml_element(variable_element); variable_element=NULL; } } /* FIXME - could add <link> inside <head> */ /* </head> */ raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 2); raptor_xml_writer_end_element(xml_writer, element1); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); raptor_free_xml_element(element1); element1=NULL; /* Boolean Results */ if(rasqal_query_results_is_boolean(results)) { result_element=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"boolean", NULL, base_uri); if(!result_element) goto tidy; raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 2); raptor_xml_writer_start_element(xml_writer, result_element); if(rasqal_query_results_get_boolean(results)) raptor_xml_writer_raw(xml_writer, RASQAL_XSD_BOOLEAN_TRUE); else raptor_xml_writer_raw(xml_writer, RASQAL_XSD_BOOLEAN_FALSE); raptor_xml_writer_end_element(xml_writer, result_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); goto results3done; } /* Variable Binding Results */ /* <results> */ results_element=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"results", NULL, base_uri); if(!results_element) goto tidy; raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 2); raptor_xml_writer_start_element(xml_writer, results_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); /* declare result element for later multiple use */ result_element=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"result", NULL, base_uri); if(!result_element) goto tidy; while(!rasqal_query_results_finished(results)) { /* <result> */ raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 4); raptor_xml_writer_start_element(xml_writer, result_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); for(i=0; i<rasqal_query_results_get_bindings_count(results); i++) { const unsigned char *name=rasqal_query_results_get_binding_name(results, i); rasqal_literal *l=rasqal_query_results_get_binding_value(results, i); /* <binding> */ binding_element=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"binding", NULL, base_uri); if(!binding_element) goto tidy; attrs=(raptor_qname **)raptor_alloc_memory(sizeof(raptor_qname*)); if(!attrs) goto tidy; attrs[0] = raptor_new_qname_from_namespace_local_name(world->raptor_world_ptr, res_ns, (const unsigned char*)"name", name); if(!attrs[0]) { raptor_free_memory((void*)attrs); goto tidy; } raptor_xml_element_set_attributes(binding_element, attrs, 1); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 6); raptor_xml_writer_start_element(xml_writer, binding_element); if(!l) { element1=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"unbound", NULL, base_uri); if(!element1) goto tidy; raptor_xml_writer_empty_element(xml_writer, element1); } else switch(l->type) { case RASQAL_LITERAL_URI: element1=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"uri", NULL, base_uri); if(!element1) goto tidy; raptor_xml_writer_start_element(xml_writer, element1); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)raptor_uri_as_string(l->value.uri)); raptor_xml_writer_end_element(xml_writer, element1); break; case RASQAL_LITERAL_BLANK: element1=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"bnode", NULL, base_uri); if(!element1) goto tidy; raptor_xml_writer_start_element(xml_writer, element1); raptor_xml_writer_cdata(xml_writer, (const unsigned char*)l->string); raptor_xml_writer_end_element(xml_writer, element1); break; case RASQAL_LITERAL_STRING: case RASQAL_LITERAL_UDT: element1=raptor_new_xml_element_from_namespace_local_name(res_ns, (const unsigned char*)"literal", NULL, base_uri); if(!element1) goto tidy; if(l->language || l->datatype) { attrs=(raptor_qname **)raptor_alloc_memory(sizeof(raptor_qname*)); if(!attrs) goto tidy; if(l->language) attrs[0]=raptor_new_qname(nstack, (const unsigned char*)"xml:lang", (const unsigned char*)l->language); else attrs[0] = raptor_new_qname_from_namespace_local_name(world->raptor_world_ptr, res_ns, (const unsigned char*)"datatype", (const unsigned char*)raptor_uri_as_string(l->datatype)); if(!attrs[0]) { raptor_free_memory((void*)attrs); goto tidy; } raptor_xml_element_set_attributes(element1, attrs, 1); } raptor_xml_writer_start_element(xml_writer, element1); raptor_xml_writer_cdata_counted(xml_writer, (const unsigned char*)l->string, l->string_len); raptor_xml_writer_end_element(xml_writer, element1); 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_DATETIME: case RASQAL_LITERAL_INTEGER_SUBTYPE: case RASQAL_LITERAL_UNKNOWN: default: rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Cannot turn literal type %d into XML", l->type); goto tidy; } if(element1) { raptor_free_xml_element(element1); element1=NULL; } /* </binding> */ raptor_xml_writer_end_element(xml_writer, binding_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); raptor_free_xml_element(binding_element); binding_element=NULL; } raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 4); raptor_xml_writer_end_element(xml_writer, result_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); rasqal_query_results_next(results); } raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)" ", 2); raptor_xml_writer_end_element(xml_writer, results_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); results3done: rc=0; raptor_xml_writer_end_element(xml_writer, sparql_element); raptor_xml_writer_raw_counted(xml_writer, (const unsigned char*)"\n", 1); tidy: if(element1) raptor_free_xml_element(element1); if(variable_element) raptor_free_xml_element(variable_element); if(binding_element) raptor_free_xml_element(binding_element); if(result_element) raptor_free_xml_element(result_element); if(results_element) raptor_free_xml_element(results_element); if(sparql_element) raptor_free_xml_element(sparql_element); if(res_ns) raptor_free_namespace(res_ns); if(xml_writer) raptor_free_xml_writer(xml_writer); if(nstack) raptor_free_namespaces(nstack); return rc; }
int rasqal_query_write_sparql_20060406(raptor_iostream *iostr, rasqal_query* query, raptor_uri *base_uri) { int i; sparql_writer_context wc; int limit, offset; rasqal_query_verb verb; wc.world = query->world; wc.base_uri = NULL; wc.type_uri = raptor_new_uri_for_rdf_concept(query->world->raptor_world_ptr, (const unsigned char*)"type"); wc.nstack = raptor_new_namespaces(query->world->raptor_world_ptr, 1); if(base_uri) { raptor_iostream_counted_string_write("BASE ", 5, iostr); rasqal_query_write_sparql_uri(&wc, iostr, base_uri); raptor_iostream_write_byte('\n', iostr); /* from now on all URIs are relative to this */ wc.base_uri = raptor_uri_copy(base_uri); } for(i = 0; 1 ; i++) { raptor_namespace *nspace; rasqal_prefix* p = rasqal_query_get_prefix(query, i); if(!p) break; raptor_iostream_counted_string_write("PREFIX ", 7, iostr); if(p->prefix) raptor_iostream_string_write(p->prefix, iostr); raptor_iostream_counted_string_write(": ", 2, iostr); rasqal_query_write_sparql_uri(&wc, iostr, p->uri); raptor_iostream_write_byte('\n', iostr); /* Use this constructor so we copy a URI directly */ nspace = raptor_new_namespace_from_uri(wc.nstack, p->prefix, p->uri, i); raptor_namespaces_start_namespace(wc.nstack, nspace); } if(query->explain) raptor_iostream_counted_string_write("EXPLAIN ", 8, iostr); verb = query->verb; /* These terms are deprecated */ if(query->verb == RASQAL_QUERY_VERB_INSERT || query->verb == RASQAL_QUERY_VERB_DELETE) { verb = RASQAL_QUERY_VERB_UPDATE; } /* Write SPARQL 1.1 (Draft) Update forms */ if(verb == RASQAL_QUERY_VERB_UPDATE) { rasqal_update_operation* update; /* Write SPARQL Update */ for(i = 0; (update = rasqal_query_get_update_operation(query, i)); i++) { int is_always_2_args = (update->type >= RASQAL_UPDATE_TYPE_ADD && update->type <= RASQAL_UPDATE_TYPE_COPY); if(update->type == RASQAL_UPDATE_TYPE_UPDATE) { /* update operations: * WITH ... INSERT { template } DELETE { template } WHERE { template } * INSERT/DELETE { template } WHERE { template } * INSERT/DELETE DATA { triples } */ if(update->graph_uri) { raptor_iostream_counted_string_write("WITH ", 5, iostr); rasqal_query_write_sparql_uri(&wc, iostr, update->graph_uri); raptor_iostream_write_byte('\n', iostr); } if(update->delete_templates) { raptor_iostream_counted_string_write("DELETE ", 7, iostr); if(update->flags & RASQAL_UPDATE_FLAGS_DATA) raptor_iostream_counted_string_write("DATA ", 5, iostr); rasqal_query_write_sparql_triple_data(&wc, iostr, update->delete_templates, 0); raptor_iostream_write_byte('\n', iostr); } if(update->insert_templates) { raptor_iostream_counted_string_write("INSERT ", 7, iostr); if(update->flags & RASQAL_UPDATE_FLAGS_DATA) raptor_iostream_counted_string_write("DATA ", 5, iostr); rasqal_query_write_sparql_triple_data(&wc, iostr, update->insert_templates, 0); raptor_iostream_write_byte('\n', iostr); } if(update->where) { raptor_iostream_counted_string_write("WHERE ", 6, iostr); rasqal_query_write_sparql_graph_pattern(&wc, iostr, update->where, -1, 0); raptor_iostream_write_byte('\n', iostr); } } else { /* admin operations: * CLEAR GRAPH graph-uri | DEFAULT | NAMED | ALL * CREATE (SILENT) GRAPH graph-uri | DEFAULT | NAMED | ALL * DROP (SILENT) GRAPH graph-uri * LOAD (SILENT) doc-uri / LOAD (SILENT) doc-uri INTO GRAPH graph-uri * ADD (SILENT) GraphOrDefault TO GraphOrDefault * MOVE (SILENT) GraphOrDefault TO GraphOrDefault * COPY (SILENT) GraphOrDefault TO GraphOrDefault */ raptor_iostream_string_write(rasqal_update_type_label(update->type), iostr); if(update->flags & RASQAL_UPDATE_FLAGS_SILENT) raptor_iostream_counted_string_write(" SILENT", 7, iostr); if(is_always_2_args) { /* ADD, MOVE, COPY are always 2-arg admin operations */ rasqal_query_write_graphref(&wc, iostr, update->graph_uri, RASQAL_UPDATE_GRAPH_ONE); raptor_iostream_counted_string_write(" TO", 3, iostr); rasqal_query_write_graphref(&wc, iostr, update->document_uri, RASQAL_UPDATE_GRAPH_ONE); } else if(update->type == RASQAL_UPDATE_TYPE_LOAD) { /* LOAD is 1 or 2 URIs and first one never has a GRAPH prefix */ raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_uri(&wc, iostr, update->document_uri); if(update->graph_uri) { raptor_iostream_counted_string_write(" INTO", 5, iostr); rasqal_query_write_graphref(&wc, iostr, update->graph_uri, RASQAL_UPDATE_GRAPH_ONE); } } else { /* everything else is defined by update->applies; only * CLEAR and DROP may apply to >1 graph */ rasqal_query_write_graphref(&wc, iostr, update->graph_uri, update->applies); } raptor_iostream_write_byte('\n', iostr); } } goto tidy; } if(verb != RASQAL_QUERY_VERB_CONSTRUCT) raptor_iostream_string_write(rasqal_query_verb_as_string(query->verb), iostr); if(query->distinct) { if(query->distinct == 1) raptor_iostream_counted_string_write(" DISTINCT", 9, iostr); else raptor_iostream_counted_string_write(" REDUCED", 8, iostr); } if(query->wildcard) raptor_iostream_counted_string_write(" *", 2, iostr); else if(verb == RASQAL_QUERY_VERB_DESCRIBE) { raptor_sequence *lit_seq = query->describes; int count = raptor_sequence_size(lit_seq); for(i = 0; i < count; i++) { rasqal_literal* l = (rasqal_literal*)raptor_sequence_get_at(lit_seq, i); raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_literal(&wc, iostr, l); } } else if(verb == RASQAL_QUERY_VERB_SELECT) { rasqal_query_write_sparql_select(&wc, iostr, query->selects); } raptor_iostream_write_byte('\n', iostr); if(query->data_graphs) { for(i = 0; 1; i++) { rasqal_data_graph* dg = rasqal_query_get_data_graph(query, i); if(!dg) break; if(dg->flags & RASQAL_DATA_GRAPH_NAMED) continue; rasqal_query_write_data_format_comment(&wc, iostr, dg); raptor_iostream_counted_string_write("FROM ", 5, iostr); rasqal_query_write_sparql_uri(&wc, iostr, dg->uri); raptor_iostream_counted_string_write("\n", 1, iostr); } for(i = 0; 1; i++) { rasqal_data_graph* dg = rasqal_query_get_data_graph(query, i); if(!dg) break; if(!(dg->flags & RASQAL_DATA_GRAPH_NAMED)) continue; rasqal_query_write_data_format_comment(&wc, iostr, dg); raptor_iostream_counted_string_write("FROM NAMED ", 11, iostr); rasqal_query_write_sparql_uri(&wc, iostr, dg->name_uri); raptor_iostream_write_byte('\n', iostr); } } if(query->constructs) { raptor_iostream_string_write("CONSTRUCT {\n", iostr); for(i = 0; 1; i++) { rasqal_triple* t = rasqal_query_get_construct_triple(query, i); if(!t) break; raptor_iostream_counted_string_write(" ", 2, iostr); rasqal_query_write_sparql_triple(&wc, iostr, t); raptor_iostream_write_byte('\n', iostr); } raptor_iostream_counted_string_write("}\n", 2, iostr); } if(query->query_graph_pattern) { raptor_iostream_counted_string_write("WHERE ", 6, iostr); rasqal_query_write_sparql_graph_pattern(&wc, iostr, query->query_graph_pattern, -1, 0); raptor_iostream_write_byte('\n', iostr); } if(rasqal_query_get_group_conditions_sequence(query)) { raptor_iostream_counted_string_write("GROUP BY ", 9, iostr); for(i = 0; 1; i++) { rasqal_expression* expr = rasqal_query_get_group_condition(query, i); if(!expr) break; if(i > 0) raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_expression(&wc, iostr, expr); } raptor_iostream_write_byte('\n', iostr); } if(rasqal_query_get_having_conditions_sequence(query)) { raptor_iostream_counted_string_write("HAVING ", 7, iostr); for(i = 0; 1; i++) { rasqal_expression* expr = rasqal_query_get_having_condition(query, i); if(!expr) break; if(i > 0) raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_expression(&wc, iostr, expr); } raptor_iostream_write_byte('\n', iostr); } if(rasqal_query_get_order_conditions_sequence(query)) { raptor_iostream_counted_string_write("ORDER BY ", 9, iostr); for(i = 0; 1; i++) { rasqal_expression* expr = rasqal_query_get_order_condition(query, i); if(!expr) break; if(i > 0) raptor_iostream_write_byte(' ', iostr); rasqal_query_write_sparql_expression(&wc, iostr, expr); } raptor_iostream_write_byte('\n', iostr); } limit = rasqal_query_get_limit(query); offset = rasqal_query_get_offset(query); if(limit >= 0 || offset >= 0) { if(limit >= 0) { raptor_iostream_counted_string_write("LIMIT ", 7, iostr); raptor_iostream_decimal_write(limit, iostr); } if(offset >= 0) { if(limit) raptor_iostream_write_byte(' ', iostr); raptor_iostream_counted_string_write("OFFSET ", 8, iostr); raptor_iostream_decimal_write(offset, iostr); } raptor_iostream_write_byte('\n', iostr); } if(query->bindings) rasqal_write_sparql_bindings(&wc, iostr, query->bindings); tidy: raptor_free_uri(wc.type_uri); if(wc.base_uri) raptor_free_uri(wc.base_uri); raptor_free_namespaces(wc.nstack); 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); }