Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gordf #46

Merged
merged 15 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/7-rdfloader/exampleRDFLoader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"github.com/spdx/tools-golang/rdfloader"
"os"
"strings"
)

func getFilePathFromUser() string {
if len(os.Args) == 1 {
// user hasn't specified the rdf file path
panic("kindly provide path of the rdf file to be loaded as a spdx-document while running this file")
}
return os.Args[1]
}

func main() {
// example to use the rdfLoader.
filePath := getFilePathFromUser()
file, err := os.Open(filePath)
if err != nil {
panic(fmt.Errorf("error opening File: %s", err))
}

// loading the spdx-document
doc, err := rdfloader.Load2_2(file)
if err != nil {
fmt.Println(fmt.Errorf("error parsing given spdx document: %s", err))
os.Exit(1)
}

// Printing some of the document Information
fmt.Println(strings.Repeat("=", 80))
fmt.Println("Some Attributes of the Document:")
fmt.Printf("Document Name: %s\n", doc.CreationInfo.DocumentName)
fmt.Printf("DataLicense: %s\n", doc.CreationInfo.DataLicense)
fmt.Printf("Document NameSpace: %s\n", doc.CreationInfo.DocumentNamespace)
fmt.Printf("SPDX Document Version: %s\n", doc.CreationInfo.SPDXVersion)
fmt.Println(strings.Repeat("=", 80))
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/spdx/tools-golang

go 1.13

require github.com/spdx/gordf v0.0.0-20201111095634-7098f93598fb
262 changes: 262 additions & 0 deletions rdfloader/parser2v2/constants.go

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions rdfloader/parser2v2/license_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package parser2v2

import (
"fmt"
gordfParser "github.com/spdx/gordf/rdfloader/parser"
"strings"
)

/* util methods for licenses and checksums below:*/

// Given the license URI, returns the name of the license defined
// in the last part of the uri.
// This function is susceptible to false-positives.
func getLicenseStringFromURI(uri string) string {
licenseEnd := strings.TrimSpace(getLastPartOfURI(uri))
lower := strings.ToLower(licenseEnd)
if lower == "none" || lower == "noassertion" {
return strings.ToUpper(licenseEnd)
}
return licenseEnd
}

// returns the checksum algorithm and it's value
// In the newer versions, these two strings will be bound to a single checksum struct
// whose pointer will be returned.
func (parser *rdfParser2_2) getChecksumFromNode(checksumNode *gordfParser.Node) (algorithm string, value string, err error) {
var checksumValue, checksumAlgorithm string
for _, checksumTriple := range parser.nodeToTriples(checksumNode) {
switch checksumTriple.Predicate.ID {
case RDF_TYPE:
continue
case SPDX_CHECKSUM_VALUE:
// cardinality: exactly 1
checksumValue = strings.TrimSpace(checksumTriple.Object.ID)
case SPDX_ALGORITHM:
// cardinality: exactly 1
checksumAlgorithm, err = getAlgorithmFromURI(checksumTriple.Object.ID)
if err != nil {
return
}
default:
err = fmt.Errorf("unknown predicate '%s' while parsing checksum node", checksumTriple.Predicate.ID)
return
}
}
return checksumAlgorithm, checksumValue, nil
}

func getAlgorithmFromURI(algorithmURI string) (checksumAlgorithm string, err error) {
fragment := getLastPartOfURI(algorithmURI)
if !strings.HasPrefix(fragment, "checksumAlgorithm_") {
return "", fmt.Errorf("checksum algorithm uri must begin with checksumAlgorithm_. found %s", fragment)
}
algorithm := strings.TrimPrefix(fragment, "checksumAlgorithm_")
algorithm = strings.ToLower(strings.TrimSpace(algorithm))
switch algorithm {
case "md2", "md4", "md5", "md6":
checksumAlgorithm = strings.ToUpper(algorithm)
case "sha1", "sha224", "sha256", "sha384", "sha512":
checksumAlgorithm = strings.ToUpper(algorithm)
default:
return "", fmt.Errorf("unknown checksum algorithm %s", algorithm)
}
return
}

// from a list of licenses, it returns a
// list of string representation of those licenses.
func mapLicensesToStrings(licences []AnyLicenseInfo) []string {
res := make([]string, len(licences), len(licences))
for i, lic := range licences {
res[i] = lic.ToLicenseString()
}
return res
}

/****** Type Functions ******/

// TODO: should probably add brackets while linearizing a nested license.
func (lic ConjunctiveLicenseSet) ToLicenseString() string {
return strings.Join(mapLicensesToStrings(lic.members), " AND ")
}

// TODO: should probably add brackets while linearizing a nested license.
func (lic DisjunctiveLicenseSet) ToLicenseString() string {
return strings.Join(mapLicensesToStrings(lic.members), " OR ")
}

func (lic ExtractedLicensingInfo) ToLicenseString() string {
return lic.licenseID
}

func (operator OrLaterOperator) ToLicenseString() string {
return operator.member.ToLicenseString()
}

func (lic License) ToLicenseString() string {
return lic.licenseID
}

func (lic ListedLicense) ToLicenseString() string {
return lic.licenseID
}

func (lic WithExceptionOperator) ToLicenseString() string {
return lic.member.ToLicenseString()
}

func (lic SpecialLicense) ToLicenseString() string {
return string(lic.value)
}

func (lic SimpleLicensingInfo) ToLicenseString() string {
return lic.licenseID
}