xref: /aosp_15_r20/external/spdx-tools/rdfloader/parser2v2/parse_creation_info.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3package parser2v2
4
5import (
6	"fmt"
7
8	gordfParser "github.com/spdx/gordf/rdfloader/parser"
9	"github.com/spdx/tools-golang/spdx/common"
10	"github.com/spdx/tools-golang/spdx/v2_2"
11)
12
13// Cardinality: Mandatory, one.
14func (parser *rdfParser2_2) parseCreationInfoFromNode(ci *v2_2.CreationInfo, node *gordfParser.Node) error {
15	for _, triple := range parser.nodeToTriples(node) {
16		switch triple.Predicate.ID {
17		case SPDX_LICENSE_LIST_VERSION: // 2.7
18			// cardinality: max 1
19			ci.LicenseListVersion = triple.Object.ID
20		case SPDX_CREATOR: // 2.8
21			// cardinality: min 1
22			err := setCreator(triple.Object.ID, ci)
23			if err != nil {
24				return err
25			}
26		case SPDX_CREATED: // 2.9
27			// cardinality: exactly 1
28			ci.Created = triple.Object.ID
29		case RDFS_COMMENT: // 2.10
30			ci.CreatorComment = triple.Object.ID
31		case RDF_TYPE:
32			continue
33		default:
34			return fmt.Errorf("unknown predicate %v while parsing a creation info", triple.Predicate)
35		}
36	}
37	return nil
38}
39
40func setCreator(creatorStr string, ci *v2_2.CreationInfo) error {
41	entityType, entity, err := ExtractSubs(creatorStr, ":")
42	if err != nil {
43		return fmt.Errorf("error setting creator of a creation info: %s", err)
44	}
45
46	creator := common.Creator{Creator: entity}
47
48	switch entityType {
49	case "Person", "Organization", "Tool":
50		creator.CreatorType = entityType
51	default:
52		return fmt.Errorf("unknown creatorType %v in a creation info", entityType)
53	}
54
55	ci.Creators = append(ci.Creators, creator)
56
57	return nil
58}
59