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 IntegerCROSS_REF_NUM_WORDS_MATCHprotected static Map<String,String>NORMALIZE_TOKENSprotected static Set<String>PUNCTUATIONprotected static PatternREGEX_QUANTIFIER_PATTERNprotected static Set<String>SKIPPABLE_TOKENSprotected static PatternTOKEN_SPLIT_PATTERNprotected static StringTOKEN_SPLIT_REGEX -
Constructor Summary
Constructors Constructor Description LicenseCompareHelper() -
Method Summary
Modifier and Type Method Description static StringgetFirstLicenseToken(String text)static List<String>getNonOptionalLicenseText(String licenseTemplate, boolean includeVarText)Deprecated.static List<String>getNonOptionalLicenseText(String licenseTemplate, FilterTemplateOutputHandler.VarTextHandling varTextHandling)Get the text of a license minus any optional text - note: this include the default variable textstatic booleanisLicenseEqual(AnyLicenseInfo license1, 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 booleanisLicensePassBlackList(AnyLicenseInfo license, String... blackList)Detect if a license pass black listsstatic booleanisLicensePassWhiteList(AnyLicenseInfo license, String... whiteList)Detect if a license pass white listsstatic booleanisLicenseTextEquivalent(String licenseTextA, String licenseTextB)Returns true if two sets of license text is considered a match per the SPDX License matching guidelines documented at spdx.org (currently http://spdx.org/wiki/spdx-license-list-match-guidelines) There are 2 unimplemented features - bullets/numbering is not considered and comments with no whitespace between text is not skippedstatic booleanisSingleTokenString(String text)static booleanisStandardLicenseExceptionWithinText(String text, ListedLicenseException exception)Detect if a text contains the standard license exception (perhaps along with other text before and/or after)static booleanisStandardLicenseWithinText(String text, SpdxListedLicense license)Detect if a text contains the standard license (perhaps along with other text before and/or after)static CompareTemplateOutputHandler.DifferenceDescriptionisTextStandardException(LicenseException exception, String compareText)Compares exception text to the exception text of an SPDX Standard exceptionstatic CompareTemplateOutputHandler.DifferenceDescriptionisTextStandardLicense(License license, String compareText)Compares license text to the license text of an SPDX Standard Licensestatic StringlocateOriginalText(String fullLicenseText, int startToken, int endToken, Map<Integer,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 PatternnonOptionalTextToStartPattern(List<String> nonOptionalText, int numberOfWords)Creates a regular expression pattern to match the start of a license textstatic StringnormalizeText(String s)Normalize quotes and no-break spacesstatic StringremoveCommentChars(String s)Remove common comment characters from either a template or license text stringsstatic StringremoveLineSeparators(String s)static String[]tokenizeLicenseText(String licenseText, Map<Integer,LineColumn> tokenToLocation)Tokenizes the license text, normalizes quotes, lowercases and converts multi-words for better equiv.
-
Field Details
-
Constructor Details
-
LicenseCompareHelper
public LicenseCompareHelper()
-
-
Method Details
-
isLicenseTextEquivalent
Returns true if two sets of license text is considered a match per the SPDX License matching guidelines documented at spdx.org (currently http://spdx.org/wiki/spdx-license-list-match-guidelines) There are 2 unimplemented features - bullets/numbering is not considered and comments with no whitespace between text is not skipped- Parameters:
licenseTextA-licenseTextB-- Returns:
-
removeLineSeparators
- Parameters:
s- Input string- Returns:
- s without any line separators (---, ***, ===)
-
removeCommentChars
Remove common comment characters from either a template or license text strings- Parameters:
s-- Returns:
-
normalizeText
Normalize quotes and no-break spaces- Parameters:
s- String to normalize- Returns:
- String normalized for comparison
-
locateOriginalText
public static String locateOriginalText(String fullLicenseText, int startToken, int endToken, Map<Integer,LineColumn> tokenToLocation, String[] tokens)Locate the original text starting with the start token and ending with the end token- Parameters:
fullLicenseText-startToken-endToken-tokenToLocation-- Returns:
-
tokenizeLicenseText
public static String[] tokenizeLicenseText(String licenseText, Map<Integer,LineColumn> tokenToLocation)Tokenizes the license text, normalizes quotes, lowercases and converts multi-words for better equiv. comparisons- Parameters:
tokenToLocation- location for all of the tokenslicenseText-- Returns:
- tokens
-
getFirstLicenseToken
- Parameters:
text-- Returns:
- the first token in the license text
-
isSingleTokenString
- Parameters:
text-- Returns:
- true if the text contains a single token
-
isLicenseEqual
public static boolean isLicenseEqual(AnyLicenseInfo license1, AnyLicenseInfo license2, Map<String,String> xlationMap) throws SpdxCompareException, InvalidSPDXAnalysisExceptionCompares two licenses from potentially two different documents which may have different license ID's for the same license- Parameters:
license1-license2-xlationMap- Mapping the license ID's from license 1 to license 2- Returns:
- Throws:
SpdxCompareExceptionInvalidSPDXAnalysisException
-
getNonOptionalLicenseText
@Deprecated public static List<String> getNonOptionalLicenseText(String licenseTemplate, boolean includeVarText) throws SpdxCompareExceptionDeprecated.Get the text of a license minus any optional text - note: this include the default variable text- Parameters:
licenseTemplate- license template containing optional and var tagsincludeVarText- if true, include the default variable text; if false remove the variable text- Returns:
- list of strings for all non-optional license text.
- Throws:
SpdxCompareException
-
getNonOptionalLicenseText
public static List<String> getNonOptionalLicenseText(String licenseTemplate, FilterTemplateOutputHandler.VarTextHandling varTextHandling) throws SpdxCompareExceptionGet the text of a license minus any optional text - note: this include 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
-
nonOptionalTextToStartPattern
public static Pattern nonOptionalTextToStartPattern(List<String> nonOptionalText, int numberOfWords)Creates a regular expression pattern to match the start of a license text- Parameters:
nonOptionalText- List of strings of non-optional text from the license template (see List<String> getNonOptionalLicenseText)numberOfWords- Number of words to use in the match- Returns:
- Pattern which will match the start of the license text
-
isTextStandardLicense
public static CompareTemplateOutputHandler.DifferenceDescription isTextStandardLicense(License license, String compareText) throws SpdxCompareException, InvalidSPDXAnalysisExceptionCompares 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:
SpdxCompareExceptionInvalidSPDXAnalysisException
-
isTextStandardException
public static CompareTemplateOutputHandler.DifferenceDescription isTextStandardException(LicenseException exception, String compareText) throws SpdxCompareException, InvalidSPDXAnalysisExceptionCompares 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:
SpdxCompareExceptionInvalidSPDXAnalysisException
-
isStandardLicenseWithinText
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, 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 InvalidSPDXAnalysisException, SpdxCompareExceptionReturns 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:
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 InvalidSPDXAnalysisException, SpdxCompareExceptionReturns 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:
InvalidSPDXAnalysisException- If an error occurs accessing the standard licensesSpdxCompareException- If an error occurs in the comparison
-
matchingStandardLicenseIdsWithinText
public static List<String> matchingStandardLicenseIdsWithinText(String text) throws InvalidSPDXAnalysisException, SpdxCompareExceptionReturns 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 of the standard licenses- Returns:
- List of SPDX standard license IDs that match
- Throws:
InvalidSPDXAnalysisException- If an error occurs accessing the standard licensesSpdxCompareException- If an error occurs in the comparison
-
matchingStandardLicenseExceptionIdsWithinText
public static List<String> matchingStandardLicenseExceptionIdsWithinText(String text, List<String> licenseExceptionIds) throws InvalidSPDXAnalysisException, SpdxCompareExceptionReturns 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:
- Array of SPDX standard license exception IDs from licenseExceptionIds that match
- Throws:
InvalidSPDXAnalysisException- If an error occurs accessing the standard license exceptionsSpdxCompareException- If an error occurs in the comparison
-
matchingStandardLicenseExceptionIdsWithinText
public static List<String> matchingStandardLicenseExceptionIdsWithinText(String text) throws InvalidSPDXAnalysisException, SpdxCompareExceptionReturns 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 of the standard license exceptions- Returns:
- Array of SPDX standard license exception IDs that match
- Throws:
InvalidSPDXAnalysisException- If an error occurs accessing the standard license exceptionsSpdxCompareException- If an error occurs in the comparison
-
isLicensePassBlackList
public static boolean isLicensePassBlackList(AnyLicenseInfo license, String... blackList) throws InvalidSPDXAnalysisExceptionDetect if a license pass black lists- Parameters:
license- licenseblackList- license black list- Returns:
- if the license pass black lists
- Throws:
InvalidSPDXAnalysisException
-
isLicensePassWhiteList
public static boolean isLicensePassWhiteList(AnyLicenseInfo license, String... whiteList) throws InvalidSPDXAnalysisExceptionDetect if a license pass white lists- Parameters:
license- licensewhiteList- license white list- Returns:
- if the license pass white lists
- Throws:
InvalidSPDXAnalysisException
-