[tracker/parser: 21/21] Replace raptor by hand-written Turtle parser



commit 3bc4f9adf4fad4d9da0a9a6fb3cadcbb68f37d7c
Author: Jürg Billeter <j bitron ch>
Date:   Tue Aug 18 10:26:42 2009 +0200

    Replace raptor by hand-written Turtle parser

 configure.ac                                     |    5 -
 src/libtracker-common/.gitignore                 |    1 +
 src/libtracker-common/Makefile.am                |    5 +-
 src/libtracker-common/tracker-turtle-writer.vala |  180 ++++++
 src/libtracker-data/.gitignore                   |    1 +
 src/libtracker-data/Makefile.am                  |    9 +-
 src/libtracker-data/tracker-data-backup.c        |    3 -
 src/libtracker-data/tracker-data-manager.c       |   47 +-
 src/libtracker-data/tracker-sparql-scanner.vala  |   13 +
 src/libtracker-data/tracker-turtle-reader.vala   |  366 ++++++++++++
 src/libtracker-data/tracker-turtle.c             |  664 ----------------------
 src/libtracker-data/tracker-turtle.h             |   71 ---
 src/plugins/evolution/Makefile.am                |    1 -
 src/plugins/kmail/Makefile.am                    |    1 -
 src/tracker-miner-fs/tracker-indexer.c           |    1 -
 src/tracker-miner-fs/tracker-main.c              |    4 -
 src/tracker-miner-fs/tracker-removable-device.c  |   19 +-
 src/tracker-miner-fs/tracker-removable-device.h  |    2 +
 src/tracker-store/tracker-main.c                 |    3 -
 src/tracker-store/tracker-store.c                |   39 +-
 tests/libtracker-data/Makefile.am                |    6 +-
 tests/libtracker-data/tracker-ontology-test.c    |   19 +-
 tests/libtracker-data/tracker-sparql-test.c      |   19 +-
 utils/services/Makefile.am                       |   11 +-
 utils/services/data-validator.c                  |  120 ++---
 utils/services/ontology-validator.c              |   73 +--
 utils/services/ttl2html.c                        |    2 -
 utils/services/ttl_loader.c                      |  152 ++----
 28 files changed, 744 insertions(+), 1093 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 69c5232..3700c01 100644
--- a/configure.ac
+++ b/configure.ac
@@ -230,11 +230,6 @@ if test x$have_enca == "xyes"; then
   AC_DEFINE(HAVE_ENCA, 1, [Enca language detection aid])
 fi
 
-# Check for Raptor
-PKG_CHECK_MODULES(RAPTOR, [raptor >= 1.4.18])
-AC_SUBST(RAPTOR_CFLAGS)
-AC_SUBST(RAPTOR_LIBS)
-
 AC_PATH_PROG(VALAC, valac, valac)
 AC_SUBST(VALAC)
 
diff --git a/src/libtracker-common/.gitignore b/src/libtracker-common/.gitignore
index eb2fec8..2fd3f76 100644
--- a/src/libtracker-common/.gitignore
+++ b/src/libtracker-common/.gitignore
@@ -1,2 +1,3 @@
 tracker-sparql-builder.c
 tracker-sparql-builder.h
+tracker-turtle-writer.c
diff --git a/src/libtracker-common/Makefile.am b/src/libtracker-common/Makefile.am
index 448628c..cbdf8a8 100644
--- a/src/libtracker-common/Makefile.am
+++ b/src/libtracker-common/Makefile.am
@@ -51,7 +51,8 @@ endif
 endif
 
 libtracker_common_la_VALASOURCES =			\
-	tracker-sparql-builder.vala
+	tracker-sparql-builder.vala			\
+	tracker-turtle-writer.vala
 
 libtracker_common_la_SOURCES =	 			\
 	$(marshal_sources)				\
@@ -103,7 +104,7 @@ libtracker_commoninclude_HEADERS =			\
 	tracker-utils.h
 
 libtracker-common.vala.stamp: $(libtracker_common_la_VALASOURCES)
-	$(VALAC) -C $(VALAFLAGS) -H tracker-sparql-builder.h posix.vapi $^
+	$(VALAC) -C $(VALAFLAGS) --pkg gio-2.0 -H tracker-sparql-builder.h posix.vapi $^
 	touch $@
 
 libtracker_common_la_LDFLAGS = 				\
diff --git a/src/libtracker-common/tracker-turtle-writer.vala b/src/libtracker-common/tracker-turtle-writer.vala
new file mode 100644
index 0000000..311da4e
--- /dev/null
+++ b/src/libtracker-common/tracker-turtle-writer.vala
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2009, 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.
+ */
+
+public class Tracker.TurtleWriter : Object {
+	enum State {
+		SUBJECT,
+		PREDICATE,
+		OBJECT,
+		BLANK
+	}
+
+	DataOutputStream stream;
+
+	State state {
+		get { return states[states.length - 1]; }
+	}
+
+	State[] states;
+
+	IOError? err;
+
+	public TurtleWriter (OutputStream stream) {
+		this.stream = new DataOutputStream (stream);
+	}
+
+	void append (string s) {
+		if (err != null) {
+			return;
+		}
+		try {
+			stream.put_string (s, null);
+		} catch (Error e) {
+			this.err = (IOError) e;
+		}
+	}
+
+	public void subject_iri (string iri) {
+		subject ("<%s>".printf (iri));
+	}
+
+	public void subject (string s)
+		requires (states.length == 0 || state == State.OBJECT)
+	{
+		if (states.length > 0 && state == State.OBJECT) {
+			append (" .\n");
+			states.length -= 3;
+		}
+		append (s);
+		states += State.SUBJECT;
+	}
+
+	public void predicate_iri (string iri) {
+		predicate ("<%s>".printf (iri));
+	}
+
+	public void predicate (string s)
+		requires (state == State.SUBJECT || state == State.OBJECT || state == State.BLANK)
+	{
+		if (state == State.OBJECT) {
+			append (" ;");
+			states.length -= 2;
+		}
+		append (" ");
+		append (s);
+		states += State.PREDICATE;
+	}
+
+	public void object_iri (string iri) {
+		object ("<%s>".printf (iri));
+	}
+
+	public void object (string s)
+		requires (state == State.PREDICATE || state == State.OBJECT)
+	{
+		if (state == State.OBJECT) {
+			append (" ,");
+			states.length--;
+		}
+		append (" ");
+		append (s);
+		states += State.OBJECT;
+	}
+
+	public void object_string (string literal)
+		requires (state == State.PREDICATE || state == State.OBJECT)
+	{
+		if (state == State.OBJECT) {
+			append (" ,");
+			states.length--;
+		}
+
+		append (" \"");
+
+		char* p = literal;
+		while (*p != '\0') {
+			size_t len = Posix.strcspn ((string) p, "\t\n\r\"\\");
+			append (((string) p).ndup ((long) len));
+			p += len;
+			switch (*p) {
+			case '\t':
+				append ("\\t");
+				break;
+			case '\n':
+				append ("\\n");
+				break;
+			case '\r':
+				append ("\\r");
+				break;
+			case '"':
+				append ("\\\"");
+				break;
+			case '\\':
+				append ("\\\\");
+				break;
+			default:
+				continue;
+			}
+			p++;
+		}
+
+		append ("\"");
+
+		states += State.OBJECT;
+	}
+
+	public void object_boolean (bool literal) {
+		object (literal ? "true" : "false");
+	}
+
+	public void object_int64 (int64 literal) {
+		object (literal.to_string ());
+	}
+
+	public void object_date (time_t literal) {
+		var tm = Time.gm (literal);
+
+		object_string ("%04d-%02d-%02dT%02d:%02d:%02d".printf (tm.year + 1900, tm.month + 1, tm.day, tm.hour, tm.minute, tm.second));
+	}
+
+	public void object_blank_open ()
+		requires (state == State.PREDICATE || state == State.OBJECT)
+	{
+		if (state == State.OBJECT) {
+			append (" ,");
+			states.length--;
+		}
+		append (" [");
+		states += State.BLANK;
+	}
+
+	public void object_blank_close ()
+		requires (state == State.OBJECT && states[states.length - 3] == state.BLANK)
+	{
+		append ("]");
+		states.length -= 3;
+		states += State.OBJECT;
+	}
+
+	public void close () throws IOError {
+		if (err != null) {
+			throw err;
+		}
+	}
+}
diff --git a/src/libtracker-data/.gitignore b/src/libtracker-data/.gitignore
index 8ff3464..7b4ad3b 100644
--- a/src/libtracker-data/.gitignore
+++ b/src/libtracker-data/.gitignore
@@ -1,3 +1,4 @@
 tracker-sparql-query.c
 tracker-sparql-query.h
 tracker-sparql-scanner.c
