예제 #1
0
파일: rdf2rdf.go 프로젝트: postfix/rdf2rdf
func tripleToTriple(inFile io.Reader, outFile io.Writer, inFormat, outFormat rdf.Format, stream bool) int {
	dec := rdf.NewTripleDecoder(inFile, inFormat)
	// TODO set base to file name?
	enc := rdf.NewTripleEncoder(outFile, outFormat)

	i := 0
	if stream {
		for t, err := dec.Decode(); err != io.EOF; t, err = dec.Decode() {
			if err != nil {
				log.Fatal(err)
			}
			err = enc.Encode(t)
			if err != nil {
				log.Fatal(err)
			}
			i++
		}
	} else {
		tr, err := dec.DecodeAll()
		if err != nil {
			log.Fatal(err)
		}
		err = enc.EncodeAll(tr)
		if err != nil {
			log.Fatal(err)
		}
		i = len(tr)
	}
	err := enc.Close()
	if err != nil {
		log.Fatal(err)
	}
	return i
}
예제 #2
0
파일: repo.go 프로젝트: knakk/sparql
// Construct performs a SPARQL HTTP request to the Repo, and returns the
// result triples.
func (r *Repo) Construct(q string) ([]rdf.Triple, error) {
	form := url.Values{}
	form.Set("query", q)
	form.Set("format", "text/turtle")
	b := form.Encode()

	req, err := http.NewRequest(
		"POST",
		r.endpoint,
		bytes.NewBufferString(b))
	if err != nil {
		return nil, err
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Content-Length", strconv.Itoa(len(b)))
	req.Header.Set("Accept", "text/turtle")

	resp, err := r.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		b, err := ioutil.ReadAll(resp.Body)
		var msg string
		if err != nil {
			msg = "Failed to read response body"
		} else {
			if strings.TrimSpace(string(b)) != "" {
				msg = "Response body: \n" + string(b)
			}
		}
		return nil, fmt.Errorf("Construct: SPARQL request failed: %s. "+msg, resp.Status)
	}
	dec := rdf.NewTripleDecoder(resp.Body, rdf.Turtle)
	return dec.DecodeAll()
}