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
- Variables
- func GoRaptor_handle_log(user_data, msgp unsafe.Pointer)
- func GoRaptor_handle_namespace(user_data, nsp unsafe.Pointer)
- func GoRaptor_handle_statement(user_data, rsp unsafe.Pointer)
- func Reset()
- type Blank
- type Literal
- type LogHandler
- type NamespaceHandler
- type Parser
- func (p *Parser) Free()
- func (p *Parser) Parse(reader io.Reader, base_uri string) chan *Statement
- func (p *Parser) ParseFile(filename string, base_uri string) chan *Statement
- func (p *Parser) ParseUri(uri string, base_uri string) chan *Statement
- func (p *Parser) SetLogHandler(handler LogHandler)
- func (p *Parser) SetNamespaceHandler(handler NamespaceHandler)
- type Serializer
- func (s *Serializer) Add(statement *Statement) (err error)
- func (s *Serializer) AddN(ch chan *Statement)
- func (s *Serializer) EndStream() (err error)
- func (s *Serializer) Free()
- func (s *Serializer) Serialize(ch chan *Statement, base_uri string) (str string, err error)
- func (s *Serializer) SetFile(fp *os.File, base_uri string) (err error)
- func (s *Serializer) SetLogHandler(handler LogHandler)
- func (s *Serializer) SetNamespace(prefix, uri string)
- func (s *Serializer) StartStream(file *os.File, base_uri string) (err error)
- type Statement
- type Syntax
- type Term
- type Uri
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 ¶
For internal use only, callback for log messages from C. Arranges that the configured log handler will be called.
func GoRaptor_handle_namespace ¶
for internal use only. callback from the C namespace handler for the parser
func GoRaptor_handle_statement ¶
for internal use only. callback from the C statement handler for the parser
Types ¶
type LogHandler ¶
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 ¶
A handler function to be called when the parser encounters a namespace.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
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) 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 Term ¶
type Term interface { Type() uint8 N3() string String() string Equals(Term) bool GobEncode() ([]byte, error) GobDecode([]byte) error // contains filtered or unexported methods }