You are viewing a plain text version of this content. The canonical link for it is here.
Posted to triplesoup-commits@incubator.apache.org by le...@apache.org on 2007/04/13 15:19:52 UTC
svn commit: r528519 [12/12] - in
/incubator/triplesoup/donations/TRIPLES-2-libb: ./ docs/ docs/html/
docs/tmpl/ docs/xml/ redland-integration/ redland/ src/ test/ tests/ utils/
Added: incubator/triplesoup/donations/TRIPLES-2-libb/utils/rdf2b.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-2-libb/utils/rdf2b.c?view=auto&rev=528519
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-2-libb/utils/rdf2b.c (added)
+++ incubator/triplesoup/donations/TRIPLES-2-libb/utils/rdf2b.c Fri Apr 13 08:19:45 2007
@@ -0,0 +1,277 @@
+#include "b.h"
+#include <redland.h>
+
+static void
+usage (char *what)
+{
+ fprintf (stderr, "%s b_storage file.rdf\n", what);
+}
+
+int
+main (int argc, char **argv)
+{
+ librdf_uri *uri;
+ librdf_storage *storage;
+ librdf_model *model;
+ librdf_parser *parser;
+ librdf_stream *stream;
+ librdf_world *world;
+ char *buffer;
+ struct stat st;
+ b_t *data;
+ int i, fd, ret;
+
+ if (argc != 3)
+ {
+ usage (argv[0]);
+ return 1;
+ }
+
+ if (stat (argv[2], &st) || !S_ISREG (st.st_mode))
+ {
+ usage (argv[0]);
+ return 1;
+ }
+
+ if (!(buffer = malloc (sizeof (char) * (st.st_size + 1))))
+ {
+ fprintf (stderr, "Error memory.\n");
+ return 1;
+ }
+
+ if ((fd = open (argv[2], O_RDONLY)) < 0)
+ {
+ free (buffer);
+ fprintf (stderr, "Error opening %s file.\n", argv[2]);
+ return 1;
+ }
+
+ i = 0;
+ while (i < st.st_size)
+ {
+ ret = read (fd, buffer + i, st.st_size - i);
+
+ if (ret <= 0)
+ {
+ close (fd);
+ free (buffer);
+ fprintf (stderr, "Error reading from file.\n");
+ return 1;
+ }
+
+ i += ret;
+ }
+
+ close (fd);
+
+ /* World: */
+ if (!(world = librdf_new_world ()))
+ {
+ free (buffer);
+ fprintf (stderr, "Error creating a new world.\n");
+ return 1;
+ }
+
+ librdf_world_open (world);
+
+ /* Base URI: */
+ if (!(uri = librdf_new_uri (world, (unsigned char *) "rdf2b")))
+ {
+ free (buffer);
+ librdf_free_world (world);
+ fprintf (stderr, "Error creating a new URI.\n");
+ return 1;
+ }
+
+ /* Memory storage: */
+ if (!(storage = librdf_new_storage (world, "memory", NULL, NULL)))
+ {
+ free (buffer);
+ librdf_free_uri (uri);
+ librdf_free_world (world);
+ fprintf (stderr, "Error creating a memory storage.\n");
+ return 1;
+ }
+
+ /* Model: */
+ if (!(model = librdf_new_model (world, storage, NULL)))
+ {
+ free (buffer);
+ librdf_free_storage (storage);
+ librdf_free_uri (uri);
+ librdf_free_world (world);
+ fprintf (stderr, "Error creating a redland model.\n");
+ return 1;
+ }
+
+ /* Raptor (rdfxml) parser: */
+ if (!(parser = librdf_new_parser (world, "raptor", NULL, NULL)))
+ {
+ free (buffer);
+ librdf_free_model (model);
+ librdf_free_storage (storage);
+ librdf_free_uri (uri);
+ librdf_free_world (world);
+ fprintf (stderr, "Error creating a redland parser (raptor).\n");
+ return 1;
+ }
+
+ /* Parsing... */
+ if (librdf_parser_parse_string_into_model
+ (parser, (unsigned char *) buffer, uri, model))
+ {
+ free (buffer);
+ librdf_free_parser (parser);
+ librdf_free_model (model);
+ librdf_free_storage (storage);
+ librdf_free_uri (uri);
+ librdf_free_world (world);
+ fprintf (stderr, "Error parsing the buffer.\n");
+ return 1;
+ }
+
+ /* Stream of statement: */
+ if (!(stream = librdf_model_as_stream (model)))
+ {
+ free (buffer);
+ librdf_free_parser (parser);
+ librdf_free_model (model);
+ librdf_free_storage (storage);
+ librdf_free_uri (uri);
+ librdf_free_world (world);
+ fprintf (stderr, "Error creating a stream for the model.\n");
+ return 1;
+ }
+
+ /* Opening B: */
+ if (b_new (&data, (unsigned char *) argv[1]) != B_OK)
+ {
+ free (buffer);
+ librdf_free_parser (parser);
+ librdf_free_model (model);
+ librdf_free_storage (storage);
+ librdf_free_uri (uri);
+ librdf_free_world (world);
+ fprintf (stderr, "Error opening the database\n");
+ return 1;
+ }
+
+ /* For each statement... */
+ while (!librdf_stream_end (stream))
+ {
+ librdf_node *node;
+ librdf_statement *statement;
+ unsigned char *subject = NULL;
+ int subject_type;
+ unsigned char *property = NULL;
+ unsigned char *object = NULL;
+ int object_type;
+ char *lang = NULL;
+ unsigned char *datatype = NULL;
+ librdf_uri *datatype_uri;
+
+ b_triple_t *triple;
+
+ if (!(statement = librdf_stream_get_object (stream)))
+ break;
+
+ /* Subject: */
+ node = librdf_statement_get_subject (statement);
+ switch (librdf_node_get_type (node))
+ {
+ case LIBRDF_NODE_TYPE_RESOURCE:
+ subject_type = 0;
+ uri = librdf_node_get_uri (node);
+ subject = librdf_uri_as_string (uri);
+ break;
+
+ case LIBRDF_NODE_TYPE_BLANK:
+ subject_type = 1;
+ subject = librdf_node_get_blank_identifier (node);
+ break;
+
+ default:
+ fprintf (stderr, "Redland internal error\n");
+ return 1;
+ }
+
+ /* Property: */
+ node = librdf_statement_get_predicate (statement);
+ if (librdf_node_get_type (node) != LIBRDF_NODE_TYPE_RESOURCE)
+ {
+ fprintf (stderr, "Redland internal error\n");
+ return 1;
+ }
+
+ uri = librdf_node_get_uri (node);
+ property = librdf_uri_as_string (uri);
+
+ /* Object: */
+ node = librdf_statement_get_object (statement);
+ switch (librdf_node_get_type (node))
+ {
+ case LIBRDF_NODE_TYPE_RESOURCE:
+ object_type = 0;
+ uri = librdf_node_get_uri (node);
+ object = librdf_uri_as_string (uri);
+ break;
+
+ case LIBRDF_NODE_TYPE_BLANK:
+ object_type = 1;
+ object = librdf_node_get_blank_identifier (node);
+ break;
+
+ case LIBRDF_NODE_TYPE_LITERAL:
+ object_type = 2;
+ object = librdf_node_get_literal_value (node);
+
+ lang = librdf_node_get_literal_value_language (node);
+
+
+ if ((datatype_uri =
+ librdf_node_get_literal_value_datatype_uri (node)))
+ datatype = librdf_uri_as_string (datatype_uri);
+ break;
+
+ default:
+ fprintf (stderr, "Redland internal error\n");
+ return 1;
+ }
+
+ if (b_triple_new (&triple,
+ subject_type == 0 ? subject : NULL, 0,
+ subject_type == 1 ? subject : NULL, 0,
+ property, 0,
+ object_type == 0 ? object : NULL, 0,
+ object_type == 1 ? object : NULL, 0,
+ object_type == 2 ? object : NULL, 0,
+ NULL, 0, datatype, 0, (unsigned char *) lang,
+ 0) != B_OK)
+ {
+ fprintf (stderr, "B internal error\n");
+ return 1;
+ }
+
+ b_add_triple (data, triple);
+ b_triple_destroy (triple);
+
+ /* Next statement: */
+ librdf_stream_next (stream);
+ }
+
+ /* Closing b: */
+ b_destroy (data);
+ free (buffer);
+
+ /* Freeing memory: */
+ librdf_free_stream (stream);
+ librdf_free_parser (parser);
+ librdf_free_model (model);
+ librdf_free_storage (storage);
+ librdf_free_uri (uri);
+ librdf_free_world (world);
+
+ return 0;
+}
+
+/* EOF */