-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileSpec.java
More file actions
574 lines (512 loc) · 23.3 KB
/
FileSpec.java
File metadata and controls
574 lines (512 loc) · 23.3 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package org.perlonjava.runtime.perlmodule;
import org.perlonjava.runtime.runtimetypes.GlobalVariable;
import org.perlonjava.runtime.runtimetypes.RuntimeArray;
import org.perlonjava.runtime.runtimetypes.RuntimeHash;
import org.perlonjava.runtime.runtimetypes.RuntimeList;
import org.perlonjava.runtime.runtimetypes.RuntimeScalar;
import org.perlonjava.runtime.runtimetypes.SystemUtils;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Utility class for File::Spec operations in Perl.
* This class provides methods that mimic the behavior of Perl's File::Spec module,
* allowing for operations related to file path manipulation and environment-specific
* path handling.
*
* <p>Extends {@link PerlModuleBase} to leverage module initialization and method registration.</p>
*/
public class FileSpec extends PerlModuleBase {
/**
* Constructor for FileSpec.
* Initializes the module with the name "File::Spec".
*/
public FileSpec() {
super("File::Spec", false);
}
/**
* Static initializer to set up the File::Spec module.
* This method initializes the exporter and defines the symbols that can be exported.
* It also registers methods that can be called from the Perl environment.
*/
public static void initialize() {
FileSpec fileSpec = new FileSpec();
fileSpec.initializeExporter();
fileSpec.defineExport("EXPORT_OK", "canonpath", "catdir", "catfile", "curdir", "devnull", "rootdir", "tmpdir",
"updir", "no_upwards", "case_tolerant", "file_name_is_absolute", "path", "join", "splitpath", "splitdir",
"catpath", "abs2rel", "rel2abs");
try {
fileSpec.registerMethod("canonpath", "$");
fileSpec.registerMethod("catdir", "@");
fileSpec.registerMethod("catfile", "@");
fileSpec.registerMethod("curdir", "");
fileSpec.registerMethod("devnull", "");
fileSpec.registerMethod("rootdir", "");
fileSpec.registerMethod("tmpdir", "");
fileSpec.registerMethod("updir", "");
fileSpec.registerMethod("no_upwards", "@");
fileSpec.registerMethod("case_tolerant", "");
fileSpec.registerMethod("file_name_is_absolute", "$");
fileSpec.registerMethod("path", "");
fileSpec.registerMethod("join", "@");
fileSpec.registerMethod("splitpath", "$;$");
fileSpec.registerMethod("splitdir", "$");
fileSpec.registerMethod("catpath", "$$$");
fileSpec.registerMethod("abs2rel", "$;$");
fileSpec.registerMethod("rel2abs", "$;$");
} catch (NoSuchMethodException e) {
System.err.println("Warning: Missing File::Spec method: " + e.getMessage());
}
}
/**
* Converts a path to a canonical form, removing redundant separators and up-level references.
*
* @param args The arguments passed from the Perl environment, where args[1] is the path.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the canonical path.
*/
public static RuntimeList canonpath(RuntimeArray args, int ctx) {
if (args.size() != 2) {
throw new IllegalStateException("Bad number of arguments for canonpath() method");
}
String path = args.get(1).toString();
// Empty string stays empty (Perl 5 behavior)
if (path.isEmpty()) {
return new RuntimeScalar("").getList();
}
// Implement Perl 5's File::Spec::Unix::canonpath logic:
// 1. Collapse multiple slashes into one
// 2. Collapse /./ and also /. at end of string
// 3. Remove leading ./ (unless path is exactly "./")
// 4. Remove trailing / (unless path is exactly "/")
String sep = File.separator;
String quotedSep = Pattern.quote(sep);
String replSep = Matcher.quoteReplacement(sep);
// Collapse multiple separators into one
String canonPath = path.replaceAll("[/\\\\]+", replSep);
// Collapse /./ and /. at end: (?:/\.)+(?:/|$) -> /
// This handles both /./bar -> /bar and foo/. -> foo
canonPath = canonPath.replaceAll("(?:" + quotedSep + "\\.)+(?=" + quotedSep + "|$)", "");
// Remove leading ./ unless the path is exactly "./" or "."
if (!canonPath.equals("." + sep) && !canonPath.equals(".")) {
while (canonPath.startsWith("." + sep)) {
canonPath = canonPath.substring(1 + sep.length());
}
}
// Remove trailing / unless the path is exactly "/"
if (!canonPath.equals(sep) && canonPath.endsWith(sep)) {
canonPath = canonPath.substring(0, canonPath.length() - sep.length());
}
// If we reduced to empty string from a non-empty input, return "."
if (canonPath.isEmpty()) {
canonPath = ".";
}
return new RuntimeScalar(canonPath).getList();
}
/**
* Concatenates multiple directory names into a single path.
*
* @param args The arguments passed from the Perl environment, representing directory names.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the concatenated directory path.
*/
public static RuntimeList catdir(RuntimeArray args, int ctx) {
if (args.size() == 1) {
return new RuntimeScalar("").getList();
}
StringBuilder result = new StringBuilder();
boolean isWindows = SystemUtils.osIsWindows();
String separator = File.separator;
boolean isFirst = true;
for (int i = 1; i < args.size(); i++) {
String part = args.get(i).toString();
// Empty first element represents root directory on Unix
if (part.isEmpty()) {
if (isFirst && !isWindows) {
// First empty element = absolute path (root)
result.append(separator);
}
isFirst = false;
continue;
}
isFirst = false;
// For Windows, normalize slashes to the system separator
if (isWindows) {
part = part.replace('/', '\\');
}
if (result.length() == 0) {
// First component
result.append(part);
} else {
// Check if we need to add a separator
char lastChar = result.charAt(result.length() - 1);
char firstChar = part.charAt(0);
boolean lastHasSep = (lastChar == '/' || lastChar == '\\');
boolean firstHasSep = (firstChar == '/' || firstChar == '\\');
if (!lastHasSep && !firstHasSep) {
// Neither has separator, add one
result.append(separator);
} else if (lastHasSep && firstHasSep) {
// Both have separator, skip the first char of part
part = part.substring(1);
}
// else: exactly one has separator, just append
result.append(part);
}
}
// Apply canonpath to the result, matching Perl's File::Spec::Unix behavior
// where catdir calls canonpath(join('/', @_, ''))
RuntimeArray canonArgs = new RuntimeArray();
canonArgs.push(new RuntimeScalar("dummy"));
canonArgs.push(new RuntimeScalar(result.toString()));
return canonpath(canonArgs, ctx);
}
/**
* Concatenates multiple file names into a single path.
* Uses catdir for the directory components and canonpath for the file component.
*
* @param args The arguments passed from the Perl environment, representing file names.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the concatenated file path.
*/
public static RuntimeList catfile(RuntimeArray args, int ctx) {
if (args.size() <= 2) {
// 0 or 1 real args (first is invocant) — just canonpath the single arg
if (args.size() == 2) {
return canonpath(args, ctx);
}
return new RuntimeScalar("").getList();
}
// Last real arg is the file component; everything before is directories
RuntimeScalar file = args.get(args.size() - 1);
// Build directory portion using catdir
RuntimeArray dirArgs = new RuntimeArray();
for (int i = 0; i < args.size() - 1; i++) {
dirArgs.push(args.get(i));
}
String dir = catdir(dirArgs, ctx).elements.get(0).toString();
// Canonpath the file part
RuntimeArray fileCanonArgs = new RuntimeArray();
fileCanonArgs.push(new RuntimeScalar("dummy"));
fileCanonArgs.push(file);
String filePart = canonpath(fileCanonArgs, ctx).elements.get(0).toString();
// Combine: if dir is empty, just return the file
if (dir.isEmpty()) {
return new RuntimeScalar(filePart).getList();
}
// Ensure proper separator between dir and file
String separator = File.separator;
char lastChar = dir.charAt(dir.length() - 1);
if (lastChar == '/' || lastChar == '\\') {
return new RuntimeScalar(dir + filePart).getList();
}
return new RuntimeScalar(dir + separator + filePart).getList();
}
/**
* Returns the current directory symbol.
*
* @param args The arguments passed from the Perl environment.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the current directory symbol.
*/
public static RuntimeList curdir(RuntimeArray args, int ctx) {
return new RuntimeScalar(".").getList();
}
/**
* Returns the null device for the current operating system.
*
* @param args The arguments passed from the Perl environment.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the null device path.
*/
public static RuntimeList devnull(RuntimeArray args, int ctx) {
String devNull = SystemUtils.osIsWindows() ? "NUL" : "/dev/null";
return new RuntimeScalar(devNull).getList();
}
/**
* Returns the root directory for the current operating system.
*
* @param args The arguments passed from the Perl environment.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the root directory path.
*/
public static RuntimeList rootdir(RuntimeArray args, int ctx) {
String rootDir = SystemUtils.osIsWindows() ? "\\" : "/";
return new RuntimeScalar(rootDir).getList();
}
/**
* Returns the temporary directory path for the current operating system.
*
* @param args The arguments passed from the Perl environment.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the temporary directory path.
*/
public static RuntimeList tmpdir(RuntimeArray args, int ctx) {
String tmpDir = System.getenv("TMPDIR");
if (tmpDir == null || tmpDir.isEmpty()) {
tmpDir = SystemUtils.osIsWindows() ? System.getenv("TEMP") : "/tmp";
}
return new RuntimeScalar(tmpDir).getList();
}
/**
* Returns the parent directory symbol.
*
* @param args The arguments passed from the Perl environment.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the parent directory symbol.
*/
public static RuntimeList updir(RuntimeArray args, int ctx) {
return new RuntimeScalar("..").getList();
}
/**
* Filters out the current and parent directory symbols from a list of directory names.
*
* @param args The arguments passed from the Perl environment, representing directory names.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the filtered directory names.
*/
public static RuntimeList no_upwards(RuntimeArray args, int ctx) {
List<RuntimeScalar> filtered = new ArrayList<>();
for (int i = 1; i < args.size(); i++) {
String dir = args.get(i).toString();
if (!dir.equals(".") && !dir.equals("..")) {
filtered.add(args.get(i));
}
}
return new RuntimeList(filtered);
}
/**
* Determines if the current file system is case-tolerant.
*
* @param args The arguments passed from the Perl environment.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing a boolean indicating case tolerance.
*/
public static RuntimeList case_tolerant(RuntimeArray args, int ctx) {
boolean caseTolerant = SystemUtils.osIsWindows();
return new RuntimeScalar(caseTolerant).getList();
}
/**
* Checks if a given file name is an absolute path.
*
* @param args The arguments passed from the Perl environment, where args[1] is the file name.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing a boolean indicating if the path is absolute.
*/
public static RuntimeList file_name_is_absolute(RuntimeArray args, int ctx) {
if (args.size() != 2) {
throw new IllegalStateException("Bad number of arguments for file_name_is_absolute() method");
}
String path = args.get(1).toString();
// PerlOnJava: Also recognize jar: paths as absolute
if (path.startsWith("jar:")) {
return new RuntimeScalar(true).getList();
}
boolean isAbsolute = Paths.get(path).isAbsolute();
return new RuntimeScalar(isAbsolute).getList();
}
/**
* Retrieves the system's PATH environment variable as a list of directories.
*
* @param args The arguments passed from the Perl environment.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the directories in the PATH.
*/
public static RuntimeList path(RuntimeArray args, int ctx) {
// Read PATH from Perl's %ENV (not Java's System.getenv) so that
// modifications to $ENV{PATH} in Perl code are respected.
RuntimeHash perlEnv = GlobalVariable.getGlobalHash("main::ENV");
RuntimeScalar pathScalar = perlEnv.get(new RuntimeScalar("PATH"));
String path = pathScalar.getDefinedBoolean() ? pathScalar.toString() : System.getenv("PATH");
String[] paths = path != null ? path.split(File.pathSeparator) : new String[0];
List<RuntimeScalar> pathList = new ArrayList<>();
for (String p : paths) {
pathList.add(new RuntimeScalar(p));
}
return new RuntimeList(pathList);
}
/**
* Joins multiple path components into a single path.
* This method is an alias for {@link #catfile(RuntimeArray, int)}.
*
* @param args The arguments passed from the Perl environment, representing path components.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the joined path.
*/
public static RuntimeList join(RuntimeArray args, int ctx) {
return catfile(args, ctx);
}
/**
* Splits a path into volume, directory, and file components.
*
* @param args The arguments passed from the Perl environment, where args[1] is the path and args[2] is optional.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the volume, directory, and file components.
*/
public static RuntimeList splitpath(RuntimeArray args, int ctx) {
if (args.size() < 2 || args.size() > 3) {
throw new IllegalStateException("Bad number of arguments for splitpath() method");
}
String path = args.get(1).toString();
boolean noFile = args.size() == 3 && args.get(2).getBoolean();
String volume = "";
String directory = "";
String file = "";
if (SystemUtils.osIsWindows()) {
int colonIndex = path.indexOf(':');
if (colonIndex != -1) {
volume = path.substring(0, colonIndex + 1);
path = path.substring(colonIndex + 1);
}
}
if (noFile) {
// If noFile is true, entire path is directory
directory = path;
} else {
int lastSeparator = path.lastIndexOf(File.separator);
if (lastSeparator != -1) {
directory = path.substring(0, lastSeparator + 1);
file = path.substring(lastSeparator + 1);
} else {
// No separator - entire path is the filename
file = path;
}
}
return new RuntimeList(
new RuntimeScalar(volume), new RuntimeScalar(directory), new RuntimeScalar(file));
}
/**
* Splits a directory path into its individual components.
*
* @param args The arguments passed from the Perl environment, where args[1] is the directory path.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the directory components.
*/
public static RuntimeList splitdir(RuntimeArray args, int ctx) {
if (args.size() != 2) {
throw new IllegalStateException("Bad number of arguments for splitdir() method");
}
String directories = args.get(1).toString();
// Empty string returns empty list (Perl 5 behavior)
if (directories.isEmpty()) {
return new RuntimeList(new ArrayList<>());
}
String[] dirs = directories.split(Pattern.quote(File.separator), -1);
List<RuntimeScalar> dirList = new ArrayList<>();
for (String dir : dirs) {
dirList.add(new RuntimeScalar(dir));
}
return new RuntimeList(dirList);
}
/**
* Constructs a complete path from volume, directory, and file components.
*
* @param args The arguments passed from the Perl environment, where args[1] is the volume, args[2] is the directory, and args[3] is the file.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the constructed path.
*/
public static RuntimeList catpath(RuntimeArray args, int ctx) {
if (args.size() != 4) {
throw new IllegalStateException("Bad number of arguments for catpath() method");
}
String volume = args.get(1).toString();
String directory = args.get(2).toString();
String file = args.get(3).toString();
StringBuilder fullPath = new StringBuilder();
// Add volume (for Windows drive letters)
if (!volume.isEmpty()) {
fullPath.append(volume);
// Ensure volume ends with colon on Windows
if (SystemUtils.osIsWindows() && !volume.endsWith(":")) {
fullPath.append(":");
}
}
// Add directory
if (!directory.isEmpty()) {
fullPath.append(directory);
// Ensure directory ends with separator if file is provided
if (!file.isEmpty()) {
char lastChar = directory.charAt(directory.length() - 1);
if (lastChar != '/' && lastChar != '\\') {
fullPath.append(File.separator);
}
}
}
// Add file
fullPath.append(file);
// Clean up the path
String result = canonpath(new RuntimeArray(
new RuntimeScalar("dummy"),
new RuntimeScalar(fullPath.toString())
), 0).elements.get(0).toString();
return new RuntimeScalar(result).getList();
}
/**
* Converts an absolute path to a relative path based on a given base path.
*
* @param args The arguments passed from the Perl environment, where args[1] is the absolute path and args[2] is optional base path.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the relative path.
*/
public static RuntimeList abs2rel(RuntimeArray args, int ctx) {
if (args.size() < 2 || args.size() > 3) {
throw new IllegalStateException("Bad number of arguments for abs2rel() method");
}
String path = args.get(1).toString();
String base = args.size() == 3 ? args.get(2).toString() : System.getProperty("user.dir");
// Ensure both paths are absolute before relativizing (like Perl does)
// Note: We use user.dir explicitly because Java's Path.toAbsolutePath()
// doesn't respect System.setProperty("user.dir", ...) set by chdir()
Path pathObj = Paths.get(path);
Path baseObj = Paths.get(base);
String userDir = System.getProperty("user.dir");
if (!pathObj.isAbsolute()) {
pathObj = Paths.get(userDir).resolve(pathObj).normalize();
}
if (!baseObj.isAbsolute()) {
baseObj = Paths.get(userDir).resolve(baseObj).normalize();
}
String relPath = baseObj.relativize(pathObj).toString();
// Perl's File::Spec->abs2rel returns "." (curdir) when path equals base,
// but Java's Path.relativize returns an empty string in that case.
if (relPath.isEmpty()) {
relPath = ".";
}
return new RuntimeScalar(relPath).getList();
}
/**
* Converts a relative path to an absolute path based on a given base path.
*
* @param args The arguments passed from the Perl environment, where args[1] is the relative path and args[2] is optional base path.
* @param ctx The context in which the method is called.
* @return A {@link RuntimeList} containing the absolute path.
*/
public static RuntimeList rel2abs(RuntimeArray args, int ctx) {
if (args.size() < 2 || args.size() > 3) {
throw new IllegalStateException("Bad number of arguments for rel2abs() method");
}
String path = args.get(1).toString();
String base = args.size() == 3 ? args.get(2).toString() : System.getProperty("user.dir");
// PerlOnJava: jar: paths are already absolute, return as-is
if (path.startsWith("jar:")) {
return new RuntimeScalar(path).getList();
}
// If the path is already absolute, return it as-is (normalized)
if (Paths.get(path).isAbsolute()) {
String absPath = Paths.get(path).toAbsolutePath().normalize().toString();
return new RuntimeScalar(absPath).getList();
}
// If base is relative, resolve it against current working directory first
Path basePath = Paths.get(base);
if (!basePath.isAbsolute()) {
basePath = Paths.get(System.getProperty("user.dir")).resolve(basePath);
}
// For relative paths, resolve against the base directory
String absPath = basePath.resolve(path).normalize().toString();
return new RuntimeScalar(absPath).getList();
}
}