1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
<chapter id="tutorial-serializing" xmlns:xi="http://www.w3.org/2003/XInclude">
<title>Serializing RDF triples to a syntax</title>
<section id="tutorial-serializing-intro">
<title>Introduction</title>
<para>
The typical sequence of operations to serialize is to create a
serializer object, set various callback and features, start the
serializing, send some RDF triples to the serializer object,
finish the serializing and destroy the serializer object.
</para>
</section>
<section id="tutorial-serializer-create">
<title>Create the Serializer object</title>
<para>The serializer can be created directly from a known name using
<link linkend="raptor-new-serializer"><function>raptor_new_serializer()</function></link>
such as <literal>rdfxml</literal> for the W3C Recommendation RDF/XML syntax:
<programlisting>
raptor_serializer* rdf_serializer;
rdf_serializer = raptor_new_serializer("rdfxml");
</programlisting>
or the name can be discovered from an <emphasis>enumeration</emphasis>
as discussed in
<link linkend="tutorial-querying-functionality">Querying Functionality</link>
</para>
</section>
<section id="tutorial-serializer-features">
<title>Serializer features</title>
<para>There are several options that can be set on serializers, called
<emphasis>features</emphasis>. The exact list of features can be
found via
<link linkend="tutorial-querying-functionality">Querying Functionality</link>
or in the API reference for
<link linkend="raptor-serializer-set-feature"><function>raptor_serializer_set_feature()</function></link>.
</para>
<para>Features are integer enumerations of the
<link linkend="raptor-feature"><type>raptor_feature</type></link> enum and have values
that are either integers (often acting as booleans) or strings.
The two functions that set features are:
<programlisting>
/* Set an integer (or boolean) valued feature */
raptor_serializer_set_feature(rdf_serializer, feature, 1);
/* Set a string valued feature */
raptor_serializer_set_feature_string(rdf_serializer, feature, "abc");
</programlisting>
</para>
<para>
There are also two corresponding functions for reading the values of serializer
features:
<link linkend="raptor-serializer-get-feature"><function>raptor_serializer_get_feature()</function></link>
and
<link linkend="raptor-serializer-get-feature-string"><function>raptor_serializer_get_feature_string()</function></link>
taken the feature enumeration parameter and returning the integer or string
value correspondingly.
</para>
</section>
<section id="tutorial-serializer-declare-namespace">
<title>Declare namespaces</title>
<para>Raptor can use namespace prefix/URIs to abbreviate syntax
in some syntaxes such as Turtle or any XML syntax including RDF/XML,
RSS1.0 and Atom 1.0. These are declared
with <link linkend="raptor-serialize-set-namespace"><function>raptor_serialize_set_namespace()</function></link>
using a prefix and URI argument pair like this:
<programlisting>
const unsigned char* prefix="ex";
raptor_uri* uri=raptor_new_uri("http://example.org");
raptor_serialize_set_namespace(rdf_serializer, prefix, uri);
</programlisting>
</para>
<para>or
<link linkend="raptor-serialize-set-namespace-from-namespace"><function>raptor_serialize_set_namespace_from_namespace()</function></link>
from an existing namespace. This can be useful when connected up the
the namespace declarations that are generated from a parser via a
namespace handler set with
<link linkend="raptor-set-namespace-handler"><function>raptor_set_namespace_handler()</function></link>
</para>
like this:
<programlisting>
static void
relay_namespaces(void* user_data, raptor_namespace *nspace)
{
raptor_serialize_set_namespace_from_namespace(rdf_serializer, nspace);
}
rdf_parser=raptor_new_parser(syntax_name);
raptor_set_namespace_handler(rdf_parser, rdf_serializer, relay_namespaces);
</programlisting>
</section>
<section id="tutorial-serializer-set-error-warning-handlers">
<title>Set error and warning handlers</title>
<para>There are several other callback handlers that can be set
on serializers. These can be set any time before serializing is started.
Errors and warnings from serializing can be returned with functions
that all take a callback of type
<link linkend="raptor-message-handler"><type>raptor_message_handler</type></link>
and signature:
<programlisting>
void
message_handler(void *user_data, raptor_locator* locator,
const char *message)
{
/* do something with the message */
}
</programlisting>
returning the user data given, associated location information
as a <link linkend="raptor-locator"><type>raptor_locator</type></link>
and the error/warning message itself. The <emphasis>locator</emphasis>
structure contains full information on the details of where in the
serialized file or URI the message occurred.
</para>
<para>The fatal error, error and warning handlers are all set with
similar functions that take a handler as follows:
<programlisting>
raptor_serializer_set_error_handler(rdf_serializer, user_data, error_handler);
raptor_serializer_set_warning_handler(rdf_serializer, user_data, warning_handler);
</programlisting>
</para>
</section>
<section id="tutorial-serializer-to-destination">
<title>Provide a destination for the serialized syntax</title>
<para>The operation of turning RDF triples into a syntax has several
alternatives from functions that do most of the work writing to a file
or string to functions that allow passing in a
<link linkend="raptor-iostream"><type>raptor_iostream</type></link>
which can be entirely user-constructed.</para>
<section id="serialize-to-filename">
<title>Serialize to a filename (<link linkend="raptor-serialize-start-to-filename"><function>raptor_serialize_start_to_filename()</function></link>)</title>
<para>Serialize to a new filename
(using <link linkend="raptor-new-iostream-to-filename"><function>raptor_new_iostream_to_filename()</function></link> internally)
and uses asf base URI, the file's URI.
<programlisting>
const char *filename="raptor.rdf";
raptor_serialize_start_to_filename(rdf_serializer, filename);
</programlisting>
</para>
</section>
<section id="serialize-to-string">
<title>Serialize to a string (<link linkend="raptor-serialize-start-to-string"><function>raptor_serialize_start_to_string()</function></link>)</title>
<para>Serialize to a string that is allocated by the serializer
(using <link linkend="raptor-new-iostream-to-string"><function>raptor_new_iostream_to_string()</function></link> internally). The
resulting string is only constructed after <link linkend="raptor-serialize-end"><function>raptor_serialize_end()</function></link> is called and at that
point it is assigned to the string pointer passed in, with the length
written to the optional length pointer. This function
takes an optional base URI but may be required by some serializers.
<programlisting>
raptor_uri* uri=raptor_new_uri("http://example.org/base");
void *string; /* destination for string */
size_t length; /* length of constructed string */
raptor_serialize_start_to_string(rdf_serializer, uri,
&string, &length);
</programlisting>
</para>
</section>
<section id="serialize-to-filehandle">
<title>Serialize to a FILE* file handle (<link linkend="raptor-serialize-start-to-file-handle"><function>raptor_serialize_start_to_file_handle()</function></link>)</title>
<para>Serialize to an existing open C FILE* file handle
(using <link linkend="raptor-new-iostream-to-file-handle"><function>raptor_new_iostream_to_file_handle()</function></link> internally). The handle is not closed after serializing is finished. This function
takes an optional base URI but may be required by some serializers.
<programlisting>
raptor_uri* uri=raptor_new_uri("http://example.org/base");
FILE* fh=fopen("raptor.rdf", "wb");
raptor_serialize_start_to_file_handle(rdf_serializer, uri, fh);
</programlisting>
</para>
</section>
<section id="serialize-to-iostream">
<title>Serialize to an <link linkend="raptor-iostream"><type>raptor_iostream</type></link> (<link linkend="raptor-serialize-start"><function>raptor_serialize_start()</function></link>)</title>
<para>This is the most flexible serializing method as it allows
writing to any
<link linkend="raptor-iostream"><type>raptor_iostream</type></link>
which can be constructed to build any form of user-generated structure
via callbacks.
<programlisting>
raptor_uri* uri=raptor_new_uri("http://example.org/base");
raptor_iostream* iostream;
/* iostream initialized by some means */
raptor_serialize_start(rdf_serializer, uri, iostream);
</programlisting>
</para>
</section>
</section>
<section id="tutorial-serializer-get-triples">
<title>Get or construct RDF Triples</title>
<para>
An <link linkend="raptor-statement"><type>raptor_statement</type></link>
can be made either by receiving them from a
<link linkend="raptor-parser"><type>raptor_parser</type></link>
via parsing or can be constructed by hand.</para>
<para>When constructing by hand,
the <link linkend="raptor-statement"><type>raptor_statement</type></link>
structure should be allocated by the application and the fields
filled in. Each triple has three parts. The subject can be a URI
or blank node, the predicate can only be a URI and the object
can be a URI, blank node or RDF literal. RDF literals can have either
just a Unicode string, a Unicode string and a language or a Unicode
string and a datatype URI.</para>
<para>The triple part types are set as fields named
like <literal>subject_type</literal> for describing
field <literal>subject</literal>.
So to initialise the subject of the triple,
set the field <literal>statement.subject</literal> to point to a
previously allocated
<link linkend="raptor-uri"><type>raptor_uri*</type></link> object (for URI)
or <literal>char*</literal> (for blank node) and
set <literal>statement.subject_type</literal>
to <literal>RAPTOR_IDENTIFIER_TYPE_RESOURCE</literal> or
<literal>RAPTOR_IDENTIFIER_TYPE_ANONYMOUS</literal> respectively.
Triple predicates are always of type
<literal>RAPTOR_IDENTIFIER_TYPE_RESOURCE</literal>.
Triple objects are all all types given above and also
<literal>RAPTOR_IDENTIFIER_TYPE_LITERAL</literal> which takes
an <literal>unsigned char*</literal> pointer plus an optional
language <literal>char*</literal> pointer
in the <literal>object_literal_language</literal> field OR a
a <literal>raptor_uri*</literal> literal datatype pointer
in the <literal>object_literal_datatype</literal> field.
The triple part types are described under
<link linkend="raptor-identifier-type"><type>raptor_identifier_type</type>.</link>
</para>
<example id="raptor-example-rdfserialize">
<title><filename>rdfserialize.c</filename>: Serialize 1 triple to RDF/XML (Abbreviated)</title>
<programlisting>
<xi:include href="rdfserialize.c" parse="text"/>
</programlisting>
<para>Compile it like this:
<screen>
$ gcc -o rdfserialize rdfserialize.c `raptor-config --cflags` `raptor-config --libs`
</screen>
and run it with an optional base URI argument
<screen>
$ ./rdfserialize
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://example.org/subject">
<ns0:predicate xmlns:ns0="http://example.org/" xml:lang="en">An example</ns0:predicate>
</rdf:Description>
</rdf:RDF>
</screen>
</para>
</example>
</section>
<section id="tutorial-serializer-send-triples">
<title>Send RDF Triples to serializer</title>
<para>
Once the serializer has been started, RDF triples can be sent to it
via the
<link linkend="raptor-serialize-statement"><function>raptor_serialize_statement()</function></link>
function with a
<link linkend="raptor-statement"><type>raptor_statement</type></link>
value.
</para>
<para>Once all triples are sent, the serializing must be finished
with a call to
<link linkend="raptor-serialize-end"><function>raptor_serialize_end()</function></link>.
In particular, only at this point does the
<link linkend="raptor-iostream"><type>raptor_iostream</type></link>
get flushed or any string constructed for
<link linkend="raptor-serialize-start-to-string"><function>raptor_serialize_start_to_string()</function></link>.
<programlisting>
/* start the serializing somehow */
while( /* got RDF triples */ ) {
raptor_serialize_statement(rdf_serializer, triple);
}
raptor_serialize_end(rdf_serializer);
/* now can use the serializing result (FILE, string, raptor_iostream) */
</programlisting>
</para>
</section>
<section id="tutorial-serializer-runtime-info">
<title>Querying serializer run-time information</title>
<para>
<link linkend="raptor-serializer-get-iostream"><function>raptor_serializer_get_iostream()</function></link>
gets the current serializer's raptor_iostream.
</para>
<para>
<link linkend="raptor-serializer-get-locator"><function>raptor_serializer_get_locator()</function></link>
returns the <link linkend="raptor-locator"><type>raptor_locator</type></link>
for the current position in the output stream. The <emphasis>locator</emphasis>
structure contains full information on the details of where in the
file or URI the current serializer has reached.
</para>
</section>
<section id="tutorial-serializer-destroy">
<title>Destroy the serializer</title>
<para>
To tidy up, delete the serializer object as follows:
<programlisting>
raptor_free_serializer(rdf_serializer);
</programlisting>
</para>
</section>
<section id="tutorial-serializer-example">
<title>Serializing example code</title>
<example id="raptor-example-rdfcat">
<title><filename>rdfcat.c</filename>: Read any RDF syntax and serialize to RDF/XML (Abbreviated)</title>
<programlisting>
<xi:include href="rdfcat.c" parse="text"/>
</programlisting>
<para>Compile it like this:
<screen>
$ gcc -o rdfcat rdfcat.c `raptor-config --cflags` `raptor-config --libs`
</screen>
and run it on an RDF file as:
<screen>
$ ./rdfcat raptor.rdf
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://usefulinc.com/ns/doap#">
<rdf:Description rdf:about="">
<foaf:maker>
<foaf:Person>
<foaf:name>Dave Beckett</foaf:name>
...
</screen>
</para>
</example>
</section>
</chapter>
<!--
Local variables:
mode: sgml
sgml-parent-document: ("raptor-docs.xml" "book" "part")
End:
-->
|