Redland RDF toolkit ObjC bindings
I’m thinking of adding some RDF-specific features to XML Nanny for version 2.1, so after my morning jog today, I started tinkering with Dave Beckett’s Redland C toolkit and the awesome Redland Cocoa/ObjC Bindings provided by René Puls.
Once again, there didn’t seem to be any examples out on the web. But Redland comes with a few nice examples, and the ObjC bindings come with test cases that can get you going. The API is well-designed and straight-forward anyway.
So I thought I’d contribute a Redland ObjC code sample to the Google-sphere. Here’s a small command line program that parses a local RDF/XML document into a Redland RDF graph of tripes in ObjC. Pretty simple stuff:
#import <Foundation/Foundation.h>
// Import the Redland ObjC framework
#import <Redland/Redland-ObjC.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// First, intialize Redland framework using the RedlandWorld class
// Here we do the default initialization with no cusotmization
RedlandWorld *world = [RedlandWorld defaultWorld];
NSLog(@"world: %@", world);
// Next, create a "storage" area in which to put our RDF triples.
// Here we use the default storage which is "in-memory".
// You can also use Berkeley DB or other XML stores.
RedlandStorage *storage = [RedlandStorage storage];
NSLog(@"storage: %@", storage);
// Path to an RDF/XML file
NSString *path = @"/Users/itod/Desktop/raptor/raptor.rdf";
// Create a "RedlandURI" for our file's path for use with the RDF parser.
// This will serve as the base URI for our parsed RDF/XML file
RedlandURI *uri = [RedlandURI URIWithString:path];
NSLog(@"uri: %@", uri);
// Create a "Model" object. This is a container for a set of RDF triples
// (in this case, the triples in our RDF/XML document)
RedlandModel *model = [RedlandModel model];
NSLog(@"model: %@", model);
// Create an RDF/XML parser
RedlandParser *parser = [RedlandParser parserWithName:RedlandRDFXMLParserName];
NSLog(@"parser: %@", parser);
// The ObjC parser doesn't seem to accept a file path, so load our RDF/XML file's
// contents in to an NSString and use that.
NSString *rdfString = [NSString stringWithContentsOfFile:path];
// Parse our RDF/XML NSString into our model using our base uri.
[parser parseString:rdfString intoModel:model withBaseURI:uri];
// Let's se what we've got:
NSLog(@"model :");
[model print];
[pool release];
return 0;
}
The output will be a collection of RDF triples in a simple serialization that looks something like this:
2006-07-09 14:42:53.603 RedlandTest[22305] world: <redlandworld : 0x5073f0>
2006-07-09 14:42:53.611 RedlandTest[22305] storage: <redlandstorage : 0x514970>
2006-07-09 14:42:53.614 RedlandTest[22305] uri: /Users/itod/Desktop/raptor/raptor.rdf
2006-07-09 14:42:53.619 RedlandTest[22305] model: <redlandmodel : 0x515730>
2006-07-09 14:42:53.623 RedlandTest[22305] parser: <redlandparser : 0x515d50>
2006-07-09 14:42:53.669 RedlandTest[22305] model :
[[
{[/Users/itod/Desktop/raptor/raptor.rdf], [http://xmlns.com/foaf/0.1/maker], (r1152481373r5)}
{(r1152481373r1), [http://usefulinc.com/ns/doap#description], "Raptor is the RDF Parser Toolkit for
Redland that provides a set of Resource Description Framework (RDF)
parsers and serializers, generating RDF triples from RDF/XML,
N-Triples, Turtle, several XML RSS tag soup formats and Atom 0.3
along with serializers to RDF/XML, N-Triples and RSS 1.0.@en"}
{(r1152481373r5), [http://xmlns.com/foaf/0.1/homepage], [http://purl.org/net/dajobe/]}
{(r1152481373r2), [http://www.w3.org/1999/02/22-rdf-syntax-ns#type], [http://xmlns.com/foaf/0.1/Person]}
{(r1152481373r1), [http://usefulinc.com/ns/doap#download-mirror], [http://sourceforge.net/projects/librdf/]}
{(r1152481373r1), [http://www.w3.org/1999/02/22-rdf-syntax-ns#type], [http://usefulinc.com/ns/doap#Project]}
{(r1152481373r1), [http://usefulinc.com/ns/doap#license], [http://usefulinc.com/doap/licenses/asl20]}
{(r1152481373r1), [http://usefulinc.com/ns/doap#license], [http://usefulinc.com/doap/licenses/lgpl]}
{(r1152481373r1), [http://usefulinc.com/ns/doap#license], [http://usefulinc.com/doap/licenses/gpl]}
{(r1152481373r1), [http://usefulinc.com/ns/doap#name], "Raptor"}
{(r1152481373r1), [http://usefulinc.com/ns/doap#repository], (r1152481373r4)}
{(r1152481373r1), [http://usefulinc.com/ns/doap#homepage], [http://librdf.org/raptor/]}
{(r1152481373r3), [http://www.w3.org/1999/02/22-rdf-syntax-ns#type], [http://usefulinc.com/ns/doap#Version]}
{(r1152481373r2), [http://xmlns.com/foaf/0.1/name], "Dave Beckett"}
{(r1152481373r2), [http://xmlns.com/foaf/0.1/homepage], [http://purl.org/net/dajobe/]}
...
{(r1152481373r2), [http://xmlns.com/foaf/0.1/mbox_sha1sum], "970987f991961f2553a1bf2574166fa29befbccb"}
{(r1152481373r3), [http://usefulinc.com/ns/doap#revision], "1.4.9"}
]]
About this entry
You’re currently reading “Redland RDF toolkit ObjC bindings,” an entry on Todd Ditchendorf’s Blog.
- Published:
- 07.09.06 / 3pm
- Category:
- Cocoa, Mac OS X, Objective-C, RDF, XML
2 Comments
Jump to comment form | comments rss [?] | trackback uri [?]