1 18 19 package org.apache.struts.util; 20 21 import java.util.Map ; 22 23 30 public class WildcardHelper { 31 32 33 protected static final int MATCH_FILE = -1; 34 35 36 protected static final int MATCH_PATH = -2; 37 38 39 protected static final int MATCH_BEGIN = -4; 40 41 42 protected static final int MATCH_THEEND = -5; 43 44 45 protected static final int MATCH_END = -3; 46 47 48 81 public int[] compilePattern(String data) { 82 83 int expr[] = new int[data.length() + 2]; 85 char buff[] = data.toCharArray(); 86 87 int y = 0; 89 boolean slash = false; 90 91 expr[y++] = MATCH_BEGIN; 93 94 if (buff.length > 0) { 95 if (buff[0] == '\\') { 96 slash = true; 97 } else if (buff[0] == '*') { 98 expr[y++] = MATCH_FILE; 99 } else { 100 expr[y++] = buff[0]; 101 } 102 103 for (int x = 1; x < buff.length; x++) { 105 if (slash) { 107 expr[y++] = buff[x]; 108 slash = false; 109 } else { 112 if (buff[x] == '\\') { 114 slash = true; 115 } else if (buff[x] == '*') { 117 if (expr[y - 1] <= MATCH_FILE) { 119 expr[y - 1] = MATCH_PATH; 120 } else { 121 expr[y++] = MATCH_FILE; 122 } 123 } else { 124 expr[y++] = buff[x]; 125 } 126 } 127 } 128 } 129 130 expr[y] = MATCH_THEEND; 132 return expr; 133 } 134 135 145 public boolean match(Map map, String data, int[] expr) { 146 if (map == null) { 147 throw new NullPointerException ("No map provided"); 148 } 149 if (data == null) { 150 throw new NullPointerException ("No data provided"); 151 } 152 if (expr == null) { 153 throw new NullPointerException ("No pattern expression provided"); 154 } 155 156 157 char buff[] = data.toCharArray(); 158 char rslt[] = new char[expr.length + buff.length]; 160 161 162 int charpos = 0; 165 166 int exprpos = 0; 168 int buffpos = 0; 169 int rsltpos = 0; 170 int offset = -1; 171 172 int mcount = 0; 174 175 map.put(Integer.toString(mcount), data); 177 178 boolean matchBegin = false; 180 if (expr[charpos] == MATCH_BEGIN) { 181 matchBegin = true; 182 exprpos = ++charpos; 183 } 184 185 while (expr[charpos] >= 0) { 188 charpos++; 189 } 190 191 int exprchr = expr[charpos]; 193 194 while (true) { 195 if (matchBegin) { 198 if (!matchArray(expr, exprpos, charpos, buff, buffpos)) { 199 return (false); 200 } 201 matchBegin = false; 202 } else { 203 offset = indexOfArray (expr, exprpos, charpos, buff, 204 buffpos); 205 if (offset < 0) { 206 return (false); 207 } 208 } 209 210 if (matchBegin) { 212 if (offset != 0) { 213 return (false); 214 } 215 matchBegin = false; 216 } 217 218 buffpos += (charpos - exprpos); 220 221 if (exprchr == MATCH_END) { 223 if (rsltpos > 0) { 224 map.put(Integer.toString(++mcount), 225 new String (rslt, 0, rsltpos)); 226 } 227 return (true); 229 } else if (exprchr == MATCH_THEEND) { 230 if (rsltpos > 0) { 231 map.put(Integer.toString(++mcount), 232 new String (rslt, 0, rsltpos)); 233 } 234 return (buffpos == buff.length); 236 } 237 238 exprpos = ++charpos; 240 while (expr[charpos] >= 0) { 241 charpos++; 242 } 243 int prevchr = exprchr; 244 exprchr = expr[charpos]; 245 246 offset = (prevchr == MATCH_FILE) 248 ? indexOfArray (expr, exprpos, charpos, buff, buffpos) 249 : lastIndexOfArray (expr, exprpos, charpos, buff, 250 buffpos); 251 252 if (offset < 0) { 253 return (false); 254 } 255 256 if (prevchr == MATCH_PATH) { 259 while (buffpos < offset) { 260 rslt[rsltpos++] = buff[buffpos++]; 261 } 262 } else { 263 while (buffpos < offset) { 265 if (buff[buffpos] == '/') { 266 return (false); 267 } 268 rslt[rsltpos++] = buff[buffpos++]; 269 } 270 } 271 272 map.put(Integer.toString(++mcount), new String (rslt, 0, rsltpos)); 273 rsltpos = 0; 274 } 275 } 276 277 292 protected int indexOfArray (int r[], int rpos, int rend, 293 char d[], int dpos) { 294 295 if (rend < rpos) { 297 throw new IllegalArgumentException ("rend < rpos"); 298 } 299 if (rend == rpos) { 301 return (d.length); } 303 if ((rend - rpos) == 1) { 305 for (int x = dpos; x < d.length; x++) { 307 if (r[rpos] == d[x]) { 308 return (x); 309 } 310 } 311 } 312 while ((dpos + rend - rpos) <= d.length) { 315 int y = dpos; 317 for (int x = rpos; x <= rend; x++) { 320 if (x == rend) { 321 return (dpos); 322 } 323 if (r[x] != d[y++]) { 324 break; 325 } 326 } 327 dpos++; 329 } 330 return (-1); 333 } 334 335 350 protected int lastIndexOfArray (int r[], int rpos, int rend, 351 char d[], int dpos) { 352 if (rend < rpos) { 354 throw new IllegalArgumentException ("rend < rpos"); 355 } 356 if (rend == rpos) { 358 return (d.length); } 360 361 if ((rend - rpos) == 1) { 363 for (int x = d.length - 1; x > dpos; x--) { 365 if (r[rpos] == d[x]) { 366 return (x); 367 } 368 } 369 } 370 371 int l = d.length - (rend - rpos); 374 while (l >= dpos) { 375 int y = l; 377 for (int x = rpos; x <= rend; x++) { 380 if (x == rend) { 381 return (l); 382 } 383 if (r[x] != d[y++]) { 384 break; 385 } 386 } 387 l--; 389 } 390 return (-1); 393 } 394 395 409 protected boolean matchArray (int r[], int rpos, int rend, 410 char d[], int dpos) { 411 if (d.length - dpos < rend - rpos) { 412 return (false); 413 } 414 for (int i = rpos; i < rend; i++) { 415 if (r[i] != d[dpos++]) { 416 return (false); 417 } 418 } 419 return (true); 420 } 421 } 422 | Popular Tags |