aboutsummaryrefslogtreecommitdiff
path: root/rdfloader/parser2v2/parse_license.go
diff options
context:
space:
mode:
Diffstat (limited to 'rdfloader/parser2v2/parse_license.go')
-rw-r--r--rdfloader/parser2v2/parse_license.go146
1 files changed, 40 insertions, 106 deletions
diff --git a/rdfloader/parser2v2/parse_license.go b/rdfloader/parser2v2/parse_license.go
index 6bab096..4ebdccf 100644
--- a/rdfloader/parser2v2/parse_license.go
+++ b/rdfloader/parser2v2/parse_license.go
@@ -9,6 +9,12 @@ import (
"strings"
)
+// AnyLicense is a baseClass for all the licenses
+// All the types of licenses is a sub-type of AnyLicense,
+// either directly or indirectly.
+// This function acts as a mux for all the licenses. Based on the input, it
+// decides which type of license it is and passes control to that type of
+// license parser to parse the given input.
func (parser *rdfParser2_2) getAnyLicenseFromNode(node *gordfParser.Node) (AnyLicenseInfo, error) {
associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil)
if len(associatedTriples) == 0 {
@@ -43,6 +49,8 @@ func (parser *rdfParser2_2) getLicenseExceptionFromNode(node *gordfParser.Node)
for _, triple := range associatedTriples {
value := triple.Object.ID
switch triple.Predicate.ID {
+ case RDF_TYPE:
+ continue
case SPDX_LICENSE_EXCEPTION_ID:
exception.licenseExceptionId = value
case SPDX_LICENSE_EXCEPTION_TEXT:
@@ -56,6 +64,8 @@ func (parser *rdfParser2_2) getLicenseExceptionFromNode(node *gordfParser.Node)
exception.name = value
case SPDX_EXAMPLE:
exception.example = value
+ case RDFS_COMMENT:
+ exception.comment = value
default:
return exception, fmt.Errorf("invalid predicate(%s) for LicenseException", triple.Predicate)
}
@@ -70,16 +80,27 @@ func (parser *rdfParser2_2) getSimpleLicensingInfoFromNode(node *gordfParser.Nod
func (parser *rdfParser2_2) getWithExceptionOperatorFromNode(node *gordfParser.Node) (operator WithExceptionOperator, err error) {
associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil)
+ var memberFound bool
for _, triple := range associatedTriples {
switch triple.Predicate.ID {
+ case RDF_TYPE:
+ continue
case SPDX_MEMBER:
+ if memberFound {
+ return operator,
+ fmt.Errorf("more than one member found in the WithExceptionOperator (expected only 1)")
+ }
+ memberFound = true
member, err := parser.getSimpleLicensingInfoFromNode(triple.Object)
if err != nil {
return operator, fmt.Errorf("error parsing member of a WithExceptionOperator: %v", err)
}
- operator.license = member
+ operator.member = member
case SPDX_LICENSE_EXCEPTION:
operator.licenseException, err = parser.getLicenseExceptionFromNode(triple.Object)
+ if err != nil {
+ return operator, fmt.Errorf("error parsing licenseException of WithExceptionOperator: %v", err)
+ }
default:
return operator, fmt.Errorf("unknown predicate (%s) for a WithExceptionOperator", triple.Predicate.ID)
}
@@ -94,14 +115,24 @@ func (parser *rdfParser2_2) getOrLaterOperatorFromNode(node *gordfParser.Node) (
return operator, fmt.Errorf("orLaterOperator must be associated with exactly one tag. found %v triples", n-1)
}
for _, triple := range associatedTriples {
- operator.license, err = parser.getSimpleLicensingInfoFromNode(triple.Object)
- if err != nil {
- return operator, fmt.Errorf("error parsing simpleLicensingInfo of OrLaterOperator: %v", err)
+ switch triple.Predicate.ID {
+ case RDF_TYPE:
+ continue
+ case SPDX_MEMBER:
+ operator.member, err = parser.getSimpleLicensingInfoFromNode(triple.Object)
+ if err != nil {
+ return operator, fmt.Errorf("error parsing simpleLicensingInfo of OrLaterOperator: %v", err)
+ }
+ default:
+ return operator, fmt.Errorf("unknown predicate %s", triple.Predicate.ID)
}
}
return operator, nil
}
+// SpecialLicense is a type of license which is not defined in any of the
+// spdx documents, it is a type of license defined for the sake of brevity.
+// It can be [NONE|NOASSERTION|LicenseRef-<string>]
func (parser *rdfParser2_2) getSpecialLicenseFromNode(node *gordfParser.Node) (lic SpecialLicense, err error) {
uri := strings.TrimSpace(node.ID)
switch uri {
@@ -161,6 +192,8 @@ func (parser *rdfParser2_2) getConjunctiveLicenseSetFromNode(node *gordfParser.N
return licenseSet, fmt.Errorf("error parsing conjunctive license set: %v", err)
}
licenseSet.members = append(licenseSet.members, member)
+ default:
+ return licenseSet, fmt.Errorf("unknown subTag for ConjunctiveLicenseSet: %s", triple.Predicate.ID)
}
}
return licenseSet, nil
@@ -176,6 +209,9 @@ func (parser *rdfParser2_2) getSimpleLicensingInfoFromTriples(triples []*gordfPa
case SPDX_NAME:
lic.name = triple.Object.ID
case RDFS_SEE_ALSO:
+ if !isUriValid(triple.Object.ID) {
+ return lic, fmt.Errorf("%s is not a valid uri for seeAlso attribute of a License", triple.Object.ID)
+ }
lic.seeAlso = append(lic.seeAlso, triple.Object.ID)
case SPDX_EXAMPLE:
lic.example = triple.Object.ID
@@ -207,12 +243,6 @@ func (parser *rdfParser2_2) getLicenseFromNode(node *gordfParser.Node) (lic Lice
lic.standardLicenseTemplate = value
case SPDX_STANDARD_LICENSE_HEADER_TEMPLATE:
lic.standardLicenseHeaderTemplate = value
- case RDFS_SEE_ALSO:
- if !isUriValid(value) {
- return lic, fmt.Errorf("%s is not a valid uri for seeAlso attribute of a License", value)
- }
- lic.seeAlso = value
-
case SPDX_IS_DEPRECATED_LICENSE_ID:
lic.isDeprecatedLicenseID, err = boolFromString(value)
if err != nil {
@@ -233,99 +263,3 @@ func (parser *rdfParser2_2) getLicenseFromNode(node *gordfParser.Node) (lic Lice
}
return lic, nil
}
-
-/* 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 = parser.getAlgorithmFromURI(checksumTriple.Object.ID)
- if err != nil {
- return
- }
- }
- }
- return checksumAlgorithm, checksumValue, nil
-}
-
-func (parser *rdfParser2_2) 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
-}
-
-func mapLicensesToStrings(licences []AnyLicenseInfo) []string {
- res := make([]string, len(licences), len(licences))
- for i, lic := range licences {
- res[i] = lic.ToLicenseString()
- }
- return res
-}
-
-func (lic ConjunctiveLicenseSet) ToLicenseString() string {
- return strings.Join(mapLicensesToStrings(lic.members), " AND ")
-}
-
-func (lic DisjunctiveLicenseSet) ToLicenseString() string {
- return strings.Join(mapLicensesToStrings(lic.members), " OR ")
-}
-
-/****** Type Functions ******/
-func (lic ExtractedLicensingInfo) ToLicenseString() string {
- return lic.licenseID
-}
-
-func (operator OrLaterOperator) ToLicenseString() string {
- return operator.license.ToLicenseString()
-}
-
-func (lic License) ToLicenseString() string {
- return lic.licenseID
-}
-
-func (lic ListedLicense) ToLicenseString() string {
- return lic.licenseID
-}
-
-func (lic WithExceptionOperator) ToLicenseString() string {
- return lic.license.ToLicenseString()
-}
-
-func (lic SpecialLicense) ToLicenseString() string {
- return string(lic.value)
-}