+tracker-turtle-reader.c
diff --git a/src/libtracker-data/Makefile.am b/src/libtracker-data/Makefile.am
index 7e13120..2864455 100644
--- a/src/libtracker-data/Makefile.am
+++ b/src/libtracker-data/Makefile.am
@@ -9,7 +9,6 @@ INCLUDES =								\
 	$(GLIB2_CFLAGS)							\
 	$(DBUS_CFLAGS)							\
 	$(UUID_CFLAGS)							\
-	$(RAPTOR_CFLAGS)						\
 	$(GCOV_CFLAGS)
 
 BUILT_SOURCES = libtracker-data.vala.stamp
@@ -19,14 +18,14 @@ libtracker_data_LTLIBRARIES = libtracker-data.la
 
 libtracker_data_la_VALASOURCES = 					\
 	tracker-sparql-query.vala					\
-	tracker-sparql-scanner.vala
+	tracker-sparql-scanner.vala					\
+	tracker-turtle-reader.vala
 
 libtracker_data_la_SOURCES = 						\
 	tracker-data-backup.c						\
 	tracker-data-manager.c						\
 	tracker-data-query.c						\
 	tracker-data-update.c						\
-	tracker-turtle.c						\
 	libtracker-data.vala.stamp					\
 	$(libtracker_data_la_VALASOURCES:.vala=.c)
 
@@ -35,8 +34,7 @@ noinst_HEADERS =							\
 	tracker-data-manager.h						\
 	tracker-data-query.h						\
 	tracker-data-update.h						\
-	tracker-sparql-query.h						\
-	tracker-turtle.h
+	tracker-sparql-query.h
 
 libtracker-data.vala.stamp: $(libtracker_data_la_VALASOURCES)
 	$(VALAC) -C $(VALAFLAGS) -H tracker-sparql-query.h ../libtracker-common/posix.vapi ../libtracker-common/libtracker-common.vapi libtracker-data.vapi ../libtracker-db/libtracker-db.vapi $^
@@ -51,7 +49,6 @@ libtracker_data_la_LIBADD = 						\
 	$(DBUS_LIBS)							\
 	$(GLIB2_LIBS)							\
 	$(UUID_LIBS)							\
-	$(RAPTOR_LIBS)							\
 	$(GCOV_LIBS)							\
 	-lz
 
diff --git a/src/libtracker-data/tracker-data-backup.c b/src/libtracker-data/tracker-data-backup.c
index 20e703e..124d53a 100644
--- a/src/libtracker-data/tracker-data-backup.c
+++ b/src/libtracker-data/tracker-data-backup.c
@@ -23,10 +23,7 @@
 #include <glib.h>
 #include <glib/gstdio.h>
 
-#include <raptor.h>
-
 #include <libtracker-data/tracker-data-query.h>
-#include <libtracker-data/tracker-turtle.h>
 
 #include "tracker-data-backup.h"
 
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 3f4c00e..09dd5f7 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -42,7 +42,7 @@
 
 #include "tracker-data-manager.h"
 #include "tracker-data-update.h"
-#include "tracker-turtle.h"
+#include "tracker-sparql-query.h"
 
 #define RDF_PREFIX TRACKER_RDF_PREFIX
 #define RDF_PROPERTY RDF_PREFIX "Property"
@@ -68,13 +68,16 @@ static gboolean            initialized;
 static void
 load_ontology_file_from_path (const gchar	 *ontology_file)
 {
-	tracker_turtle_reader_init (ontology_file, NULL);
-	while (tracker_turtle_reader_next ()) {
+	TrackerTurtleReader *reader;
+	GError              *error = NULL;
+
+	reader = tracker_turtle_reader_new (ontology_file);
+	while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
 		const gchar *subject, *predicate, *object;
 
-		subject = tracker_turtle_reader_get_subject ();
-		predicate = tracker_turtle_reader_get_predicate ();
-		object = tracker_turtle_reader_get_object ();
+		subject = tracker_turtle_reader_get_subject (reader);
+		predicate = tracker_turtle_reader_get_predicate (reader);
+		object = tracker_turtle_reader_get_object (reader);
 
 		if (strcmp (predicate, RDF_TYPE) == 0) {
 			if (strcmp (object, RDFS_CLASS) == 0) {
@@ -240,6 +243,13 @@ load_ontology_file_from_path (const gchar	 *ontology_file)
 			tracker_namespace_set_prefix (namespace, object);
 		}
 	}
+
+	g_object_unref (reader);
+
+	if (error) {
+		g_critical ("Turtle parse error: %s", error->message);
+		g_error_free (error);
+	}
 }
 
 static void
@@ -253,33 +263,12 @@ load_ontology_file (const gchar	      *filename)
 }
 
 static void
-import_ontology_file_from_path (const gchar	 *ontology_file)
-{
-	tracker_turtle_reader_init (ontology_file, NULL);
-	while (tracker_turtle_reader_next ()) {
-		if (tracker_turtle_reader_object_is_uri ()) {
-			tracker_data_insert_statement_with_uri (
-				tracker_turtle_reader_get_subject (),
-				tracker_turtle_reader_get_predicate (),
-				tracker_turtle_reader_get_object (),
-				NULL);
-		} else {
-			tracker_data_insert_statement_with_string (
-				tracker_turtle_reader_get_subject (),
-				tracker_turtle_reader_get_predicate (),
-				tracker_turtle_reader_get_object (),
-				NULL);
-		}
-	}
-}
-
-static void
 import_ontology_file (const gchar	      *filename)
 {
 	gchar		*ontology_file;
 
 	ontology_file = g_build_filename (ontologies_dir, filename, NULL);
-	import_ontology_file_from_path (ontology_file);
+	tracker_turtle_reader_load (ontology_file);
 	g_free (ontology_file);
 }
 
@@ -844,7 +833,7 @@ tracker_data_manager_init (TrackerDBManagerFlags       flags,
 			import_ontology_file (l->data);
 		}
 		if (test_schema) {
-			import_ontology_file_from_path (test_schema_path);
+			tracker_turtle_reader_load (test_schema_path);
 			g_free (test_schema_path);
 		}
 
diff --git a/src/libtracker-data/tracker-sparql-scanner.vala b/src/libtracker-data/tracker-sparql-scanner.vala
index 34eab5d..036786f 100644
--- a/src/libtracker-data/tracker-sparql-scanner.vala
+++ b/src/libtracker-data/tracker-sparql-scanner.vala
@@ -481,6 +481,17 @@ public class Tracker.SparqlScanner : Object {
 					current++;
 				}
 				break;
+			case '@':
+				type = SparqlTokenType.NONE;
+				current++;
+				if (current < end - "prefix".len () && matches (current, "PREFIX")) {
+					type = SparqlTokenType.ATPREFIX;
+					current += "prefix".len ();
+				} else if (current < end - "base".len () && matches (current, "BASE")) {
+					type = SparqlTokenType.ATBASE;
+					current += "base".len ();
+				}
+				break;
 			case '|':
 				type = SparqlTokenType.NONE;
 				current++;
