Package: MappingFileParser
MappingFileParser
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
MappingFileParser(Map) |
|
|
|
|
|
||||||||||||||||||||
getMappings() |
|
|
|
|
|
||||||||||||||||||||
resolveLocation(File, String) |
|
|
|
|
|
||||||||||||||||||||
resolveMappingFile(String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: package cz.cvut.kbss.ontodriver.owlapi.util;
2:
3: import cz.cvut.kbss.ontodriver.owlapi.config.OwlapiOntoDriverProperties;
4: import cz.cvut.kbss.ontodriver.owlapi.exception.MappingFileParserException;
5: import org.slf4j.Logger;
6: import org.slf4j.LoggerFactory;
7:
8: import java.io.BufferedReader;
9: import java.io.File;
10: import java.io.FileReader;
11: import java.io.IOException;
12: import java.net.URI;
13: import java.util.HashMap;
14: import java.util.Map;
15: import java.util.StringTokenizer;
16:
17: public class MappingFileParser {
18:
19: private static final Logger LOG = LoggerFactory.getLogger(MappingFileParser.class);
20:
21: private static final String[] REMOTE_URL_SCHEMES = {"http://", "https://", "ftp://", "sftp://"};
22:
23: private final File mappingFile;
24:
25: private final String delimiter;
26:
27: public MappingFileParser(Map<String, String> properties) {
28: final String mappingFilePath = properties.get(OwlapiOntoDriverProperties.MAPPING_FILE_LOCATION);
29:• assert mappingFilePath != null;
30: this.mappingFile = resolveMappingFile(mappingFilePath);
31:• if (properties.containsKey(OwlapiOntoDriverProperties.IRI_MAPPING_DELIMITER)) {
32: this.delimiter = properties.get(OwlapiOntoDriverProperties.IRI_MAPPING_DELIMITER);
33: } else {
34: this.delimiter = OwlapiOntoDriverProperties.DEFAULT_IRI_MAPPING_DELIMITER;
35: }
36: }
37:
38: private File resolveMappingFile(String path) {
39: File mapping = new File(path);
40:• if (mapping.exists()) {
41: return mapping;
42: }
43: try {
44: mapping = new File(URI.create(path));
45:• if (mapping.exists()) {
46: return mapping;
47: }
48: } catch (IllegalArgumentException e) {
49: throw new MappingFileParserException(
50: "Mapping file path " + path + " is neither a valid file path nor a valid URI.");
51: }
52: throw new MappingFileParserException("Mapping file " + path + " does not exist.");
53: }
54:
55: public Map<URI, URI> getMappings() {
56: final Map<URI, URI> map = new HashMap<>();
57: String line;
58: final File defaultDir = mappingFile.getParentFile();
59: try (final BufferedReader r = new BufferedReader(new FileReader(mappingFile))) {
60:• while ((line = r.readLine()) != null) {
61: final StringTokenizer t = new StringTokenizer(line, delimiter);
62:• if (t.countTokens() != 2) {
63: LOG.warn("Ignoring line '" + line + "' - invalid number of tokens = " + t.countTokens());
64: continue;
65: }
66: final String uriName = t.nextToken().trim();
67: final String fileName = t.nextToken().trim();
68: final URI fileUri = resolveLocation(defaultDir, fileName);
69:
70: LOG.trace("Mapping ontology {} to location {}.", uriName, fileUri);
71: map.put(URI.create(uriName), fileUri);
72: }
73: } catch (IOException e) {
74: LOG.error("Unable to parse mapping file." + e);
75: throw new MappingFileParserException(e);
76: }
77: return map;
78: }
79:
80: private URI resolveLocation(File defaultDir, String targetUri) {
81:• for (String scheme : REMOTE_URL_SCHEMES) {
82:• if (targetUri.startsWith(scheme)) {
83: try {
84: return URI.create(targetUri);
85: } catch (IllegalArgumentException e) {
86: LOG.error("Target URI {} looks like a remote URI, but is not valid.", targetUri);
87: throw new MappingFileParserException(e);
88: }
89: }
90: }
91: final File actualFile =
92:• (new File(targetUri).isAbsolute()) ? new File(targetUri) : new File(defaultDir, targetUri);
93: return actualFile.toURI();
94: }
95: }