View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU Lesser General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU Lesser General Public License for more details.
15  	
16  	You should have received a copy of the GNU Lesser General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
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  }