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
|
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
package parser2v2
import (
"errors"
"fmt"
gordfParser "github.com/spdx/gordf/rdfloader/parser"
"github.com/spdx/tools-golang/spdx"
)
// creates a new instance of annotation and sets the annotation attributes
// associated with the given node.
// The newly created annotation is appended to the doc.
func (parser *rdfParser2_2) parseAnnotationFromNode(node *gordfParser.Node) (err error) {
ann := &spdx.Annotation2_2{}
for _, subTriple := range parser.nodeToTriples(node) {
switch subTriple.Predicate.ID {
case SPDX_ANNOTATOR:
// cardinality: exactly 1
err = setAnnotatorFromString(subTriple.Object.ID, ann)
case SPDX_ANNOTATION_DATE:
// cardinality: exactly 1
ann.AnnotationDate = subTriple.Object.ID
case RDFS_COMMENT:
// cardinality: exactly 1
ann.AnnotationComment = subTriple.Object.ID
case SPDX_ANNOTATION_TYPE:
// cardinality: exactly 1
err = setAnnotationType(subTriple.Object.ID, ann)
case RDF_TYPE:
// cardinality: exactly 1
continue
default:
err = fmt.Errorf("unknown predicate %s while parsing annotation", subTriple.Predicate.ID)
}
if err != nil {
return err
}
}
return setAnnotationToParser(parser, ann)
}
func setAnnotationToParser(parser *rdfParser2_2, annotation *spdx.Annotation2_2) error {
if parser.doc == nil {
return errors.New("uninitialized spdx document")
}
if parser.doc.Annotations == nil {
parser.doc.Annotations = []*spdx.Annotation2_2{}
}
parser.doc.Annotations = append(parser.doc.Annotations, annotation)
return nil
}
// annotator is of type [Person|Organization|Tool]:String
func setAnnotatorFromString(annotatorString string, ann *spdx.Annotation2_2) error {
subkey, subvalue, err := ExtractSubs(annotatorString, ":")
if err != nil {
return err
}
if subkey == "Person" || subkey == "Organization" || subkey == "Tool" {
ann.AnnotatorType = subkey
ann.Annotator = subvalue
return nil
}
return fmt.Errorf("unrecognized Annotator type %v while parsing annotation", subkey)
}
// it can be NS_SPDX+annotationType_[review|other]
func setAnnotationType(annType string, ann *spdx.Annotation2_2) error {
switch annType {
case SPDX_ANNOTATION_TYPE_OTHER:
ann.AnnotationType = "OTHER"
case SPDX_ANNOTATION_TYPE_REVIEW:
ann.AnnotationType = "REVIEW"
default:
return fmt.Errorf("unknown annotation type %s", annType)
}
return nil
}
|