1 21 package proguard.util; 22 23 import java.util.*; 24 25 42 public class BasicMatcher implements StringMatcher 43 { 44 private static final String SINGLE_CHARACTER_WILDCARD = "?"; 45 private static final String MULTIPLE_CHARACTERS_WILDCARD1 = "*"; 46 private static final String MULTIPLE_CHARACTERS_WILDCARD2 = "**"; 47 private static final String SPECIAL_CHARACTER_WILDCARD = "%"; 48 49 private String [] expressionParts; 50 private char[] wildcardCharacters; 51 private char[] extendedWildcardCharacters; 52 private char[] specialWildcardCharacters; 53 54 55 61 public BasicMatcher(String regularExpression) 62 { 63 this(regularExpression, null, null, null); 64 } 65 66 67 76 public BasicMatcher(String regularExpression, 77 char[] wildcardCharacters, 78 char[] extendedWildcardCharacters, 79 char[] specialWildcardCharacters) 80 { 81 this.wildcardCharacters = wildcardCharacters; 82 this.extendedWildcardCharacters = extendedWildcardCharacters; 83 this.specialWildcardCharacters = specialWildcardCharacters; 84 85 88 List expressionPartsList = new ArrayList(); 90 91 String wildcard = null; 92 int previousIndex = 0; 93 int index = 0; 94 int regularExpressionLength = regularExpression.length(); 95 while (index < regularExpressionLength) 96 { 97 wildcard = 98 regularExpression.regionMatches(index, MULTIPLE_CHARACTERS_WILDCARD2, 0, MULTIPLE_CHARACTERS_WILDCARD2.length()) ? MULTIPLE_CHARACTERS_WILDCARD2 : 99 regularExpression.regionMatches(index, MULTIPLE_CHARACTERS_WILDCARD1, 0, MULTIPLE_CHARACTERS_WILDCARD1.length()) ? MULTIPLE_CHARACTERS_WILDCARD1 : 100 regularExpression.regionMatches(index, SINGLE_CHARACTER_WILDCARD, 0, SINGLE_CHARACTER_WILDCARD.length()) ? SINGLE_CHARACTER_WILDCARD : 101 regularExpression.regionMatches(index, SPECIAL_CHARACTER_WILDCARD, 0, SINGLE_CHARACTER_WILDCARD.length()) ? SPECIAL_CHARACTER_WILDCARD : 102 null; 103 if (wildcard != null) 104 { 105 if (previousIndex < index) 107 { 108 expressionPartsList.add(regularExpression.substring(previousIndex, index)); 109 } 110 111 expressionPartsList.add(wildcard); 113 114 index += wildcard.length(); 116 previousIndex = index; 117 } 118 else 119 { 120 index++; 122 } 123 } 124 125 if (wildcard == null) 127 { 128 expressionPartsList.add(regularExpression.substring(previousIndex)); 129 } 130 131 expressionParts = new String [expressionPartsList.size()]; 133 expressionPartsList.toArray(expressionParts); 134 } 135 136 137 139 public boolean matches(String string) 140 { 141 return matches(string, 0, 0); 142 } 143 144 145 149 private boolean matches(String string, 150 int stringStartIndex, 151 int expressionIndex) 152 { 153 if (expressionIndex == expressionParts.length) 155 { 156 return stringStartIndex == string.length(); 158 } 159 160 String expressionPart = expressionParts[expressionIndex]; 161 162 if (expressionPart.equals(SINGLE_CHARACTER_WILDCARD)) 164 { 165 if (stringStartIndex == string.length()) 167 { 168 return false; 170 } 171 172 return 175 matchesWildcard(string.charAt(stringStartIndex)) && 176 matches(string, stringStartIndex + 1, expressionIndex + 1); 177 } 178 else if (expressionPart.equals(MULTIPLE_CHARACTERS_WILDCARD1)) 179 { 180 for (int stringEndIndex = stringStartIndex; 183 stringEndIndex <= string.length(); 184 stringEndIndex++) 185 { 186 if (stringEndIndex > stringStartIndex) 188 { 189 if (!matchesWildcard(string.charAt(stringEndIndex-1))) 191 { 192 return false; 194 } 195 } 196 197 if (matches(string, stringEndIndex, expressionIndex + 1)) 200 { 201 return true; 202 } 203 } 204 205 return false; 208 } 209 else if (expressionPart.equals(MULTIPLE_CHARACTERS_WILDCARD2)) 210 { 211 for (int stringEndIndex = stringStartIndex; 213 stringEndIndex <= string.length(); 214 stringEndIndex++) 215 { 216 if (stringEndIndex > stringStartIndex) 218 { 219 if (!matchesExtendedWildcard(string.charAt(stringEndIndex-1))) 221 { 222 return false; 224 } 225 } 226 227 if (matches(string, stringEndIndex, expressionIndex + 1)) 230 { 231 return true; 232 } 233 } 234 235 return stringStartIndex == string.length(); 238 } 239 else if (expressionPart.equals(SPECIAL_CHARACTER_WILDCARD)) 240 { 241 if (stringStartIndex == string.length()) 243 { 244 return false; 246 } 247 248 return 251 matchesSpecialWildcard(string.charAt(stringStartIndex)) && 252 matches(string, stringStartIndex + 1, expressionIndex + 1); 253 } 254 else 255 { 256 int expressionPartLength = expressionPart.length(); 259 return 260 string.regionMatches(stringStartIndex, expressionPart, 0, expressionPartLength) && 261 matches(string, stringStartIndex + expressionPartLength, expressionIndex + 1); 262 } 263 } 264 265 266 269 private boolean matchesWildcard(char character) 270 { 271 if (Character.isJavaIdentifierPart(character)) 272 { 273 return true; 274 } 275 276 if (wildcardCharacters != null) 277 { 278 for (int index = 0; index < wildcardCharacters.length; index++) 279 { 280 if (character == wildcardCharacters[index]) 281 { 282 return true; 283 } 284 } 285 } 286 287 return false; 288 } 289 290 291 294 private boolean matchesExtendedWildcard(char character) 295 { 296 if (matchesWildcard(character)) 297 { 298 return true; 299 } 300 301 if (extendedWildcardCharacters != null) 302 { 303 for (int index = 0; index < extendedWildcardCharacters.length; index++) 304 { 305 if (character == extendedWildcardCharacters[index]) 306 { 307 return true; 308 } 309 } 310 } 311 312 return false; 313 } 314 315 316 319 private boolean matchesSpecialWildcard(char character) 320 { 321 if (specialWildcardCharacters != null) 322 { 323 for (int index = 0; index < specialWildcardCharacters.length; index++) 324 { 325 if (character == specialWildcardCharacters[index]) 326 { 327 return true; 328 } 329 } 330 } 331 332 return false; 333 } 334 335 336 339 public static void main(String [] args) 340 { 341 try 342 { 343 System.out.println("Regular expression ["+args[0]+"]"); 344 BasicMatcher matcher = 345 new BasicMatcher(args[0], null, new char[] {'/'}, null); 346 347 for (int index = 1; index < args.length; index++) 348 { 349 String string = args[index]; 350 System.out.print("String ["+string+"]"); 351 System.out.println(" -> match = "+matcher.matches(args[index])); 352 } 353 } 354 catch (Exception ex) 355 { 356 ex.printStackTrace(); 357 } 358 } 359 } 360 | Popular Tags |