Skip to content

Package: MappingFileParser

MappingFileParser

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