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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// 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/RishabhBhatnagar/gordf/rdfwriter"
"github.com/spdx/tools-golang/spdx"
"strings"
)
// parsing the relationship that exists in the rdf document.
// Relationship is of type RefA relationType RefB.
// parsing the relationship appends the relationship to the current document's
// Relationships Slice.
func (parser *rdfParser2_2) parseRelationship(triple *gordfParser.Triple) (err error) {
reln := spdx.Relationship2_2{}
reln.RefA, err = getReferenceFromURI(triple.Subject.ID)
if err != nil {
return err
}
for _, subTriple := range parser.nodeToTriples(triple.Object) {
switch subTriple.Predicate.ID {
case SPDX_RELATIONSHIP_TYPE:
// cardinality: exactly 1
reln.Relationship, err = getRelationshipType(subTriple.Object.ID)
case RDF_TYPE:
// cardinality: exactly 1
continue
case SPDX_RELATED_SPDX_ELEMENT:
// cardinality: exactly 1
// assumes: spdx-element is a uri
reln.RefB, err = getReferenceFromURI(subTriple.Object.ID)
if err != nil {
return err
}
relatedSpdxElementTriples := parser.nodeToTriples(subTriple.Object)
if len(relatedSpdxElementTriples) == 0 {
continue
}
typeTriples := rdfwriter.FilterTriples(relatedSpdxElementTriples, &subTriple.Object.ID, &RDF_TYPE, nil)
if len(typeTriples) != 1 {
return fmt.Errorf("expected %s to have exactly one rdf:type triple. found %d triples", subTriple.Object, len(typeTriples))
}
err = parser.parseRelatedElementFromTriple(reln, typeTriples[0])
if err != nil {
return err
}
case RDFS_COMMENT:
// cardinality: max 1
reln.RelationshipComment = subTriple.Object.ID
default:
return fmt.Errorf("unexpected predicate id: %s", subTriple.Predicate.ID)
}
}
parser.doc.Relationships = append(parser.doc.Relationships, &reln)
return nil
}
func (parser *rdfParser2_2) parseRelatedElementFromTriple(reln spdx.Relationship2_2, triple *gordfParser.Triple) error {
// iterate over relatedElement Type and check which SpdxElement it is.
var err error
switch triple.Object.ID {
case SPDX_FILE:
file, err := parser.getFileFromNode(triple.Subject)
if err != nil {
return fmt.Errorf("error setting a package: %v", err)
}
reln.RefB, err = ExtractDocElementID(getLastPartOfURI(triple.Subject.ID))
if err != nil {
return err
}
parser.files[file.FileSPDXIdentifier] = file
case SPDX_PACKAGE:
pkg, err := parser.getPackageFromNode(triple.Subject)
if err != nil {
return fmt.Errorf("error setting a package inside a relationship: %v", err)
}
reln.RefB, err = ExtractDocElementID(getLastPartOfURI(triple.Subject.ID))
if err != nil {
return err
}
parser.packages[pkg.PackageSPDXIdentifier] = pkg
case SPDX_SPDX_ELEMENT:
// it shouldn't be associated with any other triple.
// it must be a uri reference.
reln.RefB, err = ExtractDocElementID(getLastPartOfURI(triple.Subject.ID))
if err != nil {
return err
}
default:
return fmt.Errorf("undefined relatedElement %s found while parsing relationship", triple.Object.ID)
}
return nil
}
func getReferenceFromURI(uri string) (spdx.DocElementID, error) {
fragment := getLastPartOfURI(uri)
switch strings.ToLower(strings.TrimSpace(fragment)) {
case "noassertion", "none":
return spdx.DocElementID{
DocumentRefID: "",
ElementRefID: spdx.ElementID(strings.ToUpper(fragment)),
}, nil
}
return ExtractDocElementID(fragment)
}
func getRelationshipType(relnType string) (string, error) {
relnType = strings.TrimSpace(relnType)
if !strings.HasPrefix(relnType, PREFIX_RELATIONSHIP_TYPE) {
return "", fmt.Errorf("relationshipType must start with %s. found %s", PREFIX_RELATIONSHIP_TYPE, relnType)
}
relnType = strings.TrimPrefix(relnType, PREFIX_RELATIONSHIP_TYPE)
relnType = strings.TrimSpace(relnType)
for _, validRelationshipType := range AllRelationshipTypes() {
if relnType == validRelationshipType {
return relnType, nil
}
}
return "", fmt.Errorf("unknown relationshipType: %s", relnType)
}
|