1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.ochafik.io;
20
21 import java.io.File;
22 import java.util.Collection;
23 import java.util.HashSet;
24
25 public class FileListUtils {
26
27 public static Collection<String> resolveShellLikeFileList(String d) {
28 HashSet<String> v=new HashSet<String>();
29 File f=new File(d);
30 String name=f.getName();
31 File par=f.getParentFile();
32 if (par==null) {
33 par=new File(".");
34 }
35 String fs[]=par.list();
36 if (fs==null) return v;
37 for (int i=0; i<fs.length;i++) {
38 String fsi=fs[i];
39 if (complies(fsi,name))
40 v.add((new File(par,fsi)).toString());
41 }
42 return v;
43 }
44 public static boolean complies(String s, String model) {
45 int len=model.length(),slen=s.length();
46 if (len==0) {
47 return slen==0;
48 } else if (slen==0) {
49 return model.equals("") || model.equals("*");
50 } else {
51 char c=model.charAt(0);
52 if (c=='*') {
53 String smod=model.substring(1);
54 for (int i=0;i<slen;i++) {
55 if (complies(s.substring(i),smod)) return true;
56 }
57 } else if (c=='?') {
58 String smod=model.substring(1);
59 if (complies(s.substring(1),smod)) return true;
60 } else {
61 return s.charAt(0)==c && complies(s.substring(1),model.substring(1));
62 }
63 }
64 return false;
65 }
66 }