goraptor

package module
v0.0.0-...-6c405b4 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2013 License: LGPL-2.1 Imports: 10 Imported by: 1

README

PACKAGE

package goraptor
import "bitbucket.org/ww/goraptor"

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", "")
    for {
        statement, ok := <-ch
        if ! ok {
            break
        }

        // 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", "")
    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.


CONSTANTS

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
)

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

var LogLevels map[int]string
For convenience a mapping of log levels to human readable strings.

var ParserSyntax map[string]*Syntax
global map of parser name to parser description

var SerializerSyntax map[string]*Syntax
global map of serializer name to serializer description


FUNCTIONS

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.
export GoRaptor_handle_log

func GoRaptor_handle_namespace(user_data, nsp unsafe.Pointer)
for internal use only. callback from the C namespace handler for the parser
export GoRaptor_handle_namespace

func GoRaptor_handle_statement(user_data, rsp unsafe.Pointer)
for internal use only. callback from the C statement handler for the parser
export GoRaptor_handle_statement

func Reset()


TYPES

type Blank string

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

func (b *Blank) GobDecode(buf []byte) (err os.Error)

func (b *Blank) GobEncode() (buf []byte, err os.Error)

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

func (b *Blank) String() string

func (b *Blank) Type() uint8

type Literal struct {
    Value    string
    Lang     string
    Datatype string
}

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

func (l *Literal) GobDecode(buf []byte) (err os.Error)

func (l *Literal) GobEncode() (buf []byte, err os.Error)

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

func (l *Literal) String() string

func (l *Literal) Type() uint8

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 func(prefix string, uri string)
A handler function to be called when the parser encounters
a namespace.

type Parser struct {
    // contains unexported fields
}

func NewParser(name string) *Parser

func (p *Parser) Free()

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

func (p *Parser) ParseFile(filename string, base_uri string) chan *Statement
parse a local file

func (p *Parser) ParseUri(uri string, base_uri string) chan *Statement
parse a network resource

func (p *Parser) SetLogHandler(handler LogHandler)
set the log handler which by default will use the generic log package

func (p *Parser) SetNamespaceHandler(handler NamespaceHandler)
set the namespace handler which is by default a noop

type Serializer struct {
    // contains unexported fields
}

func NewSerializer(name string) *Serializer

func (s *Serializer) Add(statement *Statement) (err os.Error)

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

func (s *Serializer) Free()

func (s *Serializer) Serialize(ch chan *Statement, base_uri string) (str string, err os.Error)

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

func (s *Serializer) SetLogHandler(handler LogHandler)
set the log handler which by default will use the generic log package

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

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

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

func (s *Statement) GobDecode(buf []byte) (err os.Error)

func (s *Statement) GobEncode() (buf []byte, err os.Error)

func (s *Statement) N3() string

func (s *Statement) String() string

type Syntax struct {
    Label    string
    Name     string
    MimeType string
}
struct holding some details of available parsers or serializers

func (s *Syntax) String() string

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

func TermDecode(buf []byte) (t Term, err os.Error)

type Uri string

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

func (u *Uri) GobDecode(buf []byte) (err os.Error)

func (u *Uri) GobEncode() (buf []byte, err os.Error)

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

func (u *Uri) String() string

func (u *Uri) Type() uint8


OTHER PACKAGES

main

SUBDIRECTORIES

	.hg

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", "")
for {
    statement, ok := <-ch
    if ! ok {
        break
    }

    // 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", "")
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.

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

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