Skip to content

Commit c4ac79b

Browse files
authored
Merge pull request #80 from xenoamess-fork/add_contains_license
add license black list & white list detect function in LicenseCompareHelper
2 parents 5df4f76 + 0773d1a commit c4ac79b

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,89 @@ public static String[] matchingStandardLicenseIds(String licenseText) throws Inv
782782
}
783783
return matchingIds.toArray(new String[matchingIds.size()]);
784784
}
785+
786+
private static <T> boolean contains(
787+
T[] array,
788+
T value
789+
) {
790+
for (T t : array) {
791+
if (Objects.equals(t, value)) {
792+
return true;
793+
}
794+
}
795+
return false;
796+
}
797+
798+
/**
799+
* Detect if a license pass black lists
800+
* @param license license
801+
* @param blackList license black list
802+
* @return if the license pass black lists
803+
* @throws InvalidSPDXAnalysisException
804+
*/
805+
public static boolean isLicensePassBlackList(
806+
AnyLicenseInfo license,
807+
String... blackList
808+
) throws InvalidSPDXAnalysisException {
809+
if (license == null) {
810+
return true;
811+
}
812+
if (blackList == null || blackList.length == 0) {
813+
return true;
814+
}
815+
if (license instanceof ConjunctiveLicenseSet) {
816+
for (AnyLicenseInfo member : ((ConjunctiveLicenseSet) license).getMembers()) {
817+
if (!isLicensePassBlackList(member, blackList)) {
818+
return false;
819+
}
820+
}
821+
return true;
822+
} else if (license instanceof DisjunctiveLicenseSet) {
823+
for (AnyLicenseInfo member : ((DisjunctiveLicenseSet) license).getMembers()) {
824+
if (isLicensePassBlackList(member, blackList)) {
825+
return true;
826+
}
827+
}
828+
return false;
829+
} else {
830+
return !contains(blackList, license.toString());
831+
}
832+
}
833+
834+
/**
835+
* Detect if a license pass white lists
836+
* @param license license
837+
* @param whiteList license white list
838+
* @return if the license pass white lists
839+
* @throws InvalidSPDXAnalysisException
840+
*/
841+
public static boolean isLicensePassWhiteList(
842+
AnyLicenseInfo license,
843+
String... whiteList
844+
) throws InvalidSPDXAnalysisException {
845+
if (license == null) {
846+
return false;
847+
}
848+
if (whiteList == null || whiteList.length == 0) {
849+
return false;
850+
}
851+
if (license instanceof ConjunctiveLicenseSet) {
852+
for (AnyLicenseInfo member : ((ConjunctiveLicenseSet) license).getMembers()) {
853+
if (!isLicensePassWhiteList(member, whiteList)) {
854+
return false;
855+
}
856+
}
857+
return true;
858+
} else if (license instanceof DisjunctiveLicenseSet) {
859+
for (AnyLicenseInfo member : ((DisjunctiveLicenseSet) license).getMembers()) {
860+
if (isLicensePassWhiteList(member, whiteList)) {
861+
return true;
862+
}
863+
}
864+
return false;
865+
} else {
866+
return contains(whiteList, license.toString());
867+
}
868+
}
869+
785870
}

0 commit comments

Comments
 (0)