FileExpander.java
4.34 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.text.GlobPattern
*/
package com.day.util;
import com.day.text.GlobPattern;
import com.day.util.HandleExpander;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
public class FileExpander
implements HandleExpander {
public static final BaseFilter RETURN_ALL = new BaseFilter("*");
public static final BaseFilter RETURN_FILES = new FileFileFilter("*");
public static final BaseFilter RETURN_DIRECTORIES = new DirectoryFileFilter("*");
private final File rootDirectory;
private final BaseFilter expandType;
public FileExpander() {
this.rootDirectory = null;
this.expandType = RETURN_FILES;
}
public FileExpander(String root, BaseFilter type) {
this(new File(root), type);
}
public FileExpander(File root, BaseFilter type) {
if (root.isFile()) {
root = root.getParentFile();
}
if (!root.isAbsolute()) {
root = root.getAbsoluteFile();
}
this.rootDirectory = root;
this.expandType = type != null ? type : RETURN_FILES;
}
public String[] expand(String pattern) {
File root = new File(pattern = pattern.replace('/', File.separatorChar)).isAbsolute() ? null : this.rootDirectory;
List result = this.traverse(new ArrayList(), root, pattern);
return result.toArray(new String[result.size()]);
}
private List traverse(List result, File directory, String rest) {
int end = rest.length();
for (int i = 0; i < end; ++i) {
int nextSlash;
char c = rest.charAt(i);
if ("*?[]".indexOf(c) < 0) continue;
int lastSlash = rest.lastIndexOf(File.separatorChar, i);
if (lastSlash >= 0) {
String part = lastSlash == 2 && rest.charAt(1) == ':' ? rest.substring(0, 3) : (lastSlash == 0 ? rest.substring(0, 1) : rest.substring(0, lastSlash));
directory = new File(directory, part);
}
if ((nextSlash = rest.indexOf(File.separatorChar, i)) < 0) {
String pat = rest.substring(lastSlash + 1);
rest = null;
File[] files = directory.listFiles(this.expandType.getInstance(pat));
if (files != null) {
for (int j = 0; j < files.length; ++j) {
result.add(files[j].getAbsolutePath());
}
}
} else {
String pat = rest.substring(lastSlash + 1, nextSlash);
rest = rest.substring(nextSlash + 1);
File[] dirs = directory.listFiles(new DirectoryFileFilter(pat));
if (dirs != null) {
for (int j = 0; j < dirs.length; ++j) {
this.traverse(result, dirs[j], rest);
}
}
}
return result;
}
if ((directory = new File(directory, rest)).exists() && this.expandType.accept(directory)) {
result.add(directory.getAbsolutePath());
}
return result;
}
private static class FileFileFilter
extends BaseFilter {
FileFileFilter(String pattern) {
super(pattern);
}
BaseFilter getInstance(String glob) {
return new FileFileFilter(glob);
}
public boolean accept(File pathname) {
return pathname.isFile() && super.accept(pathname);
}
}
private static class DirectoryFileFilter
extends BaseFilter {
DirectoryFileFilter(String pattern) {
super(pattern);
}
BaseFilter getInstance(String glob) {
return new DirectoryFileFilter(glob);
}
public boolean accept(File pathname) {
return pathname.isDirectory() && super.accept(pathname);
}
}
public static class BaseFilter
implements FileFilter {
private final GlobPattern glob;
BaseFilter(String pattern) {
this.glob = new GlobPattern(pattern.toLowerCase());
}
BaseFilter getInstance(String glob) {
return new BaseFilter(glob);
}
public boolean accept(File pathname) {
return this.glob.matches(pathname.getName().toLowerCase());
}
}
}