001 /* 002 Copyright (c) 2009 Olivier Chafik, All Rights Reserved 003 004 This file is part of JNAerator (http://jnaerator.googlecode.com/). 005 006 JNAerator is free software: you can redistribute it and/or modify 007 it under the terms of the GNU Lesser General Public License as published by 008 the Free Software Foundation, either version 3 of the License, or 009 (at your option) any later version. 010 011 JNAerator is distributed in the hope that it will be useful, 012 but WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 GNU Lesser General Public License for more details. 015 016 You should have received a copy of the GNU Lesser General Public License 017 along with JNAerator. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 package com.ochafik.io; 020 021 import java.io.File; 022 import java.util.Collection; 023 import java.util.HashSet; 024 025 public class FileListUtils { 026 027 public static Collection<String> resolveShellLikeFileList(String d) { 028 HashSet<String> v=new HashSet<String>(); 029 File f=new File(d); 030 String name=f.getName(); 031 File par=f.getParentFile(); 032 if (par==null) { 033 par=new File("."); 034 } 035 String fs[]=par.list(); 036 if (fs==null) return v; 037 for (int i=0; i<fs.length;i++) { 038 String fsi=fs[i]; 039 if (complies(fsi,name)) 040 v.add((new File(par,fsi)).toString()); 041 } 042 return v; 043 } 044 public static boolean complies(String s, String model) { 045 int len=model.length(),slen=s.length(); 046 if (len==0) { 047 return slen==0; 048 } else if (slen==0) { 049 return model.equals("") || model.equals("*"); 050 } else { 051 char c=model.charAt(0); 052 if (c=='*') { 053 String smod=model.substring(1); 054 for (int i=0;i<slen;i++) { 055 if (complies(s.substring(i),smod)) return true; 056 } 057 } else if (c=='?') { 058 String smod=model.substring(1); 059 if (complies(s.substring(1),smod)) return true; 060 } else { 061 return s.charAt(0)==c && complies(s.substring(1),model.substring(1)); 062 } 063 } 064 return false; 065 } 066 }