File: exampleXmlReader.go

package info (click to toggle)
golang-github-spdx-gordf 0.0~git20221230.b735bd5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: makefile: 4
file content (113 lines) | stat: -rw-r--r-- 3,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: MIT License

// referred the documentation from
// https://github.com/spdx/tools-golang/blob/master/examples/1-load/example_load.go

// Example for: *xmlreader*

// This example demonstrates loading an xml file from the disk into the
// memory and printing some of the tags to validate it's correctness.

package main

import (
	"bufio"
	"fmt"
	"os"
	"reflect"
	"strings"

	reader "github.com/spdx/gordf/rdfloader/xmlreader"
)

func printBlockHeader(block reader.Block) {
	// Prints name and attributes of the block.
	fmt.Printf("Name: <%v:%v>\n", block.OpeningTag.SchemaName, block.OpeningTag.Name)

	fmt.Printf("Tag Attributes:\n")
	for _, attr := range block.OpeningTag.Attrs {
		// schemaName for an attribute is optional
		if attr.SchemaName == "" {
			fmt.Printf("\t%v=\"%v\"\n", attr.Name, attr.Value)
		} else {
			fmt.Printf("\t%v:%v=\"%v\"\n", attr.SchemaName, attr.Name, attr.Value)
		}
	}
}

func main() {
	// expects user to enter the file path along with it's name as an argument
	// while running the file

	// Sample run:
	// go run exampleXmlReader.go ../sample-docs/rdf/input.rdf

	// check if we've received the right number of arguments
	if len(os.Args) != 2 {
		fmt.Printf("Usage: %v <input.rdf>\n", os.Args[0])
		fmt.Printf("\tLoad the <input.rdf> file into memory and\n")
		fmt.Printf("\tPrint some of it's tags.")
		os.Exit(1) // there was an error processing input.
	}

	// filePath indicates path to the file along with the filename.
	filePath := os.Args[1]

	// in xmlReader, we've two options of reading the file.
	// 		1. using the bufio file object
	// 		2. using the file path

	// Method 1: using file objects
	fileHandler, err := os.Open(filePath)
	if err != nil {
		fmt.Printf("error opening %v: %v\n", filePath, err)
		os.Exit(1)
	}
	xmlReader1 := reader.XMLReaderFromFileObject(bufio.NewReader(fileHandler))
	root1, err := xmlReader1.Read()
	if err != nil {
		fmt.Printf("error while parsing %v: %v\n", filePath, err)
		os.Exit(1)
	}
	fileHandler.Close()

	// Method 2: using file paths
	xmlReader2, err := reader.XMLReaderFromFilePath(filePath)
	if err != nil {
		fmt.Printf("error opening %v: %v", filePath, err)
		os.Exit(1)
	}
	root2, err := xmlReader2.Read()
	if err != nil {
		fmt.Printf("error while parsing %v: %v\n", filePath, err)
		os.Exit(1)
	}

	// comparing the results from both the reader instances.
	if !reflect.DeepEqual(root1, root2) {
		fmt.Println("outputs from both approach is not same.")
		fmt.Println("Something wrong with the implementation")
		os.Exit(1)
	}

	// printing the root tag
	fmt.Println(strings.Repeat("#", 80))
	fmt.Println("Root Tag:")
	printBlockHeader(root1)
	fmt.Println(strings.Repeat("#", 80))

	// maximum number of children to print
	maxNChild := 10

	// iterating over the children of the root tag.
	for i, childBlock := range root1.Children {
		if i >= maxNChild {
			break
		}
		fmt.Println()
		fmt.Println(strings.Repeat("=", 80))
		fmt.Printf("Child %v:\n", i+1)
		printBlockHeader(*childBlock)
		fmt.Println(strings.Repeat("=", 80))
	}
}