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: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.owl2java.prefix;
19:
20: import cz.cvut.kbss.jopa.owl2java.Constants;
21: import org.semanticweb.owlapi.model.IRI;
22: import org.slf4j.Logger;
23: import org.slf4j.LoggerFactory;
24:
25: import java.io.IOException;
26: import java.net.URI;
27: import java.net.http.HttpClient;
28: import java.net.http.HttpRequest;
29: import java.net.http.HttpResponse;
30: import java.util.Optional;
31:
32: /**
33: * Resolves ontology prefixes using the <a href="https://prefix.cc">prefix.cc</a> service reverse lookup.
34: */
35: public class PrefixCcRemotePrefixResolver implements RemotePrefixResolver {
36:
37: private static final Logger LOG = LoggerFactory.getLogger(PrefixCcRemotePrefixResolver.class);
38:
39: private static final String URL = "https://prefix.cc/reverse?format=ini&uri=";
40:
41: private final HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();
42:
43: @Override
44: public Optional<String> resolvePrefix(IRI ontologyIri) {
45: LOG.trace("Attempting to resolve prefix for IRI <{}> via prefix.cc", ontologyIri);
46: try {
47: final HttpRequest req = HttpRequest.newBuilder(URI.create(URL + ontologyIri.getIRIString())).GET()
48: .timeout(Constants.PREFIX_RESOLVE_TIMEOUT).build();
49: final HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandlers.ofString());
50:• if (resp.statusCode() != 200) {
51: LOG.debug("Prefix for ontology IRI <{}> not found.", ontologyIri);
52: return Optional.empty();
53: }
54: final String[] parts = resp.body().split("=");
55:• assert parts.length == 2;
56: return Optional.of(parts[0]);
57: } catch (IOException | InterruptedException e) {
58: LOG.error("Unable to resolve prefix for ontology IRI <{}>.", ontologyIri, e);
59: return Optional.empty();
60: }
61: }
62: }