1 16 17 package org.springframework.util; 18 19 54 public class AntPathMatcher implements PathMatcher { 55 56 57 public static final String DEFAULT_PATH_SEPARATOR = "/"; 58 59 private String pathSeparator = DEFAULT_PATH_SEPARATOR; 60 61 62 66 public void setPathSeparator(String pathSeparator) { 67 this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR); 68 } 69 70 71 public boolean isPattern(String str) { 72 return (str.indexOf('*') != -1 || str.indexOf('?') != -1); 73 } 74 75 public boolean match(String pattern, String str) { 76 if (str.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) { 77 return false; 78 } 79 80 String [] patDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator); 81 String [] strDirs = StringUtils.tokenizeToStringArray(str, this.pathSeparator); 82 83 int patIdxStart = 0; 84 int patIdxEnd = patDirs.length - 1; 85 int strIdxStart = 0; 86 int strIdxEnd = strDirs.length - 1; 87 88 while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { 90 String patDir = (String ) patDirs[patIdxStart]; 91 if (patDir.equals("**")) { 92 break; 93 } 94 if (!matchStrings(patDir, (String ) strDirs[strIdxStart])) { 95 return false; 96 } 97 patIdxStart++; 98 strIdxStart++; 99 } 100 101 if (strIdxStart > strIdxEnd) { 102 if (patIdxStart == patIdxEnd && patDirs[patIdxStart].equals("*") && 104 str.endsWith(this.pathSeparator)) { 105 return true; 106 } 107 for (int i = patIdxStart; i <= patIdxEnd; i++) { 108 if (!patDirs[i].equals("**")) { 109 return false; 110 } 111 } 112 return true; 113 } 114 else { 115 if (patIdxStart > patIdxEnd) { 116 return false; 118 } 119 } 120 121 while (patIdxStart <= patIdxEnd && strIdxStart <= strIdxEnd) { 123 String patDir = (String ) patDirs[patIdxEnd]; 124 if (patDir.equals("**")) { 125 break; 126 } 127 if (!matchStrings(patDir, (String ) strDirs[strIdxEnd])) { 128 return false; 129 } 130 patIdxEnd--; 131 strIdxEnd--; 132 } 133 if (strIdxStart > strIdxEnd) { 134 for (int i = patIdxStart; i <= patIdxEnd; i++) { 136 if (!patDirs[i].equals("**")) { 137 return false; 138 } 139 } 140 return true; 141 } 142 143 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { 144 int patIdxTmp = -1; 145 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { 146 if (patDirs[i].equals("**")) { 147 patIdxTmp = i; 148 break; 149 } 150 } 151 if (patIdxTmp == patIdxStart + 1) { 152 patIdxStart++; 154 continue; 155 } 156 int patLength = (patIdxTmp - patIdxStart - 1); 159 int strLength = (strIdxEnd - strIdxStart + 1); 160 int foundIdx = -1; 161 strLoop: 162 for (int i = 0; i <= strLength - patLength; i++) { 163 for (int j = 0; j < patLength; j++) { 164 String subPat = (String ) patDirs[patIdxStart + j + 1]; 165 String subStr = (String ) strDirs[strIdxStart + i + j]; 166 if (!matchStrings(subPat, subStr)) { 167 continue strLoop; 168 } 169 } 170 171 foundIdx = strIdxStart + i; 172 break; 173 } 174 175 if (foundIdx == -1) { 176 return false; 177 } 178 179 patIdxStart = patIdxTmp; 180 strIdxStart = foundIdx + patLength; 181 } 182 183 for (int i = patIdxStart; i <= patIdxEnd; i++) { 184 if (!patDirs[i].equals("**")) { 185 return false; 186 } 187 } 188 189 return true; 190 } 191 192 204 private boolean matchStrings(String pattern, String str) { 205 char[] patArr = pattern.toCharArray(); 206 char[] strArr = str.toCharArray(); 207 int patIdxStart = 0; 208 int patIdxEnd = patArr.length - 1; 209 int strIdxStart = 0; 210 int strIdxEnd = strArr.length - 1; 211 char ch; 212 213 boolean containsStar = false; 214 for (int i = 0; i < patArr.length; i++) { 215 if (patArr[i] == '*') { 216 containsStar = true; 217 break; 218 } 219 } 220 221 if (!containsStar) { 222 if (patIdxEnd != strIdxEnd) { 224 return false; } 226 for (int i = 0; i <= patIdxEnd; i++) { 227 ch = patArr[i]; 228 if (ch != '?') { 229 if (ch != strArr[i]) { 230 return false; } 232 } 233 } 234 return true; } 236 237 238 if (patIdxEnd == 0) { 239 return true; } 241 242 while ((ch = patArr[patIdxStart]) != '*' && strIdxStart <= strIdxEnd) { 244 if (ch != '?') { 245 if (ch != strArr[strIdxStart]) { 246 return false; } 248 } 249 patIdxStart++; 250 strIdxStart++; 251 } 252 if (strIdxStart > strIdxEnd) { 253 for (int i = patIdxStart; i <= patIdxEnd; i++) { 256 if (patArr[i] != '*') { 257 return false; 258 } 259 } 260 return true; 261 } 262 263 while ((ch = patArr[patIdxEnd]) != '*' && strIdxStart <= strIdxEnd) { 265 if (ch != '?') { 266 if (ch != strArr[strIdxEnd]) { 267 return false; } 269 } 270 patIdxEnd--; 271 strIdxEnd--; 272 } 273 if (strIdxStart > strIdxEnd) { 274 for (int i = patIdxStart; i <= patIdxEnd; i++) { 277 if (patArr[i] != '*') { 278 return false; 279 } 280 } 281 return true; 282 } 283 284 while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) { 287 int patIdxTmp = -1; 288 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { 289 if (patArr[i] == '*') { 290 patIdxTmp = i; 291 break; 292 } 293 } 294 if (patIdxTmp == patIdxStart + 1) { 295 patIdxStart++; 297 continue; 298 } 299 int patLength = (patIdxTmp - patIdxStart - 1); 302 int strLength = (strIdxEnd - strIdxStart + 1); 303 int foundIdx = -1; 304 strLoop: 305 for (int i = 0; i <= strLength - patLength; i++) { 306 for (int j = 0; j < patLength; j++) { 307 ch = patArr[patIdxStart + j + 1]; 308 if (ch != '?') { 309 if (ch != strArr[strIdxStart + i + j]) { 310 continue strLoop; 311 } 312 } 313 } 314 315 foundIdx = strIdxStart + i; 316 break; 317 } 318 319 if (foundIdx == -1) { 320 return false; 321 } 322 323 patIdxStart = patIdxTmp; 324 strIdxStart = foundIdx + patLength; 325 } 326 327 for (int i = patIdxStart; i <= patIdxEnd; i++) { 330 if (patArr[i] != '*') { 331 return false; 332 } 333 } 334 335 return true; 336 } 337 338 349 public String extractPathWithinPattern(String pattern, String path) { 350 String [] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator); 351 String [] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator); 352 353 StringBuffer buffer = new StringBuffer (); 354 355 int puts = 0; 357 for (int i = 0; i < patternParts.length; i++) { 358 String patternPart = patternParts[i]; 359 if ((patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) && pathParts.length >= i + 1) { 360 if (puts > 0) { 361 buffer.append(this.pathSeparator); 362 } 363 buffer.append(pathParts[i]); 364 puts++; 365 } 366 } 367 368 for (int i = patternParts.length; i < pathParts.length; i++) { 370 if (puts > 0 || i > 0) { 371 buffer.append(this.pathSeparator); 372 } 373 buffer.append(pathParts[i]); 374 } 375 376 return buffer.toString(); 377 } 378 379 } 380 | Popular Tags |