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 Details

    • 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 Details

    • LicenseCompareHelper

      public LicenseCompareHelper()
  • Method Details

    • licenseUriToLicenseId

      public static String licenseUriToLicenseId(String objectUri)
      Convert a license object URI to its corresponding License ID
      Parameters:
      objectUri - The URI of the license.
      Returns:
      The SPDX License ID extracted from the URI, or the original objectUri if no known prefix is found.
    • 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 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

      @Nullable public static String getFirstLicenseToken(@Nullable String text)
      Return the first license token found in the given text

      The method normalizes the input text, removes comment characters, and splits it into tokens using LicenseTextHelper.TOKEN_SPLIT_PATTERN. It returns the first non-empty token found, or null if no such token exists.

      Parameters:
      text - The license text to extract the first token from.
      Returns:
      The first non-empty token as a String, or null if none is found.
    • isSingleTokenString

      public static boolean isSingleTokenString(@Nullable String text)
      Check whether the given text contains only a single token

      A single token string is a string that contains exactly one token, as identified by the LicenseTextHelper.TOKEN_SPLIT_PATTERN. Whitespace and punctuation such as dots, commas, question marks, and quotation marks are ignored.

      Parameters:
      text - The text to test.
      Returns:
      true if the text contains exactly one token, false otherwise.
    • 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 includes 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
      Compare the provided text against a license template using SPDX matching guidelines
      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
    • 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
      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
    • 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 to
      licenseExceptionIds - 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 - license
      blackList - 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 - license
      whiteList - 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 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