Skip to content

Package: PrefixCcRemotePrefixResolver

PrefixCcRemotePrefixResolver

nameinstructionbranchcomplexitylinemethod
PrefixCcRemotePrefixResolver()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
resolvePrefix(IRI)
M: 12 C: 46
79%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 3 C: 10
77%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package cz.cvut.kbss.jopa.owl2java.prefix;
2:
3: import cz.cvut.kbss.jopa.owl2java.Constants;
4: import org.semanticweb.owlapi.model.IRI;
5: import org.slf4j.Logger;
6: import org.slf4j.LoggerFactory;
7:
8: import java.io.IOException;
9: import java.net.URI;
10: import java.net.http.HttpClient;
11: import java.net.http.HttpRequest;
12: import java.net.http.HttpResponse;
13: import java.util.Optional;
14:
15: /**
16: * Resolves ontology prefixes using the <a href="https://prefix.cc">prefix.cc</a> service reverse lookup.
17: */
18: public class PrefixCcRemotePrefixResolver implements RemotePrefixResolver {
19:
20: private static final Logger LOG = LoggerFactory.getLogger(PrefixCcRemotePrefixResolver.class);
21:
22: private static final String URL = "https://prefix.cc/reverse?format=ini&uri=";
23:
24: private final HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();
25:
26: @Override
27: public Optional<String> resolvePrefix(IRI ontologyIri) {
28: LOG.trace("Attempting to resolve prefix for IRI <{}> via prefix.cc", ontologyIri);
29: try {
30: final HttpRequest req = HttpRequest.newBuilder(URI.create(URL + ontologyIri.getIRIString())).GET()
31: .timeout(Constants.PREFIX_RESOLVE_TIMEOUT).build();
32: final HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandlers.ofString());
33:• if (resp.statusCode() != 200) {
34: LOG.debug("Prefix for ontology IRI <{}> not found.", ontologyIri);
35: return Optional.empty();
36: }
37: final String[] parts = resp.body().split("=");
38:• assert parts.length == 2;
39: return Optional.of(parts[0]);
40: } catch (IOException | InterruptedException e) {
41: LOG.error("Unable to resolve prefix for ontology IRI <{}>.", ontologyIri, e);
42: return Optional.empty();
43: }
44: }
45: }