Skip to content

Method: MappingFileParser()

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
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: *
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.jopa.util;
16:
17: import java.io.*;
18: import java.net.URI;
19: import java.util.HashMap;
20: import java.util.Map;
21: import java.util.StringTokenizer;
22:
23: public class MappingFileParser {
24:
25: public static Map<URI, URI> getMappings(final File mf) {
26: final Map<URI, URI> map = new HashMap<>();
27: String line;
28: if (!mf.exists()) {
29: throw new IllegalArgumentException("Mapping file " + mf.getPath() + " not found.");
30: }
31: final File defaultDir = mf.getParentFile();
32: try (final BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(mf)))) {
33: while ((line = r.readLine()) != null) {
34: final StringTokenizer t = new StringTokenizer(line, ">");
35: if (t.countTokens() != 2) {
36: System.out.println("Ignoring line '" + line + "' - invalid number of tokens=" + t.countTokens());
37: continue;
38: }
39:
40: final String uriName = t.nextToken().trim();
41: final String fileName = t.nextToken().trim();
42: final File actualFile =
43: (new File(fileName).isAbsolute()) ? new File(fileName) : new File(defaultDir, fileName);
44:
45: map.put(URI.create(uriName), actualFile.toURI());
46: }
47: } catch (IOException e) {
48: e.printStackTrace();
49: }
50:
51: return map;
52: }
53: }