1 16 package org.apache.cocoon.matching.helpers; 17 18 import java.util.HashMap ; 19 20 31 public class WildcardHelper { 32 33 34 protected static final int MATCH_FILE = -1; 35 36 protected static final int MATCH_PATH = -2; 37 38 protected static final int MATCH_BEGIN = -4; 39 40 protected static final int MATCH_THEEND = -5; 41 42 protected static final int MATCH_END = -3; 43 44 45 78 public static int[] compilePattern(String data) 79 throws NullPointerException { 80 81 int expr[] = new int[data.length() + 2]; 83 char buff[] = data.toCharArray(); 84 85 int y = 0; 87 boolean slash = false; 88 89 expr[y++] = MATCH_BEGIN; 91 92 if (buff.length > 0) { 93 if (buff[0]=='\\') { 94 slash = true; 95 } else if (buff[0] == '*') { 96 expr[y++] = MATCH_FILE; 97 } else { 98 expr[y++] = buff[0]; 99 } 100 101 for (int x = 1; x < buff.length; x++) { 103 if (slash) { 105 expr[y++] = buff[x]; 106 slash = false; 107 } else { 109 if (buff[x] == '\\') { 111 slash = true; 112 } else if (buff[x] == '*') { 114 if (expr[y-1] <= MATCH_FILE) { 116 expr[y-1] = MATCH_PATH; 117 } else { 118 expr[y++] = MATCH_FILE; 119 } 120 } else { 121 expr[y++]=buff[x]; 122 } 123 } 124 } 125 } 126 127 expr[y] = MATCH_THEEND; 129 return expr; 130 } 131 132 136 public static boolean match (HashMap map, String data, int[] expr) 137 throws NullPointerException { 138 if (data == null) { 139 throw new NullPointerException ("No data provided"); 140 } 141 if (expr == null) { 142 throw new NullPointerException ("No pattern expression provided"); 143 } 144 145 146 char buff[] = data.toCharArray(); 147 char rslt[] = new char[expr.length + buff.length]; 149 150 151 int charpos = 0; 154 155 int exprpos = 0; 157 int buffpos = 0; 158 int rsltpos = 0; 159 int offset = -1; 160 161 int mcount = 0; 163 164 if ( map != null ) { 165 map.put(Integer.toString(mcount),data); 167 } 168 169 boolean matchBegin = false; 171 if (expr[charpos] == MATCH_BEGIN) { 172 matchBegin = true; 173 exprpos = ++charpos; 174 } 175 176 while (expr[charpos] >= 0) 178 charpos++; 179 180 int exprchr = expr[charpos]; 182 183 while (true) { 184 if (matchBegin) { 187 if (!matchArray(expr, exprpos, charpos, buff, buffpos)) 188 return (false); 189 matchBegin = false; 190 } else { 191 offset = indexOfArray (expr, exprpos, charpos, buff, 192 buffpos); 193 if (offset < 0) 194 return (false); 195 } 196 197 if (matchBegin) { 199 if (offset != 0) 200 return (false); 201 matchBegin = false; 202 } 203 204 buffpos += (charpos - exprpos); 206 207 if (exprchr == MATCH_END) { 209 if (rsltpos > 0 && map != null) { 210 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 211 } 212 return (true); 214 } else if (exprchr == MATCH_THEEND) { 215 if (rsltpos > 0 && map != null ) { 216 map.put (Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 217 } 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 if ( map != null ) { 253 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 254 } 255 rsltpos = 0; 256 } 257 } 258 259 274 protected static int indexOfArray (int r[], int rpos, int rend, 275 char d[], int dpos) { 276 if (rend < rpos) 278 throw new IllegalArgumentException ("rend < rpos"); 279 if (rend == rpos) 281 return (d.length); if ((rend - rpos) == 1) { 284 for (int x = dpos; x < d.length; x++) 286 if (r[rpos] == d[x]) 287 return (x); 288 } 289 while ((dpos + rend - rpos) <= d.length) { 292 int y = dpos; 294 for (int x = rpos; x <= rend; x++) { 297 if (x == rend) 298 return (dpos); 299 if (r[x] != d[y++]) 300 break; 301 } 302 dpos++; 304 } 305 return (-1); 308 } 309 310 325 protected static int lastIndexOfArray (int r[], int rpos, int rend, 326 char d[], int dpos) { 327 if (rend < rpos) 329 throw new IllegalArgumentException ("rend < rpos"); 330 if (rend == rpos) 332 return (d.length); 334 if ((rend - rpos) == 1) { 336 for (int x = d.length - 1; x > dpos; x--) 338 if (r[rpos] == d[x]) 339 return (x); 340 } 341 342 int l = d.length - (rend - rpos); 345 while (l >= dpos) { 346 int y = l; 348 for (int x = rpos; x <= rend; x++) { 351 if (x == rend) 352 return (l); 353 if (r[x] != d[y++]) 354 break; 355 } 356 l--; 358 } 359 return (-1); 362 } 363 364 376 protected static boolean matchArray (int r[], int rpos, int rend, 377 char d[], 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 | Popular Tags |