LicenseMapper.java
package info.textgrid.services.textapi.apiv2;
/**
* Utility class to map TextGrid license information to SPDX license identifiers.
* TextGrid metadata provides license with name and URI, while TextAPI expects SPDX identifiers.
*/
public class LicenseMapper {
/**
* Maps a TextGrid license to an SPDX license identifier.
* If the license cannot be mapped to a known SPDX identifier, null is returned.
*
* @param licenseName the license name from TextGrid metadata (e.g., "CC BY 4.0")
* @param licenseUri the license URI from TextGrid metadata (e.g., "https://creativecommons.org/licenses/by/4.0/")
* @return SPDX license identifier as string, or null if not mappable
*/
public static String mapToSpdx(String licenseName, String licenseUri) {
// First, try to map based on license URI (more reliable)
if (licenseUri != null) {
String uri = licenseUri.toString();
if (uri.contains("creativecommons.org/licenses/by/4.0")) {
return "CC-BY-4.0";
}
if (uri.contains("creativecommons.org/licenses/by-sa/4.0")) {
return "CC-BY-SA-4.0";
}
if (uri.contains("creativecommons.org/licenses/by-nc/4.0")) {
return "CC-BY-NC-4.0";
}
if (uri.contains("creativecommons.org/licenses/by-nc-sa/4.0")) {
return "CC-BY-NC-SA-4.0";
}
if (uri.contains("creativecommons.org/licenses/by-nd/4.0")) {
return "CC-BY-ND-4.0";
}
if (uri.contains("creativecommons.org/licenses/by-nc-nd/4.0")) {
return "CC-BY-NC-ND-4.0";
}
if (uri.contains("creativecommons.org/publicdomain/zero/1.0")) {
return "CC0-1.0";
}
if (uri.contains("creativecommons.org/publicdomain/mark/1.0")) {
return "PDM-1.0";
}
if (uri.contains("spdx.org/licenses/")) {
// Extract SPDX identifier from URI like https://spdx.org/licenses/MIT
String[] parts = uri.split("/");
for (int i = 0; i < parts.length; i++) {
if ("licenses".equals(parts[i]) && i + 1 < parts.length) {
return parts[i + 1];
}
}
}
}
// Fallback: try to map based on license name
if (licenseName != null && !licenseName.isEmpty()) {
String name = licenseName.trim();
// Creative Commons licenses
if (name.equalsIgnoreCase("CC0") || name.equalsIgnoreCase("CC0-1.0")) {
return "CC0-1.0";
}
if (name.equalsIgnoreCase("CC BY 4.0")) {
return "CC-BY-4.0";
}
if (name.equalsIgnoreCase("CC BY-SA 4.0")) {
return "CC-BY-SA-4.0";
}
if (name.equalsIgnoreCase("CC BY-NC 4.0")) {
return "CC-BY-NC-4.0";
}
if (name.equalsIgnoreCase("CC BY-NC-SA 4.0")) {
return "CC-BY-NC-SA-4.0";
}
if (name.equalsIgnoreCase("CC BY-ND 4.0")) {
return "CC-BY-ND-4.0";
}
if (name.equalsIgnoreCase("CC BY-NC-ND 4.0")) {
return "CC-BY-NC-ND-4.0";
}
if (name.equalsIgnoreCase("CC0-1.0")) {
return "CC0-1.0";
}
if (name.equalsIgnoreCase("PDM")) {
return "PDM-1.0";
}
// Common open source licenses
if (name.equalsIgnoreCase("MIT")) {
return "MIT";
}
if (name.equalsIgnoreCase("Apache-2.0")) {
return "Apache-2.0";
}
if (name.equalsIgnoreCase("GPL-3.0-only") || name.equalsIgnoreCase("GPLv3")) {
return "GPL-3.0-only";
}
if (name.equalsIgnoreCase("LGPL-3.0-only")) {
return "LGPL-3.0-only";
}
if (name.equalsIgnoreCase("BSD-2-Clause")) {
return "BSD-2-Clause";
}
if (name.equalsIgnoreCase("BSD-3-Clause")) {
return "BSD-3-Clause";
}
}
// Cannot map to SPDX identifier
return null;
}
/**
* Maps a TextGrid license object to an SPDX license identifier.
*
* @param license the license object from TextGrid metadata
* @return SPDX license identifier as string, or null if not mappable
*/
public static String mapToSpdx(info.textgrid.namespaces.metadata.core._2010.EditionType.License license) {
if (license == null) {
return null;
}
return mapToSpdx(license.getValue(), license.getLicenseUri());
}
}