[tracker/wip/sam/resource] Add Turtle serialisation to tracker-resource
- From: Sam Thursfield <sthursfield src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/wip/sam/resource] Add Turtle serialisation to tracker-resource
- Date: Sat, 9 Apr 2016 16:14:16 +0000 (UTC)
commit 613fb027cd884b3a84fcbf4f3ea44c7bccb042a9
Author: Sam Thursfield <sam afuera me uk>
Date: Sat Apr 9 17:12:18 2016 +0100
Add Turtle serialisation to tracker-resource
src/libtracker-common/tracker-enums.h | 1 +
src/libtracker-sparql/tracker-resource.c | 85 ++++++++++++++++++++++++++++++
src/libtracker-sparql/tracker-resource.h | 2 +
3 files changed, 88 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-common/tracker-enums.h b/src/libtracker-common/tracker-enums.h
index edce490..5121a72 100644
--- a/src/libtracker-common/tracker-enums.h
+++ b/src/libtracker-common/tracker-enums.h
@@ -43,6 +43,7 @@ typedef enum {
TRACKER_SERIALIZATION_FORMAT_JSON,
TRACKER_SERIALIZATION_FORMAT_JSON_LD,
TRACKER_SERIALIZATION_FORMAT_SPARQL,
+ TRACKER_SERIALIZATION_FORMAT_TURTLE,
} TrackerSerializationFormat;
G_END_DECLS
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 2f7c296..dd2d582 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -534,6 +534,91 @@ tracker_resource_compare (TrackerResource *a,
}
+typedef struct {
+ TrackerNamespaceManager *namespaces;
+ GString *string;
+ GList *done_list;
+} GenerateTurtleData;
+
+void generate_turtle (TrackerResource *resource, GenerateTurtleData *data);
+
+void
+generate_turtle_resources_foreach (gpointer key,
+ gpointer value_ptr,
+ gpointer user_data)
+{
+ const char *property = key;
+ const GValue *value = value_ptr;
+ GenerateTurtleData *data = user_data;
+
+ if (G_VALUE_HOLDS (value, TRACKER_TYPE_RESOURCE)) {
+ TrackerResource *resource = g_value_get_object (value);
+ if (g_list_find_custom (data->done_list, resource, (GCompareFunc) tracker_resource_compare)
== NULL) {
+ generate_turtle (resource, data);
+ data->done_list = g_list_prepend (data->done_list, resource);
+ }
+ }
+}
+
+void
+generate_turtle_properties_foreach (gpointer key,
+ gpointer value_ptr,
+ gpointer user_data)
+{
+ const char *property = key;
+ const GValue *value = value_ptr;
+ GenerateTurtleData *data = user_data;
+
+ /* FIXME: really crappy Turtle serializer */
+ g_string_append_printf (data->string, " %s %s ;\n", property, g_value_dup_string (value));
+}
+
+void
+generate_turtle (TrackerResource *resource,
+ GenerateTurtleData *data)
+{
+ TrackerResourcePrivate *priv = GET_PRIVATE (resource);
+ GString *result;
+
+ g_hash_table_foreach (priv->properties, generate_turtle_resources_foreach, data);
+
+ g_string_append_printf (data->string, "<%s> ", priv->identifier);
+
+ g_hash_table_foreach (priv->properties, generate_turtle_properties_foreach, data);
+
+ g_string_append_printf (data->string, ".\n");
+}
+
+/**
+ * tracker_resource_print_turtle:
+ * @resource: a #TrackerResource
+ * @error: address where an error can be returned
+ *
+ * Serialize all the information in @resource as a Turtle document.
+ *
+ * The generated Turtle should correspond to this standard:
+ * <https://www.w3.org/TR/2014/REC-turtle-20140225/>
+ *
+ * Returns: a newly-allocated string
+ *
+ * Since: 1.10
+ */
+char *
+tracker_resource_print_turtle(TrackerResource *self,
+ TrackerNamespaceManager *namespaces)
+{
+ GenerateTurtleData context;
+
+ context.namespaces = namespaces;
+ context.string = g_string_new ("");
+ context.done_list = NULL;
+
+ generate_turtle (self, &context);
+
+ return g_string_free (context.string, FALSE);
+}
+
+
static void generate_jsonld_foreach (gpointer key, gpointer value_ptr, gpointer user_data);
/* FIXME: this could hit an infinite loop if there are circular resource
diff --git a/src/libtracker-sparql/tracker-resource.h b/src/libtracker-sparql/tracker-resource.h
index 260b7ca..5aea11b 100644
--- a/src/libtracker-sparql/tracker-resource.h
+++ b/src/libtracker-sparql/tracker-resource.h
@@ -69,6 +69,8 @@ const char *tracker_resource_get_first_uri (TrackerResource *self, const char *p
const char *tracker_resource_get_identifier (TrackerResource *self);
gint tracker_resource_identifier_compare_func (TrackerResource *resource, const char *identifier);
+char *tracker_resource_print_turtle(TrackerResource *self, TrackerNamespaceManager *namespaces);
+
char *tracker_resource_print_jsonld (TrackerResource *self, GError **error);
void tracker_resource_generate_sparql_update (TrackerResource *self, TrackerSparqlBuilder *builder,
TrackerNamespaceManager *namespaces, const char *graph_id, GError **error);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]