diff options
author | Philip Van Hoof <philip@codeminded.be> | 2011-02-21 15:51:46 +0100 |
---|---|---|
committer | Philip Van Hoof <philip@codeminded.be> | 2011-02-21 15:51:46 +0100 |
commit | 0637847d86b2eeee53d1eb70db92fa1f7122d809 (patch) | |
tree | 19c30ca7da6301338065bef24c9096021d333a08 /utils/tracker-resdump | |
parent | 385f3bb2da12766bb4840def28bae8869753c004 (diff) | |
download | tracker-0637847d86b2eeee53d1eb70db92fa1f7122d809.tar.gz |
utils/tracker-resdump: Added tool to dump a resource as TTL
Diffstat (limited to 'utils/tracker-resdump')
-rw-r--r-- | utils/tracker-resdump/.gitignore | 2 | ||||
-rw-r--r-- | utils/tracker-resdump/Makefile.am | 33 | ||||
-rw-r--r-- | utils/tracker-resdump/tracker-resdump.vala | 118 |
3 files changed, 153 insertions, 0 deletions
diff --git a/utils/tracker-resdump/.gitignore b/utils/tracker-resdump/.gitignore new file mode 100644 index 000000000..704302eaf --- /dev/null +++ b/utils/tracker-resdump/.gitignore @@ -0,0 +1,2 @@ +tracker-resdump +tracker-resdump.c diff --git a/utils/tracker-resdump/Makefile.am b/utils/tracker-resdump/Makefile.am new file mode 100644 index 000000000..d2c63efe1 --- /dev/null +++ b/utils/tracker-resdump/Makefile.am @@ -0,0 +1,33 @@ +include $(top_srcdir)/Makefile.decl + +noinst_PROGRAMS = tracker-resdump + +AM_VALAFLAGS = \ + --pkg gio-2.0 \ + --pkg gee-1.0 \ + $(BUILD_VALAFLAGS) \ + $(top_srcdir)/src/libtracker-sparql/tracker-sparql-$(TRACKER_API_VERSION).vapi \ + $(top_srcdir)/src/libtracker-bus/tracker-bus.vapi + +AM_CPPFLAGS =\ + $(BUILD_CFLAGS) \ + -I$(top_srcdir)/src \ + -I$(top_builddir)/src \ + $(LIBTRACKER_SPARQL_CFLAGS) \ + $(TRACKER_RESDUMP_CFLAGS) + +LDADD =\ + $(top_builddir)/src/libtracker-data/libtracker-data.la \ + $(top_builddir)/src/libtracker-direct/libtracker-direct.la \ + $(top_builddir)/src/libtracker-bus/libtracker-bus.la \ + $(top_builddir)/src/libtracker-sparql/libtracker-sparql-$(TRACKER_API_VERSION).la \ + $(top_builddir)/src/libtracker-common/libtracker-common.la \ + $(BUILD_LIBS)\ + $(LIBTRACKER_SPARQL_LIBS) \ + $(TRACKER_RESDUMP_LIBS) + +tracker_resdump_SOURCES = tracker-resdump.vala +tracker_resdump_CFLAGS = $(AM_CPPFLAGS) +tracker_resdump_VALAFLAGS = $(AM_VALAFLAGS) +tracker_resdump_LDADD = $(LDADD) + diff --git a/utils/tracker-resdump/tracker-resdump.vala b/utils/tracker-resdump/tracker-resdump.vala new file mode 100644 index 000000000..4d75818cc --- /dev/null +++ b/utils/tracker-resdump/tracker-resdump.vala @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2010, Nokia + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +using Tracker.Sparql; + +static Connection conn; + +static void usage (string[] args) { + stderr.printf("Usage: %s urn\n", args[0]); +} + +static Gee.Set<string> looked_up_iris; + +static bool dump_resource (string urn) { + Gee.List<string> iris_to_lookup = new Gee.ArrayList<string> (); + + looked_up_iris.add (urn); + + try { + Cursor cursor = conn.query ("SELECT ?p rdfs:range(?p) ?o {<%s> ?p ?o}".printf (urn)); + + Gee.List<string> type_statements = new Gee.ArrayList<string>(); + Gee.List<string> statements = new Gee.ArrayList<string>(); + + while (cursor.next ()) { + // Skip tracker internal stuff + if (cursor.get_string (0).has_prefix ("http://www.tracker-project.org/ontologies/tracker#")) { + continue; + } + + string statement = "<%s> <%s> ".printf (urn, cursor.get_string (0)); + + switch (cursor.get_string(1)) { + case "http://www.w3.org/2001/XMLSchema#string": + case "http://www.w3.org/2001/XMLSchema#dateTime": + statement += "\"%s\"".printf (escape_string (cursor.get_string (2))); + break; + case "http://www.w3.org/2001/XMLSchema#integer": + case "http://www.w3.org/2001/XMLSchema#double": + case "http://www.w3.org/2001/XMLSchema#boolean": + statement += "%s".printf (cursor.get_string (2)); + break; + default: + // Assume resource + unowned string obj = cursor.get_string (2); + if (!looked_up_iris.contains (obj)) { + iris_to_lookup.add (obj); + } + statement += "<%s>".printf (obj); + break; + } + + if (cursor.get_string (0) == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type") { + type_statements.add (statement); + } else { + statements.add (statement); + } + } + + foreach (string s in type_statements) { + stdout.printf ("%s .\n", s); + } + + foreach (string s in statements) { + stdout.printf ("%s .\n", s); + } + + foreach (string s in iris_to_lookup) { + if (!dump_resource (s)) { + return false; + } + } + } catch (GLib.Error e) { + critical ("Couldn't query info for resource %s: %s", urn, e.message); + return false; + } + + return true; +} + +static int main(string[] args) +{ + if (args.length != 2) { + usage (args); + return 1; + } + + try { + conn = Connection.get(); + } catch (GLib.Error e) { + critical("Couldn't connect to Tracker: %s", e.message); + return 1; + } + + looked_up_iris = new Gee.HashSet<string>(); + + if (dump_resource (args[1])) { + return 0; + } + + return 1; +} |