-
Notifications
You must be signed in to change notification settings - Fork 1
/
matching.clj
106 lines (94 loc) · 4.16 KB
/
matching.clj
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
;
; Copyright © 2023 Peter Monks
;
; Licensed under the Apache License Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
; SPDX-License-Identifier: Apache-2.0
;
(ns spdx.matching
"License matching functionality, primarily provided by org.spdx.utility.compare.LicenseCompareHelper."
(:require [spdx.impl.mapping :as im]))
(defn text-is-license?
"Does the entire text match the license?"
[^String text ^String license-id]
(if (and text license-id)
(if-let [lic (im/id->license license-id)]
(not (.isDifferenceFound (org.spdx.utility.compare.LicenseCompareHelper/isTextStandardLicense lic text)))
false)
false))
(defn text-is-exception?
"Does the entire text match the exception?"
[^String text ^String exception-id]
(if (and text exception-id)
(if-let [exc (im/id->exception exception-id)]
(not (.isDifferenceFound (org.spdx.utility.compare.LicenseCompareHelper/isTextStandardException exc text)))
false)
false))
(defn text-contains-license?
"Does the text contain the license somewhere within it?"
[^String text ^String license-id]
(if (and text license-id)
(if-let [lic (im/id->license license-id)]
(org.spdx.utility.compare.LicenseCompareHelper/isStandardLicenseWithinText text lic)
false)
false))
(defn text-contains-exception?
"Does the text contain the exception somewhere within it?"
[^String text ^String exception-id]
(if (and text exception-id)
(if-let [exc (im/id->exception exception-id)]
(org.spdx.utility.compare.LicenseCompareHelper/isStandardLicenseExceptionWithinText text exc)
false)
false))
(defn texts-equivalent-licenses?
"Do the two texts represent equivalent licenses?"
[^String text1 ^String text2]
(if (and text1 text2)
(org.spdx.utility.compare.LicenseCompareHelper/isLicenseTextEquivalent text1 text2)
(= nil text1 text2))) ; Two nil texts are considered equivalent
(defn texts-equivalent-exceptions?
"Do the two texts represent equivalent exceptions?"
[^String text1 ^String text2]
(texts-equivalent-licenses? text1 text2)) ; Spdx-Java-Library doesn't provide this explicitly, but the comparison logic is the same as for licenses
(defn licenses-within-text
"Returns the set of ids for all licenses found in the given text (optionally
from the provided list of license ids), or nil if none were found.
Note: this method has a substantial performance cost."
([^String text]
(when text
(some-> (seq (org.spdx.utility.compare.LicenseCompareHelper/matchingStandardLicenseIdsWithinText text))
set)))
([^String text license-ids]
(when (and text (seq license-ids))
(some-> (seq (org.spdx.utility.compare.LicenseCompareHelper/matchingStandardLicenseIdsWithinText text (seq license-ids)))
set))))
(defn exceptions-within-text
"Returns the set of ids for all exceptions found in the given text (optionally
from the provided set of exception ids), or nil if none were found.
Note: this method has a substantial performance cost."
([^String text]
(when text
(some-> (seq (org.spdx.utility.compare.LicenseCompareHelper/matchingStandardLicenseExceptionIdsWithinText text))
set)))
([^String text exception-ids]
(when (and text (seq exception-ids))
(some-> (seq (org.spdx.utility.compare.LicenseCompareHelper/matchingStandardLicenseExceptionIdsWithinText text (seq exception-ids)))
set))))
(defn init!
"Initialises this namespace upon first call (and does nothing on subsequent
calls), returning nil. Consumers of this namespace are not required to call
this fn, as initialisation will occur implicitly anyway; it is provided to
allow explicit control of the cost of initialisation to callers who need it."
[]
(im/init!)
nil)