Complete rewrite that provides a more idiomatic Node interface based on streams2.
Raptor is a mature RDF parsing and serializing library written in C by Dave Beckett. It is part of Redland, the RDF handling and storage library.
These bindings support libraptor2 only (v1.9.0 or greater).
A version of libraptor2
needs to be installed on your system
The default prefix is /usr/local
for Mac OS X (installed with brew) or /usr
on Ubuntu (installed with apt-get
).
The build script already recognizes those two.
If your path differs (try which rapper | sed -e "s/\/bin.*$//g"
) you need to update binding.gyp
accordingly.
Then run npm install
.
Include raptor bindings with
var raptor = require('raptor');
You can create parsers or serializers for certain syntax types like so:
var parser = raptor.createParser('rdfxml');
var serializer = raptor.createSerializer('turtle');
If you leave out the syntax type for the serializer Raptor will try to guess the syntax by reading some data.
There is a low-level API and a streams2 interface.
You are highly encourages to use the streaming interface.
See examples/stream.js
for a minimal stream-based example.
The stream API allows you to pipe any Node readable stream (e.g. fs.ReadStream
, http.IncomingMessage
, ...) into a Raptor parser or a Raptor serializer output into any Node writable stream.
Note that Node 0.10 or greater is required for that feature. If you are using an older Node version, check out the pre-Node-0.10
branch.
Raptor is very picky about base URIs. If a syntax might require a base URI you have to declare one. Even if the data you are passing it does not use relative URIs at all.
Use
setBaseURI('http://example.com/makingRaptorHappy/');
on a serializer or parser instance before piping any data or statements into it.
On the following conditions, events are emitted:
parser.on('data', function (statement) {
// do something with statement
});
Statements are exposed with the following structure:
var statement = {
subject: {
type: 'uri', /* 'uri' or 'bnode' */
value: 'http://example.com/exampleResource'
},
predicate: {
type: 'uri', /* always 'uri' */
value: 'http://example.com/exampleProperty'
},
object: {
type: 'typed-literal', /* 'uri', 'literal',
'typed-literal', or 'bnode' */
value: 'An example literal value',
datatype: 'http://www.w3.org/2001/XMLSchema#string'
},
toString: function () {
/* return statement as N-triples string */
}
}
parser.on('namespace', function (prefix, namespaceURI) {
// handle namespace event
});
parser.on('message', function (message) {
// log errors and warnings synchronously
if (message.type === 'error' || message.type === 'warning') {
util.log(type.toUpperCase() + ': ' + message + ' (' + code + ')');
}
});