24
24
*/
25
25
package org .graalvm .visualvm .sources ;
26
26
27
+ import java .io .File ;
27
28
import java .nio .charset .Charset ;
28
29
import java .nio .charset .StandardCharsets ;
29
30
import org .graalvm .visualvm .sources .impl .SourceRoots ;
42
43
*/
43
44
public final class SourcesRoot {
44
45
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
47
51
48
52
49
53
private static final Logger LOGGER = Logger .getLogger (SourcesRoot .class .getName ());
50
54
51
55
52
56
private final String rootPath ;
57
+ private final String [] subPaths ;
58
+ private final Charset encoding ;
53
59
54
60
55
61
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 ];
57
66
}
58
67
59
68
60
69
private SourcePathHandle getSourceHandle (String resourcePath ) {
61
- Object [] resourceWithEncoding = resolveEncoding (resourcePath );
62
- String resource = (String )resourceWithEncoding [0 ];
63
- Charset encoding = (Charset )resourceWithEncoding [1 ];
64
-
65
70
Path root = Paths .get (rootPath );
66
71
67
72
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 );
70
75
} 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
72
77
}
73
78
74
79
return null ;
75
80
}
76
81
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
+ }
80
93
}
81
94
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 {
83
96
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
+ }
86
107
}
87
108
88
109
@@ -94,8 +115,8 @@ private static SourcePathHandle getHandleInArchive(Path archive, String sourcePa
94
115
95
116
96
117
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 );
99
120
SourcePathHandle handle = root .getSourceHandle (resourcePath );
100
121
if (handle != null ) return handle ;
101
122
}
@@ -104,22 +125,88 @@ public static SourcePathHandle getPathHandle(String resourcePath) {
104
125
}
105
126
106
127
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
+
107
154
private static boolean isFile (Path path ) {
108
155
return Files .isRegularFile (path , LinkOption .NOFOLLOW_LINKS );
109
156
}
110
157
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
+ }
114
182
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 ;
117
188
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 );
121
191
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 ; }
123
210
}
124
211
125
212
}
0 commit comments