@@ -766,6 +777,8 @@ public enum Tracker.SparqlTokenType {
 	AS,
 	ASC,
 	ASK,
+	ATBASE,
+	ATPREFIX,
 	AVG,
 	BASE,
 	BLANK_NODE,
diff --git a/src/libtracker-data/tracker-turtle-reader.vala b/src/libtracker-data/tracker-turtle-reader.vala
new file mode 100644
index 0000000..e985a65
--- /dev/null
+++ b/src/libtracker-data/tracker-turtle-reader.vala
@@ -0,0 +1,366 @@
+/*
+ * Copyright (C) 2009, 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.
+ */
+
+public class Tracker.TurtleReader : Object {
+	SparqlScanner scanner;
+
+	// token buffer
+	TokenInfo[] tokens;
+	// index of current token in buffer
+	int index;
+	// number of tokens in buffer
+	int size;
+
+	const int BUFFER_SIZE = 32;
+
+	struct TokenInfo {
+		public SparqlTokenType type;
+		public SourceLocation begin;
+		public SourceLocation end;
+	}
+
+	enum State {
+		INITIAL,
+		BOS,
+		SUBJECT,
+		PREDICATE,
+		OBJECT
+	}
+
+	State state;
+
+	public string subject { get; private set; }
+	public string predicate { get; private set; }
+	public string object { get; private set; }
+	public bool object_is_uri { get; private set; }
+
+	HashTable<string,string> prefix_map;
+
+	string[] subject_stack;
+
+	int bnodeid = 0;
+	// base UUID used for blank nodes
+	uchar[] base_uuid;
+
+	MappedFile? mapped_file;
+
+	public TurtleReader (string path) {
+		mapped_file = new MappedFile (path, false);
+		scanner = new SparqlScanner (mapped_file.get_contents (), mapped_file.get_length ());
+
+		base_uuid = new uchar[16];
+		uuid_generate (base_uuid);
+
+		tokens = new TokenInfo[BUFFER_SIZE];
+		prefix_map = new HashTable<string,string>.full (str_hash, str_equal, g_free, g_free);
+	}
+
+	string generate_bnodeid (string? user_bnodeid) {
+		// user_bnodeid is NULL for anonymous nodes
+		if (user_bnodeid == null) {
+			return ":%d".printf (++bnodeid);
+		} else {
+			var checksum = new Checksum (ChecksumType.SHA1);
+			// base UUID, unique per file
+			checksum.update (base_uuid, 16);
+			// node ID
+			checksum.update ((uchar[]) user_bnodeid, -1);
+
+			string sha1 = checksum.get_string ();
+
+			// generate name based uuid
+			return "urn:uuid:%.8s-%.4s-%.4s-%.4s-%.12s".printf (
+				sha1, sha1.offset (8), sha1.offset (12), sha1.offset (16), sha1.offset (20));
+		}
+	}
+
+	inline bool next_token () throws SparqlError {
+		index = (index + 1) % BUFFER_SIZE;
+		size--;
+		if (size <= 0) {
+			SourceLocation begin, end;
+			SparqlTokenType type = scanner.read_token (out begin, out end);
+			tokens[index].type = type;
+			tokens[index].begin = begin;
+			tokens[index].end = end;
+			size = 1;
+		}
+		return (tokens[index].type != SparqlTokenType.EOF);
+	}
+
+	inline SparqlTokenType current () {
+		return tokens[index].type;
+	}
+
+	inline bool accept (SparqlTokenType type) throws SparqlError {
+		if (current () == type) {
+			next_token ();
+			return true;
+		}
+		return false;
+	}
+
+	bool expect (SparqlTokenType type) throws SparqlError {
+		if (accept (type)) {
+			return true;
+		}
+
+		throw new SparqlError.PARSE ("expected %s", type.to_string ());
+	}
+
+	string get_last_string (int strip = 0) {
+		int last_index = (index + BUFFER_SIZE - 1) % BUFFER_SIZE;
+		return ((string) (tokens[last_index].begin.pos + strip)).ndup ((tokens[last_index].end.pos - tokens[last_index].begin.pos - 2 * strip));
+	}
+
+	public bool next () throws SparqlError {
+		while (true) {
+			switch (state) {
+			case State.INITIAL:
+				next_token ();
+				state = State.BOS;
+				continue;
+			case State.BOS:
+				// begin of statement
+				if (accept (SparqlTokenType.ATPREFIX)) {
+					string ns = "";
+					if (accept (SparqlTokenType.PN_PREFIX)) {
+					       ns = get_last_string ();
+					}
+					expect (SparqlTokenType.COLON);
+					expect (SparqlTokenType.IRI_REF);
+					string uri = get_last_string (1);
+					prefix_map.insert (ns, uri);
+					expect (SparqlTokenType.DOT);
+					continue;
+				} else if (accept (SparqlTokenType.ATBASE)) {
+					expect (SparqlTokenType.IRI_REF);
+					expect (SparqlTokenType.DOT);
+					continue;
+				} else if (current () == SparqlTokenType.EOF) {
+					return false;
+				}
+				// parse subject
+				if (accept (SparqlTokenType.IRI_REF)) {
+					subject = get_last_string (1);
+					state = State.SUBJECT;
+					continue;
+				} else if (accept (SparqlTokenType.PN_PREFIX)) {
+					// prefixed name with namespace foo:bar
+					string ns = get_last_string ();
+					expect (SparqlTokenType.COLON);
+					subject = prefix_map.lookup (ns) + get_last_string ().substring (1);
+					state = State.SUBJECT;
+					continue;
+				} else if (accept (SparqlTokenType.COLON)) {
+					// prefixed name without namespace :bar
+					subject = prefix_map.lookup ("") + get_last_string ().substring (1);
+					state = State.SUBJECT;
+					continue;
+				} else if (accept (SparqlTokenType.BLANK_NODE)) {
+					// _:foo
+					expect (SparqlTokenType.COLON);
+					subject = generate_bnodeid (get_last_string ().substring (1));
+					state = State.SUBJECT;
+					continue;
+				} else {
+					// TODO throw error
+					return false;
+				}
+			case State.SUBJECT:
+				// parse predicate
+				if (accept (SparqlTokenType.IRI_REF)) {
+					predicate = get_last_string (1);
+					state = State.PREDICATE;
+					continue;
+				} else if (accept (SparqlTokenType.PN_PREFIX)) {
+					string ns = get_last_string ();
+					expect (SparqlTokenType.COLON);
+					predicate = prefix_map.lookup (ns) + get_last_string ().substring (1);
+					state = State.PREDICATE;
+					continue;
+				} else if (accept (SparqlTokenType.COLON)) {
+					predicate = prefix_map.lookup ("") + get_last_string ().substring (1);
+					state = State.PREDICATE;
+					continue;
+				} else if (accept (SparqlTokenType.A)) {
+					predicate = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";;
+					state = State.PREDICATE;
+					continue;
+				} else {
+					// TODO throw error
+					return false;
+				}
+			case State.PREDICATE:
+				// parse object
+				if (accept (SparqlTokenType.IRI_REF)) {
+					object = get_last_string (1);
+					object_is_uri = true;
+					state = State.OBJECT;
+					return true;
+				} else if (accept (SparqlTokenType.PN_PREFIX)) {
+					// prefixed name with namespace foo:bar
+					string ns = get_last_string ();
+					expect (SparqlTokenType.COLON);
+					object = prefix_map.lookup (ns) + get_last_string ().substring (1);
+					object_is_uri = true;
+					state = State.OBJECT;
+					return true;
+				} else if (accept (SparqlTokenType.COLON)) {
+					// prefixed name without namespace :bar
+					object = prefix_map.lookup ("") + get_last_string ().substring (1);
+					object_is_uri = true;
+					state = State.OBJECT;
+					return true;
+				} else if (accept (SparqlTokenType.BLANK_NODE)) {
+					// _:foo
+					expect (SparqlTokenType.COLON);
+					object = generate_bnodeid (get_last_string ().substring (1));
+					object_is_uri = true;
+					state = State.OBJECT;
+					return true;
+				} else if (accept (SparqlTokenType.OPEN_BRACKET)) {
+					// begin of anonymous blank node
+					subject_stack += subject;
+					subject = generate_bnodeid (null);
+					state = State.SUBJECT;
+					continue;
+				} else if (accept (SparqlTokenType.CLOSE_BRACKET)) {
+					// end of anonymous blank node
+					subject = subject_stack[subject_stack.length - 1];
+					subject_stack.length--;
+					state = State.OBJECT;
+					continue;
+				} else if (accept (SparqlTokenType.STRING_LITERAL1) || accept (SparqlTokenType.STRING_LITERAL2)) {
+					var sb = new StringBuilder ();
+
+					string s = get_last_string (1);
+					string* p = s;
+					string* end = p + s.size ();
+					while ((long) p < (long) end) {
+						string* q = Posix.strchr (p, '\\');
+						if (q == null) {
+							sb.append_len (p, (long) (end - p));
+							p = end;
+						} else {
+							sb.append_len (p, (long) (q - p));
+							p = q + 1;
+							switch (((char*) p)[0]) {
+							case '\'':
+							case '"':
+							case '\\':
+								sb.append_c (((char*) p)[0]);
+								break;
+							case 'b':
+								sb.append_c ('\b');
+								break;
+							case 'f':
+								sb.append_c ('\f');
+								break;
+							case 'n':
+								sb.append_c ('\n');
+								break;
+							case 'r':
+								sb.append_c ('\r');
+								break;
+							case 't':
+								sb.append_c ('\t');
+								break;
+							}
+							p++;
+						}
+					}
+					object = sb.str;
+					object_is_uri = false;
+					state = State.OBJECT;
+
+					if (accept (SparqlTokenType.DOUBLE_CIRCUMFLEX)) {
+						if (!accept (SparqlTokenType.IRI_REF)) {
+							accept (SparqlTokenType.PN_PREFIX);
+							expect (SparqlTokenType.COLON);
+						}
+					}
+
+					return true;
+				} else if (accept (SparqlTokenType.STRING_LITERAL_LONG1) || accept (SparqlTokenType.STRING_LITERAL_LONG2)) {
+					object = get_last_string (3);
+					object_is_uri = false;
+					state = State.OBJECT;
+
+					if (accept (SparqlTokenType.DOUBLE_CIRCUMFLEX)) {
+						if (!accept (SparqlTokenType.IRI_REF)) {
+							accept (SparqlTokenType.PN_PREFIX);
+							expect (SparqlTokenType.COLON);
+						}
+					}
+
+					return true;
+				} else if (accept (SparqlTokenType.INTEGER) || accept (SparqlTokenType.DECIMAL) || accept (SparqlTokenType.DOUBLE) || accept (SparqlTokenType.TRUE) || accept (SparqlTokenType.FALSE)) {
+					object = get_last_string ();
+					object_is_uri = false;
+					state = State.OBJECT;
+					return true;
+				} else {
+					// TODO throw error
+					return false;
+				}
+			case State.OBJECT:
+				if (accept (SparqlTokenType.COMMA)) {
+					state = state.PREDICATE;
+					continue;
+				} else if (accept (SparqlTokenType.SEMICOLON)) {
+					if (accept (SparqlTokenType.DOT)) {
+						// semicolon before dot is allowed in both, SPARQL and Turtle
+						state = State.BOS;
+						continue;
+					}
+					state = state.SUBJECT;
+					continue;
+				} else if (accept (SparqlTokenType.DOT)) {
+					state = State.BOS;
+					continue;
+				} else {
+					// TODO throw error
+					return false;
+				}
+			}
+		}
+	}
+
+	public static void load (string path) {
+		try {
+			Data.begin_transaction ();
+
+			var reader = new TurtleReader (path);
+			while (reader.next ()) {
+				if (reader.object_is_uri) {
+					Data.insert_statement_with_uri (reader.subject, reader.predicate, reader.object);
+				} else {
+					Data.insert_statement_with_string (reader.subject, reader.predicate, reader.object);
+				}
+			}
+		} finally {
+			Data.commit_transaction ();
+		}
+	}
+
+	[CCode (cname = "uuid_generate")]
+	public extern static void uuid_generate ([CCode (array_length = false)] uchar[] uuid);
+}
+
diff --git a/src/plugins/evolution/Makefile.am b/src/plugins/evolution/Makefile.am
index 66cb3b6..255e138 100644
--- a/src/plugins/evolution/Makefile.am
+++ b/src/plugins/evolution/Makefile.am
@@ -50,7 +50,6 @@ libtracker_module_evolution_daemon_module_la_LIBADD = 			\
 	$(GTHREAD_LIBS)                                                 \
 	$(GIO_LIBS)                                                     \
 	$(GLIB2_LIBS)                                                   \
-	$(RAPTOR_LIBS)							\
 	$(GCOV_LIBS)
 
 liborg_freedesktop_Tracker_evolution_plugin_la_SOURCES = 		\
diff --git a/src/plugins/kmail/Makefile.am b/src/plugins/kmail/Makefile.am
index f9462b5..d5f79d6 100644
--- a/src/plugins/kmail/Makefile.am
+++ b/src/plugins/kmail/Makefile.am
@@ -33,7 +33,6 @@ libtracker_module_kmail_daemon_module_la_LIBADD = 			\
 	$(GTHREAD_LIBS)                                                 \
 	$(GIO_LIBS)                                                     \
 	$(GLIB2_LIBS)                                                   \
-	$(RAPTOR_LIBS)							\
 	$(GCOV_LIBS)
 
 dbus_sources = tracker-kmail-registrar-glue.h
diff --git a/src/tracker-miner-fs/tracker-indexer.c b/src/tracker-miner-fs/tracker-indexer.c
index 527c722..59ac05e 100644
--- a/src/tracker-miner-fs/tracker-indexer.c
+++ b/src/tracker-miner-fs/tracker-indexer.c
@@ -65,7 +65,6 @@
 #include <libtracker-db/tracker-db-dbus.h>
 
 #include <libtracker-data/tracker-data-manager.h>
-#include <libtracker-data/tracker-turtle.h>
 #include <libtracker-data/tracker-data-backup.h>
 
 #include <libtracker/tracker.h>
diff --git a/src/tracker-miner-fs/tracker-main.c b/src/tracker-miner-fs/tracker-main.c
index 8b48577..135b82c 100644
--- a/src/tracker-miner-fs/tracker-main.c
+++ b/src/tracker-miner-fs/tracker-main.c
@@ -45,8 +45,6 @@
 #include <libtracker-db/tracker-db-manager.h>
 #include <libtracker-db/tracker-db-dbus.h>
 
-#include <libtracker-data/tracker-turtle.h>
-
 #include "tracker-dbus.h"
 #include "tracker-config.h"
 #include "tracker-indexer.h"
@@ -438,7 +436,6 @@ main (gint argc, gchar *argv[])
                 tracker_indexer_process_modules (indexer, modules);
         }
 
