diff options
author | Steve Winslow <steve@swinslow.net> | 2020-05-09 18:24:20 -0400 |
---|---|---|
committer | Steve Winslow <steve@swinslow.net> | 2020-05-09 18:24:20 -0400 |
commit | 1500a6e983917e450f8da95b86418d8db3af926f (patch) | |
tree | d10dd12102f2f11e115b726865fda86ea27982e0 /tvloader/parser2v1/util.go | |
parent | 4a8bcf1883c3547f2efb6af5a2f002293117c45b (diff) | |
download | spdx-tools-1500a6e983917e450f8da95b86418d8db3af926f.tar.gz |
Refactor parser to handle element ID maps
Signed-off-by: Steve Winslow <steve@swinslow.net>
Diffstat (limited to 'tvloader/parser2v1/util.go')
-rw-r--r-- | tvloader/parser2v1/util.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tvloader/parser2v1/util.go b/tvloader/parser2v1/util.go index ed99063..d2df57b 100644 --- a/tvloader/parser2v1/util.go +++ b/tvloader/parser2v1/util.go @@ -5,6 +5,8 @@ package parser2v1 import ( "fmt" "strings" + + "github.com/spdx/tools-golang/spdx" ) // used to extract key / value from embedded substrings @@ -21,3 +23,76 @@ func extractSubs(value string) (string, string, error) { return subkey, subvalue, nil } + +// used to extract DocumentRef and SPDXRef values from an SPDX Identifier +// which can point either to this document or to a different one +func extractDocElementID(value string) (spdx.DocElementID, error) { + docRefID := "" + idStr := value + + // check prefix to see if it's a DocumentRef ID + if strings.HasPrefix(idStr, "DocumentRef-") { + // extract the part that comes between "DocumentRef-" and ":" + strs := strings.Split(idStr, ":") + // should be exactly two, part before and part after + if len(strs) < 2 { + return spdx.DocElementID{}, fmt.Errorf("no colon found although DocumentRef- prefix present") + } + if len(strs) > 2 { + return spdx.DocElementID{}, fmt.Errorf("more than one colon found") + } + + // trim the prefix and confirm non-empty + docRefID = strings.TrimPrefix(strs[0], "DocumentRef-") + if docRefID == "" { + return spdx.DocElementID{}, fmt.Errorf("document identifier has nothing after prefix") + } + // and use remainder for element ID parsing + idStr = strs[1] + } + + // check prefix to confirm it's got the right prefix for element IDs + if !strings.HasPrefix(idStr, "SPDXRef-") { + return spdx.DocElementID{}, fmt.Errorf("missing SPDXRef- prefix for element identifier") + } + + // make sure no colons are present + if strings.Contains(idStr, ":") { + // we know this means there was no DocumentRef- prefix, because + // we would have handled multiple colons above if it was + return spdx.DocElementID{}, fmt.Errorf("invalid colon in element identifier") + } + + // trim the prefix and confirm non-empty + eltRefID := strings.TrimPrefix(idStr, "SPDXRef-") + if eltRefID == "" { + return spdx.DocElementID{}, fmt.Errorf("element identifier has nothing after prefix") + } + + // we're good + return spdx.DocElementID{DocumentRefID: docRefID, ElementRefID: spdx.ElementID(eltRefID)}, nil +} + +// used to extract SPDXRef values only from an SPDX Identifier which can point +// to this document only. Use extractDocElementID for parsing IDs that can +// refer either to this document or a different one. +func extractElementID(value string) (spdx.ElementID, error) { + // check prefix to confirm it's got the right prefix for element IDs + if !strings.HasPrefix(value, "SPDXRef-") { + return spdx.ElementID(""), fmt.Errorf("missing SPDXRef- prefix for element identifier") + } + + // make sure no colons are present + if strings.Contains(value, ":") { + return spdx.ElementID(""), fmt.Errorf("invalid colon in element identifier") + } + + // trim the prefix and confirm non-empty + eltRefID := strings.TrimPrefix(value, "SPDXRef-") + if eltRefID == "" { + return spdx.ElementID(""), fmt.Errorf("element identifier has nothing after prefix") + } + + // we're good + return spdx.ElementID(eltRefID), nil +} |