fr.arakne.swflangloader.loader.TxtVersionsLoader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 42
ccs 17
cts 19
cp 0.8947
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A forLanguage(URL,String) 0 8 1
A parseVersions(String,String) 0 18 5
A versionFile(URL,String) 0 6 1
1
/*
2
 * This file is part of ArakneLangLoader.
3
 *
4
 * ArakneLangLoader is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * ArakneLangLoader is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with ArakneLangLoader.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.swflangloader.loader;
21
22
import java.io.*;
23
import java.net.MalformedURLException;
24
import java.net.URL;
25
import java.util.HashMap;
26
import java.util.Map;
27
28
/**
29
 * Load version from versions_xx.txt file
30
 */
31 1
final public class TxtVersionsLoader implements VersionsLoader {
32
    final static private String VERSION_FILE_PREFIX = "versions_";
33
    final static private String VERSION_FILE_EXTENSION = ".txt";
34
35
    @Override
36
    public Map<String, Integer> forLanguage(URL baseUrl, String language) throws IOException {
37 1
        final URL url = versionFile(baseUrl, language);
38
39 1
        try (InputStream stream = new BufferedInputStream(url.openStream())) {
40 1
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
41
42 1
            return parseVersions(reader.readLine(), language);
43
        }
44
    }
45
46
    private URL versionFile(URL baseUrl, String language) throws MalformedURLException {
47 1
        return new URL(
48 1
            baseUrl.getProtocol(),
49 1
            baseUrl.getHost(),
50 1
            baseUrl.getPort(),
51 1
            baseUrl.getFile() + "/" + VERSION_FILE_PREFIX + language + VERSION_FILE_EXTENSION
52
        );
53
    }
54
55
    private Map<String, Integer> parseVersions(String content, String language) {
56 1
        if (!content.startsWith("&f=")) {
57
            throw new IllegalArgumentException("Invalid versions file");
58
        }
59
60 1
        Map<String, Integer> versions = new HashMap<>();
61
62 1
        for (int pos = 3, nextPos; (nextPos = content.indexOf('|', pos)) != -1 ; pos = nextPos + 1) {
63 1
            final String[] parts = content.substring(pos, nextPos).split(",", 3);
64
65 1
            if (parts.length != 3 || !parts[1].equals(language)) {
66
                continue;
67
            }
68
69 1
            versions.put(parts[0], Integer.parseInt(parts[2]));
70
        }
71
72 1
        return versions;
73
    }
74
}
75