-	tracker_turtle_init ();
 	tracker_volume_cleanup_init ();
 
 	g_message ("Starting...");
@@ -449,7 +446,6 @@ main (gint argc, gchar *argv[])
 	g_message ("Shutdown started");
 
 	tracker_volume_cleanup_shutdown ();
-	tracker_turtle_shutdown ();
 
         albumart_shutdown (config);
 	tracker_thumbnailer_shutdown ();
diff --git a/src/tracker-miner-fs/tracker-removable-device.c b/src/tracker-miner-fs/tracker-removable-device.c
index 19cb746..b58152c 100644
--- a/src/tracker-miner-fs/tracker-removable-device.c
+++ b/src/tracker-miner-fs/tracker-removable-device.c
@@ -22,7 +22,6 @@
 
 #include "config.h"
 
-
 #include <stdlib.h>
 #include <string.h>
 #include <sys/statvfs.h>
@@ -32,13 +31,9 @@
 #include <gio/gio.h>
 #include <gmodule.h>
 
-#include <raptor.h>
-
 #include <libtracker-data/tracker-data-query.h>
 #include <libtracker-data/tracker-data-update.h>
 
-#include <libtracker-data/tracker-turtle.h>
-
 #include "tracker-removable-device.h"
 
 typedef struct {
@@ -55,10 +50,12 @@ typedef enum {
 	MOVE
 } StorerTask;
 
+#if 0
 typedef struct {
 	raptor_serializer *serializer;
 	gchar *about_uri;
 } AddMetadataInfo;
+#endif
 
 static void
 commit_turtle_parse_info_storer (TurtleStorerInfo *info, 
@@ -165,6 +162,7 @@ void
 tracker_removable_device_optimize (TrackerIndexer *indexer,
 				   const gchar    *mount_point)
 {
+#if 0
 	gchar *file;
 
 	file = g_build_filename (mount_point,
@@ -178,12 +176,14 @@ tracker_removable_device_optimize (TrackerIndexer *indexer,
 	}
 
 	g_free (file);
+#endif
 }
 
 void
 tracker_removable_device_load (TrackerIndexer *indexer,
 			       const gchar    *mount_point)
 {
+#if 0
 	gchar *filename;
 
 	filename = g_build_filename (mount_point,
@@ -232,8 +232,10 @@ tracker_removable_device_load (TrackerIndexer *indexer,
 	}
 
 	g_free (filename);
+#endif
 }
 
+#if 0
 static void
 set_metadata (const gchar *key, 
 	      const gchar *value, 
@@ -277,6 +279,7 @@ set_metadata (const gchar *key,
 		raptor_free_uri ((raptor_uri *) statement.object);
 	}
 }
+#endif
 
 /* static void */
 /* foreach_in_metadata_set_metadata (const gchar     *subject, */
@@ -320,6 +323,7 @@ tracker_removable_device_add_metadata (TrackerIndexer        *indexer,
 				       const gchar           *uri,
 				       TrackerSparqlBuilder  *sparql)
 {
+#if 0
 	AddMetadataInfo  info;
 	gchar           *filename, *muri, *tmp;
 	const gchar     *p;
@@ -396,6 +400,7 @@ tracker_removable_device_add_metadata (TrackerIndexer        *indexer,
 	raptor_free_uri (suri);
 
 	fclose (target_file);
+#endif
 }
 
 /* TODO URI branch: path -> uri */
@@ -405,6 +410,7 @@ tracker_removable_device_add_removal (TrackerIndexer *indexer,
 				      const gchar *mount_point, 
 				      const gchar *uri)
 {
+#if 0
 	gchar               *filename, *about_uri, *muri, *tmp;
 	const gchar         *p;
 	FILE                *target_file;
@@ -475,6 +481,7 @@ tracker_removable_device_add_removal (TrackerIndexer *indexer,
 	raptor_free_serializer (serializer);
 
 	fclose (target_file);
+#endif
 }
 
 /* TODO URI branch: path -> uri */
@@ -485,6 +492,7 @@ tracker_removable_device_add_move (TrackerIndexer *indexer,
 				   const gchar *from_uri, 
 				   const gchar *to_uri)
 {
+#if 0
 	gchar               *filename, *about_uri, *to_urip, *muri, *tmp;
 	const gchar         *p;
 	FILE                *target_file;
@@ -561,5 +569,6 @@ tracker_removable_device_add_move (TrackerIndexer *indexer,
 	raptor_free_uri (suri);
 
 	fclose (target_file);
+#endif
 }
 
diff --git a/src/tracker-miner-fs/tracker-removable-device.h b/src/tracker-miner-fs/tracker-removable-device.h
index 521c22d..9683504 100644
--- a/src/tracker-miner-fs/tracker-removable-device.h
+++ b/src/tracker-miner-fs/tracker-removable-device.h
@@ -23,6 +23,8 @@
 #ifndef __TRACKER_REMOVABLE_DEVICE_H__
 #define __TRACKER_REMOVABLE_DEVICE_H__
 
+#include <libtracker-common/tracker-sparql-builder.h>
+
 #include "tracker-indexer.h"
 
 G_BEGIN_DECLS
diff --git a/src/tracker-store/tracker-main.c b/src/tracker-store/tracker-main.c
index 8519d92..5421b06 100644
--- a/src/tracker-store/tracker-main.c
+++ b/src/tracker-store/tracker-main.c
@@ -54,7 +54,6 @@
 #include <libtracker-db/tracker-db-dbus.h>
 
 #include <libtracker-data/tracker-data-manager.h>
-#include <libtracker-data/tracker-turtle.h>
 #include <libtracker-data/tracker-data-backup.h>
 #include <libtracker-data/tracker-data-query.h>
 
@@ -748,7 +747,6 @@ main (gint argc, gchar *argv[])
 #endif /* HAVE_HAL */
 
 	tracker_store_init ();
-	tracker_turtle_init ();
 
 	flags |= TRACKER_DB_MANAGER_REMOVE_CACHE;
 
@@ -821,7 +819,6 @@ shutdown:
 
 	tracker_dbus_shutdown ();
 	tracker_data_manager_shutdown ();
-	tracker_turtle_shutdown ();
 	tracker_log_shutdown ();
 
 #ifdef HAVE_HAL
diff --git a/src/tracker-store/tracker-store.c b/src/tracker-store/tracker-store.c
index 90e50a4..82ccdb5 100644
--- a/src/tracker-store/tracker-store.c
+++ b/src/tracker-store/tracker-store.c
@@ -27,7 +27,7 @@
 
 #include <libtracker-data/tracker-data-update.h>
 #include <libtracker-data/tracker-data-query.h>
-#include <libtracker-data/tracker-turtle.h>
+#include <libtracker-data/tracker-sparql-query.h>
 
 #include "tracker-store.h"
 
@@ -88,36 +88,31 @@ store_task_free (TrackerStoreTask *task)
 }
 
 static gboolean
-process_turtle_file_part (GError **error)
+process_turtle_file_part (TrackerTurtleReader *reader, GError **error)
 {
 	int i;
-	GError *new_error;
+	GError *new_error = NULL;
 
 	/* process 10 statements at once before returning to main loop */
 
 	i = 0;
-	new_error = tracker_turtle_get_error ();
 
-	while (!new_error && tracker_turtle_reader_next ()) {
+	while (new_error == NULL && tracker_turtle_reader_next (reader, &new_error)) {
 		/* insert statement */
-		if (tracker_turtle_reader_object_is_uri ()) {
+		if (tracker_turtle_reader_get_object_is_uri (reader)) {
 			tracker_data_insert_statement_with_uri (
-				tracker_turtle_reader_get_subject (),
-				tracker_turtle_reader_get_predicate (),
-				tracker_turtle_reader_get_object (),
+				tracker_turtle_reader_get_subject (reader),
+				tracker_turtle_reader_get_predicate (reader),
+				tracker_turtle_reader_get_object (reader),
 				&new_error);
 		} else {
 			tracker_data_insert_statement_with_string (
-				tracker_turtle_reader_get_subject (),
-				tracker_turtle_reader_get_predicate (),
-				tracker_turtle_reader_get_object (),
+				tracker_turtle_reader_get_subject (reader),
+				tracker_turtle_reader_get_predicate (reader),
+				tracker_turtle_reader_get_object (reader),
 				&new_error);
 		}
 
-		if (!new_error) {
-			new_error = tracker_turtle_get_error ();
-		}
-
 		i++;
 		if (!new_error && i >= 10) {
 			/* return to main loop */
@@ -125,10 +120,6 @@ process_turtle_file_part (GError **error)
 		}
 	}
 
-	if (!new_error) {
-		new_error = tracker_turtle_get_error ();
-	}
-
 	if (new_error) {
 		g_propagate_error (error, new_error);
 	}
@@ -196,15 +187,16 @@ queue_idle_handler (gpointer user_data)
 		}
 	} else if (task->type == TRACKER_STORE_TASK_TYPE_TURTLE) {
 		GError *error = NULL;
+		static TrackerTurtleReader *turtle_reader = NULL;
 
 		begin_batch (private);
 
 		if (!task->data.turtle.in_progress) {
-			tracker_turtle_reader_init (task->data.turtle.path, NULL);
+			turtle_reader = tracker_turtle_reader_new (task->data.turtle.path);
 			task->data.turtle.in_progress = TRUE;
 		}
 
-		if (process_turtle_file_part (&error)) {
+		if (process_turtle_file_part (turtle_reader, &error)) {
 			/* import still in progress */
 			private->batch_count++;
 			if (private->batch_count >= TRACKER_STORE_TRANSACTION_MAX) {
@@ -224,8 +216,9 @@ queue_idle_handler (gpointer user_data)
 				task->callback.turtle_callback (error, task->user_data);
 			}
 
+			g_object_unref (turtle_reader);
+			turtle_reader = NULL;
 			if (error) {
-				tracker_turtle_reader_cancel ();
 				g_clear_error (&error);
 			}
 		}
diff --git a/tests/libtracker-data/Makefile.am b/tests/libtracker-data/Makefile.am
index 7cfedfe..9c1c775 100644
--- a/tests/libtracker-data/Makefile.am
+++ b/tests/libtracker-data/Makefile.am
@@ -31,8 +31,7 @@ INCLUDES = 								\
 	$(SQLITE3_CFLAGS)						\
 	$(GMODULE_CFLAGS)						\
 	$(GTHREAD_CFLAGS)						\
-	$(GLIB2_CFLAGS)							\
-	$(RAPTOR_CFLAGS)
+	$(GLIB2_CFLAGS)
 
 common_ldadd =								\
 	$(top_builddir)/src/libtracker-common/libtracker-common.la 	\
@@ -42,8 +41,7 @@ common_ldadd =								\
 	$(GCOV_LIBS)							\
 	$(GMODULE_LIBS)							\
 	$(GTHREAD_LIBS)							\
-	$(GLIB2_LIBS)							\
-	$(RAPTOR_LIBS)
+	$(GLIB2_LIBS)
 
 tracker_sparql_SOURCES = 						\
 	tracker-sparql-test.c
diff --git a/tests/libtracker-data/tracker-ontology-test.c b/tests/libtracker-data/tracker-ontology-test.c
index f67d680..b4b339a 100644
--- a/tests/libtracker-data/tracker-ontology-test.c
+++ b/tests/libtracker-data/tracker-ontology-test.c
@@ -30,7 +30,7 @@
 #include <libtracker-data/tracker-data-manager.h>
 #include <libtracker-data/tracker-data-query.h>
 #include <libtracker-data/tracker-data-update.h>
-#include <libtracker-data/tracker-turtle.h>
+#include <libtracker-data/tracker-sparql-query.h>
 
 typedef struct _TestInfo TestInfo;
 
@@ -74,15 +74,6 @@ const TestInfo nmo_tests[] = {
 };
 
 static void
-consume_triple_storer (const gchar *subject,
-                       const gchar *predicate,
-                       const gchar *object,
-                       void        *user_data)
-{
-	tracker_data_insert_statement (subject, predicate, object, NULL);
-}
-
-static void
 test_query (gconstpointer test_data)
 {
 	const TestInfo *test_info;
@@ -115,9 +106,7 @@ test_query (gconstpointer test_data)
 
 		/* load data set */
 		data_filename = g_strconcat (data_prefix, ".ttl", NULL);
-		tracker_data_begin_transaction ();
-		tracker_turtle_process (data_filename, NULL, consume_triple_storer, NULL);
-		tracker_data_commit_transaction ();
+		tracker_turtle_reader_load (data_filename);
 
 		query_filename = g_strconcat (test_prefix, ".rq", NULL);
 		g_file_get_contents (query_filename, &query, NULL, &error);
@@ -246,8 +235,6 @@ main (int argc, char **argv)
 	g_setenv ("TRACKER_DB_SQL_DIR", TOP_SRCDIR "/data/db/", TRUE);
 	g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
 
-	tracker_turtle_init ();
-
 	/* add test cases */
 
 	for (i = 0; nie_tests[i].test_name; i++) {
@@ -274,8 +261,6 @@ main (int argc, char **argv)
 
 	result = g_test_run ();
 
-	tracker_turtle_shutdown ();
-
 	/* clean up */
 	g_print ("Removing temporary data\n");
 	g_spawn_command_line_async ("rm -R tracker/", NULL);
diff --git a/tests/libtracker-data/tracker-sparql-test.c b/tests/libtracker-data/tracker-sparql-test.c
index f88922a..695fd52 100644
--- a/tests/libtracker-data/tracker-sparql-test.c
+++ b/tests/libtracker-data/tracker-sparql-test.c
@@ -30,7 +30,7 @@
 #include <libtracker-data/tracker-data-manager.h>
 #include <libtracker-data/tracker-data-query.h>
 #include <libtracker-data/tracker-data-update.h>
-#include <libtracker-data/tracker-turtle.h>
+#include <libtracker-data/tracker-sparql-query.h>
 #include <libtracker-common/tracker-ontology.h>
 
 typedef struct _TestInfo TestInfo;
@@ -78,15 +78,6 @@ const TestInfo tests[] = {
 	{ NULL }
 };
 
-static void
-consume_triple_storer (const gchar *subject,
-                       const gchar *predicate,
-                       const gchar *object,
-                       void        *user_data)
-{
-	tracker_data_insert_statement (subject, predicate, object, NULL);
-}
-
 static int
 strstr_i (const char *a, const char *b)
 {
@@ -129,9 +120,7 @@ test_sparql_query (gconstpointer test_data)
 
 		/* load data set */
 		data_filename = g_strconcat (data_prefix, ".ttl", NULL);
-		tracker_data_begin_transaction ();
-		tracker_turtle_process (data_filename, NULL, consume_triple_storer, NULL);
-		tracker_data_commit_transaction ();
+		tracker_turtle_reader_load (data_filename);
 
 		query_filename = g_strconcat (test_prefix, ".rq", NULL);
 		g_file_get_contents (query_filename, &query, NULL, &error);
@@ -271,8 +260,6 @@ main (int argc, char **argv)
 
 	g_free (current_dir);
 
-	tracker_turtle_init ();
-
 	/* add test cases */
 	for (i = 0; tests[i].test_name; i++) {
 		gchar *testpath;
@@ -285,8 +272,6 @@ main (int argc, char **argv)
 	/* run tests */
 	result = g_test_run ();
 
-	tracker_turtle_shutdown ();
-
 	/* clean up */
 	g_print ("Removing temporary data\n");
 	g_spawn_command_line_async ("rm -R tracker/", NULL);
diff --git a/utils/services/Makefile.am b/utils/services/Makefile.am
index d88ec7d..14b5dcb 100644
--- a/utils/services/Makefile.am
+++ b/utils/services/Makefile.am
@@ -4,16 +4,17 @@ noinst_PROGRAMS = ontology-validator ttl2html data-validator
 
 INCLUDES = 				\
 	-DG_LOG_DOMAIN=\"Tracker\"	\
+	-I$(top_srcdir)/src		\
 	$(WARN_CFLAGS)			\
 	$(GLIB2_CFLAGS)			\
-	$(RAPTOR_CFLAGS)		\
 	$(GIO_CFLAGS)
 
 ontology_validator_SOURCES =		\
 	ontology-validator.c
 
 ontology_validator_LDADD =		\
-	$(RAPTOR_LIBS) 			\
+	$(top_builddir)/src/libtracker-data/libtracker-data.la		\
+	$(top_builddir)/src/libtracker-common/libtracker-common.la	\
 	$(GLIB2_LIBS)			\
 	$(GIO_LIBS)							
 
@@ -21,7 +22,8 @@ data_validator_SOURCES = 		\
 	data-validator.c
 
 data_validator_LDADD =			\
-	$(RAPTOR_LIBS) 			\
+	$(top_builddir)/src/libtracker-data/libtracker-data.la		\
+	$(top_builddir)/src/libtracker-common/libtracker-common.la	\
 	$(GLIB2_LIBS)			\
 	$(GIO_LIBS)							
 
@@ -35,6 +37,7 @@ ttl2html_SOURCES =			\
 	ttl_html.c
 
 ttl2html_LDADD = 			\
-	$(RAPTOR_LIBS) 			\
+	$(top_builddir)/src/libtracker-data/libtracker-data.la		\
+	$(top_builddir)/src/libtracker-common/libtracker-common.la	\
 	$(GLIB2_LIBS)			\
 	$(GIO_LIBS)	
diff --git a/utils/services/data-validator.c b/utils/services/data-validator.c
index 0423e12..909f129 100644
--- a/utils/services/data-validator.c
+++ b/utils/services/data-validator.c
@@ -4,6 +4,8 @@
 #include <gio/gio.h>
 #include <string.h>
 
+#include <libtracker-data/tracker-sparql-query.h>
+
 static gchar	     *ontology_dir = NULL;
 static gchar         *ttl_file = NULL;
 
@@ -19,25 +21,12 @@ static GOptionEntry   entries[] = {
 	{ NULL }
 };
 
-typedef void (* TurtleTripleCallback) (void *user_data, const raptor_statement *triple);
-
 #define CLASS "http://www.w3.org/2000/01/rdf-schema#Class";
 #define PROPERTY "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property";
 #define IS "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
 
 static gboolean error_flag = FALSE;
 
-static void 
-raptor_error (void *user_data, raptor_locator* locator, const char *message)
-{
-	g_message ("RAPTOR parse error: %s:%d:%d: %s\n", 
-		   (gchar *) user_data,
-		   locator->line,
-		   locator->column,
-		   message);
-        error_flag = TRUE;
-}
-
 static GList *unknown_items = NULL;
 static GList *known_items = NULL;
 
@@ -57,44 +46,28 @@ exists_or_already_reported (const gchar *item)
 }
 
 static void
-turtle_load_ontology (void                   *user_data,
-                      const raptor_statement *triple) 
+turtle_load_ontology (const gchar *turtle_subject,
+                      const gchar *turtle_predicate,
+                      const gchar *turtle_object)
 {
 
-        gchar *turtle_subject;
-        gchar *turtle_predicate;
-        char  *turtle_object;
-
-	/* set new statement */
-	turtle_subject = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->subject));
-	turtle_predicate = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->predicate));
-	turtle_object = g_strdup ((const gchar *) triple->object);
-
         if (!g_strcmp0 (turtle_predicate, IS)) {
-                known_items = g_list_prepend (known_items, turtle_subject);
+                known_items = g_list_prepend (known_items, g_strdup (turtle_subject));
         }
 
 }
 
 static void
-turtle_statement_handler (void                   *user_data,
-                          const raptor_statement *triple) 
+turtle_statement_handler (const gchar *turtle_subject,
+                          const gchar *turtle_predicate,
+                          const gchar *turtle_object)
 {
 
-        gchar *turtle_subject;
-        gchar *turtle_predicate;
-        char  *turtle_object;
-
-	/* set new statement */
-	turtle_subject = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->subject));
-	turtle_predicate = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->predicate));
-	turtle_object = g_strdup ((const gchar *) triple->object);
-
         /* Check that predicate exists in the ontology 
         */
         if (!exists_or_already_reported (turtle_predicate)){                        
                 g_print ("Unknown property %s\n", turtle_predicate);
-                unknown_items = g_list_prepend (unknown_items, turtle_predicate);
+                unknown_items = g_list_prepend (unknown_items, g_strdup (turtle_predicate));
                 error_flag = TRUE;
         }
 
@@ -105,42 +78,12 @@ turtle_statement_handler (void                   *user_data,
                 if (!exists_or_already_reported (turtle_object)){                        
                         g_print ("Unknown class %s\n", turtle_object);
                         error_flag = TRUE;
-                        unknown_items = g_list_prepend (unknown_items, turtle_object);
+                        unknown_items = g_list_prepend (unknown_items, g_strdup (turtle_object));
                 }
         }
 }
 
 
-static void 
-process_file (const gchar *ttl_file, TurtleTripleCallback handler, void *user_data)
-{
-        FILE *file;
-        raptor_parser *parser;
-        raptor_uri *uri, *buri, *base_uri;
-	unsigned char  *uri_string;
-
-        file = g_fopen (ttl_file, "r");
-
-	parser = raptor_new_parser ("turtle");
-	base_uri = raptor_new_uri ((unsigned char *) "/");
-
-	raptor_set_statement_handler (parser, user_data, 
-                                      handler);
-	raptor_set_fatal_error_handler (parser, (void *)ttl_file, raptor_error);
-	raptor_set_error_handler (parser, (void *)ttl_file, raptor_error);
-	raptor_set_warning_handler (parser, (void *)ttl_file, raptor_error);
-
-	uri_string = raptor_uri_filename_to_uri_string (ttl_file);
-	uri = raptor_new_uri (uri_string);
-	buri = raptor_new_uri ((unsigned char *) base_uri);
-
-	raptor_parse_file (parser, uri, buri);
-
-	raptor_free_uri (uri);
-	raptor_free_parser (parser);
-	fclose (file);
-}
-
 static void
 load_ontology_files (const gchar *services_dir)
 {
@@ -159,6 +102,8 @@ load_ontology_files (const gchar *services_dir)
         conf_file = g_dir_read_name (services);
                         
         while (conf_file) {
+		TrackerTurtleReader *reader;
+		GError *error = NULL;
 
                 if (!g_str_has_suffix (conf_file, "ontology")) {
                         conf_file = g_dir_read_name (services);
@@ -167,7 +112,22 @@ load_ontology_files (const gchar *services_dir)
                 
                 fullpath = g_build_filename (dir_uri, conf_file, NULL);
 
-                process_file (fullpath, turtle_load_ontology, NULL);
+		reader = tracker_turtle_reader_new (fullpath);
+
+		while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
+			turtle_load_ontology (tracker_turtle_reader_get_subject (reader),
+				              tracker_turtle_reader_get_predicate (reader),
+				              tracker_turtle_reader_get_object (reader));
+		}
+
+		g_object_unref (reader);
+
+		if (error) {
+			g_message ("Turtle parse error: %s", error->message);
+			g_error_free (error);
+		}
+
+
                 g_free (fullpath);
                 counter += 1;
                 conf_file = g_dir_read_name (services);
@@ -189,7 +149,6 @@ main (gint argc, gchar **argv)
         GOptionContext *context;
 
         g_type_init ();
-        raptor_init ();
 
 
 	/* Translators: this messagge will apper immediately after the	*/
@@ -217,13 +176,28 @@ main (gint argc, gchar **argv)
 
         //"/home/ivan/devel/codethink/tracker-ssh/data/services"
         load_ontology_files (ontology_dir);
-        process_file (ttl_file, *turtle_statement_handler, NULL);
+
+	TrackerTurtleReader *reader;
+	GError *error = NULL;
+
+	reader = tracker_turtle_reader_new (ttl_file);
+
+	while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
+		turtle_statement_handler (tracker_turtle_reader_get_subject (reader),
+		                          tracker_turtle_reader_get_predicate (reader),
+		                          tracker_turtle_reader_get_object (reader));
+	}
+
+	g_object_unref (reader);
+
+	if (error) {
+		g_message ("Turtle parse error: %s", error->message);
+		g_error_free (error);
+	}
 
         if (!error_flag) {
                 g_debug ("%s seems OK.", ttl_file);
         }
 
-        raptor_finish ();
-
         return 0;
 }
diff --git a/utils/services/ontology-validator.c b/utils/services/ontology-validator.c
index 8e0de28..35f596d 100644
--- a/utils/services/ontology-validator.c
+++ b/utils/services/ontology-validator.c
@@ -1,9 +1,10 @@
-#include <raptor.h>
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <gio/gio.h>
 #include <string.h>
 
+#include <libtracker-data/tracker-sparql-query.h>
+
 static gchar	     *ontology_dir = NULL;
 
 static GOptionEntry   entries[] = {
@@ -14,8 +15,6 @@ static GOptionEntry   entries[] = {
 	{ NULL }
 };
 
-typedef void (* TurtleTripleCallback) (void *user_data, const raptor_statement *triple);
-
 #define RDFS_CLASS "http://www.w3.org/2000/01/rdf-schema#Class";
 #define RDF_PROPERTY "http://www.w3.org/1999/02/22-rdf-syntax-ns#Property";
 #define RDFS_SUBCLASSOF  "http://www.w3.org/2000/01/rdf-schema#subClassOf";
@@ -24,16 +23,6 @@ typedef void (* TurtleTripleCallback) (void *user_data, const raptor_statement *
 #define RDFS_RANGE "http://www.w3.org/2000/01/rdf-schema#range";
 #define RDFS_DOMAIN "http://www.w3.org/2000/01/rdf-schema#domain";
 
-static void 
-raptor_error (void *user_data, raptor_locator* locator, const char *message)
-{
-	g_message ("RAPTOR parse error: %s:%d:%d: %s\n", 
-		   (gchar *) user_data,
-		   locator->line,
-		   locator->column,
-		   message);
-}
-
 static GList *unknown_items = NULL;
 static GList *known_items = NULL;
 
@@ -53,19 +42,11 @@ exists_or_already_reported (const gchar *item)
 }
 
 static void
-turtle_load_ontology (void                   *user_data,
-                      const raptor_statement *triple) 
+turtle_load_ontology (const gchar *turtle_subject,
+                      const gchar *turtle_predicate,
+                      const gchar *turtle_object)
 {
 
-        gchar *turtle_subject;
-        gchar *turtle_predicate;
-        char  *turtle_object;
-
-	/* set new statement */
-	turtle_subject = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->subject));
-	turtle_predicate = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->predicate));
-	turtle_object = g_strdup ((const gchar *) triple->object);
-
         /* nmo:Email a rdfs:Class 
          *  If rdfs:Class exists, add nmo:Email to the known_items 
          **/
@@ -91,7 +72,7 @@ turtle_load_ontology (void                   *user_data,
                         g_error ("%s is a %s but %s is not defined",
                                  turtle_subject, turtle_object, turtle_object);
                 } else {
-                        known_items = g_list_prepend (known_items, turtle_subject);
+                        known_items = g_list_prepend (known_items, g_strdup (turtle_subject));
                 }
         }
 
@@ -104,7 +85,7 @@ turtle_load_ontology (void                   *user_data,
             || !g_strcmp0 (turtle_predicate, RDFS_DOMAIN)) {
                 /* Check the class is already defined */
                 if (!exists_or_already_reported (turtle_object)) {
-                        g_error ("Class %s refears to %s but it is not defined",
+                        g_error ("Class %s refers to %s but it is not defined",
                                  turtle_subject, turtle_object);
                 }
         }
@@ -113,34 +94,27 @@ turtle_load_ontology (void                   *user_data,
 }
 
 static void 
-process_file (const gchar *ttl_file, TurtleTripleCallback handler)
+process_file (const gchar *ttl_file)
 {
-        FILE *file;
-        raptor_parser *parser;
-        raptor_uri *uri, *buri, *base_uri;
-	unsigned char  *uri_string;
+	TrackerTurtleReader *reader;
+	GError *error = NULL;
 
-        g_print ("Processing %s\n", ttl_file);
-        file = g_fopen (ttl_file, "r");
+	g_print ("Processing %s\n", ttl_file);
 
-	parser = raptor_new_parser ("turtle");
-	base_uri = raptor_new_uri ((unsigned char *) "/");
+	reader = tracker_turtle_reader_new (ttl_file);
 
-	raptor_set_statement_handler (parser, NULL, 
-                                      handler);
-	raptor_set_fatal_error_handler (parser, (void *)file, raptor_error);
-	raptor_set_error_handler (parser, (void *)file, raptor_error);
-	raptor_set_warning_handler (parser, (void *)file, raptor_error);
+	while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
+		turtle_load_ontology (tracker_turtle_reader_get_subject (reader),
+		                      tracker_turtle_reader_get_predicate (reader),
+		                      tracker_turtle_reader_get_object (reader));
+	}
 
-	uri_string = raptor_uri_filename_to_uri_string (ttl_file);
-	uri = raptor_new_uri (uri_string);
-	buri = raptor_new_uri ((unsigned char *) base_uri);
+	g_object_unref (reader);
 
-	raptor_parse_file (parser, uri, buri);
-
-	raptor_free_uri (uri);
-	raptor_free_parser (parser);
-	fclose (file);
+	if (error) {
+		g_message ("Turtle parse error: %s", error->message);
+		g_error_free (error);
+	}
 }
 
 static void
@@ -195,7 +169,6 @@ main (gint argc, gchar **argv)
         GOptionContext *context;
 
         g_type_init ();
-        raptor_init ();
 
 
 	/* Translators: this messagge will apper immediately after the	*/
@@ -225,7 +198,5 @@ main (gint argc, gchar **argv)
         //"/home/ivan/devel/codethink/tracker-ssh/data/services"
         load_ontology_files (ontology_dir);
 
-        raptor_finish ();
-
         return 0;
 }
