Debian Codegraph @

examples/2-rdfparser/exampleRDFParser.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5 "github.com/spdx/gordf/rdfloader/parser"
6 xmlreader "github.com/spdx/gordf/rdfloader/xmlreader"
7 "os"
8 )
9
10 func main() {
11 // expects user to enter the file name.
12 // sample run :
13 // go run exampleRDFParser.go ../sample-docs/rdf/input.rdf
14
15 // checking if input arguments are ok.
16 if len(os.Args) != 2 {
17 fmt.Printf("Usage: %v <input.rdf>\n", os.Args[0])
18 fmt.Printf("\tTo parse the <input.rdf> file and\n")
19 fmt.Printf("\tPrint some of it's Triples\n")
20 os.Exit(1) // there was an error processing input.
21 }
22
23 filePath := os.Args[1]
24 xmlReader, err := xmlreader.XMLReaderFromFilePath(filePath)
25 if err != nil {
26 // error reading the rdf file.
27 fmt.Printf("error reading the rdf file %v: %v", filePath, err)
28 os.Exit(1)
29 }
30 rootBlock, err := xmlReader.Read()
31 if err != nil {
32 // error parsing the xml content
33 fmt.Printf("error parsing the xml content of the rdf file")
34 os.Exit(1)
35 }
36 rdfParser := parser.New()
37 err = rdfParser.Parse(rootBlock)
38 if err != nil {
39 fmt.Printf("error parsing file: %v\n", err)
40 os.Exit(1)
41 }
42
43 // the max number of triples to display
44 maxNTriples := 10
45 if len(rdfParser.Triples) < maxNTriples {
46 // in case the number of triples is less than the declared value.
47 maxNTriples = len(rdfParser.Triples)
48 }
49
50 i := 0
51 // parser.Triples is a dictionary of the form {triple-hash => triple}
52 for tripleHash := range rdfParser.Triples {
53 if i == maxNTriples {
54 break
55 }
56 i++
57 triple := rdfParser.Triples[tripleHash]
58 fmt.Printf("Triple %v:\n", i)
59 fmt.Println("\tSubject:", triple.Subject)
60 fmt.Println("\tPredicate:", triple.Predicate)
61 fmt.Println("\tObject:", triple.Object)
62 }
63 }