| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
filePath := os.Args[1] |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 94 |
fmt.Println(strings.Repeat("#", 80)) |
| 95 |
fmt.Println("Root Tag:") |
| 96 |
printBlockHeader(root1) |
| 97 |
fmt.Println(strings.Repeat("#", 80)) |
| 98 |
|
| 99 |
|
| 100 |
maxNChild := 10 |
| 101 |
|
| 102 |
|
| 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 |
} |