diff --git a/utils/services/ttl2html.c b/utils/services/ttl2html.c
index c3bbe05..4f1c342 100644
--- a/utils/services/ttl2html.c
+++ b/utils/services/ttl2html.c
@@ -63,7 +63,6 @@ main (gint argc, gchar **argv)
         }
         g_assert (f != NULL);
 
-        ttl_loader_init ();
         description = ttl_loader_load_description (desc_file);
 
         dirname = g_path_get_dirname (desc_file);
@@ -80,7 +79,6 @@ main (gint argc, gchar **argv)
         ttl_loader_free_ontology (ontology);
         ttl_loader_free_description (description);
 
-        ttl_loader_shutdown ();
         g_option_context_free (context);
         
         fclose (f);
diff --git a/utils/services/ttl_loader.c b/utils/services/ttl_loader.c
index 98a4653..83dd007 100644
--- a/utils/services/ttl_loader.c
+++ b/utils/services/ttl_loader.c
@@ -1,8 +1,7 @@
 #include "ttl_loader.h"
-#include <raptor.h>
 #include <glib/gstdio.h>
 
-typedef void (* TurtleTripleCallback) (void *user_data, const raptor_statement *triple);
+#include <libtracker-data/tracker-sparql-query.h>
 
 /* Ontology classes */
 #define RDFS_CLASS "http://www.w3.org/2000/01/rdf-schema#Class";
