Class LicenseCompareHelper


  • public class LicenseCompareHelper
    extends Object
    Primarily a static class of helper functions for comparing two SPDX licenses
    Author:
    Gary O'Neall
    • Field Detail

      • CROSS_REF_NUM_WORDS_MATCH

        protected static final Integer CROSS_REF_NUM_WORDS_MATCH
      • REGEX_QUANTIFIER_PATTERN

        protected static final Pattern REGEX_QUANTIFIER_PATTERN
    • Constructor Detail

      • LicenseCompareHelper

        public LicenseCompareHelper()
    • 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 -
        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 text
        startToken - starting token
        endToken - ending token
        tokenToLocation - token location
        Returns:
        original text starting with the start token and ending with the end token
      • getFirstLicenseToken

        public static String getFirstLicenseToken​(String text)
      • 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 compare
        license2 - second license to compare
        xlationMap - Mapping the license ID's from license 1 to license 2
        Returns:
        true if the licenses equal
        Throws:
        SpdxCompareException - on comparison errors
        org.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 include the default variable text
        Parameters:
        licenseTemplate - license template containing optional and var tags
        varTextHandling - 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 tags
        varTextHandling - include original, exclude, or include the regex (enclosed with "~~~") for "var" text
        optionalTextHandling - 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,
                                                                                                org.spdx.core.InvalidSPDXAnalysisException
        Parameters:
        template - Template in the standard template format used for comparison
        compareText - Text to compare using the template
        Returns:
        any differences found
        Throws:
        SpdxCompareException - on comparison errors
        org.spdx.core.InvalidSPDXAnalysisException - on errors reading reading properties from the SPDX model
      • 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 compare
        compareText - Text to compare to the standard license
        Returns:
        any differences found
        Throws:
        SpdxCompareException - on comparison errors
        org.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 compare
        compareText - Text to compare to the standard exceptions
        Returns:
        any differences found
        Throws:
        SpdxCompareException - on comparison errors
        org.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 licenses
        SpdxCompareException - If an error occurs in the comparison
      • matchingStandardLicenseIdsWithinText

        public static List<String> matchingStandardLicenseIdsWithinText​(String text,
                                                                        List<String> licenseIds)
                                                                 throws org.spdx.core.InvalidSPDXAnalysisException,
                                                                        SpdxCompareException
        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 to
        licenseIds - 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
        SpdxCompareException - If an error occurs in the comparison
      • matchingStandardLicenseIdsWithinText

        public static List<String> matchingStandardLicenseIdsWithinText​(String text)
                                                                 throws org.spdx.core.InvalidSPDXAnalysisException,
                                                                        SpdxCompareException
        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 of 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
        SpdxCompareException - If an error occurs in the comparison
      • matchingStandardLicenseExceptionIdsWithinText

        public static List<String> matchingStandardLicenseExceptionIdsWithinText​(String text,
                                                                                 List<String> licenseExceptionIds)
                                                                          throws org.spdx.core.InvalidSPDXAnalysisException,
                                                                                 SpdxCompareException
        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 to
        licenseExceptionIds - License Exceptions Ids to compare against
        Returns:
        Array of SPDX standard license exception IDs from licenseExceptionIds that match
        Throws:
        org.spdx.core.InvalidSPDXAnalysisException - If an error occurs accessing the standard license exceptions
        SpdxCompareException - If an error occurs in the comparison
      • matchingStandardLicenseExceptionIdsWithinText

        public static List<String> matchingStandardLicenseExceptionIdsWithinText​(String text)
                                                                          throws org.spdx.core.InvalidSPDXAnalysisException,
                                                                                 SpdxCompareException
        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 of the standard license exceptions
        Returns:
        Array of SPDX standard license exception IDs that match
        Throws:
        org.spdx.core.InvalidSPDXAnalysisException - If an error occurs accessing the standard license exceptions
        SpdxCompareException - If an error occurs in the comparison
      • isLicensePassBlackList

        public static boolean isLicensePassBlackList​(org.spdx.library.model.v3_0_1.simplelicensing.AnyLicenseInfo license,
                                                     String... blackList)
                                              throws org.spdx.core.InvalidSPDXAnalysisException
        Detect if a license pass black lists
        Parameters:
        license - license
        blackList - 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.v3_0_1.simplelicensing.AnyLicenseInfo license,
                                                     String... whiteList)
                                              throws org.spdx.core.InvalidSPDXAnalysisException
        Detect if a license pass white lists
        Parameters:
        license - license
        whiteList - license white list
        Returns:
        if the license pass white lists
        Throws:
        org.spdx.core.InvalidSPDXAnalysisException - If an error occurs accessing the standard license exceptions
      • 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 compare
        license2 - second license to compare
        xlationMap - 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 exceptions
        SpdxCompareException - 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 compare
        compareText - Text to compare to the standard license
        Returns:
        any differences found
        Throws:
        org.spdx.core.InvalidSPDXAnalysisException - If an error occurs accessing the standard license exceptions
        SpdxCompareException - 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 compare
        compareText - Text to compare to the standard exceptions
        Returns:
        any differences found
        Throws:
        org.spdx.core.InvalidSPDXAnalysisException - If an error occurs accessing the standard license exceptions
        SpdxCompareException - 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 - license
        blackList - 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 - license
        whiteList - license white list
        Returns:
        if the license pass white lists
        Throws:
        org.spdx.core.InvalidSPDXAnalysisException - If an error occurs accessing the standard license exceptions