summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-05-01 04:01:04 +0000
committerDavid Robillard <d@drobilla.net>2007-05-01 04:01:04 +0000
commitc5eb1148b99cf88555c0a516e1218bdb74ab0080 (patch)
tree4f72d46c43ff7fe39ebce3bd39911c3b09952868 /src
parenta5ce2e3af283f3ffeb2d30a34546ceab0a84d702 (diff)
downloadraul-c5eb1148b99cf88555c0a516e1218bdb74ab0080.tar.gz
raul-c5eb1148b99cf88555c0a516e1218bdb74ab0080.tar.bz2
raul-c5eb1148b99cf88555c0a516e1218bdb74ab0080.zip
Converted Raul (and thus Ingen and Machina) to use Redland over Raptor/Rasqal independently.
Fixed patch loading for Ingen (local only, still something wrong with remote...). git-svn-id: http://svn.drobilla.net/lad/raul@486 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am21
-rw-r--r--src/Namespaces.cpp4
-rw-r--r--src/RDFModel.cpp215
-rw-r--r--src/RDFNode.cpp140
-rw-r--r--src/RDFQuery.cpp60
-rw-r--r--src/RDFWorld.cpp95
-rw-r--r--src/RDFWriter.cpp257
7 files changed, 493 insertions, 299 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index ce58a21..ce7bb2e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,22 +1,19 @@
-libraul_la_CXXFLAGS = -I$(top_srcdir) @RAPTOR_CFLAGS@ @RASQAL_CFLAGS@ @GLIBMM_CFLAGS@ @JACK_CFLAGS@ @LASH_CFLAGS@
-libraul_la_LIBADD = @RAPTOR_LIBS@ @RASQAL_LIBS@ @GLIBMM_LIBS@ @JACK_LIBS@ @LASH_LIBS@
+libraul_la_CXXFLAGS = -I$(top_srcdir) @REDLAND_CFLAGS@ @GLIBMM_CFLAGS@ @JACK_CFLAGS@ @LASH_CFLAGS@
+libraul_la_LIBADD = @REDLAND_LIBS@ @GLIBMM_LIBS@ @JACK_LIBS@ @LASH_LIBS@
lib_LTLIBRARIES = libraul.la
libraul_la_SOURCES = \
- Thread.cpp \
- Path.cpp \
- Namespaces.cpp \
Maid.cpp \
+ Namespaces.cpp \
+ Path.cpp \
+ RDFWorld.cpp \
SMFReader.cpp \
- SMFWriter.cpp
-
-if WITH_RAPTOR
-libraul_la_SOURCES += RDFWriter.cpp
-endif
+ SMFWriter.cpp \
+ Thread.cpp
-if WITH_RASQAL
-libraul_la_SOURCES += RDFQuery.cpp
+if WITH_REDLAND
+libraul_la_SOURCES += RDFModel.cpp RDFNode.cpp RDFQuery.cpp
endif
if WITH_JACK
diff --git a/src/Namespaces.cpp b/src/Namespaces.cpp
index 2f3dd99..5bdb3b9 100644
--- a/src/Namespaces.cpp
+++ b/src/Namespaces.cpp
@@ -26,9 +26,9 @@ namespace Raul {
* Namespaces, @a uri will be returned unmodified.
*/
std::string
-Namespaces::qualify(std::string uri)
+Namespaces::qualify(std::string uri) const
{
- for (iterator i = begin(); i != end(); ++i) {
+ for (const_iterator i = begin(); i != end(); ++i) {
size_t ns_len = i->second.length();
if (uri.substr(0, ns_len) == i->second)
diff --git a/src/RDFModel.cpp b/src/RDFModel.cpp
new file mode 100644
index 0000000..d05864f
--- /dev/null
+++ b/src/RDFModel.cpp
@@ -0,0 +1,215 @@
+/* This file is part of Raul.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * Raul 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.
+ *
+ * Raul 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 details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <sstream>
+#include "raul/RDFWorld.h"
+#include "raul/RDFModel.h"
+#include "raul/RDFNode.h"
+#include "raul/AtomRedland.h"
+
+#define U(x) ((const unsigned char*)(x))
+
+using namespace std;
+
+namespace Raul {
+namespace RDF {
+
+
+//static const char* const RDF_LANG = "rdfxml-abbrev";
+static const char* const RDF_LANG = "turtle";
+
+
+/** Create an empty in-memory RDF model.
+ */
+Model::Model(RDF::World& world)
+ : _world(world)
+ , _serializer(NULL)
+{
+ _storage = librdf_new_storage(_world.world(), "hashes", NULL, "hash-type='memory'");
+ _model = librdf_new_model(_world.world(), _storage, NULL);
+}
+
+
+/** Load a model from a URI (local file or remote).
+ */
+Model::Model(World& world, const Glib::ustring& data_uri)
+ : _world(world)
+ , _serializer(NULL)
+{
+ _storage = librdf_new_storage(_world.world(), "hashes", NULL, "hash-type='memory'");
+ _model = librdf_new_model(_world.world(), _storage, NULL);
+
+ librdf_uri* uri = librdf_new_uri(world.world(), (const unsigned char*)data_uri.c_str());
+ librdf_parser* parser = librdf_new_parser(world.world(), "guess", NULL, NULL);
+ librdf_parser_parse_into_model(parser, uri, NULL, _model);
+ librdf_free_uri(uri);
+ librdf_free_parser(parser);
+
+ cout << endl << "Loaded model from " << data_uri << ":" << endl;
+ serialize_to_file_handle(stdout);
+ cout << endl << endl;
+}
+
+
+Model::~Model()
+{
+ if (_serializer)
+ librdf_free_serializer(_serializer);
+
+ librdf_free_model(_model);
+ librdf_free_storage(_storage);
+}
+
+
+void
+Model::setup_prefixes()
+{
+ assert(_serializer);
+
+ for (Namespaces::const_iterator i = _world.prefixes().begin(); i != _world.prefixes().end(); ++i) {
+ librdf_serializer_set_namespace(_serializer,
+ librdf_new_uri(_world.world(), U(i->second.c_str())), i->first.c_str());
+ }
+}
+
+
+/** Begin a serialization to a C file handle.
+ *
+ * This must be called before any write methods.
+ */
+void
+Model::serialize_to_file_handle(FILE* fd)
+{
+ _serializer = librdf_new_serializer(_world.world(), RDF_LANG, NULL, NULL);
+ setup_prefixes();
+ librdf_serializer_serialize_model_to_file_handle(_serializer, fd, NULL, _model);
+ librdf_free_serializer(_serializer);
+ _serializer = NULL;
+}
+
+
+/** Begin a serialization to a file.
+ *
+ * This must be called before any write methods.
+ */
+void
+Model::serialize_to_file(const string& filename)
+{
+ _serializer = librdf_new_serializer(_world.world(), RDF_LANG, NULL, NULL);
+ setup_prefixes();
+ librdf_serializer_serialize_model_to_file(_serializer, filename.c_str(), NULL, _model);
+ librdf_free_serializer(_serializer);
+ _serializer = NULL;
+}
+
+
+/** Begin a serialization to a string.
+ *
+ * This must be called before any write methods.
+ *
+ * The results of the serialization will be returned by the finish() method after
+ * the desired objects have been serialized.
+ */
+string
+Model::serialize_to_string()
+{
+ _serializer = librdf_new_serializer(_world.world(), RDF_LANG, NULL, NULL);
+ setup_prefixes();
+
+ unsigned char* c_str
+ = librdf_serializer_serialize_model_to_string(_serializer, NULL, _model);
+
+ string result((const char*)c_str);
+ free(c_str);
+
+ librdf_free_serializer(_serializer);
+ _serializer = NULL;
+
+ return result;
+}
+
+
+void
+Model::add_statement(const Node& subject,
+ const Node& predicate,
+ const Node& object)
+{
+ librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(),
+ subject.get_node(), predicate.get_node(), object.get_node());
+
+ librdf_model_add_statement(_model, triple);
+}
+
+
+void
+Model::add_statement(const Node& subject,
+ const string& predicate_id,
+ const Node& object)
+{
+ const string predicate_uri = _world.expand_uri(predicate_id);
+ librdf_node* predicate = librdf_new_node_from_uri_string(_world.world(),
+ (const unsigned char*)predicate_uri.c_str());
+
+ librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(),
+ subject.get_node(), predicate, object.get_node());
+
+ librdf_model_add_statement(_model, triple);
+}
+
+
+void
+Model::add_statement(const Node& subject,
+ const Node& predicate,
+ const Atom& object)
+{
+ librdf_node* atom_node = AtomRedland::atom_to_rdf_node(_world.world(), object);
+
+ if (atom_node) {
+ librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(),
+ subject.get_node(), predicate.get_node(), atom_node);
+ librdf_model_add_statement(_model, triple);
+ } else {
+ cerr << "WARNING: Unable to add statement (unserializable Atom)" << endl;
+ }
+}
+
+
+void
+Model::add_statement(const Node& subject,
+ const string& predicate_id,
+ const Atom& object)
+{
+ const string predicate_uri = _world.expand_uri(predicate_id);
+ librdf_node* predicate = librdf_new_node_from_uri_string(_world.world(),
+ (const unsigned char*)predicate_uri.c_str());
+
+ librdf_node* atom_node = AtomRedland::atom_to_rdf_node(_world.world(), object);
+
+ if (atom_node) {
+ librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(),
+ subject.get_node(), predicate, atom_node);
+ librdf_model_add_statement(_model, triple);
+ } else {
+ cerr << "WARNING: Unable to add statement (unserializable Atom)" << endl;
+ }
+}
+
+
+
+} // namespace RDF
+} // namespace Raul
+
diff --git a/src/RDFNode.cpp b/src/RDFNode.cpp
new file mode 100644
index 0000000..2278a7a
--- /dev/null
+++ b/src/RDFNode.cpp
@@ -0,0 +1,140 @@
+/* This file is part of Raul.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * Raul 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.
+ *
+ * Raul 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 details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <string>
+#include <raul/RDFWorld.h>
+#include <raul/RDFNode.h>
+
+using namespace std;
+
+namespace Raul {
+namespace RDF {
+
+
+Node::Node(World& world, Type type, const std::string& s)
+{
+ if (type == RESOURCE)
+ _node = librdf_new_node_from_uri_string(world.world(), (const unsigned char*)s.c_str());
+ else if (type == LITERAL)
+ _node = librdf_new_node_from_literal(world.world(), (const unsigned char*)s.c_str(), NULL, 0);
+ else if (type == BLANK)
+ _node = librdf_new_node_from_blank_identifier(world.world(), (const unsigned char*)s.c_str());
+ else
+ _node = NULL;
+
+ assert(this->type() == type);
+}
+
+
+Node::Node(World& world)
+ : _node(librdf_new_node(world.world()))
+{
+}
+
+
+Node::Node(librdf_node* node)
+ : _node(librdf_new_node_from_node(node))
+{
+ assert(node);
+}
+
+
+Node::Node(const Node& other)
+ : _node(other._node ? librdf_new_node_from_node(other._node): NULL)
+{
+}
+
+
+Node::~Node()
+{
+ if (_node)
+ librdf_free_node(_node);
+}
+
+
+string
+Node::to_string() const
+{
+ const Type type = this->type();
+ if (type == RESOURCE) {
+ return string((const char*)librdf_uri_as_string(librdf_node_get_uri(_node)));
+ } else if (type == LITERAL) {
+ return string((const char*)librdf_node_get_literal_value(_node));
+ } else if (type == BLANK) {
+ return string((const char*)librdf_node_get_blank_identifier(_node));
+ } else {
+ return "";
+ }
+}
+
+
+string
+Node::to_quoted_uri_string() const
+{
+ assert(type() == RESOURCE);
+ string str = "<";
+ str.append((const char*)librdf_uri_as_string(librdf_node_get_uri(_node)));
+ str.append(">");
+ return str;
+}
+
+
+bool
+Node::is_int()
+{
+ if (_node && librdf_node_get_type(_node) == LIBRDF_NODE_TYPE_LITERAL) {
+ librdf_uri* datatype_uri = librdf_node_get_literal_value_datatype_uri(_node);
+ if (datatype_uri && !strcmp((const char*)librdf_uri_as_string(datatype_uri),
+ "http://www.w3.org/2001/XMLSchema#integer"))
+ return true;
+ }
+ return false;
+}
+
+
+int
+Node::to_int()
+{
+ assert(is_int());
+ return strtol((const char*)librdf_node_get_literal_value(_node), NULL, 10);
+}
+
+
+bool
+Node::is_float()
+{
+ if (_node && librdf_node_get_type(_node) == LIBRDF_NODE_TYPE_LITERAL) {
+ librdf_uri* datatype_uri = librdf_node_get_literal_value_datatype_uri(_node);
+ if (datatype_uri && !strcmp((const char*)librdf_uri_as_string(datatype_uri),
+ "http://www.w3.org/2001/XMLSchema#decimal"))
+ return true;
+ }
+ return false;
+}
+
+
+float
+Node::to_float()
+{
+ assert(is_float());
+ return strtod((const char*)librdf_node_get_literal_value(_node), NULL);
+}
+
+
+} // namespace RDF
+} // namespace Raul
+
diff --git a/src/RDFQuery.cpp b/src/RDFQuery.cpp
index 90f6c35..2cc2209 100644
--- a/src/RDFQuery.cpp
+++ b/src/RDFQuery.cpp
@@ -19,65 +19,69 @@
#include <cassert>
#include <rasqal.h>
#include "raul/RDFQuery.h"
+#include "raul/RDFModel.h"
using namespace std;
namespace Raul {
+namespace RDF {
-RDFQuery::Results
-RDFQuery::run(const Glib::ustring base_uri_str) const
+Query::Results
+Query::run(World& world, Model& model, const Glib::ustring base_uri_str) const
{
- Results result;
-
- rasqal_init();
+ //cout << "\n**************** QUERY *******************\n";
+ //cout << _query << endl;
+ //cout << "******************************************\n\n";
- rasqal_query *rq = rasqal_new_query("sparql", NULL);
+ Results result;
- raptor_uri* base_uri = NULL;
+ librdf_uri* base_uri = NULL;
if (base_uri_str != "")
- base_uri = raptor_new_uri((const unsigned char*)base_uri_str.c_str());
- rasqal_query_prepare(rq, (unsigned char*)_query.c_str(), base_uri);
+ base_uri = librdf_new_uri(world.world(),
+ (const unsigned char*)base_uri_str.c_str());
- rasqal_query_results* results = rasqal_query_execute(rq);
+ librdf_query* q = librdf_new_query(world.world(), "sparql",
+ NULL, (unsigned char*)_query.c_str(), base_uri);
+
+ if (!q) {
+ cerr << "Unable to create query:" << endl << _query << endl;
+ return result; /* Return an empty Results */
+ }
+
+ librdf_query_results* results = librdf_query_execute(q, model._model);
if (!results) {
- cerr << "Failed Query was:" << endl << _query << endl;
+ cerr << "Failed query:" << endl << _query << endl;
return result; /* Return an empty Results */
}
- while (!rasqal_query_results_finished(results)) {
+ while (!librdf_query_results_finished(results)) {
Bindings bindings;
- for (int i=0; i < rasqal_query_results_get_bindings_count(results); i++) {
- const unsigned char* rname = rasqal_query_results_get_binding_name(results, i);
- rasqal_literal* rvalue = rasqal_query_results_get_binding_value(results, i);
- Glib::ustring name((const char*)rname);
+ for (int i=0; i < librdf_query_results_get_bindings_count(results); i++) {
+ const char* name = (char*)librdf_query_results_get_binding_name(results, i);
+ librdf_node* value = librdf_query_results_get_binding_value(results, i);
- const unsigned char* str = rasqal_literal_as_string(rvalue);
-
- if (str) {
- Glib::ustring value((const char*)str);
- bindings.insert(std::make_pair(name, value));
- }
+ if (name && value)
+ bindings.insert(std::make_pair(std::string(name), Node(value)));
}
result.push_back(bindings);
- rasqal_query_results_next(results);
+ librdf_query_results_next(results);
}
- rasqal_free_query_results(results);
- rasqal_free_query(rq);
+ librdf_free_query_results(results);
+ librdf_free_query(q);
if (base_uri)
- raptor_free_uri(base_uri);
-
- rasqal_finish();
+ librdf_free_uri(base_uri);
return result;
}
+} // namespace RDF
} // namespace Raul
diff --git a/src/RDFWorld.cpp b/src/RDFWorld.cpp
new file mode 100644
index 0000000..caf6fb8
--- /dev/null
+++ b/src/RDFWorld.cpp
@@ -0,0 +1,95 @@
+/* This file is part of Raul.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * Raul 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.
+ *
+ * Raul 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 details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <sstream>
+#include "raul/RDFWorld.h"
+#include "raul/RDFNode.h"
+#include "raul/AtomRedland.h"
+
+#define U(x) ((const unsigned char*)(x))
+
+using namespace std;
+
+namespace Raul {
+namespace RDF {
+
+
+//static const char* const RDF_LANG = "rdfxml-abbrev";
+static const char* const RDF_LANG = "turtle";
+
+
+/** Create an empty in-memory RDF model.
+ */
+World::World()
+{
+ _world = librdf_new_world();
+ assert(_world);
+ librdf_world_open(_world);
+
+ add_prefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
+}
+
+
+World::~World()
+{
+ librdf_free_world(_world);
+}
+
+
+void
+World::add_prefix(const string& prefix, const string& uri)
+{
+ _prefixes[prefix] = uri;
+}
+
+
+/** Expands the prefix of URI, if the prefix is registered.
+ */
+string
+World::expand_uri(const string& uri) const
+{
+ assert(uri.find(":") != string::npos);
+
+ for (Namespaces::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i)
+ if (uri.substr(0, i->first.length()+1) == i->first + ":")
+ return i->second + uri.substr(i->first.length()+1);
+
+ return uri;
+}
+
+
+/** Opposite of expand_uri
+ */
+string
+World::qualify(const string& uri) const
+{
+ return _prefixes.qualify(uri);
+}
+
+
+Node
+World::blank_id()
+{
+ std::ostringstream ss;
+ ss << "n" << _next_blank_id++;
+ return Node(*this, Node::BLANK, ss.str());
+}
+
+
+} // namespace RDF
+} // namespace Raul
+
diff --git a/src/RDFWriter.cpp b/src/RDFWriter.cpp
deleted file mode 100644
index d6a33a4..0000000
--- a/src/RDFWriter.cpp
+++ /dev/null
@@ -1,257 +0,0 @@
-/* This file is part of Raul.
- * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
- *
- * Raul 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.
- *
- * Raul 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 details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <sstream>
-#include "raul/RDFWriter.h"
-#include "raul/AtomRaptor.h"
-
-#define U(x) ((const unsigned char*)(x))
-
-using namespace std;
-
-namespace Raul {
-
-
-//static const char* const RDF_LANG = "rdfxml-abbrev";
-static const char* const RDF_LANG = "turtle";
-
-
-RDFWriter::RDFWriter()
- : _serializer(NULL)
- , _string_output(NULL)
- , _next_blank_id(0)
-{
- add_prefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
-}
-
-
-void
-RDFWriter::add_prefix(const string& prefix, const string& uri)
-{
- _prefixes[prefix] = uri;
- if (_serializer)
- raptor_serialize_set_namespace(_serializer,
- raptor_new_uri(U(uri.c_str())), U(prefix.c_str()));
-}
-
-
-void
-RDFWriter::setup_prefixes()
-{
- assert(_serializer);
-
- for (Namespaces::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i) {
- raptor_serialize_set_namespace(_serializer,
- raptor_new_uri(U(i->second.c_str())), U(i->first.c_str()));
- }
-}
-
-
-/** Expands the prefix of URI, if the prefix is registered.
- */
-string
-RDFWriter::expand_uri(const string& uri)
-{
- for (Namespaces::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i)
- if (uri.substr(0, i->first.length()+1) == i->first + ":")
- return i->second + uri.substr(i->first.length()+1);
-
- return uri;
-}
-
-
-RdfId
-RDFWriter::blank_id()
-{
- std::ostringstream ss;
- ss << "n" << _next_blank_id++;
- return RdfId(RdfId::ANONYMOUS, ss.str());
-}
-
-
-/** Begin a serialization to a C file handle.
- *
- * This must be called before any write methods.
- */
-void
-RDFWriter::start_to_file_handle(FILE* fd) throw (std::logic_error)
-{
- if (_serializer)
- throw std::logic_error("start_to_string called with serialization in progress");
-
- raptor_init();
- _serializer = raptor_new_serializer(RDF_LANG);
- raptor_serialize_start_to_file_handle(_serializer, NULL, fd);
- setup_prefixes();
-}
-
-
-/** Begin a serialization to a file.
- *
- * This must be called before any write methods.
- */
-void
-RDFWriter::start_to_filename(const string& filename) throw (std::logic_error)
-{
- if (_serializer)
- throw std::logic_error("start_to_string called with serialization in progress");
-
- raptor_init();
- _serializer = raptor_new_serializer(RDF_LANG);
- raptor_serialize_start_to_filename(_serializer, filename.c_str());
- setup_prefixes();
-}
-
-
-/** Begin a serialization to a string.
- *
- * This must be called before any write methods.
- *
- * The results of the serialization will be returned by the finish() method after
- * the desired objects have been serialized.
- */
-void
-RDFWriter::start_to_string() throw (std::logic_error)
-{
- if (_serializer)
- throw std::logic_error("start_to_string called with serialization in progress");
-
- raptor_init();
- _serializer = raptor_new_serializer(RDF_LANG);
- raptor_serialize_start_to_string(_serializer,
- NULL /*base_uri*/,
- (void**)&_string_output,
- NULL /*size*/);
- setup_prefixes();
-}
-
-
-/** Finish a serialization.
- *
- * If this was a serialization to a string, the serialization output
- * will be returned, otherwise the empty string is returned.
- */
-string
-RDFWriter::finish() throw(std::logic_error)
-{
- string ret = "";
-
- if (!_serializer)
- throw std::logic_error("finish() called with no serialization in progress");
-
- raptor_serialize_end(_serializer);
-
- if (_string_output) {
- ret = string((char*)_string_output);
- free(_string_output);
- _string_output = NULL;
- }
-
- raptor_free_serializer(_serializer);
- _serializer = NULL;
-
- raptor_finish();
-
- return ret;
-}
-
-
-void
-RDFWriter::write(const RdfId& subject,
- const RdfId& predicate,
- const RdfId& object)
-{
- assert(_serializer);
-
- raptor_statement triple;
-
- // FIXME: leaks?
-
- if (subject.type() == RdfId::RESOURCE) {
- triple.subject = (void*)raptor_new_uri((const unsigned char*)
- expand_uri(subject.to_string()).c_str());
- triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
- } else {
- assert(subject.type() == RdfId::ANONYMOUS);
- triple.subject = (unsigned char*)(strdup(subject.to_string().c_str()));
- triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
- }
-
- assert(predicate.type() == RdfId::RESOURCE);
- triple.predicate = (void*)raptor_new_uri((const unsigned char*)
- expand_uri(predicate.to_string()).c_str());
- triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
-
- if (object.type() == RdfId::RESOURCE) {
- triple.object = (void*)raptor_new_uri((const unsigned char*)
- expand_uri(object.to_string()).c_str());
- triple.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
- } else {
- assert(object.type() == RdfId::ANONYMOUS);
- triple.object = (unsigned char*)(strdup(object.to_string().c_str()));
- triple.object_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
- }
-
- raptor_serialize_statement(_serializer, &triple);
-
- if (subject.type() == RdfId::RESOURCE)
- raptor_free_uri((raptor_uri*)triple.subject);
-
- raptor_free_uri((raptor_uri*)triple.predicate);
-
- if (object.type() == RdfId::RESOURCE)
- raptor_free_uri((raptor_uri*)triple.object);
-}
-
-
-void
-RDFWriter::write(const RdfId& subject,
- const RdfId& predicate,
- const Atom& object)
-{
- assert(_serializer);
-
- raptor_statement triple;
-
- if (subject.type() == RdfId::RESOURCE) {
- triple.subject = (void*)raptor_new_uri((const unsigned char*)
- expand_uri(subject.to_string()).c_str());
- triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
- } else {
- assert(subject.type() == RdfId::ANONYMOUS);
- triple.subject = (unsigned char*)(strdup(subject.to_string().c_str()));
- triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS;
- }
-
- assert(predicate.type() == RdfId::RESOURCE);
- triple.predicate = (void*)raptor_new_uri((const unsigned char*)
- expand_uri(predicate.to_string()).c_str());
- triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE;
-
- AtomRaptor::atom_to_triple_object(&triple, object);
-
- raptor_serialize_statement(_serializer, &triple);
-
- if (subject.type() == RdfId::RESOURCE)
- raptor_free_uri((raptor_uri*)triple.subject);
-
- raptor_free_uri((raptor_uri*)triple.predicate);
-}
-
-
-} // namespace Raul
-