static void raptor_dot_serializer_write_uri(raptor_serializer* serializer, raptor_uri* uri) { raptor_dot_context* context = (raptor_dot_context*)serializer->context; unsigned char* full = raptor_uri_as_string(uri); int i; for(i = 0 ; i < raptor_sequence_size(context->namespaces) ; i++ ) { raptor_namespace* ns = (raptor_namespace*)raptor_sequence_get_at(context->namespaces, i); const unsigned char* ns_uri_string; size_t ns_uri_string_len; ns_uri_string = raptor_uri_as_counted_string(ns->uri, &ns_uri_string_len); if(!strncmp((char*)full, (char*)ns_uri_string, ns_uri_string_len) ) { const unsigned char* prefix = raptor_namespace_get_prefix(ns); if(prefix) { raptor_iostream_string_write(prefix, serializer->iostream); raptor_iostream_write_byte(':', serializer->iostream); } raptor_iostream_string_write(full + ns_uri_string_len, serializer->iostream); return; } } raptor_iostream_string_write(full, serializer->iostream); }
static void emit_format_description(const char* type_name, const raptor_syntax_description* sd, raptor_iostream* iostr) { unsigned int i; if(!sd->mime_types_count) return; /* term */ emit_start_desc_list_term(iostr); emit_format_description_name(type_name, sd, iostr); /* definition */ emit_start_desc_list_defn(iostr); raptor_iostream_string_write("\n ", iostr); emit_start_list(iostr); for(i = 0; i < sd->mime_types_count; i++) { const raptor_type_q* mime_type = &sd->mime_types[i]; if(!sd) break; raptor_iostream_string_write(" ", iostr); emit_start_list_item(iostr); emit_mime_type(mime_type, iostr); emit_end_list_item(iostr); } raptor_iostream_string_write(" ", iostr); emit_end_list(iostr); emit_end_desc_list_item(iostr); }
static void emit_literal(const char* literal, raptor_iostream* iostr) { raptor_iostream_string_write("<literal>", iostr); raptor_iostream_string_write(literal, iostr); raptor_iostream_string_write("</literal>", iostr); }
static void emit_mime_type_q(unsigned char q, raptor_iostream* iostr) { if(q < 10) { raptor_iostream_string_write("q 0.", iostr); raptor_iostream_decimal_write((int)q, iostr); } else raptor_iostream_string_write("q 1.0", iostr); }
/** * raptor_turtle_writer_namespace_prefix: * @turtle_writer: Turtle writer object * @ns: Namespace to write prefix declaration for * * Write a namespace prefix declaration (@prefix) * * Must only be used at the beginning of a document. */ void raptor_turtle_writer_namespace_prefix(raptor_turtle_writer* turtle_writer, raptor_namespace* ns) { raptor_iostream_string_write("@prefix ", turtle_writer->iostr); if(ns->prefix) raptor_iostream_string_write(raptor_namespace_get_prefix(ns), turtle_writer->iostr); raptor_iostream_counted_string_write(": ", 2, turtle_writer->iostr); raptor_turtle_writer_reference(turtle_writer, raptor_namespace_get_uri(ns)); raptor_iostream_counted_string_write(" .\n", 3, turtle_writer->iostr); }
static void emit_start_section(const char* id, const char* title, raptor_iostream* iostr) { raptor_iostream_string_write("<section id=\"", iostr); raptor_iostream_string_write(id, iostr); raptor_iostream_string_write( "\">\n" "<title>", iostr); raptor_xml_escape_string_write((const unsigned char*)title, strlen(title), '\0', iostr); raptor_iostream_string_write("</title>\n", iostr); }
static void emit_start_desc_list(const char* title, raptor_iostream *iostr) { raptor_iostream_string_write( " <variablelist>\n", iostr); if(title) { raptor_iostream_string_write( " <title>", iostr); raptor_iostream_string_write(title, iostr); raptor_iostream_string_write("</title>\n", iostr); } raptor_iostream_write_byte('\n', iostr); }
/** * rasqal_variable_write: * @v: the #rasqal_variable object * @iostr: the #raptor_iostream handle to write to * * Write a Rasqal variable to an iostream in a debug format. * * The write debug format may change in any release. * **/ void rasqal_variable_write(rasqal_variable* v, raptor_iostream* iostr) { if(!v || !iostr) return; if(v->type == RASQAL_VARIABLE_TYPE_ANONYMOUS) raptor_iostream_counted_string_write("anon-variable(", 14, iostr); else raptor_iostream_counted_string_write("variable(", 9, iostr); raptor_iostream_string_write(v->name, iostr); if(v->expression) { raptor_iostream_write_byte('=', iostr); rasqal_expression_write(v->expression, iostr); } if(v->value) { raptor_iostream_write_byte('=', iostr); rasqal_literal_write(v->value, iostr); } #ifdef RASQAL_DEBUG_VARIABLE_USAGE raptor_iostream_write_byte('[', iostr); raptor_iostream_decimal_write(v->usage, iostr); raptor_iostream_write_byte(']', iostr); #endif raptor_iostream_write_byte(')', iostr); }
static void emit_start_desc_list_term(raptor_iostream *iostr) { raptor_iostream_string_write( " <varlistentry><term>", iostr); }
static void emit_mime_type(const raptor_type_q* mt, raptor_iostream* iostr) { emit_mime_type_name(mt->mime_type, iostr); raptor_iostream_string_write(" with ", iostr); emit_mime_type_q(mt->q, iostr); }
static void emit_format_description_name(const char* type_name, const raptor_syntax_description* sd, raptor_iostream* iostr) { raptor_xml_escape_string_write((const unsigned char*)sd->label, strlen(sd->label), '\0', iostr); if(type_name) { raptor_iostream_write_byte(' ', iostr); raptor_iostream_string_write(type_name, iostr); } raptor_iostream_string_write(" (", iostr); emit_literal(sd->names[0], iostr); raptor_iostream_write_byte(')', iostr); }
static void emit_function(const char* name, raptor_iostream* iostr) { int i; char c; raptor_iostream_string_write("<link linkend=\">", iostr); for(i = 0; (c = name[i]); i++) { if(c == '_') c = '-'; raptor_iostream_write_byte(c, iostr); } raptor_iostream_string_write("\"><function>", iostr); raptor_iostream_string_write(name, iostr); raptor_iostream_string_write("()</function></link>", iostr); }
static void rasqal_query_write_sparql_expression_op(sparql_writer_context *wc, raptor_iostream* iostr, rasqal_expression* e) { rasqal_op op = e->op; const char* string; if(op > RASQAL_EXPR_LAST) op = RASQAL_EXPR_UNKNOWN; string = rasqal_sparql_op_labels[(int)op]; if(string) raptor_iostream_string_write(string, iostr); else raptor_iostream_string_write("NONE", iostr); }
/* start a serialize */ static int raptor_dot_serializer_start(raptor_serializer* serializer) { raptor_iostream_string_write((const unsigned char*)"digraph {\n\trankdir = LR;\n\tcharset=\"utf-8\";\n\n", serializer->iostream); return 0; }
static void emit_end_section(raptor_iostream *iostr) { raptor_iostream_string_write( "</section>\n" "\n", iostr); }
static void emit_start_desc_list_defn(raptor_iostream *iostr) { raptor_iostream_string_write( "</term>\n" " <listitem>", iostr); }
static void emit_end_desc_list_item(raptor_iostream *iostr) { raptor_iostream_string_write( " </listitem>\n" " </varlistentry>\n" "\n", iostr); }
static void emit_header(const char* id, raptor_iostream* iostr) { raptor_iostream_string_write( "<!DOCTYPE refentry PUBLIC \"-//OASIS//DTD DocBook XML V4.3//EN\" \n" " \"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd\">\n" "<chapter id=\"", iostr); raptor_iostream_string_write(id, iostr); raptor_iostream_string_write( "\">\n" "<title>Syntax Formats supported in Raptor</title>\n" "\n" "<para>This chapter describes the syntax formats supported\n" "by parsers and serializers in Raptor.\n" "</para>\n" "\n", iostr); }
static void rasqal_query_write_data_format_comment(sparql_writer_context* wc, raptor_iostream *iostr, rasqal_data_graph* dg) { if(dg->format_type || dg->format_name || dg->format_uri) { raptor_iostream_counted_string_write("# format ", 9, iostr); if(dg->format_type) { raptor_iostream_counted_string_write("type ", 5, iostr); raptor_iostream_string_write(dg->format_type, iostr); } if(dg->format_type) { raptor_iostream_counted_string_write("name ", 5, iostr); raptor_iostream_string_write(dg->format_name, iostr); } if(dg->format_type) { raptor_iostream_counted_string_write("uri ", 4, iostr); rasqal_query_write_sparql_uri(wc, iostr, dg->format_uri); } } }
static int raptor_json_serialize_end(raptor_serializer* serializer) { raptor_json_context* context = (raptor_json_context*)serializer->context; char* value; raptor_json_writer_newline(context->json_writer); if(context->is_resource) { /* start outer object */ raptor_json_writer_start_block(context->json_writer, '{'); raptor_json_writer_newline(context->json_writer); raptor_avltree_visit(context->avltree, raptor_json_serialize_avltree_visit, serializer); /* end last triples block */ if(context->last_statement) { raptor_json_writer_newline(context->json_writer); raptor_json_writer_end_block(context->json_writer, ']'); raptor_json_writer_newline(context->json_writer); raptor_json_writer_end_block(context->json_writer, '}'); raptor_json_writer_newline(context->json_writer); } } else { /* end triples array */ raptor_json_writer_end_block(context->json_writer, ']'); raptor_json_writer_newline(context->json_writer); } value = RAPTOR_OPTIONS_GET_STRING(serializer, RAPTOR_OPTION_JSON_EXTRA_DATA); if(value) { raptor_iostream_write_byte(',', serializer->iostream); raptor_json_writer_newline(context->json_writer); raptor_iostream_string_write(value, serializer->iostream); raptor_json_writer_newline(context->json_writer); } /* end outer object */ raptor_json_writer_end_block(context->json_writer, '}'); raptor_json_writer_newline(context->json_writer); /* end callback */ if(RAPTOR_OPTIONS_GET_STRING(serializer, RAPTOR_OPTION_JSON_CALLBACK)) raptor_iostream_counted_string_write((const unsigned char*)");", 2, serializer->iostream); return 0; }
static void raptor_xml_writer_write_xml_declaration(raptor_xml_writer* xml_writer) { if(!xml_writer->xml_declaration_checked) { /* check that it should be written once only */ xml_writer->xml_declaration_checked = 1; if(RAPTOR_OPTIONS_GET_NUMERIC(xml_writer, RAPTOR_OPTION_WRITER_XML_DECLARATION)) { raptor_iostream_string_write((const unsigned char*)"<?xml version=\"", xml_writer->iostr); raptor_iostream_counted_string_write((XML_WRITER_XML_VERSION(xml_writer) == 10) ? (const unsigned char*)"1.0" : (const unsigned char*)"1.1", 3, xml_writer->iostr); raptor_iostream_string_write((const unsigned char*)"\" encoding=\"utf-8\"?>\n", xml_writer->iostr); } } }
/** * raptor_xml_writer_raw: * @xml_writer: XML writer object * @s: string to write * * Write a string raw to the XML writer. * * Closes any previous empty element if XML writer option AUTO_EMPTY * is enabled. * **/ void raptor_xml_writer_raw(raptor_xml_writer* xml_writer, const unsigned char *s) { raptor_xml_writer_write_xml_declaration(xml_writer); XML_WRITER_FLUSH_CLOSE_BRACKET(xml_writer); raptor_iostream_string_write(s, xml_writer->iostr); if(xml_writer->current_element) xml_writer->current_element->content_cdata_seen = 1; }
static void rasqal_iostream_write_json_boolean(raptor_iostream* iostr, const char* name, int json_bool) { raptor_iostream_write_byte('\"', iostr); raptor_iostream_string_write(name, iostr); raptor_iostream_counted_string_write("\" : ",4, iostr); if(json_bool) raptor_iostream_counted_string_write("true", 4, iostr); else raptor_iostream_counted_string_write("false", 5, iostr); }
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 emit_footer(raptor_iostream *iostr) { raptor_iostream_string_write( "</chapter>\n" "\n" "<!--\n" "Local variables:\n" "mode: sgml\n" "sgml-parent-document: (\"raptor-docs.xml\" \"book\" \"part\")\n" "End:\n" "-->\n" "\n", iostr); }
/* serialize a statement */ static int raptor_dot_serializer_statement(raptor_serializer* serializer, raptor_statement *statement) { /* Cache the nodes for later. */ raptor_dot_serializer_assert_node(serializer, statement->subject); raptor_dot_serializer_assert_node(serializer, statement->object); raptor_iostream_string_write((const unsigned char*)"\t\"", serializer->iostream); raptor_dot_serializer_write_term_type(serializer, statement->subject->type); raptor_dot_serializer_write_term(serializer, statement->subject); raptor_iostream_string_write((const unsigned char*)"\" -> \"", serializer->iostream); raptor_dot_serializer_write_term_type(serializer, statement->object->type); raptor_dot_serializer_write_term(serializer, statement->object); raptor_iostream_string_write((const unsigned char*)"\" [ label=\"", serializer->iostream); raptor_dot_serializer_write_term(serializer, statement->predicate); raptor_iostream_string_write((const unsigned char*)"\" ];\n", serializer->iostream); return 0; }
static int raptor_json_serialize_start(raptor_serializer* serializer) { raptor_json_context* context = (raptor_json_context*)serializer->context; raptor_uri* base_uri; char* value; base_uri = RAPTOR_OPTIONS_GET_NUMERIC(serializer, RAPTOR_OPTION_RELATIVE_URIS) ? serializer->base_uri : NULL; context->json_writer = raptor_new_json_writer(serializer->world, base_uri, serializer->iostream); if(!context->json_writer) return 1; if(context->is_resource) { context->avltree = raptor_new_avltree((raptor_data_compare_handler)raptor_statement_compare, (raptor_data_free_handler)raptor_free_statement, 0); if(!context->avltree) { raptor_free_json_writer(context->json_writer); context->json_writer = NULL; return 1; } } /* start callback */ value = RAPTOR_OPTIONS_GET_STRING(serializer, RAPTOR_OPTION_JSON_CALLBACK); if(value) { raptor_iostream_string_write(value, serializer->iostream); raptor_iostream_write_byte('(', serializer->iostream); } if(!context->is_resource) { /* start outer object */ raptor_json_writer_start_block(context->json_writer, '{'); raptor_json_writer_newline(context->json_writer); /* start triples array */ raptor_iostream_counted_string_write((const unsigned char*)"\"triples\" : ", 12, serializer->iostream); raptor_json_writer_start_block(context->json_writer, '['); raptor_json_writer_newline(context->json_writer); } return 0; }
static void rasqal_query_write_sparql_variable(sparql_writer_context *wc, raptor_iostream* iostr, rasqal_variable* v) { if(v->expression) { raptor_iostream_counted_string_write("( ", 2, iostr); rasqal_query_write_sparql_expression(wc, iostr, v->expression); raptor_iostream_counted_string_write(" AS ", 4, iostr); } if(v->type == RASQAL_VARIABLE_TYPE_ANONYMOUS) raptor_iostream_counted_string_write("_:", 2, iostr); else if(!v->expression) raptor_iostream_write_byte('?', iostr); raptor_iostream_string_write(v->name, iostr); if(v->expression) raptor_iostream_counted_string_write(" )", 2, iostr); }
/** * rasqal_variable_write: * @v: the #rasqal_variable object * @iostr: the #raptor_iostream handle to write to * * Write a Rasqal variable to an iostream in a debug format. * * The write debug format may change in any release. * **/ void rasqal_variable_write(rasqal_variable* v, raptor_iostream* iostr) { if(v->type == RASQAL_VARIABLE_TYPE_ANONYMOUS) raptor_iostream_counted_string_write("anon-variable(", 14, iostr); else raptor_iostream_counted_string_write("variable(", 9, iostr); raptor_iostream_string_write(v->name, iostr); if(v->expression) { raptor_iostream_write_byte('=', iostr); rasqal_expression_write(v->expression, iostr); } if(v->value) { raptor_iostream_write_byte('=', iostr); rasqal_literal_write(v->value, iostr); } raptor_iostream_write_byte(')', iostr); }
static void emit_format_to_syntax_list(raptor_iostream* iostr, type_syntax* type_syntaxes, const char* mime_type, int start, int end) { int i; int parser_seen = 0; int serializer_seen = 0; /* term */ emit_start_desc_list_term(iostr); emit_mime_type_name(mime_type, iostr); /* definition */ emit_start_desc_list_defn(iostr); raptor_iostream_string_write("\n ", iostr); emit_start_list(iostr); for(i = start; i <= end; i++) { raptor_iostream_string_write(" ", iostr); emit_start_list_item(iostr); if(type_syntaxes[i].parser_sd) { emit_format_description_name("Parser", type_syntaxes[i].parser_sd, iostr); parser_seen++; } else { emit_format_description_name("Serializer", type_syntaxes[i].serializer_sd, iostr); serializer_seen++; } raptor_iostream_string_write(" with ", iostr); emit_mime_type_q(type_syntaxes[i].q, iostr); emit_end_list_item(iostr); } if(!parser_seen || !serializer_seen) { emit_start_list_item(iostr); if(!parser_seen) raptor_iostream_string_write("No parser.", iostr); else raptor_iostream_string_write("No serializer.", iostr); emit_end_list_item(iostr); } raptor_iostream_string_write(" ", iostr); emit_end_list(iostr); emit_end_desc_list_item(iostr); }