1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util;
20
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import static java.util.Objects.requireNonNull;
26
27
28
29
30
31
32 public final class PathUtils {
33 private PathUtils() {
34
35 }
36
37 private static final Map<String, String> ILLEGAL_PATH_SEGMENT_REPLACEMENTS;
38
39 static {
40 HashMap<String, String> illegalPathSegmentReplacements = new HashMap<>();
41 illegalPathSegmentReplacements.put("\\", "-BACKSLASH-");
42 illegalPathSegmentReplacements.put("/", "-SLASH-");
43 illegalPathSegmentReplacements.put(":", "-COLON-");
44 illegalPathSegmentReplacements.put("\"", "-QUOTE-");
45 illegalPathSegmentReplacements.put("<", "-LT-");
46 illegalPathSegmentReplacements.put(">", "-GT-");
47 illegalPathSegmentReplacements.put("|", "-PIPE-");
48 illegalPathSegmentReplacements.put("?", "-QMARK-");
49 illegalPathSegmentReplacements.put("*", "-ASTERISK-");
50 ILLEGAL_PATH_SEGMENT_REPLACEMENTS = Collections.unmodifiableMap(illegalPathSegmentReplacements);
51 }
52
53
54
55
56
57
58
59
60
61
62 public static String stringToPathSegment(String string) {
63 requireNonNull(string);
64 StringBuilder result = new StringBuilder(string);
65 for (Map.Entry<String, String> entry : ILLEGAL_PATH_SEGMENT_REPLACEMENTS.entrySet()) {
66 String illegal = entry.getKey();
67 int pos = result.indexOf(illegal);
68 while (pos >= 0) {
69 result.replace(pos, pos + illegal.length(), entry.getValue());
70 pos = result.indexOf(illegal);
71 }
72 }
73 return result.toString();
74 }
75 }