1 19 20 package jode.obfuscator.modules; 21 import jode.obfuscator.*; 22 import java.util.Collection ; 23 24 public class WildCard implements IdentifierMatcher, OptionHandler { 25 26 String wildcard; 27 int firstStar; 28 29 public WildCard() { 30 } 31 32 public WildCard(String wild) { 33 wildcard = wild; 34 firstStar = wildcard.indexOf('*'); 35 } 36 37 public void setOption(String option, Collection values) { 38 if (option.equals("value")) { 39 if (values.size() != 1) 40 throw new IllegalArgumentException 41 ("Wildcard supports only one value."); 42 wildcard = (String ) values.iterator().next(); 43 firstStar = wildcard.indexOf('*'); 44 } else 45 throw new IllegalArgumentException ("Invalid option `"+option+"'."); 46 } 47 48 public String getNextComponent(Identifier ident) { 49 String prefix = ident.getFullName(); 50 if (prefix.length() > 0) 51 prefix += "."; 52 53 int lastDot = prefix.length(); 54 if (!wildcard.startsWith(prefix)) 55 return null; 56 57 int nextDot = wildcard.indexOf('.', lastDot); 58 if (nextDot > 0 59 && (nextDot <= firstStar || firstStar == -1)) 60 return wildcard.substring(lastDot, nextDot); 61 else if (firstStar == -1) 62 return wildcard.substring(lastDot); 63 else 64 return null; 65 } 66 67 public boolean matchesSub(Identifier ident, String subident) { 68 String prefix = ident.getFullName(); 69 if (prefix.length() > 0) 70 prefix += "."; 71 if (subident != null) 72 prefix += subident; 73 if (firstStar == -1 || firstStar >= prefix.length()) 74 return wildcard.startsWith(prefix); 75 return prefix.startsWith(wildcard.substring(0, firstStar)); 76 } 77 78 public boolean matches(Identifier ident) { 79 String test = ident.getFullName(); 80 if (firstStar == -1) { 81 if (wildcard.equals(test)) { 82 return true; 83 } 84 return false; 85 } 86 if (!test.startsWith(wildcard.substring(0, firstStar))) 87 return false; 88 89 test = test.substring(firstStar); 90 int lastWild = firstStar; 91 int nextWild; 92 while ((nextWild = wildcard.indexOf('*', lastWild + 1)) != -1) { 93 String pattern = wildcard.substring(lastWild+1, nextWild); 94 while (!test.startsWith(pattern)) { 95 if (test.length() == 0) 96 return false; 97 test = test.substring(1); 98 } 99 test = test.substring(nextWild - lastWild - 1); 100 lastWild = nextWild; 101 } 102 103 return test.endsWith(wildcard.substring(lastWild+1)); 104 } 105 106 public String toString() { 107 return "Wildcard "+wildcard; 108 } 109 } 110 | Popular Tags |