Debian Codegraph @

examples/1-xmlread/exampleXmlReader.go

raw symbols SCIP
1 // SPDX-License-Identifier: MIT License
2
3 // referred the documentation from
4 // https://github.com/spdx/tools-golang/blob/master/examples/1-load/example_load.go
5
6 // Example for: *xmlreader*
7
8 // This example demonstrates loading an xml file from the disk into the
9 // memory and printing some of the tags to validate it's correctness.
10
11 package main
12
13 import (
14 "bufio"
15 "fmt"
16 "os"
17 "reflect"
18 "strings"
19
20 reader "github.com/spdx/gordf/rdfloader/xmlreader"
21 )
22
23 func printBlockHeader(block reader.Block) {
24 // Prints name and attributes of the block.
25 fmt.Printf("Name: <%v:%v>\n", block.OpeningTag.SchemaName, block.OpeningTag.Name)
26
27 fmt.Printf("Tag Attributes:\n")
28 for _, attr := range block.OpeningTag.Attrs {
29 // schemaName for an attribute is optional
30 if attr.SchemaName == "" {
31 fmt.Printf("\t%v=\"%v\"\n", attr.Name, attr.Value)
32 } else {
33 fmt.Printf("\t%v:%v=\"%v\"\n", attr.SchemaName, attr.Name, attr.Value)
34 }
35 }
36 }
37
38 func main() {
39 // expects user to enter the file path along with it's name as an argument
40 // while running the file
41
42 // Sample run:
43 // go run exampleXmlReader.go ../sample-docs/rdf/input.rdf
44
45 // check if we've received the right number of arguments
46 if len(os.Args) != 2 {
47 fmt.Printf("Usage: %v <input.rdf>\n", os.Args[0])
48 fmt.Printf("\tLoad the <input.rdf> file into memory and\n")
49 fmt.Printf("\tPrint some of it's tags.")
50 os.Exit(1) // there was an error processing input.
51 }
52
53 // filePath indicates path to the file along with the filename.
54 filePath := os.Args[1]
55
56 // in xmlReader, we've two options of reading the file.
57 // 1. using the bufio file object
58 // 2. using the file path
59
60 // Method 1: using file objects
61 fileHandler, err := os.Open(filePath)
62 if err != nil {
63 fmt.Printf("error opening %v: %v\n", filePath, err)
64 os.Exit(1)
65 }
66 xmlReader1 := reader.XMLReaderFromFileObject(bufio.NewReader(fileHandler))
67 root1, err := xmlReader1.Read()
68 if err != nil {
69 fmt.Printf("error while parsing %v: %v\n", filePath, err)
70 os.Exit(1)
71 }
72 fileHandler.Close()
73
74 // Method 2: using file paths
75 xmlReader2, err := reader.XMLReaderFromFilePath(filePath)
76 if err != nil {
77 fmt.Printf("error opening %v: %v", filePath, err)
78 os.Exit(1)
79 }
80 root2, err := xmlReader2.Read()
81 if err != nil {
82 fmt.Printf("error while parsing %v: %v\n", filePath, err)
83 os.Exit(1)
84 }
85
86 // comparing the results from both the reader instances.
87 if !reflect.DeepEqual(root1, root2) {
88 fmt.Println("outputs from both approach is not same.")
89 fmt.Println("Something wrong with the implementation")
90 os.Exit(1)
91 }
92
93 // printing the root tag
94 fmt.Println(strings.Repeat("#", 80))
95 fmt.Println("Root Tag:")
96 printBlockHeader(root1)
97 fmt.Println(strings.Repeat("#", 80))
98
99 // maximum number of children to print
100 maxNChild := 10
101
102 // iterating over the children of the root tag.
103 for i, childBlock := range root1.Children {
104 if i >= maxNChild {
105 break
106 }
107 fmt.Println()
108 fmt.Println(strings.Repeat("=", 80))
109 fmt.Printf("Child %v:\n", i+1)
110 printBlockHeader(*childBlock)
111 fmt.Println(strings.Repeat("=", 80))
112 }
113 }