Skip to content

Commit e6e4d22

Browse files
committed
Customizing subfolders and encoding for source roots
1 parent 1b45d0a commit e6e4d22

File tree

2 files changed

+426
-38
lines changed

2 files changed

+426
-38
lines changed

plugins/sources/src/org/graalvm/visualvm/sources/SourcesRoot.java

Lines changed: 114 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package org.graalvm.visualvm.sources;
2626

27+
import java.io.File;
2728
import java.nio.charset.Charset;
2829
import java.nio.charset.StandardCharsets;
2930
import org.graalvm.visualvm.sources.impl.SourceRoots;
@@ -42,47 +43,67 @@
4243
*/
4344
public final class SourcesRoot {
4445

45-
private static final String ENCODING_PREFIX = "[encoding="; // NOI18N
46-
private static final String ENCODING_SUFFIX = "]"; // NOI18N
46+
private static final String SUBPATHS_PREFIX = "[subpaths="; // NOI18N
47+
private static final String SUBPATHS_SUFFIX = "]"; // NOI18N
48+
49+
private static final String ENCODING_PREFIX = "[encoding="; // NOI18N
50+
private static final String ENCODING_SUFFIX = "]"; // NOI18N
4751

4852

4953
private static final Logger LOGGER = Logger.getLogger(SourcesRoot.class.getName());
5054

5155

5256
private final String rootPath;
57+
private final String[] subPaths;
58+
private final Charset encoding;
5359

5460

5561
private SourcesRoot(String rootPath) {
56-
this.rootPath = rootPath;
62+
Object[] resolved = resolve(rootPath);
63+
this.rootPath = (String)resolved[0];
64+
this.subPaths = (String[])resolved[1];
65+
this.encoding = (Charset)resolved[2];
5766
}
5867

5968

6069
private SourcePathHandle getSourceHandle(String resourcePath) {
61-
Object[] resourceWithEncoding = resolveEncoding(resourcePath);
62-
String resource = (String)resourceWithEncoding[0];
63-
Charset encoding = (Charset)resourceWithEncoding[1];
64-
6570
Path root = Paths.get(rootPath);
6671

6772
try {
68-
if (Files.isDirectory(root, LinkOption.NOFOLLOW_LINKS)) return getHandleInDirectory(root, resource, encoding);
69-
else if (Files.isRegularFile(root, LinkOption.NOFOLLOW_LINKS)) return getHandleInArchive(root, resource, encoding);
73+
if (Files.isDirectory(root, LinkOption.NOFOLLOW_LINKS)) return getHandleInDirectory(root, resourcePath, subPaths, encoding);
74+
else if (Files.isRegularFile(root, LinkOption.NOFOLLOW_LINKS)) return getHandleInArchive(root, resourcePath, subPaths, encoding);
7075
} catch (Throwable t) {
71-
LOGGER.log(Level.INFO, "Failed resolving source file " + resource + " in " + root, t); // NOI18N
76+
LOGGER.log(Level.INFO, "Failed resolving source file " + resourcePath + " in " + root, t); // NOI18N
7277
}
7378

7479
return null;
7580
}
7681

77-
private static SourcePathHandle getHandleInDirectory(Path directory, String sourcePath, Charset encoding) throws Throwable {
78-
Path sourceFile = directory.resolve(sourcePath);
79-
return isFile(sourceFile) ? new SourcePathHandle(sourceFile, false, encoding) : null;
82+
private static SourcePathHandle getHandleInDirectory(Path directory, String sourcePath, String[] subPaths, Charset encoding) throws Throwable {
83+
if (subPaths == null) {
84+
Path sourceFile = directory.resolve(sourcePath);
85+
return isFile(sourceFile) ? new SourcePathHandle(sourceFile, false, encoding) : null;
86+
} else {
87+
for (String subPath : subPaths) {
88+
Path sourceFile = directory.resolve(subPath + "/" + sourcePath); // NOI18N
89+
if (isFile(sourceFile)) return new SourcePathHandle(sourceFile, false, encoding);
90+
}
91+
return null;
92+
}
8093
}
8194

82-
private static SourcePathHandle getHandleInArchive(Path archive, String sourcePath, Charset encoding) throws Throwable {
95+
private static SourcePathHandle getHandleInArchive(Path archive, String sourcePath, String[] subPaths, Charset encoding) throws Throwable {
8396
FileSystem archiveFileSystem = FileSystems.newFileSystem(archive, null);
84-
Path sourceFile = archiveFileSystem.getPath(sourcePath);
85-
return isFile(sourceFile) ? new SourcePathHandle(sourceFile, true, encoding) : null;
97+
if (subPaths == null) {
98+
Path sourceFile = archiveFileSystem.getPath(sourcePath);
99+
return isFile(sourceFile) ? new SourcePathHandle(sourceFile, true, encoding) : null;
100+
} else {
101+
for (String subPath : subPaths) {
102+
Path sourceFile = archiveFileSystem.getPath(subPath, sourcePath);
103+
if (isFile(sourceFile)) return new SourcePathHandle(sourceFile, true, encoding);
104+
}
105+
return null;
106+
}
86107
}
87108

88109

@@ -94,8 +115,8 @@ private static SourcePathHandle getHandleInArchive(Path archive, String sourcePa
94115

95116

96117
public static SourcePathHandle getPathHandle(String resourcePath) {
97-
for (String string : SourceRoots.getRoots()) {
98-
SourcesRoot root = new SourcesRoot(string);
118+
for (String rootPath : SourceRoots.getRoots()) {
119+
SourcesRoot root = new SourcesRoot(rootPath);
99120
SourcePathHandle handle = root.getSourceHandle(resourcePath);
100121
if (handle != null) return handle;
101122
}
@@ -104,22 +125,88 @@ public static SourcePathHandle getPathHandle(String resourcePath) {
104125
}
105126

106127

128+
public static String createString(String rootPath, String[] subPaths, String encoding) {
129+
if ((subPaths == null || subPaths.length == 0) && encoding == null) return rootPath;
130+
131+
StringBuilder sb = new StringBuilder();
132+
133+
if (subPaths != null && subPaths.length > 0) {
134+
normalizeSubpaths(subPaths);
135+
136+
for (String subPath : subPaths) {
137+
if (sb.length() > 0) sb.append(":"); // NOI18N
138+
sb.append(subPath);
139+
}
140+
141+
sb.insert(0, SUBPATHS_PREFIX);
142+
sb.append(SUBPATHS_SUFFIX);
143+
}
144+
145+
if (StandardCharsets.UTF_8.name().equals(encoding)) encoding = null;
146+
if (encoding != null) sb.append(ENCODING_PREFIX).append(encoding).append(ENCODING_SUFFIX);
147+
148+
sb.insert(0, rootPath);
149+
150+
return sb.toString();
151+
}
152+
153+
107154
private static boolean isFile(Path path) {
108155
return Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS);
109156
}
110157

111-
private static Object[] resolveEncoding(String resourcePath) {
112-
int idx = resourcePath.endsWith(ENCODING_SUFFIX) ? resourcePath.indexOf(ENCODING_PREFIX) : -1;
113-
if (idx == -1) return new Object[] { resourcePath, StandardCharsets.UTF_8 };
158+
159+
private static Object[] resolve(String root) {
160+
int idx = root.indexOf("["); // NOI18N
161+
String[] subpaths = null;
162+
Charset encoding = StandardCharsets.UTF_8;
163+
164+
if (idx != -1) {
165+
String params = root.substring(idx);
166+
root = root.substring(0, idx);
167+
168+
String[] paramsArr = params.split("\\]\\["); // NOI18N
169+
for (String paramS : paramsArr) {
170+
if (!paramS.startsWith("[")) paramS = "[" + paramS; // NOI18N
171+
paramS = paramS.replace("]", ""); // NOI18N
172+
173+
if (paramS.startsWith(SUBPATHS_PREFIX)) {
174+
paramS = paramS.substring(SUBPATHS_PREFIX.length());
175+
subpaths = subpaths(paramS);
176+
} else if (paramS.startsWith(ENCODING_PREFIX)) {
177+
paramS = paramS.substring(ENCODING_PREFIX.length());
178+
encoding = charset(paramS);
179+
}
180+
}
181+
}
114182

115-
String resource = resourcePath.substring(0, idx);
116-
String charset = resourcePath.substring(idx + ENCODING_PREFIX.length(), resourcePath.length() - ENCODING_SUFFIX.length());
183+
return new Object[] { root, subpaths, encoding };
184+
}
185+
186+
private static String[] subpaths(String subpaths) {
187+
if (subpaths.isEmpty()) return null;
117188

118-
Charset encoding;
119-
try { encoding = Charset.forName(charset); }
120-
catch (Exception e) { encoding = StandardCharsets.UTF_8; }
189+
String[] paths = subpaths.split(":"); // NOI18N
190+
normalizeSubpaths(paths);
121191

122-
return new Object[] { resource, encoding };
192+
return paths;
193+
}
194+
195+
private static void normalizeSubpaths(String[] subpaths) {
196+
for (int i = 0; i < subpaths.length; i++) {
197+
String path = subpaths[i];
198+
199+
if (!"/".equals(File.separator)) path = path.replace(File.separator, "/"); // NOI18N
200+
if (path.startsWith("/")) path = path.substring(1); // NOI18N
201+
if (path.endsWith("/")) path = path.substring(0, path.length() - 1); // NOI18N
202+
203+
subpaths[i] = path;
204+
}
205+
}
206+
207+
private static Charset charset(String charset) {
208+
try { return Charset.forName(charset); }
209+
catch (Exception e) { return StandardCharsets.UTF_8; }
123210
}
124211

125212
}

0 commit comments

Comments
 (0)