1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 | package Torello.Java;
class DotDots
{
static String parentDir(
final String directoryStr,
final char separator,
final short nLevels
)
{
if (nLevels < 1) throw new IllegalArgumentException(
"The parameter nLevels may not be less than 1, nor negative. You have passed: " +
nLevels
);
if ((separator != '/') && (separator != '\\')) throw new IllegalArgumentException(
"The separator character provided to this method must be either a forward-slash '/' " +
"or a back-slash ('\\') character. You have provided: ['" + separator + "']."
);
int count = 0;
for (int i=directoryStr.length() - 1; i >= 0; i--)
if (directoryStr.charAt(i) == separator)
if (++count == (nLevels + 1))
return directoryStr.substring(0, i + 1);
throw new IllegalArgumentException(
"The parameter nLevels was: " + nLevels + ", but unfortunately there only were: " +
count + "'" + separator + "' characters found in the directory-string."
);
}
static String specifiedAncestor(
final String fileName,
final String ancestorDirectory,
final char separator
)
{
if ((separator != '/') && (separator != '\\')) throw new IllegalArgumentException(
"The separator character provided to this method must be either a forward-slash '/' " +
"or a back-slash ('\\') character. You have provided: ['" + separator + "']."
);
if (! fileName.startsWith(ancestorDirectory)) throw new IllegalArgumentException(
"The file-name you have provided [" + fileName + "] is a String that does " +
"start with the ancestorDirectory String [" + ancestorDirectory + "]. " +
"Therefore there is no relative path using the dot-dot construct to the named " +
"ancestor directory fromm the directory where the named file resides."
);
int levelsDeep = StringParse.countCharacters(fileName, separator) -
StringParse.countCharacters(ancestorDirectory, separator);
String dotDots = "";
while (levelsDeep-- > 0) dotDots = dotDots + ".." + separator;
return dotDots;
}
}
|