Documentation ¶
Overview ¶
Package rdf provides functionality for working with RDF resources, including parsing and serialization of the various RDF formats.
Data model ¶
The package adhers to the RDF data model as described in http://www.w3.org/TR/rdf11-concepts/.
Data structures ¶
TODO.
Encoding and decoding ¶
The package aims to support all the RDF serialization formats standardized by W3C. Currently the following are implemented:
Format | Decode | Encode -----------|--------|-------- RDF/XML | x | - N-Triples | x | x N-Quads | x | x Turtle | x | x TriG | - | - JSON-LD | - | -
The parsers are implemented as streaming decoders, consuming an io.Reader and emitting triples/quads as soon as they are available. Simply call Decode() until the reader is exhausted and emits io.EOF:
f, err := os.Open("mytriples.ttl") if err != nil { // handle error } dec := rdf.NewTripleDecoder(f, rdf.Turtle) for triple, err := dec.Decode(); err != io.EOF; triple, err = dec.Decode() { // do something with triple .. }
The encoders work similarily. For a complete working example, see the rdf2rdf application, which converts between different serialization formats using the decoders and encoders of the rdf package: https://github.com/knakk/rdf2rdf.
Index ¶
- Variables
- func QuadsEqual(a, b Quad) bool
- func TermsEqual(a, b Term) bool
- func TriplesEqual(a, b Triple) bool
- type Blank
- type Context
- type Format
- type IRI
- type Literal
- type Object
- type ParseOption
- type Predicate
- type Quad
- type QuadDecoder
- type QuadEncoder
- type Subject
- type Term
- type TermType
- type Triple
- type TripleDecoder
- type TripleEncoder
Constants ¶
This section is empty.
Variables ¶
var DateFormat = time.RFC3339
DateFormat defines the string representation of xsd:DateTime values. You can override it if you need another layout.
var ErrEncoderClosed = errors.New("Encoder is closed and cannot encode anymore")
ErrEncoderClosed is the error returned from Encode() when the Triple/Quad-Encoder is closed
Functions ¶
func TermsEqual ¶
TermsEqual returns true if two Terms are equal, or false if they are not.
func TriplesEqual ¶
TriplesEqual tests if two Triples are identical.
Types ¶
type Blank ¶
type Blank struct {
// contains filtered or unexported fields
}
Blank represents a RDF blank node; an unqualified IRI with identified by a label.
func NewBlank ¶
NewBlank returns a new blank node with a given label. It returns an error only if the supplied label is blank.
type Context ¶
type Context interface { Term // contains filtered or unexported methods }
Context interface distiguishes which Terms are valid as a Quad's Context. Incidently, this is the same as Terms valid as a Subject of a Triple.
type IRI ¶
type IRI struct {
// contains filtered or unexported fields
}
IRI represents a RDF IRI resource.
func NewIRI ¶
NewIRI returns a new IRI, or an error if it's not valid.
A valid IRI cannot be empty, or contain any of the disallowed characters: [\x00-\x20<>"{}|^`\].
type Literal ¶
type Literal struct { // The datatype of the Literal. DataType IRI // contains filtered or unexported fields }
Literal represents a RDF literal; a value with a datatype and (optionally) an associated language tag for strings.
func NewLangLiteral ¶
NewLangLiteral creates a RDF literal with a given language tag, or fails if the language tag is not well-formed.
The literal will have the datatype IRI xsd:String.
func NewLiteral ¶
NewLiteral returns a new Literal, or an error on invalid input. It tries to map the given Go values to a corresponding xsd datatype.
func NewTypedLiteral ¶
NewTypedLiteral returns a literal with the given datatype.
type Object ¶
type Object interface { Term // contains filtered or unexported methods }
Object interface distiguishes which Terms are valid as a Object of a Triple.
type ParseOption ¶
type ParseOption int
A ParseOption allows to customize the behaviour of a decoder.
const ( // Base IRI to resolve relative IRIs against (for formats that support // relative IRIs: Turtle, RDF/XML, TriG, JSON-LD) Base ParseOption = iota )
Options which can configure a decoder.
type Predicate ¶
type Predicate interface { Term // contains filtered or unexported methods }
Predicate interface distiguishes which Terms are valid as a Predicate of a Triple.
type QuadDecoder ¶
type QuadDecoder struct { DefaultGraph Context // default graph // contains filtered or unexported fields }
QuadDecoder parses RDF quads in one of the following formats: N-Quads.
For streaming parsing, use the Decode() method to decode a single Quad at a time. Or, if you want to read the whole source in one go, DecodeAll().
func NewQuadDecoder ¶
func NewQuadDecoder(r io.Reader, f Format) *QuadDecoder
NewQuadDecoder returns a new QuadDecoder capable of parsing quads from the given io.Reader in the given serialization format.
func (*QuadDecoder) Decode ¶
func (d *QuadDecoder) Decode() (Quad, error)
Decode returns the next valid Quad, or an error
func (*QuadDecoder) DecodeAll ¶
func (d *QuadDecoder) DecodeAll() ([]Quad, error)
DecodeAll decodes and returns all Quads from source, or an error
type QuadEncoder ¶
type QuadEncoder struct {
// contains filtered or unexported fields
}
QuadEncoder serializes RDF Quads. Currently only supports N-Quads.
func NewQuadEncoder ¶
func NewQuadEncoder(w io.Writer, f Format) *QuadEncoder
NewQuadEncoder returns a new QuadEncoder on the given writer. The only supported format is NQuads.
func (*QuadEncoder) Close ¶
func (e *QuadEncoder) Close() error
Close closes the encoder and flushes the underlying buffering writer.
func (*QuadEncoder) EncodeAll ¶
func (e *QuadEncoder) EncodeAll(qs []Quad) error
EncodeAll encodes all quads.
type Subject ¶
type Subject interface { Term // contains filtered or unexported methods }
Subject interface distiguishes which Terms are valid as a Subject of a Triple.
type Term ¶
type Term interface { // Serialize returns a string representation of the Term in the specified serialization format. Serialize(Format) string // String returns the term as it is stored, without any modifications. String() string // Type returns the Term type. Type() TermType }
Term represents an RDF term. There are 3 term types: Blank node, Literal and IRI.
type TermType ¶
type TermType int
TermType describes the type of RDF term: Blank node, IRI or Literal
type Triple ¶
Triple represents a RDF triple.
type TripleDecoder ¶
type TripleDecoder interface { // Decode parses a RDF document and return the next valid triple. // It returns io.EOF when the whole document is parsed. Decode() (Triple, error) // DecodeAll parses the entire RDF document and return all valid // triples, or an error. DecodeAll() ([]Triple, error) // SetOption sets a parsing option to the given value. Not all options // are supported by all serialization formats. SetOption(ParseOption, interface{}) error }
TripleDecoder parses RDF documents (serializations of an RDF graph).
For streaming parsing, use the Decode() method to decode a single Triple at a time. Or, if you want to read the whole document in one go, use DecodeAll().
The decoder can be instructed with numerous options. Note that not all options are supported by all formats. Consult the following table:
Option Description Value (default) Format support ------------------------------------------------------------------------------ Base Base IRI IRI (empty IRI) Turtle, RDF/XML Strict Strict mode true/false (true) TODO ErrOut Error output io.Writer (nil) TODO
func NewTripleDecoder ¶
func NewTripleDecoder(r io.Reader, f Format) TripleDecoder
NewTripleDecoder returns a new TripleDecoder capable of parsing triples from the given io.Reader in the given serialization format.
type TripleEncoder ¶
type TripleEncoder struct { Namespaces map[string]string // IRI->prefix custom mappings. OpenStatement bool // True when triple statement hasn't been closed (i.e. in a predicate/object list) GenerateNamespaces bool // True to auto generate namespaces, false if you give it some custom namespaces and do not want generated ones // contains filtered or unexported fields }
TripleEncoder serializes RDF Triples into one of the following formats: N-Triples, Turtle, RDF/XML.
For streaming serialization, use the Encode() method to encode a single Triple at a time. Or, if you want to encode multiple triples in one batch, use EncodeAll(). In either case; when done serializing, Close() must be called, to ensure that all writes are persisted, since the Encoder uses buffered IO.
func NewTripleEncoder ¶
func NewTripleEncoder(w io.Writer, f Format) *TripleEncoder
NewTripleEncoder returns a new TripleEncoder capable of serializing into the given io.Writer in the given serialization format.
func (*TripleEncoder) Close ¶
func (e *TripleEncoder) Close() error
Close finalizes an encoding session, ensuring that any concluding tokens are written should it be needed (eg.g close the root tag for RDF/XML) and flushes the underlying buffered writer of the encoder.
The encoder cannot encode anymore when Close() has been called.
func (*TripleEncoder) Encode ¶
func (e *TripleEncoder) Encode(t Triple) error
Encode serializes a single Triple to the io.Writer of the TripleEncoder.
func (*TripleEncoder) EncodeAll ¶
func (e *TripleEncoder) EncodeAll(ts []Triple) error
EncodeAll serializes a slice of Triples to the io.Writer of the TripleEncoder. It will ignore duplicate triples.
Note that this function will modify the given slice of triples by sorting it in-place.