@@ -30,56 +29,14 @@ typedef void (* TurtleTripleCallback) (void *user_data, const raptor_statement *
 #define DSC_BASEURI DSC_PREFIX "baseUrl"
 #define DSC_RELPATH DSC_PREFIX "relativePath"
 
-static gboolean initialized = FALSE;
-
-void
-ttl_loader_init (void)
-{
-        if (!initialized) {
-                raptor_init ();
-                initialized = TRUE;
-        }
-}
-
-void
-ttl_loader_shutdown (void)
-{
-        if (initialized) {
-                raptor_finish ();
-                initialized = FALSE;
-        }
-}
-
-
-static void 
-raptor_error (void *user_data, raptor_locator* locator, const char *message)
-{
-	g_message ("RAPTOR parse error: %s:%d:%d: %s\n", 
-		   (gchar *) user_data,
-		   locator->line,
-		   locator->column,
-		   message);
-}
-
-
-
 static void
-load_in_memory (void                   *user_data,
-                const raptor_statement *triple) 
+load_in_memory (Ontology    *ontology,
+                const gchar *turtle_subject,
+                const gchar *turtle_predicate,
+                const gchar *turtle_object)
 {
-
-        gchar *turtle_subject;
-        gchar *turtle_predicate;
-        char  *turtle_object;
-        Ontology *ontology = (Ontology *)user_data;
-
         g_return_if_fail (ontology != NULL);
 
-	/* set new statement */
-	turtle_subject = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->subject));
-	turtle_predicate = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->predicate));
-	turtle_object = g_strdup ((const gchar *) triple->object);
-
         if (!g_strcmp0 (turtle_predicate, RDFS_TYPE)) {
                 /* It is a definition of class or property */
                 if (!g_strcmp0 (turtle_object, RDFS_CLASS)) {
@@ -222,26 +179,14 @@ load_in_memory (void                   *user_data,
                 */
         }
 
-        g_free (turtle_subject);
-        g_free (turtle_predicate);
-        g_free (turtle_object);
-
 }
 
 static void
-load_description (void                   *user_data,
-                  const raptor_statement *triple) 
+load_description (OntologyDescription *desc,
+                  const gchar         *turtle_subject,
+                  const gchar         *turtle_predicate,
+                  const gchar         *turtle_object)
 {
-        gchar *turtle_subject;
-        gchar *turtle_predicate;
-        char  *turtle_object;
-        OntologyDescription *desc = (OntologyDescription *)user_data;
-
-	/* set new statement */
-	turtle_subject = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->subject));
-	turtle_predicate = g_strdup ((const gchar *) raptor_uri_as_string ((raptor_uri *) triple->predicate));
-	turtle_object = g_strdup ((const gchar *) triple->object);
-
         if (!g_strcmp0 (turtle_predicate, RDFS_TYPE)) {
                 g_assert (!g_strcmp0 (turtle_object, DSC_ONTOLOGY));
         } else if (!g_strcmp0 (turtle_predicate, DSC_TITLE)) {
@@ -260,47 +205,6 @@ load_description (void                   *user_data,
         } else {
                 g_critical ("Unhandled element %s", turtle_predicate);
         }
-
-        g_free (turtle_subject);
-        g_free (turtle_predicate);
-        g_free (turtle_object);
-}
-
-
-static void 
-process_file (const gchar *ttl_file, 
-              TurtleTripleCallback handler,
-              gpointer user_data)
-{
-        FILE *file;
-        raptor_parser *parser;
-        raptor_uri *uri, *buri, *base_uri;
-	unsigned char  *uri_string;
-
-        file = g_fopen (ttl_file, "r");
-
-	parser = raptor_new_parser ("turtle");
-	base_uri = raptor_new_uri ((unsigned char *) "/");
-
-	raptor_set_statement_handler (parser, user_data, 
-                                      handler);
-	raptor_set_fatal_error_handler (parser, (void *)file, raptor_error);
-	raptor_set_error_handler (parser, (void *)file, raptor_error);
-	raptor_set_warning_handler (parser, (void *)file, raptor_error);
-
-	uri_string = raptor_uri_filename_to_uri_string (ttl_file);
-	uri = raptor_new_uri (uri_string);
-	buri = raptor_new_uri ((unsigned char *) base_uri);
-
-	raptor_parse_file (parser, uri, buri);
-
-        raptor_free_uri (base_uri);
-        raptor_free_uri (buri);
-	raptor_free_uri (uri);
-	raptor_free_parser (parser);
-        g_free (uri_string);
-
-	fclose (file);
 }
 
 
