goraptor

package module
v0.0.0-...-1be71b6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2014 License: LGPL-2.1 Imports: 10 Imported by: 3

README

goraptor

Go bindings for the Raptor RDF parser / serializer.

This is a updated version of goraptor written by William Waites ww@styx.org. We've done a little bit of cleanup and testing with Go 1.1.1. This library is not complete.

For basic usage, see Documentation.txt.

Prerequisites

Install the development libraries for Raptor version 2. On Debian / Ubuntu run:

sudo apt-get install libraptor2-dev

Documentation

Overview

Go bindings for the raptor RDF parser / seraliser.

Written in 2011 by William Waites <ww@styx.org>. Distributed under the terms of the LGPL version 2.1 or any later version.

To build you must have raptor version 2 or greater installed. You can get raptor from http://librdf.org/raptor/

Example usage:

parser := goraptor.NewParser("guess")
defer parser.Free()

ch := parser.ParseUri("http://www.w3.org/People/Berners-Lee/card.rdf", "")
for statement := range ch {
    // do something with statement
}

Serialisers are analogous. For example to read in one serialisation and write in another, preserving namespaces:

parser := goraptor.NewParser("guess")
defer parser.Free()

serializer := goraptor.NewSerializer("turtle")
defer serializer.Free()

parser.SetNamespaceHandler(func(pfx, uri string) { serializer.SetNamespace(pfx, uri) })

statements := parser.ParseUri("http://www.w3.org/People/Berners-Lee/card.rdf", "")
str, err := serializer.Serialize(statements, "")

fmt.Print(str)

The step of setting the namespace handler is strictly unnecessary and is basically used so that the output is more aesthetically pleasing. If instead of serializing to a string you want to serialize to a file, you can do instead:

fp := os.Open("output.ttl", os.O_WRONLY, 0644)
serializer.SetFile(fp, "")
serializer.AddN(statements)

Note that it is strictly necessary to free the serializer for only then can it be guaranteed that any buffered output is written to the destination file.

The basic datatype is the Term which represents an RDF URI, blank node or literal value. Terms are grouped into compound Statement datatypes which contain four Terms, Subject, Predicate, Object and Graph. Both of these datatypes are memory managed by Go but can be converted back and forth to/from raptor's internal representation. The datatypes support a compact binary encoding for use with the gob package.

Using the locator:

parser := goraptor.NewParser("guess")
defer parser.Free()

ch := parser.ParseUri("http://www.w3.org/People/Berners-Lee/card.rdf", "")
locCh := parser.LocatorChan()
for statement := range ch {
    locator := <- locCh
    // do something with statement and locator
    // e.g.: fmt.Printf("Found statement %s at line %d", statement, locator.Line)
}

Index

Constants

View Source
const (
	RAPTOR_TERM_TYPE_URI     = C.RAPTOR_TERM_TYPE_URI
	RAPTOR_TERM_TYPE_BLANK   = C.RAPTOR_TERM_TYPE_BLANK
	RAPTOR_TERM_TYPE_LITERAL = C.RAPTOR_TERM_TYPE_LITERAL
)
View Source
const (
	RAPTOR_LOG_LEVEL_NONE  = C.RAPTOR_LOG_LEVEL_NONE
	RAPTOR_LOG_LEVEL_TRACE = C.RAPTOR_LOG_LEVEL_TRACE
	RAPTOR_LOG_LEVEL_DEBUG = C.RAPTOR_LOG_LEVEL_DEBUG
	RAPTOR_LOG_LEVEL_INFO  = C.RAPTOR_LOG_LEVEL_INFO
	RAPTOR_LOG_LEVEL_WARN  = C.RAPTOR_LOG_LEVEL_WARN
	RAPTOR_LOG_LEVEL_ERROR = C.RAPTOR_LOG_LEVEL_ERROR
	RAPTOR_LOG_LEVEL_FATAL = C.RAPTOR_LOG_LEVEL_FATAL
)

Variables

View Source
var LogLevels map[int]string

For convenience a mapping of log levels to human readable strings.

View Source
var ParserSyntax map[string]*Syntax

global map of parser name to parser description

View Source
var SerializerSyntax map[string]*Syntax

global map of serializer name to serializer description

Functions

func GoRaptor_handle_log

func GoRaptor_handle_log(user_data, msgp unsafe.Pointer)

For internal use only, callback for log messages from C. Arranges that the configured log handler will be called.

func GoRaptor_handle_namespace

func GoRaptor_handle_namespace(user_data, nsp unsafe.Pointer)

for internal use only. callback from the C namespace handler for the parser

func GoRaptor_handle_statement

func GoRaptor_handle_statement(user_data, rsp unsafe.Pointer)

for internal use only. callback from the C statement handler for the parser

func Reset

func Reset()

Types

type Blank

type Blank string

func (*Blank) Equals

func (b *Blank) Equals(other Term) (eq bool)

func (*Blank) GobDecode

func (b *Blank) GobDecode(buf []byte) (err error)

func (*Blank) GobEncode

func (b *Blank) GobEncode() (buf []byte, err error)

func (*Blank) N3

func (b *Blank) N3() (s string)

