Skip to content

0xfeedface/node_raptor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node_raptor -- libraptor2 bindings for Node.js

News

Complete rewrite that provides a more idiomatic Node interface based on streams2.

Synopsis

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).

Building

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.

General Usage

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.

API

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.

Declaring base URIs

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.

Emitted Events (streams2 API)

On the following conditions, events are emitted:

A new statement was parsed

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 */
    }
}

A namespace has been declared

parser.on('namespace', function (prefix, namespaceURI) {
    // handle namespace event
});

A message has been retrieved

parser.on('message', function (message) {
    // log errors and warnings synchronously
    if (message.type === 'error' || message.type === 'warning') {
        util.log(type.toUpperCase() + ': ' + message + ' (' + code + ')');
    }
});

About

Raptor RDF parser and serializer bindings for Node.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published