@@ -321,7 +225,24 @@ ttl_loader_load_ontology (const gchar *ttl_file)
                                                       (GDestroyNotify)ttl_model_property_free);
 
         if (ttl_file) {
-                process_file (ttl_file, load_in_memory, ontology);
+		TrackerTurtleReader *reader;
+		GError *error;
+
+		reader = tracker_turtle_reader_new (ttl_file);
+
+		while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
+			load_in_memory (ontology,
+			                tracker_turtle_reader_get_subject (reader),
+				        tracker_turtle_reader_get_predicate (reader),
+				        tracker_turtle_reader_get_object (reader));
+		}
+
+		g_object_unref (reader);
+
+		if (error) {
+			g_message ("Turtle parse error: %s", error->message);
+			g_error_free (error);
+		}
         } else {
                 g_warning ("Unable to open '%s'", ttl_file);
         }
@@ -336,7 +257,24 @@ ttl_loader_load_description (const gchar *filename)
         
         desc = ttl_model_description_new ();
 
-        process_file (filename, load_description, desc);
+	TrackerTurtleReader *reader;
+	GError *error;
+
+	reader = tracker_turtle_reader_new (filename);
+
+	while (error == NULL && tracker_turtle_reader_next (reader, &error)) {
+		load_description (desc,
+		                  tracker_turtle_reader_get_subject (reader),
+			          tracker_turtle_reader_get_predicate (reader),
+			          tracker_turtle_reader_get_object (reader));
+	}
+
+	g_object_unref (reader);
+
+	if (error) {
+		g_message ("Turtle parse error: %s", error->message);
+		g_error_free (error);
+	}
 
         return desc;
 }



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