func (*Blank) String

func (b *Blank) String() string

func (*Blank) Type

func (b *Blank) Type() uint8

type Literal

type Literal struct {
	Value    string
	Lang     string
	Datatype string
}

func (*Literal) Equals

func (l *Literal) Equals(other Term) (eq bool)

func (*Literal) GobDecode

func (l *Literal) GobDecode(buf []byte) (err error)

func (*Literal) GobEncode

func (l *Literal) GobEncode() (buf []byte, err error)

func (*Literal) N3

func (l *Literal) N3() (s string)

func (*Literal) String

func (l *Literal) String() string

func (*Literal) Type

func (l *Literal) Type() uint8

type Locator

type Locator struct {
	Byte, Line, Column int
	Uri                *Uri
	File               string
}

Represents raptor_locator.

type LogHandler

type LogHandler func(int, string)

LogHandler functions are called from parsers and serialisers. They are colled with a log level integer and a log message string. The default implementation pretty prints the level and the string using the generic log package

type NamespaceHandler

type NamespaceHandler func(prefix string, uri string)

A handler function to be called when the parser encounters a namespace.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser

func NewParser(name string) *Parser

func (*Parser) Free

func (p *Parser) Free()

func (*Parser) IgnoreLocatorChan

func (p *Parser) IgnoreLocatorChan()

Close and remove the locator channel associated with this parser.

func (*Parser) Locator

func (p *Parser) Locator() *Locator

Get raptor_locator of the current position. Strongly recommended to use LocatorChan() instead.

func (*Parser) LocatorChan

func (p *Parser) LocatorChan() chan *Locator

Sets up a channel for locators and return it.

If there is a channel for locators, a *Locator is created after a statement is sent over the statements channel (one reutrned by Parse* functions).

Not reading the *Locator channel may cause the parser to stop (waiting for a read).

The locator channel is buffered (with 1 locator), so raptor library can continue parsing the next statement even when the locator associated with the last statement is not read.

func (*Parser) Parse

func (p *Parser) Parse(reader io.Reader, base_uri string) chan *Statement

Parse RDF data from an io.Reader

func (*Parser) ParseFile

func (p *Parser) ParseFile(filename string, base_uri string) chan *Statement

parse a local file

func (*Parser) ParseUri

func (p *Parser) ParseUri(uri string, base_uri string) chan *Statement

parse a network resource

func (*Parser) SetLogHandler

func (p *Parser) SetLogHandler(handler LogHandler)

set the log handler which by default will use the generic log package

func (*Parser) SetNamespaceHandler

func (p *Parser) SetNamespaceHandler(handler NamespaceHandler)

set the namespace handler which is by default a noop

type Serializer

type Serializer struct {
	// contains filtered or unexported fields
}

func NewSerializer

func NewSerializer(name string) *Serializer

func (*Serializer) Add

func (s *Serializer) Add(statement *Statement) (err error)

func (*Serializer) AddN

func (s *Serializer) AddN(ch chan *Statement) (err error)

func (*Serializer) EndStream

func (s *Serializer) EndStream() (err error)

func (*Serializer) Free

func (s *Serializer) Free()

func (*Serializer) Serialize

func (s *Serializer) Serialize(ch chan *Statement, base_uri string) (str string, err error)

func (*Serializer) SetFile

func (s *Serializer) SetFile(fp *os.File, base_uri string) (err error)

func (*Serializer) SetLogHandler

func (s *Serializer) SetLogHandler(handler LogHandler)

set the log handler which by default will use the generic log package

func (*Serializer) SetNamespace

func (s *Serializer) SetNamespace(prefix, uri string)

func (*Serializer) StartStream

func (s *Serializer) StartStream(file *os.File, base_uri string) (err error)

type Statement

type Statement struct {
	Subject, Predicate, Object, Graph Term
}

func (*Statement) Equals

func (s *Statement) Equals(other *Statement) (eq bool)

func (*Statement) GobDecode

func (s *Statement) GobDecode(buf []byte) (err error)

func (*Statement) GobEncode

func (s *Statement) GobEncode() (buf []byte, err error)

func (*Statement) N3

func (s *Statement) N3() string

func (*Statement) String

func (s *Statement) String() string

type Syntax

type Syntax struct {
	Label    string
	Name     string
	MimeType string
}

struct holding some details of available parsers or serializers

func (*Syntax) String

func (s *Syntax) String() string

type Term

type Term interface {
	Type() uint8
	N3() string
	String() string
	Equals(Term) bool
	GobEncode() ([]byte, error)
	GobDecode([]byte) error
	// contains filtered or unexported methods
}

func TermDecode

func TermDecode(buf []byte) (t Term, err error)

type Uri

type Uri string

func (*Uri) Equals

func (u *Uri) Equals(other Term) (eq bool)

func (*Uri) GobDecode

func (u *Uri) GobDecode(buf []byte) (err error)

func (*Uri) GobEncode

func (u *Uri) GobEncode() (buf []byte, err error)

func (*Uri) N3

func (u *Uri) N3() (s string)

func (*Uri) String

func (u *Uri) String() string

func (*Uri) Type

func (u *Uri) Type() uint8

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL