1 4 package org.oddjob.launch; 5 6 import java.io.File ; 7 import java.io.FileFilter ; 8 9 13 public class FileSpec { 14 15 private File filespec; 16 17 19 private boolean caseSensative; 20 21 public FileSpec(File filespec) { 22 this.filespec = filespec; 23 } 24 25 30 public File [] getFiles() { 31 File dir = filespec.getParentFile(); 32 File [] result = dir.listFiles(new FileFilter () { 33 public boolean accept(File pathname) { 34 return match(filespec.getName(), pathname.getPath(), caseSensative); 35 } 36 }); 37 if (result == null) { 38 result = new File [0]; 39 } 40 return result; 41 } 42 43 45 62 public static boolean match(String pattern, String str, 63 boolean isCaseSensitive) { 64 char[] patArr = pattern.toCharArray(); 65 char[] strArr = str.toCharArray(); 66 int patIdxStart = 0; 67 int patIdxEnd = patArr.length - 1; 68 int strIdxStart = 0; 69 int strIdxEnd = strArr.length - 1; 70 char ch; 71 72 boolean containsStar = false; 73 for (int i = 0; i < patArr.length; i++) { 74 if (patArr[i] == '*') { 75 containsStar = true; 76 break; 77 } 78 } 79 80 if (!containsStar) { 81 if (patIdxEnd != strIdxEnd) { 83 return false; } 85 for (int i = 0; i <= patIdxEnd; i++) { 86 ch = patArr[i]; 87 if (ch != '?') { 88 if (isCaseSensitive && ch != strArr[i]) { 89 return false; } 91 if (!isCaseSensitive && Character.toUpperCase(ch) 92 != Character.toUpperCase(strArr[i])) { 93 return false; } 95 } 96 } 97 return true; } 99 100 if (patIdxEnd == 0) { 101 return true; } 103 104 while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) { 106 if (ch != '?') { 107 if (isCaseSensitive && ch != strArr[strIdxStart]) { 108 return false; } 110 if (!isCaseSensitive && Character.toUpperCase(ch) 111 != Character.toUpperCase(strArr[strIdxStart])) { 112 return false; } 114 } 115 patIdxStart++; 116 strIdxStart++; 117 } 118 if (strIdxStart > strIdxEnd) { 119 for (int i = patIdxStart; i <= patIdxEnd; i++) { 122 if (patArr[i] != '*') { 123 return false; 124 } 125 } 126 return true; 127 } 128 129 while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) { 131 if (ch != '?') { 132 if (isCaseSensitive && ch != strArr[strIdxEnd]) { 133 return false; } 135 if (!isCaseSensitive && Character.toUpperCase(ch) 136 != Character.toUpperCase(strArr[strIdxEnd])) { 137 return false; } 139 } 140 patIdxEnd--; 141 strIdxEnd--; 142 } 143 if (strIdxStart > strIdxEnd) { 144 for (int i = patIdxStart; i <= patIdxEnd; i++) { 147 if (patArr[i] != '*') { 148 return false; 149 } 150 } 151 return true; 152 } 153 154 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { 157 int patIdxTmp = -1; 158 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { 159 if (patArr[i] == '*') { 160 patIdxTmp = i; 161 break; 162 } 163 } 164 if (patIdxTmp == patIdxStart + 1) { 165 patIdxStart++; 167 continue; 168 } 169 int patLength = (patIdxTmp - patIdxStart - 1); 172 int strLength = (strIdxEnd - strIdxStart + 1); 173 int foundIdx = -1; 174 strLoop: 175 for (int i = 0; i <= strLength - patLength; i++) { 176 for (int j = 0; j < patLength; j++) { 177 ch = patArr[patIdxStart + j + 1]; 178 if (ch != '?') { 179 if (isCaseSensitive && ch != strArr[strIdxStart + i 180 + j]) { 181 continue strLoop; 182 } 183 if (!isCaseSensitive 184 && Character.toUpperCase(ch) 185 != Character.toUpperCase(strArr[strIdxStart + i + j])) { 186 continue strLoop; 187 } 188 } 189 } 190 191 foundIdx = strIdxStart + i; 192 break; 193 } 194 195 if (foundIdx == -1) { 196 return false; 197 } 198 199 patIdxStart = patIdxTmp; 200 strIdxStart = foundIdx + patLength; 201 } 202 203 for (int i = patIdxStart; i <= patIdxEnd; i++) { 206 if (patArr[i] != '*') { 207 return false; 208 } 209 } 210 return true; 211 } 212 213 } 214 | Popular Tags |