Debian Codegraph @

examples/4-rdfwriter/exampleRDFWriter.go

raw symbols SCIP
1 package main
2
3 import (
4 "bufio"
5 "bytes"
6 "fmt"
7 "github.com/spdx/gordf/rdfloader/parser"
8 xmlreader "github.com/spdx/gordf/rdfloader/xmlreader"
9 "github.com/spdx/gordf/rdfwriter"
10 "io"
11 "os"
12 "strings"
13 )
14
15 func xmlreaderFromString(fileContent string) xmlreader.XMLReader {
16 return xmlreader.XMLReaderFromFileObject(bufio.NewReader(io.Reader(bytes.NewReader([]byte(fileContent)))))
17 }
18
19 func main() {
20 testString := `
21 <?xml version="1.0"?>
22 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
23 xmlns:dc="http://purl.org/dc/elements/1.1/">
24 <rdf:Description rdf:about="http://www.w3.org/">
25 <dc:title>World Wide Web Consortium</dc:title>
26 </rdf:Description>
27 </rdf:RDF>
28 `
29
30 // in the real world, this will be replaced with
31 xmlReader := xmlreaderFromString(testString)
32
33 // parsing the underlying xml structure of rdf file.
34 rootBlock, err := xmlReader.Read()
35 if err != nil {
36 fmt.Printf("XML reading error: %v\n", err)
37 os.Exit(1)
38 }
39
40 // creating a new parser object
41 rdfParser := parser.New()
42
43 // sets rdf triples from the xml elements from the xmlreader
44 rdfParser.Parse(rootBlock)
45
46 // Example 1:
47 // Getting string of all the triples
48 tab := " "
49 opString, err := rdfwriter.TriplesToString(rdfParser.Triples, rdfParser.SchemaDefinition, tab)
50 if err != nil {
51 fmt.Printf("error in a valid example: %v\n", err)
52 os.Exit(1)
53 }
54 asterisks := strings.Repeat("*", 33)
55 fmt.Println(asterisks, "OUTPUT String", asterisks)
56 fmt.Println(opString)
57
58 // Example 2: writing rdf-triples to a file.
59 var b bytes.Buffer
60
61 // the output will be written to the buffer.
62 err = rdfwriter.WriteToFile(&b, rdfParser.Triples, rdfParser.SchemaDefinition, tab)
63 if err != nil {
64 fmt.Printf("write to file error: %v\n", err)
65 os.Exit(1)
66 }
67 }