1 16 package org.apache.cocoon.matching.helpers; 17 18 import java.util.HashMap ; 19 20 31 public class WildcardURIMatcher { 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 48 public static boolean match (HashMap map, String data, 49 int[] expr) throws NullPointerException { 50 if (map == null) 51 throw new NullPointerException ("No map provided"); 52 if (data == null) 53 throw new NullPointerException ("No data provided"); 54 if (expr == null) 55 throw new NullPointerException ("No pattern expression provided"); 56 57 58 char buff[] = data.toCharArray(); 59 char rslt[] = new char[expr.length + buff.length]; 61 62 63 int charpos = 0; 66 67 int exprpos = 0; 69 int buffpos = 0; 70 int rsltpos = 0; 71 int offset = -1; 72 73 int mcount = 0; 75 76 map.put(Integer.toString(mcount),data); 78 79 boolean matchBegin = false; 81 if (expr[charpos] == MATCH_BEGIN) { 82 matchBegin = true; 83 exprpos = ++charpos; 84 } 85 86 while (expr[charpos] >= 0) 88 charpos++; 89 90 int exprchr = expr[charpos]; 92 93 while (true) { 94 if (matchBegin) { 97 if (!matchArray(expr, exprpos, charpos, buff, buffpos)) 98 return (false); 99 matchBegin = false; 100 } else { 101 offset = indexOfArray (expr, exprpos, charpos, buff, 102 buffpos); 103 if (offset < 0) 104 return (false); 105 } 106 107 if (matchBegin) { 109 if (offset != 0) 110 return (false); 111 matchBegin = false; 112 } 113 114 buffpos += (charpos - exprpos); 116 117 if (exprchr == MATCH_END) { 119 if (rsltpos > 0) 120 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 121 return (true); 123 } else if (exprchr == MATCH_THEEND) { 124 if (rsltpos > 0) 125 map.put (Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 126 return (buffpos == buff.length); 128 } 129 130 exprpos = ++charpos; 132 while (expr[charpos] >= 0) 133 charpos++; 134 int prevchr = exprchr; 135 exprchr = expr[charpos]; 136 137 offset = (prevchr == MATCH_FILE) ? 139 indexOfArray (expr, exprpos, charpos, buff, buffpos) : 140 lastIndexOfArray (expr, exprpos, charpos, buff, 141 buffpos); 142 143 if (offset < 0) 144 return (false); 145 146 if (prevchr == MATCH_PATH) { 149 while (buffpos < offset) 150 rslt[rsltpos++] = buff[buffpos++]; 151 } else { 152 while (buffpos < offset) { 154 if (buff[buffpos] == '/') 155 return (false); 156 rslt[rsltpos++] = buff[buffpos++]; 157 } 158 } 159 160 map.put(Integer.toString(++mcount),new String (rslt, 0, rsltpos)); 161 rsltpos = 0; 162 } 163 } 164 165 180 protected static int indexOfArray (int r[], int rpos, int rend, 181 char d[], int dpos) { 182 if (rend < rpos) 184 throw new IllegalArgumentException ("rend < rpos"); 185 if (rend == rpos) 187 return (d.length); if ((rend - rpos) == 1) { 190 for (int x = dpos; x < d.length; x++) 192 if (r[rpos] == d[x]) 193 return (x); 194 } 195 while ((dpos + rend - rpos) <= d.length) { 198 int y = dpos; 200 for (int x = rpos; x <= rend; x++) { 203 if (x == rend) 204 return (dpos); 205 if (r[x] != d[y++]) 206 break; 207 } 208 dpos++; 210 } 211 return (-1); 214 } 215 216 231 protected static int lastIndexOfArray (int r[], int rpos, int rend, 232 char d[], int dpos) { 233 if (rend < rpos) 235 throw new IllegalArgumentException ("rend < rpos"); 236 if (rend == rpos) 238 return (d.length); 240 if ((rend - rpos) == 1) { 242 for (int x = d.length - 1; x > dpos; x--) 244 if (r[rpos] == d[x]) 245 return (x); 246 } 247 248 int l = d.length - (rend - rpos); 251 while (l >= dpos) { 252 int y = l; 254 for (int x = rpos; x <= rend; x++) { 257 if (x == rend) 258 return (l); 259 if (r[x] != d[y++]) 260 break; 261 } 262 l--; 264 } 265 return (-1); 268 } 269 270 282 protected static boolean matchArray (int r[], int rpos, int rend, 283 char d[], int dpos) { 284 if (d.length - dpos < rend - rpos) 285 return (false); 286 for (int i = rpos; i < rend; i++) 287 if (r[i] != d[dpos++]) 288 return (false); 289 return (true); 290 } 291 } 292 | Popular Tags |