aboutsummaryrefslogtreecommitdiff
path: root/rdfloader/parser2v2/parse_other_license_info.go
blob: dd1c19335508ca483de3fe52072163b6bff91695 (plain)
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
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package parser2v2

import (
	"fmt"
	gordfParser "github.com/RishabhBhatnagar/gordf/rdfloader/parser"
	"github.com/spdx/tools-golang/spdx"
	"strings"
)

func (parser *rdfParser2_2) getExternalLicensingInfoFromNode(node *gordfParser.Node) (*spdx.OtherLicense2_2, error) {
	lic := &spdx.OtherLicense2_2{}
	licensePrefix := "LicenseRef-"
	for _, triple := range parser.nodeToTriples[node.String()] {
		switch triple.Predicate.ID {
		case RDF_TYPE:
			continue
		case SPDX_LICENSE_ID:
			fragment := strings.TrimSpace(getLastPartOfURI(triple.Subject.ID))
			if !strings.HasPrefix(fragment, licensePrefix) {
				return nil, fmt.Errorf("license ID must be of type \"LicenseRef-[idstring]\"; found %s", fragment)
			}
			lic.LicenseIdentifier = strings.TrimSuffix(fragment, licensePrefix)
		case SPDX_EXTRACTED_TEXT:
			lic.ExtractedText = triple.Object.ID
		case SPDX_NAME:
			lic.LicenseName = triple.Object.ID
		case RDFS_SEE_ALSO:
			lic.LicenseCrossReferences = append(lic.LicenseCrossReferences, triple.Object.ID)
		case RDFS_COMMENT:
			lic.LicenseComment = triple.Object.ID
		default:
			return nil, fmt.Errorf("unknown predicate %v while parsing extractedLicensingInfo", triple.Predicate)
		}
	}
	return lic, nil
}

// parses the other license and appends it to the doc if no error is encountered.
func (parser *rdfParser2_2) parseOtherLicenseFromNode(node *gordfParser.Node) error {
	ol := &spdx.OtherLicense2_2{}
	ol.LicenseIdentifier = getLicenseStringFromURI(node.ID) // 6.1
	for _, triple := range parser.nodeToTriples[node.String()] {
		switch triple.Predicate.ID {
		case RDF_TYPE:
			continue
		case SPDX_EXTRACTED_TEXT: // 6.2
			ol.ExtractedText = triple.Object.ID
		case SPDX_NAME: // 6.3
			ol.LicenseName = triple.Object.ID
		case RDFS_SEE_ALSO: // 6.4
			ol.LicenseCrossReferences = append(ol.LicenseCrossReferences, triple.Object.ID)
		case RDFS_COMMENT: // 6.5
			ol.LicenseComment = triple.Object.ID
		case SPDX_LICENSE_ID:
			// override licenseId from the rdf:about tag.
			ol.LicenseIdentifier = getLicenseStringFromURI(triple.Object.ID)
		default:
			return fmt.Errorf("unknown predicate (%s) while parsing other license", triple.Predicate.ID)
		}
	}

	parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, ol)
	return nil
}