Skip to content
Snippets Groups Projects

Resolve "Robusteres Hinzufügen der Lizenzinformationen"

Merged Thorge Petersen requested to merge 24-robusteres-hinzufugen-der-lizenzinformationen into main
1 file
+ 42
8
Compare changes
  • Side-by-side
  • Inline
@@ -25,8 +25,10 @@ import org.slf4j.Logger;
@@ -25,8 +25,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.IOException;
 
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URL;
 
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDate;
import java.util.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Collectors;
@@ -47,6 +49,7 @@ public class MDMetadata2Dataset {
@@ -47,6 +49,7 @@ public class MDMetadata2Dataset {
boolean ignoreInvalidMapping = true;
boolean ignoreInvalidMapping = true;
private CswInterface cswInterface;
private CswInterface cswInterface;
private BridgeSettings settings;
private BridgeSettings settings;
 
private final Map<String, JSONObject> url2license = new HashMap<>();
public MDMetadata2Dataset(Model model, CswInterface cswInterface, BridgeSettings bridgeSettings) {
public MDMetadata2Dataset(Model model, CswInterface cswInterface, BridgeSettings bridgeSettings) {
this(model);
this(model);
@@ -77,6 +80,12 @@ public class MDMetadata2Dataset {
@@ -77,6 +80,12 @@ public class MDMetadata2Dataset {
final String value = entry.getString(key);
final String value = entry.getString(key);
keywordMapping.put(key, value);
keywordMapping.put(key, value);
}
}
 
 
JSONArray licenses = new JSONArray(new JSONTokener(Objects.requireNonNull(getClass().getResourceAsStream("/licenses.json"))));
 
for (Object l : licenses) {
 
JSONObject license = (JSONObject) l;
 
url2license.put(license.getString("uri"), license);
 
}
}
}
static String getTextOrNull(Node node) {
static String getTextOrNull(Node node) {
@@ -533,18 +542,43 @@ public class MDMetadata2Dataset {
@@ -533,18 +542,43 @@ public class MDMetadata2Dataset {
}
}
private void addLicenseInformation(Resource resource, List<Node> otherLegalConstraints) {
private void addLicenseInformation(Resource resource, List<Node> otherLegalConstraints) {
final JSONObject licenceInformation = findLicenseInformation(otherLegalConstraints);
final JSONObject licenseInformation = findLicenseInformation(otherLegalConstraints);
if (licenceInformation != null) {
resource.addLiteral(DCATAPde.licenseAttributionByText, licenceInformation.getString("quelle"));
if (licenseInformation != null) {
if (licenceInformation.has("url") && licenceInformation.getString("url").startsWith("http://dcat-ap.de/def/licenses/")) {
resource.addLiteral(DCATAPde.licenseAttributionByText, licenseInformation.getString("quelle"));
// Some publishers specify the licence as the URL and not the id.
resource.addProperty(DCTerms.license, model.createResource(licenceInformation.getString("url")));
if (licenseInformation.has("url")) {
 
String licenseURL = licenseInformation.getString("url");
 
 
if (licenseURL.startsWith("http://dcat-ap.de/def/licenses/")) {
 
resource.addProperty(DCTerms.license, model.createResource(licenseURL));
 
} else {
 
final JSONObject license = url2license.get(licenseURL);
 
 
if (license != null && license.has("uri")) {
 
resource.addProperty(DCTerms.license, model.createResource(license.getString("uri")));
 
} else {
 
log.info("Unknown license: {}", licenseURL);
 
handleUnknownLicense(resource, licenseInformation.getString("id"));
 
}
 
}
} else {
} else {
resource.addProperty(DCTerms.license, model.createResource("http://dcat-ap.de/def/licenses/" + licenceInformation.getString("id")));
handleUnknownLicense(resource, licenseInformation.getString("id"));
}
}
}
}
}
}
 
private void handleUnknownLicense(Resource resource, String licenseId) {
 
try {
 
licenseId = URLEncoder.encode(licenseId, "UTF-8")
 
.replaceAll("%2F", "/"); // Replace the encoded slash with the original slash
 
resource.addProperty(DCTerms.license, model.createResource("http://dcat-ap.de/def/licenses/" + licenseId));
 
log.info("Falling back to: {}", licenseId);
 
} catch (UnsupportedEncodingException e) {
 
log.info("Unable to encode licenseId: {}", e);
 
}
 
}
 
private Resource guessFormat(String url) {
private Resource guessFormat(String url) {
final String lowerCaseURL = url.toLowerCase();
final String lowerCaseURL = url.toLowerCase();
if (lowerCaseURL.contains("service=wms")) {
if (lowerCaseURL.contains("service=wms")) {
Loading