[tracker/wip/carlosg/resources-as-cursors: 7/18] libtracker-sparql: Add generic public method to print RDF from a TrackerResource




commit 28dbda94b87b55033ebeb7aef7e313f998ee4009
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sat Jul 2 19:58:54 2022 +0200

    libtracker-sparql: Add generic public method to print RDF from a TrackerResource
    
    This function takes a TrackerRdfFormat, and allows serializing a TrackerResource
    into all the supported formats.
    
    Internally, it uses the builtin RDF serializers by feeding the resource as a
    cursor.

 src/libtracker-sparql/tracker-resource.c | 82 ++++++++++++++++++++++++++++++++
 src/libtracker-sparql/tracker-resource.h |  7 +++
 2 files changed, 89 insertions(+)
---
diff --git a/src/libtracker-sparql/tracker-resource.c b/src/libtracker-sparql/tracker-resource.c
index 4022091d6..ff5cb4d92 100644
--- a/src/libtracker-sparql/tracker-resource.c
+++ b/src/libtracker-sparql/tracker-resource.c
@@ -27,6 +27,7 @@
 
 #include <string.h>
 
+#include <tracker-deserializer-resource.h>
 #include <tracker-uri.h>
 #include <tracker-resource.h>
 #include <tracker-ontologies.h>
@@ -1770,6 +1771,87 @@ tracker_resource_print_jsonld (TrackerResource         *self,
        return result;
 }
 
+static TrackerSerializerFormat
+convert_format (TrackerRdfFormat format)
+{
+       switch (format) {
+       case TRACKER_RDF_FORMAT_TURTLE:
+               return TRACKER_SERIALIZER_FORMAT_TTL;
+       case TRACKER_RDF_FORMAT_TRIG:
+               return TRACKER_SERIALIZER_FORMAT_TRIG;
+       case TRACKER_N_RDF_FORMATS:
+               g_assert_not_reached ();
+       }
+
+       return -1;
+}
+
+/**
+ * tracker_resource_print_rdf:
+ * @self: a #TrackerResource
+ * @namespaces: a set of prefixed URLs
+ * @format: RDF format of the printed string
+ * @graph: target graph of the resource RDF, or %NULL for the default graph
+ *
+ * Serialize all the information in @resource into the selected RDF format.
+ *
+ * The @namespaces object is used to expand any compact URI values. In most
+ * cases you should pass the one returned by tracker_sparql_connection_get_namespace_manager()
+ * from the connection that is the intended recipient of this data.
+ *
+ * Returns: a newly-allocated string containing RDF data in the requested format.
+ *
+ * Since: 3.4
+ **/
+char *
+tracker_resource_print_rdf (TrackerResource         *self,
+                            TrackerNamespaceManager *namespaces,
+                            TrackerRdfFormat         format,
+                            const gchar             *graph)
+{
+       TrackerSparqlCursor *deserializer;
+       GInputStream *serializer;
+       GString *str;
+
+       g_return_val_if_fail (TRACKER_IS_RESOURCE (self), NULL);
+       g_return_val_if_fail (TRACKER_IS_NAMESPACE_MANAGER (namespaces), NULL);
+       g_return_val_if_fail (format < TRACKER_N_RDF_FORMATS, NULL);
+
+#define BUF_SIZE 4096
+       deserializer = tracker_deserializer_resource_new (self, namespaces, graph);
+       serializer = tracker_serializer_new (TRACKER_SPARQL_CURSOR (deserializer),
+                                            namespaces,
+                                            convert_format (format));
+       g_object_unref (deserializer);
+
+       str = g_string_new (NULL);
+
+       while (TRUE) {
+               GBytes *bytes;
+
+               bytes = g_input_stream_read_bytes (serializer, BUF_SIZE, NULL, NULL);
+               if (!bytes) {
+                       g_string_free (str, TRUE);
+                       return NULL;
+               }
+
+               if (g_bytes_get_size (bytes) == 0) {
+                       g_bytes_unref (bytes);
+                       break;
+               }
+
+               g_string_append_len (str,
+                                    g_bytes_get_data (bytes, NULL),
+                                    g_bytes_get_size (bytes));
+               g_bytes_unref (bytes);
+       }
+#undef BUF_SIZE
+
+       g_object_unref (serializer);
+
+       return g_string_free (str, FALSE);
+}
+
 static GVariant *
 tracker_serialize_single_value (TrackerResource         *resource,
                                 const GValue            *value)
diff --git a/src/libtracker-sparql/tracker-resource.h b/src/libtracker-sparql/tracker-resource.h
index 129685eb0..420230470 100644
--- a/src/libtracker-sparql/tracker-resource.h
+++ b/src/libtracker-sparql/tracker-resource.h
@@ -21,6 +21,7 @@
 #define __LIBTRACKER_RESOURCE_H__
 
 #include <glib-object.h>
+#include <libtracker-sparql/tracker-enums.h>
 #include <libtracker-sparql/tracker-version.h>
 #include <libtracker-sparql/tracker-namespace-manager.h>
 
@@ -114,6 +115,12 @@ char *tracker_resource_print_sparql_update (TrackerResource *self, TrackerNamesp
 TRACKER_AVAILABLE_IN_ALL
 char *tracker_resource_print_jsonld (TrackerResource *self, TrackerNamespaceManager *namespaces);
 
+TRACKER_AVAILABLE_IN_3_4
+char * tracker_resource_print_rdf (TrackerResource         *self,
+                                   TrackerNamespaceManager *namespaces,
+                                   TrackerRdfFormat         format,
+                                   const gchar             *graph);
+
 TRACKER_AVAILABLE_IN_ALL
 GVariant * tracker_resource_serialize (TrackerResource *resource);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]