diff options
author | RishabhBhatnagar <bhatnagarrishabh4@gmail.com> | 2020-08-20 22:34:10 +0530 |
---|---|---|
committer | RishabhBhatnagar <bhatnagarrishabh4@gmail.com> | 2020-08-20 22:34:10 +0530 |
commit | d8eb2d8eb65d7079b14f4cba9be301e5c0fa9f28 (patch) | |
tree | 73a691697e17d34435a099e51bbea9097c79f7d6 /rdfloader | |
parent | f6091982769ca08e7604a1be79afdb8902717114 (diff) | |
download | spdx-tools-d8eb2d8eb65d7079b14f4cba9be301e5c0fa9f28.tar.gz |
Add Support For License
- The update allows the model to handle all types of licenses.
- Removed all methods identified as unused.
Signed-off-by: RishabhBhatnagar <bhatnagarrishabh4@gmail.com>
Diffstat (limited to 'rdfloader')
-rw-r--r-- | rdfloader/parser2v2/constants.go | 37 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_annotation.go | 2 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_creation_info.go | 2 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_file.go | 10 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_license.go | 309 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_other_license_info.go | 65 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_package.go | 18 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_relationship.go | 4 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_review.go | 2 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_snippet_info.go | 8 | ||||
-rw-r--r-- | rdfloader/parser2v2/parse_spdx_document.go | 19 | ||||
-rw-r--r-- | rdfloader/parser2v2/parser.go | 196 | ||||
-rw-r--r-- | rdfloader/parser2v2/types.go | 83 | ||||
-rw-r--r-- | rdfloader/parser2v2/utils.go | 56 |
14 files changed, 462 insertions, 349 deletions
diff --git a/rdfloader/parser2v2/constants.go b/rdfloader/parser2v2/constants.go index fcab50d..27a807c 100644 --- a/rdfloader/parser2v2/constants.go +++ b/rdfloader/parser2v2/constants.go @@ -55,8 +55,6 @@ var ( SPDX_SUPPLIER = NS_SPDX + "supplier" SPDX_ORIGINATOR = NS_SPDX + "originator" SPDX_DOWNLOAD_LOCATION = NS_SPDX + "downloadLocation" - SPDX_NOASSERTION = NS_SPDX + "noassertion" - SPDX_NONE = NS_SPDX + "none" SPDX_FILES_ANALYZED = NS_SPDX + "filesAnalyzed" SPDX_PACKAGE_VERIFICATION_CODE = NS_SPDX + "packageVerificationCode" SPDX_SOURCE_INFO = NS_SPDX + "sourceInfo" @@ -84,14 +82,33 @@ var ( SPDX_REFERENCE_CATEGORY_SECURITY = NS_SPDX + "referenceCategory_security" SPDX_REFERENCE_CATEGORY_OTHER = NS_SPDX + "referenceCategory_other" - SPDX_REFERENCE_TYPE = NS_SPDX + "referenceType" - SPDX_REFERENCE_LOCATOR = NS_SPDX + "referenceLocator" - SPDX_ANNOTATION_DATE = NS_SPDX + "annotationDate" - SPDX_ANNOTATOR = NS_SPDX + "annotator" - SPDX_MEMBER = NS_SPDX + "member" - SPDX_DISJUNCTIVE_LICENSE_SET = NS_SPDX + "DisjunctiveLicenseSet" - SPDX_CONJUNCTIVE_LICENSE_SET = NS_SPDX + "ConjunctiveLicenseSet" - SPDX_EXTRACTED_LICENSING_INFO = NS_SPDX + "ExtractedLicensingInfo" + SPDX_REFERENCE_TYPE = NS_SPDX + "referenceType" + SPDX_REFERENCE_LOCATOR = NS_SPDX + "referenceLocator" + SPDX_ANNOTATION_DATE = NS_SPDX + "annotationDate" + SPDX_ANNOTATOR = NS_SPDX + "annotator" + SPDX_MEMBER = NS_SPDX + "member" + SPDX_DISJUNCTIVE_LICENSE_SET = NS_SPDX + "DisjunctiveLicenseSet" + SPDX_CONJUNCTIVE_LICENSE_SET = NS_SPDX + "ConjunctiveLicenseSet" + SPDX_EXTRACTED_LICENSING_INFO = NS_SPDX + "ExtractedLicensingInfo" + SPDX_NONE_CAPS = NS_SPDX + "NONE" + SPDX_NOASSERTION_CAPS = NS_SPDX + "NOASSERTION" + SPDX_NONE_SMALL = NS_SPDX + "none" + SPDX_NOASSERTION_SMALL = NS_SPDX + "noassertion" + SPDX_LICENSE = NS_SPDX + "License" + SPDX_LISTED_LICENSE = NS_SPDX + "ListedLicense" + SPDX_EXAMPLE = NS_SPDX + "example" + SPDX_IS_OSI_APPROVED = NS_SPDX + "isOsiApproved" + SPDX_STANDARD_LICENSE_TEMPLATE = NS_SPDX + "standardLicenseTemplate" + SPDX_IS_DEPRECATED_LICENSE_ID = NS_SPDX + "isDeprecatedLicenseId" + SPDX_IS_FSF_LIBRE = NS_SPDX + "isFsfLibre" + SPDX_LICENSE_TEXT = NS_SPDX + "licenseText" + SPDX_STANDARD_LICENSE_HEADER = NS_SPDX + "standardLicenseHeader" + SPDX_LICENSE_EXCEPTION_ID = NS_SPDX + "licenseExceptionId" + SPDX_LICENSE_EXCEPTION_TEXT = NS_SPDX + "licenseExceptionText" + SPDX_LICENSE_EXCEPTION = NS_SPDX + "licenseException" + SPDX_WITH_EXCEPTION_OPERATOR = NS_SPDX + "WithExceptionOperator" + SPDX_OR_LATER_OPERATOR = NS_SPDX + "OrLaterOperator" + SPDX_STANDARD_LICENSE_HEADER_TEMPLATE = NS_SPDX + "standardLicenseHeaderTemplate" // RDFS properties RDFS_COMMENT = NS_RDFS + "comment" diff --git a/rdfloader/parser2v2/parse_annotation.go b/rdfloader/parser2v2/parse_annotation.go index 01d762b..5d9cdbc 100644 --- a/rdfloader/parser2v2/parse_annotation.go +++ b/rdfloader/parser2v2/parse_annotation.go @@ -13,7 +13,7 @@ import ( // 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.String()] { + for _, subTriple := range parser.nodeToTriples(node) { switch subTriple.Predicate.ID { case SPDX_ANNOTATOR: // cardinality: exactly 1 diff --git a/rdfloader/parser2v2/parse_creation_info.go b/rdfloader/parser2v2/parse_creation_info.go index 78104bd..63efd00 100644 --- a/rdfloader/parser2v2/parse_creation_info.go +++ b/rdfloader/parser2v2/parse_creation_info.go @@ -10,7 +10,7 @@ import ( // Cardinality: Mandatory, one. func (parser *rdfParser2_2) parseCreationInfoFromNode(ci *spdx.CreationInfo2_2, node *gordfParser.Node) error { - for _, triple := range parser.nodeToTriples[node.String()] { + for _, triple := range parser.nodeToTriples(node) { switch triple.Predicate.ID { case SPDX_LICENSE_LIST_VERSION: // 2.7 // cardinality: max 1 diff --git a/rdfloader/parser2v2/parse_file.go b/rdfloader/parser2v2/parse_file.go index d99f1f1..c91db64 100644 --- a/rdfloader/parser2v2/parse_file.go +++ b/rdfloader/parser2v2/parse_file.go @@ -18,7 +18,7 @@ func (parser *rdfParser2_2) getFileFromNode(fileNode *gordfParser.Node) (file *s return nil, err } - for _, subTriple := range parser.nodeToTriples[fileNode.String()] { + for _, subTriple := range parser.nodeToTriples(fileNode) { switch subTriple.Predicate.ID { case SPDX_FILE_NAME: // 4.1 // cardinality: exactly 1 @@ -38,7 +38,11 @@ func (parser *rdfParser2_2) getFileFromNode(fileNode *gordfParser.Node) (file *s err = parser.setFileChecksumFromNode(file, subTriple.Object) case SPDX_LICENSE_CONCLUDED: // 4.5 // cardinality: (exactly 1 anyLicenseInfo) or (None) or (Noassertion) - file.LicenseConcluded, err = parser.getLicenseFromTriple(subTriple) + anyLicense, err := parser.getAnyLicenseFromNode(subTriple.Object) + if err != nil { + return nil, fmt.Errorf("error parsing licenseConcluded: %v", err) + } + file.LicenseConcluded = anyLicense.ToLicenseString() case SPDX_LICENSE_INFO_IN_FILE: // 4.6 // cardinality: min 1 lastPart := getLastPartOfURI(subTriple.Object.ID) @@ -117,7 +121,7 @@ func (parser *rdfParser2_2) getArtifactFromNode(node *gordfParser.Node) (*spdx.A artifactOf.URI = node.ID } // parsing rest triples and attributes of the artifact. - for _, triple := range parser.nodeToTriples[node.String()] { + for _, triple := range parser.nodeToTriples(node) { switch triple.Predicate.ID { case RDF_TYPE: case DOAP_HOMEPAGE: diff --git a/rdfloader/parser2v2/parse_license.go b/rdfloader/parser2v2/parse_license.go index ffd3ccd..6bab096 100644 --- a/rdfloader/parser2v2/parse_license.go +++ b/rdfloader/parser2v2/parse_license.go @@ -9,55 +9,233 @@ import ( "strings" ) -// either single tag or a compound license with member combination of single tags. -// todo: allow all types of licenses. -func (parser *rdfParser2_2) getLicenseFromTriple(triple *gordfParser.Triple) (licenseConcluded string, err error) { - licenseShortIdentifier := getLicenseStringFromURI(triple.Object.ID) - // return if license is None|Noassertion - if licenseShortIdentifier == "NONE" || licenseShortIdentifier == "NOASSERTION" { - return licenseShortIdentifier, nil +func (parser *rdfParser2_2) getAnyLicenseFromNode(node *gordfParser.Node) (AnyLicenseInfo, error) { + associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil) + if len(associatedTriples) == 0 { + // just a license uri string was found. + return parser.getSpecialLicenseFromNode(node) } - // return if the license tag is not associated with any other triples. - if len(parser.nodeToTriples[triple.Object.String()]) == 0 { - return licenseShortIdentifier, nil + // we have some attributes associated with the license node. + nodeType, err := parser.getNodeTypeFromTriples(associatedTriples, node) + if err != nil { + return nil, fmt.Errorf("error parsing license triple: %v", err) } + switch nodeType { + case SPDX_DISJUNCTIVE_LICENSE_SET: + return parser.getDisjunctiveLicenseSetFromNode(node) + case SPDX_CONJUNCTIVE_LICENSE_SET: + return parser.getConjunctiveLicenseSetFromNode(node) + case SPDX_EXTRACTED_LICENSING_INFO: + return parser.getExtractedLicensingInfoFromNode(node) + case SPDX_LISTED_LICENSE, SPDX_LICENSE: + return parser.getLicenseFromNode(node) + case SPDX_WITH_EXCEPTION_OPERATOR: + return parser.getWithExceptionOperatorFromNode(node) + case SPDX_OR_LATER_OPERATOR: + return parser.getOrLaterOperatorFromNode(node) + } + return nil, fmt.Errorf("Unknown subTag (%s) found while parsing AnyLicense", nodeType) +} - // no need to parse standard licenses as they have constant fields. - // return if the license is among the standard licenses. - for _, stdLicenseId := range AllStandardLicenseIDS() { - if stdLicenseId == licenseShortIdentifier { - return licenseShortIdentifier, nil +func (parser *rdfParser2_2) getLicenseExceptionFromNode(node *gordfParser.Node) (exception LicenseException, err error) { + associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil) + for _, triple := range associatedTriples { + value := triple.Object.ID + switch triple.Predicate.ID { + case SPDX_LICENSE_EXCEPTION_ID: + exception.licenseExceptionId = value + case SPDX_LICENSE_EXCEPTION_TEXT: + exception.licenseExceptionText = value + case RDFS_SEE_ALSO: + if !isUriValid(value) { + return exception, fmt.Errorf("invalid uri (%s) for seeAlso attribute of LicenseException", value) + } + exception.seeAlso = value + case SPDX_NAME: + exception.name = value + case SPDX_EXAMPLE: + exception.example = value + default: + return exception, fmt.Errorf("invalid predicate(%s) for LicenseException", triple.Predicate) } } + return exception, nil +} - // apart from the license being in the uri form, this function allows - // license to be a collection of licenses joined by a single operator - // (either conjunction or disjunction) +func (parser *rdfParser2_2) getSimpleLicensingInfoFromNode(node *gordfParser.Node) (SimpleLicensingInfo, error) { + simpleLicensingTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil) + return parser.getSimpleLicensingInfoFromTriples(simpleLicensingTriples) +} - typeTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &triple.Object.ID, &RDF_TYPE, nil) - if len(typeTriples) == 0 { - return "", fmt.Errorf("node(%v) not associated with a type triple", triple.Object) - } - if len(typeTriples) > 1 { - return "", fmt.Errorf("node is associated with more than one type triple") +func (parser *rdfParser2_2) getWithExceptionOperatorFromNode(node *gordfParser.Node) (operator WithExceptionOperator, err error) { + associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil) + for _, triple := range associatedTriples { + switch triple.Predicate.ID { + case SPDX_MEMBER: + member, err := parser.getSimpleLicensingInfoFromNode(triple.Object) + if err != nil { + return operator, fmt.Errorf("error parsing member of a WithExceptionOperator: %v", err) + } + operator.license = member + case SPDX_LICENSE_EXCEPTION: + operator.licenseException, err = parser.getLicenseExceptionFromNode(triple.Object) + default: + return operator, fmt.Errorf("unknown predicate (%s) for a WithExceptionOperator", triple.Predicate.ID) + } } - switch typeTriples[0].Object.ID { - case SPDX_DISJUNCTIVE_LICENSE_SET, SPDX_CONJUNCTIVE_LICENSE_SET: + return operator, nil +} - case SPDX_EXTRACTED_LICENSING_INFO: - err = parser.parseOtherLicenseFromNode(triple.Object) +func (parser *rdfParser2_2) getOrLaterOperatorFromNode(node *gordfParser.Node) (operator OrLaterOperator, err error) { + associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil) + n := len(associatedTriples) + if n != 2 { + return operator, fmt.Errorf("orLaterOperator must be associated with exactly one tag. found %v triples", n-1) + } + for _, triple := range associatedTriples { + operator.license, err = parser.getSimpleLicensingInfoFromNode(triple.Object) if err != nil { - return "", err + return operator, fmt.Errorf("error parsing simpleLicensingInfo of OrLaterOperator: %v", err) + } + } + return operator, nil +} + +func (parser *rdfParser2_2) getSpecialLicenseFromNode(node *gordfParser.Node) (lic SpecialLicense, err error) { + uri := strings.TrimSpace(node.ID) + switch uri { + case SPDX_NONE_CAPS, SPDX_NONE_SMALL: + return SpecialLicense{ + value: NONE, + }, nil + case SPDX_NOASSERTION_SMALL, SPDX_NOASSERTION_CAPS: + return SpecialLicense{ + value: NOASSERTION, + }, nil + } + + // the license is neither NONE nor NOASSERTION + // checking if the license is among the standardLicenses + licenseAbbreviation := getLastPartOfURI(uri) + for _, stdLicense := range AllStandardLicenseIDS() { + if licenseAbbreviation == stdLicense { + return SpecialLicense{ + value: SpecialLicenseValue(stdLicense), + }, nil + } + } + return lic, fmt.Errorf("found a custom license uri (%s) without any associated fields", uri) +} + +func (parser *rdfParser2_2) getDisjunctiveLicenseSetFromNode(node *gordfParser.Node) (DisjunctiveLicenseSet, error) { + licenseSet := DisjunctiveLicenseSet{ + members: []AnyLicenseInfo{}, + } + for _, triple := range parser.nodeToTriples(node) { + switch triple.Predicate.ID { + case RDF_TYPE: + continue + case SPDX_MEMBER: + member, err := parser.getAnyLicenseFromNode(triple.Object) + if err != nil { + return licenseSet, fmt.Errorf("error parsing disjunctive license set: %v", err) + } + licenseSet.members = append(licenseSet.members, member) + } + } + return licenseSet, nil +} + +func (parser *rdfParser2_2) getConjunctiveLicenseSetFromNode(node *gordfParser.Node) (ConjunctiveLicenseSet, error) { + licenseSet := ConjunctiveLicenseSet{ + members: []AnyLicenseInfo{}, + } + for _, triple := range parser.nodeToTriples(node) { + switch triple.Predicate.ID { + case RDF_TYPE: + continue + case SPDX_MEMBER: + member, err := parser.getAnyLicenseFromNode(triple.Object) + if err != nil { + return licenseSet, fmt.Errorf("error parsing conjunctive license set: %v", err) + } + licenseSet.members = append(licenseSet.members, member) + } + } + return licenseSet, nil +} + +func (parser *rdfParser2_2) getSimpleLicensingInfoFromTriples(triples []*gordfParser.Triple) (lic SimpleLicensingInfo, err error) { + for _, triple := range triples { + switch triple.Predicate.ID { + case RDFS_COMMENT: + lic.comment = triple.Object.ID + case SPDX_LICENSE_ID: + lic.licenseID = triple.Object.ID + case SPDX_NAME: + lic.name = triple.Object.ID + case RDFS_SEE_ALSO: + lic.seeAlso = append(lic.seeAlso, triple.Object.ID) + case SPDX_EXAMPLE: + lic.example = triple.Object.ID + case RDF_TYPE: + continue + default: + return lic, fmt.Errorf("unknown predicate(%s) for simple licensing info", triple.Predicate) } - othLic := parser.doc.OtherLicenses[len(parser.doc.OtherLicenses)-1] - return othLic.LicenseIdentifier, nil - default: - return "", fmt.Errorf("not implemented error: cannot parse %s", typeTriples[0].Object) } - return parser.getLicenseFromLicenseSetNode(triple.Object) + return lic, nil } +func (parser *rdfParser2_2) getLicenseFromNode(node *gordfParser.Node) (lic License, err error) { + associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil) + var restTriples []*gordfParser.Triple + for _, triple := range associatedTriples { + value := triple.Object.ID + switch triple.Predicate.ID { + case SPDX_IS_OSI_APPROVED: + lic.isOsiApproved, err = boolFromString(value) + if err != nil { + return lic, fmt.Errorf("error parsing isOsiApproved attribute of a License: %v", err) + } + case SPDX_LICENSE_TEXT: + lic.licenseText = value + case SPDX_STANDARD_LICENSE_HEADER: + lic.standardLicenseHeader = value + case SPDX_STANDARD_LICENSE_TEMPLATE: + lic.standardLicenseTemplate = value + case SPDX_STANDARD_LICENSE_HEADER_TEMPLATE: + lic.standardLicenseHeaderTemplate = value + case RDFS_SEE_ALSO: + if !isUriValid(value) { + return lic, fmt.Errorf("%s is not a valid uri for seeAlso attribute of a License", value) + } + lic.seeAlso = value + + case SPDX_IS_DEPRECATED_LICENSE_ID: + lic.isDeprecatedLicenseID, err = boolFromString(value) + if err != nil { + return lic, fmt.Errorf("error parsing isDeprecatedLicenseId attribute of a License: %v", err) + } + case SPDX_IS_FSF_LIBRE: + lic.isFsfLibre, err = boolFromString(value) + if err != nil { + return lic, fmt.Errorf("error parsing isFsfLibre attribute of a License: %v", err) + } + default: + restTriples = append(restTriples, triple) + } + } + lic.SimpleLicensingInfo, err = parser.getSimpleLicensingInfoFromTriples(restTriples) + if err != nil { + return lic, fmt.Errorf("error setting simple licensing information of a License: %s", err) + } + return lic, nil +} + +/* util methods for licenses and checksums below:*/ + // Given the license URI, returns the name of the license defined // in the last part of the uri. // This function is susceptible to false-positives. @@ -75,7 +253,7 @@ func getLicenseStringFromURI(uri string) string { // whose pointer will be returned. func (parser *rdfParser2_2) getChecksumFromNode(checksumNode *gordfParser.Node) (algorithm string, value string, err error) { var checksumValue, checksumAlgorithm string - for _, checksumTriple := range parser.nodeToTriples[checksumNode.String()] { + for _, checksumTriple := range parser.nodeToTriples(checksumNode) { switch checksumTriple.Predicate.ID { case RDF_TYPE: continue @@ -111,28 +289,43 @@ func (parser *rdfParser2_2) getAlgorithmFromURI(algorithmURI string) (checksumAl return } -func (parser *rdfParser2_2) getLicenseFromLicenseSetNode(node *gordfParser.Node) (s string, err error) { - typeLicenseSet := "undefined" - var licenseSets []string - for _, lst := range parser.nodeToTriples[node.String()] { - switch lst.Predicate.ID { - case RDF_TYPE: - _, typeLicenseSet, err = ExtractSubs(lst.Object.ID, "#") - if err != nil { - return - } - case SPDX_MEMBER: - licenseSets = append(licenseSets, getLicenseStringFromURI(lst.Object.ID)) - default: - return "", fmt.Errorf("undefined predicate %s while parsing license set", lst.Predicate.ID) - } - } - switch typeLicenseSet { - case "DisjunctiveLicenseSet": - return strings.Join(licenseSets, " OR "), nil - case "ConjunctiveLicenseSet": - return strings.Join(licenseSets, " AND "), nil - default: - return "", fmt.Errorf("unknown licenseSet type %s", typeLicenseSet) +func mapLicensesToStrings(licences []AnyLicenseInfo) []string { + res := make([]string, len(licences), len(licences)) + for i, lic := range licences { + res[i] = lic.ToLicenseString() } + return res +} + +func (lic ConjunctiveLicenseSet) ToLicenseString() string { + return strings.Join(mapLicensesToStrings(lic.members), " AND ") +} + +func (lic DisjunctiveLicenseSet) ToLicenseString() string { + return strings.Join(mapLicensesToStrings(lic.members), " OR ") +} + +/****** Type Functions ******/ +func (lic ExtractedLicensingInfo) ToLicenseString() string { + return lic.licenseID +} + +func (operator OrLaterOperator) ToLicenseString() string { + return operator.license.ToLicenseString() +} + +func (lic License) ToLicenseString() string { + return lic.licenseID +} + +func (lic ListedLicense) ToLicenseString() string { + return lic.licenseID +} + +func (lic WithExceptionOperator) ToLicenseString() string { + return lic.license.ToLicenseString() +} + +func (lic SpecialLicense) ToLicenseString() string { + return string(lic.value) } diff --git a/rdfloader/parser2v2/parse_other_license_info.go b/rdfloader/parser2v2/parse_other_license_info.go index dd1c193..887ae55 100644 --- a/rdfloader/parser2v2/parse_other_license_info.go +++ b/rdfloader/parser2v2/parse_other_license_info.go @@ -5,62 +5,33 @@ package parser2v2 import ( "fmt" gordfParser "github.com/RishabhBhatnagar/gordf/rdfloader/parser" + "github.com/RishabhBhatnagar/gordf/rdfwriter" "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()] { +func (parser *rdfParser2_2) getExtractedLicensingInfoFromNode(node *gordfParser.Node) (lic ExtractedLicensingInfo, err error) { + associatedTriples := rdfwriter.FilterTriples(parser.gordfParserObj.Triples, &node.ID, nil, nil) + restTriples := []*gordfParser.Triple{} + for _, triple := range associatedTriples { 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 + lic.extractedText = triple.Object.ID default: - return nil, fmt.Errorf("unknown predicate %v while parsing extractedLicensingInfo", triple.Predicate) + restTriples = append(restTriples, triple) } } + lic.SimpleLicensingInfo, err = parser.getSimpleLicensingInfoFromTriples(restTriples) + if err != nil { + return lic, fmt.Errorf("error setting simple licensing information of extracted licensing info: %s", err) + } 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 +func (parser *rdfParser2_2) extractedLicenseToOtherLicense(extLicense ExtractedLicensingInfo) (othLic spdx.OtherLicense2_2) { + othLic.LicenseIdentifier = extLicense.licenseID + othLic.ExtractedText = extLicense.extractedText + othLic.LicenseComment = extLicense.comment + othLic.LicenseCrossReferences = extLicense.seeAlso + othLic.LicenseName = extLicense.name + return othLic } diff --git a/rdfloader/parser2v2/parse_package.go b/rdfloader/parser2v2/parse_package.go index 484804f..089f3bf 100644 --- a/rdfloader/parser2v2/parse_package.go +++ b/rdfloader/parser2v2/parse_package.go @@ -20,7 +20,7 @@ func (parser *rdfParser2_2) getPackageFromNode(packageNode *gordfParser.Node) (p pkg.PackageSPDXIdentifier = eId // 3.2 // iterate over all the triples associated with the provided package packageNode. - for _, subTriple := range parser.nodeToTriples[packageNode.String()] { + for _, subTriple := range parser.nodeToTriples(packageNode) { switch subTriple.Predicate.ID { case RDF_TYPE: // cardinality: exactly 1 @@ -76,21 +76,21 @@ func (parser *rdfParser2_2) getPackageFromNode(packageNode *gordfParser.Node) (p pkg.PackageSourceInfo = subTriple.Object.ID case SPDX_LICENSE_CONCLUDED: // 3.13 // cardinality: exactly 1 - licenseConcluded, err := parser.getLicenseFromTriple(subTriple) + anyLicenseInfo, err := parser.getAnyLicenseFromNode(subTriple.Object) if err != nil { return nil, err } - pkg.PackageLicenseConcluded = licenseConcluded + pkg.PackageLicenseConcluded = anyLicenseInfo.ToLicenseString() case SPDX_LICENSE_INFO_FROM_FILES: // 3.14 // cardinality: min 0 pkg.PackageLicenseInfoFromFiles = append(pkg.PackageLicenseInfoFromFiles, getLicenseStringFromURI(subTriple.Object.ID)) case SPDX_LICENSE_DECLARED: // 3.15 // cardinality: exactly 1 - license, err := parser.getLicenseFromTriple(subTriple) + anyLicenseInfo, err := parser.getAnyLicenseFromNode(subTriple.Object) if err != nil { return nil, err } - pkg.PackageLicenseDeclared = license + pkg.PackageLicenseDeclared = anyLicenseInfo.ToLicenseString() case SPDX_LICENSE_COMMENTS: // 3.16 // cardinality: max 1 pkg.PackageLicenseComments = subTriple.Object.ID @@ -149,7 +149,7 @@ func (parser *rdfParser2_2) getPackageFromNode(packageNode *gordfParser.Node) (p // parses externalReference found in the package by the associated triple. func (parser *rdfParser2_2) getPackageExternalRef(triple *gordfParser.Triple) (externalDocRef *spdx.PackageExternalReference2_2, err error) { externalDocRef = &spdx.PackageExternalReference2_2{} - for _, subTriple := range parser.nodeToTriples[triple.Object.String()] { + for _, subTriple := range parser.nodeToTriples(triple.Object) { switch subTriple.Predicate.ID { case SPDX_REFERENCE_CATEGORY: // cardinality: exactly 1 @@ -187,7 +187,7 @@ func (parser *rdfParser2_2) getPackageExternalRef(triple *gordfParser.Triple) (e } func (parser *rdfParser2_2) setPackageVerificationCode(pkg *spdx.Package2_2, node *gordfParser.Node) error { - for _, subTriple := range parser.nodeToTriples[node.String()] { + for _, subTriple := range parser.nodeToTriples(node) { switch subTriple.Predicate.ID { case SPDX_PACKAGE_VERIFICATION_CODE_VALUE: // cardinality: exactly 1 @@ -265,9 +265,9 @@ func setPackageOriginator(pkg *spdx.Package2_2, value string) error { // validates the uri and sets the location if it is valid func setDocumentLocationFromURI(locationURI string, pkg *spdx.Package2_2) error { switch locationURI { - case SPDX_NOASSERTION: + case SPDX_NOASSERTION_CAPS, SPDX_NOASSERTION_CAPS: pkg.PackageDownloadLocation = "NOASSERTION" - case SPDX_NONE: + case SPDX_NONE_CAPS, SPDX_NONE_SMALL: pkg.PackageDownloadLocation = "NONE" default: if !isUriValid(locationURI) { diff --git a/rdfloader/parser2v2/parse_relationship.go b/rdfloader/parser2v2/parse_relationship.go index d021595..b51105b 100644 --- a/rdfloader/parser2v2/parse_relationship.go +++ b/rdfloader/parser2v2/parse_relationship.go @@ -22,7 +22,7 @@ func (parser *rdfParser2_2) parseRelationship(triple *gordfParser.Triple) (err e return err } - for _, subTriple := range parser.nodeToTriples[triple.Object.String()] { + for _, subTriple := range parser.nodeToTriples(triple.Object) { switch subTriple.Predicate.ID { case SPDX_RELATIONSHIP_TYPE: // cardinality: exactly 1 @@ -38,7 +38,7 @@ func (parser *rdfParser2_2) parseRelationship(triple *gordfParser.Triple) (err e return err } - relatedSpdxElementTriples := parser.nodeToTriples[subTriple.Object.String()] + relatedSpdxElementTriples := parser.nodeToTriples(subTriple.Object) if len(relatedSpdxElementTriples) == 0 { continue } diff --git a/rdfloader/parser2v2/parse_review.go b/rdfloader/parser2v2/parse_review.go index 08e805d..ed89865 100644 --- a/rdfloader/parser2v2/parse_review.go +++ b/rdfloader/parser2v2/parse_review.go @@ -10,7 +10,7 @@ import ( func (parser *rdfParser2_2) setReviewFromNode(reviewedNode *gordfParser.Node) error { review := spdx.Review2_2{} - for _, triple := range parser.nodeToTriples[reviewedNode.String()] { + for _, triple := range parser.nodeToTriples(reviewedNode) { switch triple.Predicate.ID { case RDF_TYPE: // cardinality: exactly 1 diff --git a/rdfloader/parser2v2/parse_snippet_info.go b/rdfloader/parser2v2/parse_snippet_info.go index 7bd5624..8db67c1 100644 --- a/rdfloader/parser2v2/parse_snippet_info.go +++ b/rdfloader/parser2v2/parse_snippet_info.go @@ -20,7 +20,7 @@ func (parser *rdfParser2_2) getSnippetInformationFromTriple2_2(triple *gordfPars return nil, err } - for _, siTriple := range parser.nodeToTriples[triple.Subject.String()] { + for _, siTriple := range parser.nodeToTriples(triple.Subject) { switch siTriple.Predicate.ID { case RDF_TYPE: // cardinality: exactly 1 @@ -79,14 +79,14 @@ func (parser *rdfParser2_2) setSnippetRangeFromNode(node *gordfParser.Node, si * // todo: apply DRY in this method. rangeType := 0 // 0: undefined range, 1: byte, 2: line var start, end string - for _, t := range parser.nodeToTriples[node.String()] { + for _, t := range parser.nodeToTriples(node) { switch t.Predicate.ID { case RDF_TYPE: if t.Object.ID != PTR_START_END_POINTER { return fmt.Errorf("expected range to have sub tag of type StartEndPointer, found %v", t.Object.ID) } case PTR_START_POINTER: - for _, subTriple := range parser.nodeToTriples[t.Object.String()] { + for _, subTriple := range parser.nodeToTriples(t.Object) { switch subTriple.Predicate.ID { case RDF_TYPE: switch subTriple.Object.ID { @@ -115,7 +115,7 @@ func (parser *rdfParser2_2) setSnippetRangeFromNode(node *gordfParser.Node, si * } } case PTR_END_POINTER: - subTriples := parser.nodeToTriples[t.Object.String()] + subTriples := parser.nodeToTriples(t.Object) for _, subTriple := range subTriples { switch subTriple.Predicate.ID { case RDF_TYPE: diff --git a/rdfloader/parser2v2/parse_spdx_document.go b/rdfloader/parser2v2/parse_spdx_document.go index 7769e18..8371b0f 100644 --- a/rdfloader/parser2v2/parse_spdx_document.go +++ b/rdfloader/parser2v2/parse_spdx_document.go @@ -22,7 +22,7 @@ func (parser *rdfParser2_2) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) ci.SPDXIdentifier = spdx.ElementID(offset) // 2.3 // parse other associated triples. - for _, subTriple := range parser.nodeToTriples[spdxDocNode.String()] { + for _, subTriple := range parser.nodeToTriples(spdxDocNode) { objectValue := subTriple.Object.ID switch subTriple.Predicate.ID { case SPDX_SPEC_VERSION: // 2.1: specVersion @@ -30,19 +30,23 @@ func (parser *rdfParser2_2) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) ci.SPDXVersion = objectValue case SPDX_DATA_LICENSE: // 2.2: dataLicense // cardinality: exactly 1 - ci.DataLicense, err = parser.getLicenseFromTriple(subTriple) // todo: sort it out. + dataLicense, err := parser.getAnyLicenseFromNode(subTriple.Object) + if err != nil { + return err + } + ci.DataLicense = dataLicense.ToLicenseString() case SPDX_NAME: // 2.4: DocumentName // cardinality: exactly 1 ci.DocumentName = objectValue case SPDX_EXTERNAL_DOCUMENT_REF: // 2.6: externalDocumentReferences // cardinality: min 0 var extRef string - extRef, err = parser.getExternalDocumentRefFromTriples(parser.nodeToTriples[subTriple.Object.String()]) + extRef, err = parser.getExternalDocumentRefFromTriples(parser.nodeToTriples(subTriple.Object)) ci.ExternalDocumentReferences = append(ci.ExternalDocumentReferences, extRef) case SPDX_CREATION_INFO: // 2.7 - 2.10: // cardinality: exactly 1 err = parser.parseCreationInfoFromNode(ci, subTriple.Object) - case SPDX_COMMENT: // 2.11: Document Comment + case RDFS_COMMENT: // 2.11: Document Comment // cardinality: max 1 ci.DocumentComment = objectValue case SPDX_REVIEWED: // reviewed: @@ -58,7 +62,12 @@ func (parser *rdfParser2_2) parseSpdxDocumentNode(spdxDocNode *gordfParser.Node) parser.doc.Packages[pkg.PackageSPDXIdentifier] = pkg case SPDX_HAS_EXTRACTED_LICENSING_INFO: // hasExtractedLicensingInfo // cardinality: min 0 - err = parser.parseOtherLicenseFromNode(subTriple.Object) // todo: sort this out. + extractedLicensingInfo, err := parser.getExtractedLicensingInfoFromNode(subTriple.Object) + if err != nil { + return fmt.Errorf("error setting extractedLicensingInfo in spdxDocument: %v", err) + } + othLicense := parser.extractedLicenseToOtherLicense(extractedLicensingInfo) + parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, &othLicense) case SPDX_RELATIONSHIP: // relationship // cardinality: min 0 err = parser.parseRelationship(subTriple) diff --git a/rdfloader/parser2v2/parser.go b/rdfloader/parser2v2/parser.go index 3145688..dd50f13 100644 --- a/rdfloader/parser2v2/parser.go +++ b/rdfloader/parser2v2/parser.go @@ -3,7 +3,6 @@ package parser2v2 import ( - "errors" "fmt" gordfParser "github.com/RishabhBhatnagar/gordf/rdfloader/parser" gordfWriter "github.com/RishabhBhatnagar/gordf/rdfwriter" @@ -13,8 +12,8 @@ import ( // returns a new instance of rdfParser2_2 given the gordf object and nodeToTriples mapping func NewParser2_2(gordfParserObj *gordfParser.Parser, nodeToTriples map[string][]*gordfParser.Triple) *rdfParser2_2 { parser := rdfParser2_2{ - gordfParserObj: gordfParserObj, - nodeToTriples: nodeToTriples, + gordfParserObj: gordfParserObj, + nodeStringToTriples: nodeToTriples, doc: &spdx.Document2_2{ CreationInfo: &spdx.CreationInfo2_2{}, Packages: map[spdx.ElementID]*spdx.Package2_2{}, @@ -101,10 +100,10 @@ func (parser *rdfParser2_2) getSpdxDocNode() (node *gordfParser.Node, err error) var spdxDocNode *gordfParser.Node for _, rootNode := range gordfWriter.GetRootNodes(parser.gordfParserObj.Triples) { typeTriples := gordfWriter.FilterTriples( - parser.nodeToTriples[rootNode.String()], // triples - &rootNode.ID, // Subject - &RDF_TYPE, // Predicate - nil, // Object + parser.nodeToTriples(rootNode), // triples + &rootNode.ID, // Subject + &RDF_TYPE, // Predicate + nil, // Object ) if len(typeTriples) != 1 { return nil, fmt.Errorf("rootNode (%v) must be associated with exactly one"+ @@ -124,186 +123,3 @@ func (parser *rdfParser2_2) getSpdxDocNode() (node *gordfParser.Node, err error) } return spdxDocNode, nil } - -// unused -func (parser *rdfParser2_2) setFiles() error { - allFilesTriples, err := parser.filterTriplesByRegex(parser.gordfParserObj.Triples, ".*", RDF_TYPE+"$", SPDX_FILE+"$") - if err != nil { - return err - } - for _, fileTriple := range allFilesTriples { - file, err := parser.getFileFromNode(fileTriple.Subject) - if err != nil { - return fmt.Errorf("error setting a file: %v", err) - } - parser.files[file.FileSPDXIdentifier] = file - } - return nil -} - -// unused -func (parser *rdfParser2_2) setPackages() error { - allPackagesTriples, err := parser.filterTriplesByRegex(parser.gordfParserObj.Triples, ".*", RDF_TYPE+"$", SPDX_PACKAGE+"$") - if err != nil { - return err - } - for _, pkgTriple := range allPackagesTriples { - pkg, err := parser.getPackageFromNode(pkgTriple.Subject) - if err != nil { - return fmt.Errorf("error setting a package: %v", err) - } - parser.packages[pkg.PackageSPDXIdentifier] = pkg - } - return nil -} - -// unused -// assumes that the document's namespace is already set. -func (parser *rdfParser2_2) setSnippetToDoc(si *spdx.Snippet2_2, snippetNode *gordfParser.Node) (err error) { - if parser.doc == nil || parser.doc.CreationInfo == nil { - return errors.New("document namespace not set yet") - } - docNS := parser.doc.CreationInfo.DocumentNamespace - snippetNS := stripLastPartOfUri(snippetNode.ID) - if !isUriSame(docNS, snippetNS) { - // found a snippet which doesn't belong to current document being set - return fmt.Errorf("document namespace(%s) and snippet namespace(%s) doesn't match", docNS, snippetNS) - } - - return nil -} - -// unused -func (parser *rdfParser2_2) setAnnotations(spdxDocNode *gordfParser.Node) error { - triples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_ANNOTATION, nil) - for _, triple := range triples { - err := parser.parseAnnotationFromNode(triple.Object) - if err != nil { - return err - } - } - return nil -} - -// unused -func (parser *rdfParser2_2) getSpecVersion(spdxDocNode *gordfParser.Node) (string, error) { - specVersionTriples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_SPEC_VERSION, nil) - n := len(specVersionTriples) - if n == 0 { - return "", fmt.Errorf("no specVersion found for the given spdxNode") - } - if n > 1 { - return "", fmt.Errorf("there must be exactly one specVersion. found %d specVersion", n) - } - return specVersionTriples[0].Object.ID, nil -} - -// unused -func (parser *rdfParser2_2) getDataLicense(spdxDocNode *gordfParser.Node) (string, error) { - dataLicenseTriples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_DATA_LICENSE, nil) - n := len(dataLicenseTriples) - if n == 0 { - return "", fmt.Errorf("no dataLicense found for the given spdxNode") - } - if n > 1 { - return "", fmt.Errorf("there must be exactly one dataLicense. found %d dataLicense", n) - } - return parser.getLicenseFromTriple(dataLicenseTriples[0]) -} - -// unused -func (parser *rdfParser2_2) getDocumentName(spdxDocNode *gordfParser.Node) (string, error) { - triples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_NAME, nil) - n := len(triples) - if n == 0 { - return "", fmt.Errorf("no documentName found for the given spdxNode") - } - if n > 1 { - return "", fmt.Errorf("there must be exactly one documentName. found %d documentName", n) - } - return triples[0].Object.ID, nil -} - -// unused -func (parser *rdfParser2_2) setCreationInfo(spdxDocNode *gordfParser.Node, ci *spdx.CreationInfo2_2) error { - docNS := stripJoiningChars(stripLastPartOfUri(spdxDocNode.ID)) - allCreationInfoTriples, err := parser.filterTriplesByRegex(parser.gordfParserObj.Triples, docNS+".*", SPDX_CREATION_INFO, ".*") - if err != nil { - return err - } - n := len(allCreationInfoTriples) - if n > 1 { - return fmt.Errorf("document(%s) must have exactly one creation info. found %d", docNS, n) - } - if n == 0 { - return fmt.Errorf("no creation info found for the document identified by %s", docNS) - } - err = parser.parseCreationInfoFromNode(ci, allCreationInfoTriples[0].Object) - if err != nil { - return err - } - parser.doc.CreationInfo = ci - return nil -} - -// unused -func (parser *rdfParser2_2) getDocumentComment(spdxDocNode *gordfParser.Node) (string, error) { - triples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_NAME, nil) - n := len(triples) - if n > 1 { - return "", fmt.Errorf("there must be atmost one documentComment. found %d documentComment", n) - } - if n == 0 { - return triples[0].Object.ID, nil - } - return "", nil -} - -// unused -func (parser *rdfParser2_2) setReviewed(spdxDocNode *gordfParser.Node) error { - triples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_REVIEWED, nil) - for _, triple := range triples { - err := parser.setReviewFromNode(triple.Object) - if err != nil { - return err - } - } - return nil -} - -// unused -func (parser *rdfParser2_2) setDescribesPackage(spdxDocNode *gordfParser.Node) error { - triples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_DESCRIBES_PACKAGE, nil) - for _, triple := range triples { - pkg, err := parser.getPackageFromNode(triple.Object) - if err != nil { - return err - } - parser.doc.Packages[pkg.PackageSPDXIdentifier] = pkg - } - return nil -} - -// unused -func (parser *rdfParser2_2) setExtractedLicensingInfo(spdxDocNode *gordfParser.Node) error { - triples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_HAS_EXTRACTED_LICENSING_INFO, nil) - for _, triple := range triples { - err := parser.parseOtherLicenseFromNode(triple.Object) - if err != nil { - return err - } - } - return nil -} - -// unused -func (parser *rdfParser2_2) setRelationships(spdxDocNode *gordfParser.Node) error { - triples := gordfWriter.FilterTriples(parser.gordfParserObj.Triples, &spdxDocNode.ID, &SPDX_RELATIONSHIP, nil) - for _, triple := range triples { - err := parser.parseRelationship(triple) - if err != nil { - return err - } - } - return nil -} diff --git a/rdfloader/parser2v2/types.go b/rdfloader/parser2v2/types.go index 05ac65a..d37c14b 100644 --- a/rdfloader/parser2v2/types.go +++ b/rdfloader/parser2v2/types.go @@ -10,8 +10,8 @@ import ( type rdfParser2_2 struct { // fields associated with gordf project which // will be required by rdfloader - gordfParserObj *gordfParser.Parser - nodeToTriples map[string][]*gordfParser.Triple + gordfParserObj *gordfParser.Parser + nodeStringToTriples map[string][]*gordfParser.Triple // document into which data is being parsed doc *spdx.Document2_2 @@ -24,3 +24,82 @@ type rdfParser2_2 struct { // mapping of nodeStrings to parsed object to save double computation. cache map[string]interface{} } + +type AnyLicenseInfo interface { + // ToLicenseString returns the representation of license about how it will + // be stored in the tools-golang data model + ToLicenseString() string +} + +type SimpleLicensingInfo struct { + AnyLicenseInfo + comment string + licenseID string + name string + seeAlso []string + example string +} + +type ExtractedLicensingInfo struct { + SimpleLicensingInfo + extractedText string +} + +type OrLaterOperator struct { + AnyLicenseInfo + license SimpleLicensingInfo +} + +type ConjunctiveLicenseSet struct { + AnyLicenseInfo + members []AnyLicenseInfo +} + +type DisjunctiveLicenseSet struct { + AnyLicenseInfo + members []AnyLicenseInfo +} + +type License struct { + SimpleLicensingInfo + isOsiApproved bool + licenseText string + standardLicenseHeader string + standardLicenseTemplate string + standardLicenseHeaderTemplate string + seeAlso string + isDeprecatedLicenseID bool + isFsfLibre bool +} + +type ListedLicense struct { + License +} + +type LicenseException struct { + licenseExceptionId string + licenseExceptionText string + seeAlso string // must be a valid uri + name string + example string +} + +type WithExceptionOperator struct { + AnyLicenseInfo + license SimpleLicensingInfo + licenseException LicenseException +} + +// custom LicenseType to provide support for licences of +// type Noassertion, None and customLicenses +type SpecialLicense struct { + AnyLicenseInfo + value SpecialLicenseValue +} + +type SpecialLicenseValue string + +const ( + NONE SpecialLicenseValue = "NONE" + NOASSERTION SpecialLicenseValue = "NOASSERTION" +) diff --git a/rdfloader/parser2v2/utils.go b/rdfloader/parser2v2/utils.go index e0c3557..fd9ab77 100644 --- a/rdfloader/parser2v2/utils.go +++ b/rdfloader/parser2v2/utils.go @@ -1,8 +1,10 @@ package parser2v2 import ( + "errors" "fmt" gordfParser "github.com/RishabhBhatnagar/gordf/rdfloader/parser" + "github.com/RishabhBhatnagar/gordf/rdfwriter" urilib "github.com/RishabhBhatnagar/gordf/uri" "github.com/spdx/tools-golang/spdx" "regexp" @@ -18,20 +20,6 @@ func getLastPartOfURI(uri string) string { return parts[len(parts)-1] } -func stripLastPartOfUri(uri string) string { - lastPart := getLastPartOfURI(uri) - uri = strings.TrimSuffix(uri, lastPart) - return uri -} - -func stripJoiningChars(uri string) string { - return strings.TrimSuffix(strings.TrimSuffix(uri, "/"), "#") -} - -func isUriSame(uri1, uri2 string) bool { - return stripJoiningChars(uri1) == stripJoiningChars(uri2) -} - func (parser *rdfParser2_2) filterAllTriplesByString(subject, predicate, object string) (retTriples []*gordfParser.Triple) { for _, triple := range parser.gordfParserObj.Triples { if triple.Subject.ID == subject && triple.Predicate.ID == predicate && triple.Object.ID == object { @@ -68,8 +56,44 @@ func isUriValid(uri string) bool { return err == nil } +func (parser *rdfParser2_2) getNodeTypeFromTriples(triples []*gordfParser.Triple, node *gordfParser.Node) (string, error) { + if node == nil { + return "", errors.New("empty node passed to find node type") + } + typeTriples := rdfwriter.FilterTriples(triples, &node.ID, &RDF_TYPE, nil) + switch len(typeTriples) { + case 0: + return "", fmt.Errorf("node{%v} not associated with any type triple", node) + case 1: + return typeTriples[0].Object.ID, nil + default: + return "", fmt.Errorf("node{%v} is associated with more than one type triples", node) + } +} + +func (parser *rdfParser2_2) getNodeType(node *gordfParser.Node) (string, error) { + return parser.getNodeTypeFromTriples(parser.gordfParserObj.Triples, node) +} -// Function Below this line is taken from the tvloader/parser2v2/utils.go +func (parser *rdfParser2_2) nodeToTriples(node *gordfParser.Node) []*gordfParser.Triple { + if node == nil { + return []*gordfParser.Triple{} + } + return parser.nodeStringToTriples[node.String()] +} + +func boolFromString(boolString string) (bool, error) { + switch strings.ToLower(boolString) { + case "true": + return true, nil + case "false": + return false, nil + default: + return false, fmt.Errorf("boolean string can be either true/false") + } +} + +/* Function Below this line is taken from the tvloader/parser2v2/utils.go */ // used to extract DocumentRef and SPDXRef values from an SPDX Identifier // which can point either to this document or to a different one @@ -157,4 +181,4 @@ func ExtractSubs(value string, sep string) (string, string, error) { subvalue := strings.TrimSpace(sp[1]) return subkey, subvalue, nil -}
\ No newline at end of file +} |