1 16 package org.sapia.soto.util.matcher; 17 18 import java.util.Map ; 19 20 35 public class UriPatternHelper { 36 37 38 protected static final int MATCH_FILE = -1; 39 40 protected static final int MATCH_PATH = -2; 41 42 protected static final int MATCH_BEGIN = -4; 43 44 protected static final int MATCH_THEEND = -5; 45 46 protected static final int MATCH_END = -3; 47 48 49 82 static int[] compilePattern(String data) 83 throws NullPointerException { 84 85 int expr[] = new int[data.length() + 2]; 87 char buff[] = data.toCharArray(); 88 89 int y = 0; 91 boolean slash = false; 92 93 expr[y++] = MATCH_BEGIN; 95 96 if (buff.length > 0) { 97 if (buff[0]=='\\') { 98 slash = true; 99 } else if (buff[0] == '*') { 100 expr[y++] = MATCH_FILE; 101 } else { 102 expr[y++] = buff[0]; 103 } 104 105 for (int x = 1; x < buff.length; x++) { 107 if (slash) { 109 expr[y++] = buff[x]; 110 slash = false; 111 } else { 113 if (buff[x] == '\\') { 115 slash = true; 116 } else if (buff[x] == '*') { 118 if (expr[y-1] <= MATCH_FILE) { 120 expr[y-1] = MATCH_PATH; 121 } else { 122 expr[y++] = MATCH_FILE; 123 } 124 } else { 125 expr[y++]=buff[x]; 126 } 127 } 128 } 129 } 130 131 expr[y] = MATCH_THEEND; 133 return expr; 134 } 135 136 140 static boolean match (Map map, String data, 141 int[] expr) throws NullPointerException { 142 if (map == null) 143 throw new NullPointerException ("No map provided"); 144 if (data == null) 145 throw new NullPointerException ("No data provided"); 146 if (expr == null) 147 throw new NullPointerException ("No pattern expression provided"); 148 149 150 char buff[] = data.toCharArray(); 151 char rslt[] = new char[expr.length + buff.length]; 153 154 155 int charpos = 0; 158 159 int exprpos = 0; 161 int buffpos = 0; 162 int rsltpos = 0; 163 int offset = -1; 164 165 int mcount = 0; 167 168 map.put(Integer.toString(mcount),data); 170 171 boolean matchBegin = false; 173 if (expr[charpos] == MATCH_BEGIN) { 174 matchBegin = true; 175 exprpos = ++charpos; 176 } 177 178 while (expr[charpos] >= 0) 180 charpos++; 181 182 int exprchr = expr[charpos]; 184 185 while (true) { 186 if (matchBegin) { 189 if (!matchArray(expr, exprpos, charpos, buff, buffpos)) 190 return (false); 191 matchBegin = false; 192 } else { 193 offset = indexOfArray (expr, exprpos, charpos, buff, 194 buffpos); 195 if (offset < 0) 196 return (false); 197 } 198 199 if (matchBegin) { 201 if (offset != 0) 202 return (false); 203 matchBegin = false; 204 } 205 206 buffpos += (charpos - exprpos); 208 209 if (exprchr == MATCH_END) { 211 if (rsltpos > 0) 212 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 213 return (true); 215 } else if (exprchr == MATCH_THEEND) { 216 if (rsltpos > 0) 217 map.put (Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 218 return (buffpos == buff.length); 220 } 221 222 exprpos = ++charpos; 224 while (expr[charpos] >= 0) 225 charpos++; 226 int prevchr = exprchr; 227 exprchr = expr[charpos]; 228 229 offset = (prevchr == MATCH_FILE) ? 231 indexOfArray (expr, exprpos, charpos, buff, buffpos) : 232 lastIndexOfArray (expr, exprpos, charpos, buff, 233 buffpos); 234 235 if (offset < 0) 236 return (false); 237 238 if (prevchr == MATCH_PATH) { 241 while (buffpos < offset) 242 rslt[rsltpos++] = buff[buffpos++]; 243 } else { 244 while (buffpos < offset) { 246 if (buff[buffpos] == '/') 247 return (false); 248 rslt[rsltpos++] = buff[buffpos++]; 249 } 250 } 251 252 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 253 rsltpos = 0; 254 } 255 } 256 257 272 protected static int indexOfArray (int r[], int rpos, int rend, 273 char d[], int dpos) { 274 if (rend < rpos) 276 throw new IllegalArgumentException ("rend < rpos"); 277 if (rend == rpos) 279 return (d.length); if ((rend - rpos) == 1) { 282 for (int x = dpos; x < d.length; x++) 284 if (r[rpos] == d[x]) 285 return (x); 286 } 287 while ((dpos + rend - rpos) <= d.length) { 290 int y = dpos; 292 for (int x = rpos; x <= rend; x++) { 295 if (x == rend) 296 return (dpos); 297 if (r[x] != d[y++]) 298 break; 299 } 300 dpos++; 302 } 303 return (-1); 306 } 307 308 323 protected static int lastIndexOfArray (int r[], int rpos, int rend, 324 char d[], int dpos) { 325 if (rend < rpos) 327 throw new IllegalArgumentException ("rend < rpos"); 328 if (rend == rpos) 330 return (d.length); 332 if ((rend - rpos) == 1) { 334 for (int x = d.length - 1; x > dpos; x--) 336 if (r[rpos] == d[x]) 337 return (x); 338 } 339 340 int l = d.length - (rend - rpos); 343 while (l >= dpos) { 344 int y = l; 346 for (int x = rpos; x <= rend; x++) { 349 if (x == rend) 350 return (l); 351 if (r[x] != d[y++]) 352 break; 353 } 354 l--; 356 } 357 return (-1); 360 } 361 362 374 protected static boolean matchArray (int r[], int rpos, int rend, 375 char d[], int dpos) { 376 if (d.length - dpos < rend - rpos) 377 return (false); 378 for (int i = rpos; i < rend; i++) 379 if (r[i] != d[dpos++]) 380 return (false); 381 return (true); 382 } 383 384 } 385 | Popular Tags |