Skip to content

Package: MappingFileParser

MappingFileParser

nameinstructionbranchcomplexitylinemethod
MappingFileParser()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getMappings(File)
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%
getMappings(File, String)
M: 0 C: 97
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
resolveLocation(File, String)
M: 0 C: 53
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.util;
14:
15: import cz.cvut.kbss.jopa.owlapi.exception.MappingFileParserException;
16: import org.slf4j.Logger;
17: import org.slf4j.LoggerFactory;
18:
19: import java.io.BufferedReader;
20: import java.io.File;
21: import java.io.FileReader;
22: import java.io.IOException;
23: import java.net.URI;
24: import java.util.HashMap;
25: import java.util.Map;
26: import java.util.Objects;
27: import java.util.StringTokenizer;
28:
29: public final class MappingFileParser {
30:
31: private static final Logger LOG = LoggerFactory.getLogger(MappingFileParser.class);
32:
33: /**
34: * Default delimiter of mappings in the mapping file.
35: * <p>
36: * That is, on the left hand side of the delimiter would the original IRI and on the right hand side is the mapped
37: * value.
38: */
39: public static final String DEFAULT_DELIMITER = ">";
40:
41: private static final String[] REMOTE_URL_SCHEMES = {"http://", "https://", "ftp://", "sftp://"};
42:
43: private MappingFileParser() {
44: throw new AssertionError();
45: }
46:
47: /**
48: * Retrieves IRI mappings from the specified file using the default mapping delimiter - {@link #DEFAULT_DELIMITER}.
49: *
50: * @param mappingFile Mapping file to use
51: * @return Map of IRI mappings
52: */
53: public static Map<URI, URI> getMappings(final File mappingFile) {
54: return getMappings(mappingFile, DEFAULT_DELIMITER);
55: }
56:
57: /**
58: * Retrieves IRI mappings from the specified file using the specified mapping delimiter.
59: *
60: * @param mappingFile Mapping file to use
61: * @param delimiter Delimiter of the mapped IRI
62: * @return Map of IRI mappings
63: */
64: public static Map<URI, URI> getMappings(final File mappingFile, String delimiter) {
65: Objects.requireNonNull(mappingFile);
66: Objects.requireNonNull(delimiter);
67: final Map<URI, URI> map = new HashMap<>();
68: String line;
69: final File defaultDir = mappingFile.getParentFile();
70: try (final BufferedReader r = new BufferedReader(new FileReader(mappingFile))) {
71:• while ((line = r.readLine()) != null) {
72: final StringTokenizer t = new StringTokenizer(line, delimiter);
73:• if (t.countTokens() != 2) {
74: LOG.warn("Ignoring line '" + line + "' - invalid number of tokens = " + t.countTokens());
75: continue;
76: }
77: final String uriName = t.nextToken().trim();
78: final String fileName = t.nextToken().trim();
79: final URI fileUri = resolveLocation(defaultDir, fileName);
80:
81: LOG.debug("Mapping ontology {} to location {}.", uriName, fileUri);
82: map.put(URI.create(uriName), fileUri);
83: }
84: } catch (IOException e) {
85: LOG.error("Unable to parse mapping file." + e);
86: throw new MappingFileParserException(e);
87: }
88: return map;
89: }
90:
91: private static URI resolveLocation(File defaultDir, String targetUri) {
92:• for (String scheme : REMOTE_URL_SCHEMES) {
93:• if (targetUri.startsWith(scheme)) {
94: try {
95: return URI.create(targetUri);
96: } catch (IllegalArgumentException e) {
97: LOG.error("Target URI {} looks like a remote URI, but is not valid.", targetUri);
98: throw new MappingFileParserException(e);
99: }
100: }
101: }
102: final File actualFile =
103:• new File(targetUri).isAbsolute() ? new File(targetUri) : new File(defaultDir, targetUri);
104: return actualFile.toURI();
105: }
106: }