• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / ro-crate-java / #576

10 Nov 2025 11:54AM UTC coverage: 91.45% (+0.6%) from 90.804%
#576

Pull #279

github

Pfeil
fix: error in local file path detection
Pull Request #279: Next Version (2.1.0-rc4)

238 of 253 new or added lines in 8 files covered. (94.07%)

1 existing line in 1 file now uncovered.

2246 of 2456 relevant lines covered (91.45%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/main/java/edu/kit/datamanager/ro_crate/util/FileSystemUtil.java
1
package edu.kit.datamanager.ro_crate.util;
2

3
import org.apache.commons.io.FileUtils;
4

5
import java.io.File;
6
import java.io.IOException;
7
import java.util.Collection;
8
import java.util.regex.Matcher;
9

10
public class FileSystemUtil {
11
    private FileSystemUtil() {
12
        // Utility class, no instantiation
13
    }
14

15
    /**
16
     * Checks if the given ID appears to be a file path.
17
     * <p>
18
     * Specifically excludes IDs starting with "doi:", "http", or "https".
19
     *
20
     * @param id the ID to check
21
     * @return true if it looks like a file path, false otherwise
22
     */
23
    public static boolean isFilePath(String id) {
24
        return id != null && !(
1✔
25
                id.startsWith("doi:") ||
1✔
26
                id.startsWith("http://") ||
1✔
27
                id.startsWith("https://")
1✔
28
        );
29
    }
30

31
    /**
32
     * Gets the parent path of a given path.
33
     * @param path the path to evaluate.
34
     * @return the parent path, or null if no parent exists.
35
     */
36
    public static String getParentPath(String path) {
37
        if (path == null || path.equals("./") || path.isEmpty()) {
1✔
38
            return null;
1✔
39
        }
40

41
        // Normalize path - remove trailing slash for consistency
42
        String normalizedPath = path.endsWith("/")
1✔
43
                ? path.substring(0, path.length() - 1)
1✔
44
                : path;
1✔
45

46
        int lastSlash = normalizedPath.lastIndexOf('/');
1✔
47
        if (lastSlash == -1) {
1✔
48
            return "./"; // Root directory
1✔
49
        }
50

51
        String parentPath = normalizedPath.substring(0, lastSlash);
1✔
52

53
        // If parent is empty, it's root
54
        if (parentPath.isEmpty()) {
1✔
NEW
55
            return "./";
×
56
        }
57

58
        // For validation, we need to check both with and without trailing slash
59
        // since files don't have trailing slash but folders do
60
        return parentPath;
1✔
61
    }
62

63
    /**
64
     * Removes a specific set of given file extensions from a file name, if present.
65
     * The extensions are case-insensitive. Given "ELN", "eln" or "Eln" will also match.
66
     * The dot (.) before the extension is also assumed and removed implicitly:
67
     * <p>
68
     * Example:
69
     * filterExtensionsFromFileName("test.eln", Set.of("ELN")) -> "test"
70
     *
71
     * @param filename the file name to filter (must not be null)
72
     * @param extensionsToRemove the extensions to remove (must not be null)
73
     * @return the filtered file name
74
     * @throws NullPointerException if any parameter is null
75
     */
76
    public static String filterExtensionsFromFileName(String filename, Collection<String> extensionsToRemove) {
77
        String dot = Matcher.quoteReplacement(".");
1✔
78
        String end = Matcher.quoteReplacement("$");
1✔
79
        for (String extension : extensionsToRemove) {
1✔
80
            // (?i) removes case sensitivity
81
            filename = filename.replaceFirst("(?i)" + dot + extension + end, "");
1✔
82
        }
1✔
83
        return filename;
1✔
84
    }
85

86
    /**
87
     * Ensures that a given path ends with a trailing slash.
88
     *
89
     * @param path the path to check
90
     * @return the path with a trailing slash if it didn't have one, or the original path
91
     */
92
    public static String ensureTrailingSlash(String path) {
93
        if (path == null || path.isEmpty()) {
1✔
94
            return path;
1✔
95
        }
96
        if (!path.endsWith("/")) {
1✔
97
            return path + "/";
1✔
98
        }
99
        return path;
1✔
100
    }
101

102
    /**
103
     * Creates a directory or deletes its content if it already exists.
104
     *
105
     * @param folder the folder to create or delete content from
106
     * @throws IOException if an I/O error occurs
107
     */
108
    public static void mkdirOrDeleteContent(File folder) throws IOException {
109
        File[] files = folder.listFiles();
1✔
110
        boolean isNonEmptyDir = folder.exists()
1✔
111
                && folder.isDirectory()
1✔
112
                && files != null
113
                && files.length > 0;
114
        boolean isFile = folder.exists()
1✔
115
                && !folder.isDirectory();
1✔
116

117
        if (isNonEmptyDir || isFile) {
1✔
118
            FileUtils.forceDelete(folder);
1✔
119
        }
120
        FileUtils.forceMkdir(folder);
1✔
121
    }
1✔
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc