1 18 19 package alt.jiapi; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.StringTokenizer ; 25 26 import java.util.regex.Pattern ; 27 import java.util.regex.PatternSyntaxException ; 28 import org.apache.log4j.Category; 30 31 34 48 public class Rule implements Comparable { 49 private static Category log = Runtime.getLogCategory(Rule.class); 50 private Pattern pattern; 52 private double precedence; 54 private String ruleString; 55 56 private int SUMS[] = { 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 }; 57 58 64 public Rule(String rule) throws JiapiException { 65 ruleString = rule; 66 StringBuffer _rule = new StringBuffer (rule); 67 68 _rule = replaceText(_rule, ".", "\\."); 70 _rule = replaceText(_rule, "*", ".*"); 72 _rule.insert(0, "^"); 73 _rule.append("$"); 74 75 precedence = countPrecedence(ruleString); 76 try { 77 this.pattern = Pattern.compile(_rule.toString()); 79 } 80 catch (PatternSyntaxException se) { 81 throw new JiapiException("invalid rule syntax: " + ruleString, se); 82 } 83 } 84 85 90 public boolean match(String s) { 91 boolean b = pattern.matcher(s).matches(); 92 log.debug("matching " + s + " to " + pattern.pattern() + ": " + b); 93 return b; 94 } 96 97 105 public double getPrecedence() { 106 return precedence; 107 } 108 109 115 public boolean isMoreSignificant(Rule other) { 116 log.error("isMoreSignificant : this(" + ruleString +") " + 117 precedence + ", other(" + other.getRuleString() + 118 " " + other.getPrecedence()); 119 120 if (compareTo(other) > 0) { 121 return true; 122 } 123 124 return false; 125 } 126 127 132 public int compareTo(Object o) { 133 Rule other = (Rule) o; 134 135 if (precedence > other.getPrecedence()) { 136 return 1; 137 } 138 139 if (precedence < other.getPrecedence()) { 140 return -1; 141 } 142 143 if (this.equals(other)) { 144 return 0; 145 } 146 147 return -1; 150 } 151 152 158 public boolean equals(Object o) { 159 if (o instanceof Rule) { 160 return ruleString.equals(((Rule) o).getRuleString()); 161 } 162 163 return false; 164 } 165 166 public int hashCode() { 167 return ruleString.hashCode(); 168 } 169 170 public String toString() { 171 return ruleString; 172 } 173 174 177 String getRuleString() { 178 return ruleString; 179 } 180 181 184 private StringBuffer replaceText(StringBuffer sb, String old, 185 String replace) { 186 int a = sb.length(); 187 String s = sb.toString(); 188 int len = old.length(); 189 while (a >= 0) { 190 a = s.lastIndexOf(old, a); 191 if (a >= 0) { 192 sb.replace(a, a + len, replace); 193 a -= len; 194 } 195 } 196 197 return sb; 198 } 199 200 222 private double countPrecedence(String r) { 223 double s = 0.0; 224 225 List parts = new ArrayList (); 226 StringTokenizer st = new StringTokenizer (r, "."); 227 while (st.hasMoreTokens()) { 228 parts.add(st.nextToken()); 229 } 230 231 int divider = countSum(parts.size()); 232 int j = parts.size(); 233 for (Iterator i = parts.iterator(); i.hasNext(); j--) { 234 String part = (String ) i.next(); 235 236 if (part.indexOf('*') != -1) { 237 continue; 238 } 239 240 s += (((double) j) / divider); 241 } 242 243 return s; 244 } 245 246 249 private int countSum(int n) { 250 if (n < 0) { 251 throw new RuntimeException ("invalid argument:" + n); 252 } 253 254 if (n <= 10) { 255 return SUMS[n]; 256 } 257 258 int total = 0; 259 for (int i = 1; i < n; i++) { 260 total += i; 261 } 262 263 return total; 264 } 265 } 266 | Popular Tags |