Exemplo n.º 1
0
// Parse is used to parse RDF data from a reader, using the provided mime type
func (g *Graph) Parse(reader io.Reader, mime string) {
	parserName := mimeParser[mime]
	if len(parserName) == 0 {
		parserName = "guess"
	}
	if parserName == "jsonld" {
		buf := new(bytes.Buffer)
		buf.ReadFrom(reader)
		jsonData, err := jsonld.ReadJSON(buf.Bytes())
		options := &jsonld.Options{}
		options.Base = ""
		options.ProduceGeneralizedRdf = false
		dataSet, err := jsonld.ToRDF(jsonData, options)
		if err != nil {
			log.Println(err)
			return
		}
		for t := range dataSet.IterTriples() {
			g.AddTriple(jterm2term(t.Subject), jterm2term(t.Predicate), jterm2term(t.Object))
		}

	} else {
		parser := crdf.NewParser(parserName)
		parser.SetLogHandler(func(level int, message string) {
			log.Println(message)
		})
		defer parser.Free()
		out := parser.Parse(reader, g.uri)
		for s := range out {
			g.AddStatement(s)
		}
	}
}
Exemplo n.º 2
0
// ParseBase is used to parse RDF data from a reader, using the provided mime type and a base URI
func (g *Graph) ParseBase(reader io.Reader, mime string, baseURI string) {
	if len(baseURI) < 1 {
		baseURI = g.uri
	}
	parserName := mimeParser[mime]
	if len(parserName) == 0 {
		parserName = "guess"
	}
	parser := crdf.NewParser(parserName)
	defer parser.Free()
	out := parser.Parse(reader, baseURI)
	for s := range out {
		g.AddStatement(s)
	}
}