1 16 package org.sapia.regis.loader; 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 76 static int[] compilePattern(String data) throws NullPointerException { 77 78 int expr[] = new int[data.length() + 2]; 80 char buff[] = data.toCharArray(); 81 82 int y = 0; 84 boolean slash = false; 85 86 expr[y++] = MATCH_BEGIN; 88 89 if(buff.length > 0) { 90 if(buff[0] == '\\') { 91 slash = true; 92 } else if(buff[0] == '*') { 93 expr[y++] = MATCH_FILE; 94 } else { 95 expr[y++] = buff[0]; 96 } 97 98 for(int x = 1; x < buff.length; x++) { 100 if(slash) { 102 expr[y++] = buff[x]; 103 slash = false; 104 } else { 106 if(buff[x] == '\\') { 108 slash = true; 109 } else if(buff[x] == '*') { 111 if(expr[y - 1] <= MATCH_FILE) { 113 expr[y - 1] = MATCH_PATH; 114 } else { 115 expr[y++] = MATCH_FILE; 116 } 117 } else { 118 expr[y++] = buff[x]; 119 } 120 } 121 } 122 } 123 124 expr[y] = MATCH_THEEND; 126 return expr; 127 } 128 129 133 static boolean match(Map map, String data, int[] expr) 134 throws NullPointerException { 135 if(map == null) 136 throw new NullPointerException ("No map provided"); 137 if(data == null) 138 throw new NullPointerException ("No data provided"); 139 if(expr == null) 140 throw new NullPointerException ("No pattern expression provided"); 141 142 char buff[] = data.toCharArray(); 143 char rslt[] = new char[expr.length + buff.length]; 145 146 int charpos = 0; 149 150 int exprpos = 0; 152 int buffpos = 0; 153 int rsltpos = 0; 154 int offset = -1; 155 156 int mcount = 0; 158 159 map.put(Integer.toString(mcount), data); 161 162 boolean matchBegin = false; 164 if(expr[charpos] == MATCH_BEGIN) { 165 matchBegin = true; 166 exprpos = ++charpos; 167 } 168 169 while(expr[charpos] >= 0) 172 charpos++; 173 174 int exprchr = expr[charpos]; 176 177 while(true) { 178 if(matchBegin) { 181 if(!matchArray(expr, exprpos, charpos, buff, buffpos)) 182 return (false); 183 matchBegin = false; 184 } else { 185 offset = indexOfArray(expr, exprpos, charpos, buff, buffpos); 186 if(offset < 0) 187 return (false); 188 } 189 190 if(matchBegin) { 192 if(offset != 0) 193 return (false); 194 matchBegin = false; 195 } 196 197 buffpos += (charpos - exprpos); 199 200 if(exprchr == MATCH_END) { 202 if(rsltpos > 0) 203 map.put(Integer.toString(++mcount), new String (rslt, 0, rsltpos)); 204 return (true); 206 } else if(exprchr == MATCH_THEEND) { 207 if(rsltpos > 0) 208 map.put(Integer.toString(++mcount), new String (rslt, 0, rsltpos)); 209 return (buffpos == buff.length); 211 } 212 213 exprpos = ++charpos; 215 while(expr[charpos] >= 0) 216 charpos++; 217 int prevchr = exprchr; 218 exprchr = expr[charpos]; 219 220 offset = (prevchr == MATCH_FILE) ? indexOfArray(expr, exprpos, charpos, 222 buff, buffpos) : lastIndexOfArray(expr, exprpos, charpos, buff, 223 buffpos); 224 225 if(offset < 0) 226 return (false); 227 228 if(prevchr == MATCH_PATH) { 231 while(buffpos < offset) 232 rslt[rsltpos++] = buff[buffpos++]; 233 } else { 234 while(buffpos < offset) { 236 if(buff[buffpos] == '/') 237 return (false); 238 rslt[rsltpos++] = buff[buffpos++]; 239 } 240 } 241 242 map.put(Integer.toString(++mcount), new String (rslt, 0, rsltpos)); 243 rsltpos = 0; 244 } 245 } 246 247 266 protected static int indexOfArray(int r[], int rpos, int rend, char d[], 267 int dpos) { 268 if(rend < rpos) 270 throw new IllegalArgumentException ("rend < rpos"); 271 if(rend == rpos) 273 return (d.length); if((rend - rpos) == 1) { 276 for(int x = dpos; x < d.length; x++) 278 if(r[rpos] == d[x]) 279 return (x); 280 } 281 while((dpos + rend - rpos) <= d.length) { 284 int y = dpos; 286 for(int x = rpos; x <= rend; x++) { 289 if(x == rend) 290 return (dpos); 291 if(r[x] != d[y++]) 292 break; 293 } 294 dpos++; 296 } 297 return (-1); 300 } 301 302 321 protected static int lastIndexOfArray(int r[], int rpos, int rend, char d[], 322 int dpos) { 323 if(rend < rpos) 325 throw new IllegalArgumentException ("rend < rpos"); 326 if(rend == rpos) 328 return (d.length); 330 if((rend - rpos) == 1) { 332 for(int x = d.length - 1; x > dpos; x--) 334 if(r[rpos] == d[x]) 335 return (x); 336 } 337 338 int l = d.length - (rend - rpos); 341 while(l >= dpos) { 342 int y = l; 344 for(int x = rpos; x <= rend; x++) { 347 if(x == rend) 348 return (l); 349 if(r[x] != d[y++]) 350 break; 351 } 352 l--; 354 } 355 return (-1); 358 } 359 360 376 protected static boolean matchArray(int r[], int rpos, int rend, char d[], 377 int dpos) { 378 if(d.length - dpos < rend - rpos) 379 return (false); 380 for(int i = rpos; i < rend; i++) 381 if(r[i] != d[dpos++]) 382 return (false); 383 return (true); 384 } 385 386 } 387 | Popular Tags |