/** * Returns a list of SPDX Standard License ID's that match the text provided using the SPDX * matching guidelines. * * @param licenseText Text to compare to the standard license texts * @return Array of SPDX standard license IDs that match * @throws InvalidSPDXAnalysisException If an error occurs accessing the standard licenses * @throws SpdxCompareException If an error occurs in the comparison */ public static String[] matchingStandardLicenseIds(String licenseText) throws InvalidSPDXAnalysisException, SpdxCompareException { String[] stdLicenseIds = LicenseInfoFactory.getSpdxListedLicenseIds(); List<String> matchingIds = Lists.newArrayList(); for (String stdLicId : stdLicenseIds) { SpdxListedLicense license = LicenseInfoFactory.getListedLicenseById(stdLicId); if (isTextStandardLicense(license, licenseText)) { matchingIds.add(license.getLicenseId()); } } return matchingIds.toArray(new String[matchingIds.size()]); }
/** * Compares license text to the license text of an SPDX Standard License * * @param license SPDX Standard License to compare * @param compareText Text to compare to the standard license * @return True if the license text is the same per the license matching guidelines * @throws SpdxCompareException */ public static boolean isTextStandardLicense(SpdxListedLicense license, String compareText) throws SpdxCompareException { String licenseTemplate = license.getStandardLicenseTemplate(); if (licenseTemplate == null || licenseTemplate.trim().isEmpty()) { return isLicenseTextEquivalent(license.getLicenseText(), compareText); } CompareTemplateOutputHandler compareTemplateOutputHandler = new CompareTemplateOutputHandler(compareText); try { SpdxLicenseTemplateHelper.parseTemplate(licenseTemplate, compareTemplateOutputHandler); } catch (LicenseTemplateRuleException e) { throw (new SpdxCompareException( "Invalid template rule found during compare: " + e.getMessage(), e)); } return compareTemplateOutputHandler.matches(); }