Package org.spdx.utility.compare
Class LicenseCompareHelper
- java.lang.Object
-
- org.spdx.utility.compare.LicenseCompareHelper
-
public class LicenseCompareHelper extends Object
Primarily a static class of helper functions for comparing two SPDX licenses- Author:
- Gary O'Neall
-
-
Field Summary
Fields Modifier and Type Field Description protected static Integer
CROSS_REF_NUM_WORDS_MATCH
protected static Pattern
REGEX_QUANTIFIER_PATTERN
-
Constructor Summary
Constructors Constructor Description LicenseCompareHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
getFirstLicenseToken(String text)
static List<String>
getNonOptionalLicenseText(String licenseTemplate, FilterTemplateOutputHandler.VarTextHandling varTextHandling)
Get the text of a license minus any optional text - note: this includes the default variable textstatic List<String>
getNonOptionalLicenseText(String licenseTemplate, FilterTemplateOutputHandler.VarTextHandling varTextHandling, FilterTemplateOutputHandler.OptionalTextHandling optionalTextHandling)
Get the text of a license converting variable and optional text according to the optionsstatic boolean
isLicenseEqual(org.spdx.library.model.v2.license.AnyLicenseInfo license1, org.spdx.library.model.v2.license.AnyLicenseInfo license2, Map<String,String> xlationMap)
Compares two licenses from potentially two different documents which may have different license ID's for the same licensestatic boolean
isLicenseEqual(org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license1, org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license2, Map<String,String> xlationMap)
Compares two licenses from potentially two different documents which may have different license ID's for the same licensestatic boolean
isLicensePassBlackList(org.spdx.library.model.v2.license.AnyLicenseInfo license, String... blackList)
Detect if a license pass black listsstatic boolean
isLicensePassBlackList(org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license, String... blackList)
Detect if a license pass black listsstatic boolean
isLicensePassWhiteList(org.spdx.library.model.v2.license.AnyLicenseInfo license, String... whiteList)
Detect if a license pass white listsstatic boolean
isLicensePassWhiteList(org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license, String... whiteList)
Detect if a license pass white listsstatic boolean
isSingleTokenString(String text)
static boolean
isStandardLicenseExceptionWithinText(String text, org.spdx.library.model.v2.license.ListedLicenseException exception)
Detect if a text contains the standard license exception (perhaps along with other text before and/or after)static boolean
isStandardLicenseExceptionWithinText(String text, org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicenseException exception)
Detect if a text contains the standard license exception (perhaps along with other text before and/or after)static boolean
isStandardLicenseWithinText(String text, org.spdx.library.model.v2.license.SpdxListedLicense license)
Detect if a text contains the standard license (perhaps along with other text before and/or after)static boolean
isStandardLicenseWithinText(String text, org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicense license)
Detect if a text contains the standard license (perhaps along with other text before and/or after)static CompareTemplateOutputHandler.DifferenceDescription
isTextMatchingTemplate(String template, String compareText)
static CompareTemplateOutputHandler.DifferenceDescription
isTextStandardException(org.spdx.library.model.v2.license.LicenseException exception, String compareText)
Compares exception text to the exception text of an SPDX Standard exceptionstatic CompareTemplateOutputHandler.DifferenceDescription
isTextStandardException(org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicenseException exception, String compareText)
Compares exception text to the exception text of an SPDX Standard exceptionstatic CompareTemplateOutputHandler.DifferenceDescription
isTextStandardLicense(org.spdx.library.model.v2.license.License license, String compareText)
Compares license text to the license text of an SPDX Standard Licensestatic CompareTemplateOutputHandler.DifferenceDescription
isTextStandardLicense(org.spdx.library.model.v3_0_1.expandedlicensing.License license, String compareText)
Compares license text to the license text of an SPDX Standard Licensestatic String
licenseUriToLicenseId(String objectUri)
static String
locateOriginalText(String fullLicenseText, int startToken, int endToken, Map<Integer,org.spdx.licenseTemplate.LineColumn> tokenToLocation, String[] tokens)
Locate the original text starting with the start token and ending with the end tokenstatic List<String>
matchingStandardLicenseExceptionIdsWithinText(String text)
Returns a list of SPDX Standard License Exception ID's that were found within the text, using the SPDX matching guidelines.static List<String>
matchingStandardLicenseExceptionIdsWithinText(String text, List<String> licenseExceptionIds)
Returns a list of SPDX Standard License Exception ID's from the provided list that were found within the text, using the SPDX matching guidelines.static String[]
matchingStandardLicenseIds(String licenseText)
Returns a list of SPDX Standard License ID's that match the text provided using the SPDX matching guidelines.static List<String>
matchingStandardLicenseIdsWithinText(String text)
Returns a list of SPDX Standard License ID's that were found within the text, using the SPDX matching guidelines.static List<String>
matchingStandardLicenseIdsWithinText(String text, List<String> licenseIds)
Returns a list of SPDX Standard License ID's from the provided list that were found within the text, using the SPDX matching guidelines.static String
removeCommentChars(String s)
Remove common comment characters from either a template or license text strings
-
-
-
Method Detail
-
licenseUriToLicenseId
public static String licenseUriToLicenseId(String objectUri)
- Parameters:
objectUri
- URI of the license- Returns:
- license ID
-
removeCommentChars
public static String removeCommentChars(String s)
Remove common comment characters from either a template or license text strings- Parameters:
s
- string source- Returns:
- string without comment characters
-
locateOriginalText
public static String locateOriginalText(String fullLicenseText, int startToken, int endToken, Map<Integer,org.spdx.licenseTemplate.LineColumn> tokenToLocation, String[] tokens)
Locate the original text starting with the start token and ending with the end token- Parameters:
fullLicenseText
- entire license textstartToken
- starting tokenendToken
- ending tokentokenToLocation
- token location- Returns:
- original text starting with the start token and ending with the end token
-
isSingleTokenString
public static boolean isSingleTokenString(String text)
- Parameters:
text
- text to test- Returns:
- true if the text contains a single token
-
isLicenseEqual
public static boolean isLicenseEqual(org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license1, org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license2, Map<String,String> xlationMap) throws SpdxCompareException, org.spdx.core.InvalidSPDXAnalysisException
Compares two licenses from potentially two different documents which may have different license ID's for the same license- Parameters:
license1
- first license to comparelicense2
- second license to comparexlationMap
- Mapping the license ID's from license 1 to license 2- Returns:
- true if the licenses equal
- Throws:
SpdxCompareException
- on comparison errorsorg.spdx.core.InvalidSPDXAnalysisException
- on errors reading reading properties from the SPDX model
-
getNonOptionalLicenseText
public static List<String> getNonOptionalLicenseText(String licenseTemplate, FilterTemplateOutputHandler.VarTextHandling varTextHandling) throws SpdxCompareException
Get the text of a license minus any optional text - note: this includes the default variable text- Parameters:
licenseTemplate
- license template containing optional and var tagsvarTextHandling
- include original, exclude, or include the regex (enclosed with "~~~") for "var" text- Returns:
- list of strings for all non-optional license text.
- Throws:
SpdxCompareException
- on comparison errors
-
getNonOptionalLicenseText
public static List<String> getNonOptionalLicenseText(String licenseTemplate, FilterTemplateOutputHandler.VarTextHandling varTextHandling, FilterTemplateOutputHandler.OptionalTextHandling optionalTextHandling) throws SpdxCompareException
Get the text of a license converting variable and optional text according to the options- Parameters:
licenseTemplate
- license template containing optional and var tagsvarTextHandling
- include original, exclude, or include the regex (enclosed with "~~~") for "var" textoptionalTextHandling
- include optional text, exclude, or include a regex for the optional text- Returns:
- list of strings for all non-optional license text.
- Throws:
SpdxCompareException
- on comparison errors
-
isTextMatchingTemplate
public static CompareTemplateOutputHandler.DifferenceDescription isTextMatchingTemplate(String template, String compareText) throws SpdxCompareException
- Parameters:
template
- Template in the standard template format used for comparisoncompareText
- Text to compare using the template- Returns:
- any differences found
- Throws:
SpdxCompareException
- on comparison errors
-
isTextStandardLicense
public static CompareTemplateOutputHandler.DifferenceDescription isTextStandardLicense(org.spdx.library.model.v3_0_1.expandedlicensing.License license, String compareText) throws SpdxCompareException, org.spdx.core.InvalidSPDXAnalysisException
Compares license text to the license text of an SPDX Standard License- Parameters:
license
- SPDX Standard License to comparecompareText
- Text to compare to the standard license- Returns:
- any differences found
- Throws:
SpdxCompareException
- on comparison errorsorg.spdx.core.InvalidSPDXAnalysisException
- on errors reading reading properties from the SPDX model
-
isTextStandardException
public static CompareTemplateOutputHandler.DifferenceDescription isTextStandardException(org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicenseException exception, String compareText) throws SpdxCompareException, org.spdx.core.InvalidSPDXAnalysisException
Compares exception text to the exception text of an SPDX Standard exception- Parameters:
exception
- SPDX Standard exception to comparecompareText
- Text to compare to the standard exceptions- Returns:
- any differences found
- Throws:
SpdxCompareException
- on comparison errorsorg.spdx.core.InvalidSPDXAnalysisException
- on errors reading reading properties from the SPDX model
-
isStandardLicenseWithinText
public static boolean isStandardLicenseWithinText(String text, org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicense license)
Detect if a text contains the standard license (perhaps along with other text before and/or after)- Parameters:
text
- The text to search within (should not be null)license
- The standard SPDX license to search for (should not be null)- Returns:
- True if the license is found within the text, false otherwise (or if either argument is null)
-
isStandardLicenseExceptionWithinText
public static boolean isStandardLicenseExceptionWithinText(String text, org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicenseException exception)
Detect if a text contains the standard license exception (perhaps along with other text before and/or after)- Parameters:
text
- The text to search within (should not be null)exception
- The standard SPDX license exception to search for (should not be null)- Returns:
- True if the license exception is found within the text, false otherwise (or if either argument is null)
-
matchingStandardLicenseIds
public static String[] matchingStandardLicenseIds(String licenseText) throws org.spdx.core.InvalidSPDXAnalysisException, SpdxCompareException
Returns a list of SPDX Standard License ID's that match the text provided using the SPDX matching guidelines.- Parameters:
licenseText
- Text to compare to the standard license texts- Returns:
- Array of SPDX standard license IDs that match
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard licensesSpdxCompareException
- If an error occurs in the comparison
-
matchingStandardLicenseIdsWithinText
public static List<String> matchingStandardLicenseIdsWithinText(String text, List<String> licenseIds) throws org.spdx.core.InvalidSPDXAnalysisException
Returns a list of SPDX Standard License ID's from the provided list that were found within the text, using the SPDX matching guidelines.- Parameters:
text
- Text to compare tolicenseIds
- License ids to compare against- Returns:
- List of SPDX standard license IDs from licenseIds that match
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard licenses
-
matchingStandardLicenseIdsWithinText
public static List<String> matchingStandardLicenseIdsWithinText(String text) throws org.spdx.core.InvalidSPDXAnalysisException
Returns a list of SPDX Standard License ID's that were found within the text, using the SPDX matching guidelines.- Parameters:
text
- Text to compare to all the standard licenses- Returns:
- List of SPDX standard license IDs that match
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard licenses
-
matchingStandardLicenseExceptionIdsWithinText
public static List<String> matchingStandardLicenseExceptionIdsWithinText(String text, List<String> licenseExceptionIds) throws org.spdx.core.InvalidSPDXAnalysisException
Returns a list of SPDX Standard License Exception ID's from the provided list that were found within the text, using the SPDX matching guidelines.- Parameters:
text
- Text to compare tolicenseExceptionIds
- License Exceptions Ids to compare against- Returns:
- List of SPDX standard license exception IDs from licenseExceptionIds that match
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard license exceptions
-
matchingStandardLicenseExceptionIdsWithinText
public static List<String> matchingStandardLicenseExceptionIdsWithinText(String text) throws org.spdx.core.InvalidSPDXAnalysisException
Returns a list of SPDX Standard License Exception ID's that were found within the text, using the SPDX matching guidelines.- Parameters:
text
- Text to compare to all the standard license exceptions- Returns:
- List of SPDX standard license exception IDs that match
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard license exceptions
-
isLicensePassBlackList
public static boolean isLicensePassBlackList(org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license, String... blackList)
Detect if a license pass black lists- Parameters:
license
- licenseblackList
- license black list- Returns:
- if the license pass black lists
-
isLicensePassWhiteList
public static boolean isLicensePassWhiteList(org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license, String... whiteList)
Detect if a license pass white lists- Parameters:
license
- licensewhiteList
- license white list- Returns:
- if the license pass white lists
-
isLicenseEqual
public static boolean isLicenseEqual(org.spdx.library.model.v2.license.AnyLicenseInfo license1, org.spdx.library.model.v2.license.AnyLicenseInfo license2, Map<String,String> xlationMap) throws SpdxCompareException, org.spdx.core.InvalidSPDXAnalysisException
Compares two licenses from potentially two different documents which may have different license ID's for the same license- Parameters:
license1
- first license to comparelicense2
- second license to comparexlationMap
- Mapping the license URIs from license 1 to license 2- Returns:
- true if the 2 licenses are equal
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard license exceptionsSpdxCompareException
- If an error occurs in the comparison
-
isTextStandardLicense
public static CompareTemplateOutputHandler.DifferenceDescription isTextStandardLicense(org.spdx.library.model.v2.license.License license, String compareText) throws SpdxCompareException, org.spdx.core.InvalidSPDXAnalysisException
Compares license text to the license text of an SPDX Standard License- Parameters:
license
- SPDX Standard License to comparecompareText
- Text to compare to the standard license- Returns:
- any differences found
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard license exceptionsSpdxCompareException
- If an error occurs in the comparison
-
isTextStandardException
public static CompareTemplateOutputHandler.DifferenceDescription isTextStandardException(org.spdx.library.model.v2.license.LicenseException exception, String compareText) throws SpdxCompareException, org.spdx.core.InvalidSPDXAnalysisException
Compares exception text to the exception text of an SPDX Standard exception- Parameters:
exception
- SPDX Standard exception to comparecompareText
- Text to compare to the standard exceptions- Returns:
- any differences found
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard license exceptionsSpdxCompareException
- If an error occurs in the comparison
-
isStandardLicenseWithinText
public static boolean isStandardLicenseWithinText(String text, org.spdx.library.model.v2.license.SpdxListedLicense license)
Detect if a text contains the standard license (perhaps along with other text before and/or after)- Parameters:
text
- The text to search within (should not be null)license
- The standard SPDX license to search for (should not be null)- Returns:
- True if the license is found within the text, false otherwise (or if either argument is null)
-
isStandardLicenseExceptionWithinText
public static boolean isStandardLicenseExceptionWithinText(String text, org.spdx.library.model.v2.license.ListedLicenseException exception)
Detect if a text contains the standard license exception (perhaps along with other text before and/or after)- Parameters:
text
- The text to search within (should not be null)exception
- The standard SPDX license exception to search for (should not be null)- Returns:
- True if the license exception is found within the text, false otherwise (or if either argument is null)
-
isLicensePassBlackList
public static boolean isLicensePassBlackList(org.spdx.library.model.v2.license.AnyLicenseInfo license, String... blackList) throws org.spdx.core.InvalidSPDXAnalysisException
Detect if a license pass black lists- Parameters:
license
- licenseblackList
- license black list- Returns:
- if the license pass black lists
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard license exceptions
-
isLicensePassWhiteList
public static boolean isLicensePassWhiteList(org.spdx.library.model.v2.license.AnyLicenseInfo license, String... whiteList) throws org.spdx.core.InvalidSPDXAnalysisException
Detect if a license pass white lists- Parameters:
license
- licensewhiteList
- license white list- Returns:
- if the license pass white lists
- Throws:
org.spdx.core.InvalidSPDXAnalysisException
- If an error occurs accessing the standard license exceptions
-
-