1 22 23 24 package org.jboss.web.rewrite; 25 26 import java.util.Map ; 27 import java.util.regex.Matcher ; 28 import java.util.regex.Pattern ; 29 30 public class RewriteCond { 31 32 public abstract class Condition { 33 public abstract boolean evaluate(String value, Resolver resolver); 34 } 35 36 public class PatternCondition extends Condition { 37 public Pattern pattern; 38 public Matcher matcher = null; 39 public boolean evaluate(String value, Resolver resolver) { 40 Matcher m = pattern.matcher(value); 41 if (m.matches()) { 42 matcher = m; 43 return true; 44 } else { 45 return false; 46 } 47 } 48 } 49 50 public class LexicalCondition extends Condition { 51 56 public int type = 0; 57 public String condition; 58 public boolean evaluate(String value, Resolver resolver) { 59 int result = value.compareTo(condition); 60 switch (type) { 61 case -1: 62 return (result < 0); 63 case 0: 64 return (result == 0); 65 case 1: 66 return (result > 0); 67 default: 68 return false; 69 } 70 71 } 72 } 73 74 public class ResourceCondition extends Condition { 75 80 public int type = 0; 81 public boolean evaluate(String value, Resolver resolver) { 82 switch (type) { 83 case 0: 84 return true; 85 case 1: 86 return true; 87 case 2: 88 return true; 89 default: 90 return false; 91 } 92 93 } 94 } 95 96 protected String testString = null; 97 protected String condPattern = null; 98 99 public String getCondPattern() { 100 return condPattern; 101 } 102 103 public void setCondPattern(String condPattern) { 104 this.condPattern = condPattern; 105 } 106 107 public String getTestString() { 108 return testString; 109 } 110 111 public void setTestString(String testString) { 112 this.testString = testString; 113 } 114 115 public void parse(Map maps) { 116 test = new Substitution(); 117 test.setSub(testString); 118 test.parse(maps); 119 if (condPattern.startsWith("!")) { 120 positive = false; 121 condPattern = condPattern.substring(1); 122 } 123 if (condPattern.startsWith("<")) { 124 LexicalCondition condition = new LexicalCondition(); 125 condition.type = -1; 126 condition.condition = condPattern.substring(1); 127 } else if (condPattern.startsWith(">")) { 128 LexicalCondition condition = new LexicalCondition(); 129 condition.type = 1; 130 condition.condition = condPattern.substring(1); 131 } else if (condPattern.startsWith("=")) { 132 LexicalCondition condition = new LexicalCondition(); 133 condition.type = 0; 134 condition.condition = condPattern.substring(1); 135 } else if (condPattern.equals("-d")) { 136 ResourceCondition ncondition = new ResourceCondition(); 137 ncondition.type = 0; 138 } else if (condPattern.equals("-f")) { 139 ResourceCondition ncondition = new ResourceCondition(); 140 ncondition.type = 1; 141 } else if (condPattern.equals("-s")) { 142 ResourceCondition ncondition = new ResourceCondition(); 143 ncondition.type = 2; 144 } else { 145 PatternCondition condition = new PatternCondition(); 146 int flags = 0; 147 if (isNocase()) { 148 flags |= Pattern.CASE_INSENSITIVE; 149 } 150 condition.pattern = Pattern.compile(condPattern, flags); 151 } 152 } 153 154 public Matcher getMatcher() { 155 Object condition = this.condition.get(); 156 if (condition instanceof PatternCondition) { 157 return ((PatternCondition) condition).matcher; 158 } 159 return null; 160 } 161 162 165 public String toString() { 166 return "RewriteCond " + testString + " " + condPattern; 168 } 169 170 171 protected boolean positive = true; 172 173 protected Substitution test = null; 174 175 protected ThreadLocal condition = new ThreadLocal (); 176 177 183 public boolean nocase = false; 184 185 188 public boolean ornext = false; 189 190 197 public boolean evaluate(Matcher rule, Matcher cond, Resolver resolver) { 198 String value = test.evaluate(rule, cond, resolver); 199 if (nocase) { 200 value = value.toLowerCase(); 201 } 202 Condition condition = (Condition) this.condition.get(); 203 if (condition == null) { 204 if (condPattern.startsWith("<")) { 205 LexicalCondition ncondition = new LexicalCondition(); 206 ncondition.type = -1; 207 ncondition.condition = condPattern.substring(1); 208 condition = ncondition; 209 } else if (condPattern.startsWith(">")) { 210 LexicalCondition ncondition = new LexicalCondition(); 211 ncondition.type = 1; 212 ncondition.condition = condPattern.substring(1); 213 condition = ncondition; 214 } else if (condPattern.startsWith("=")) { 215 LexicalCondition ncondition = new LexicalCondition(); 216 ncondition.type = 0; 217 ncondition.condition = condPattern.substring(1); 218 condition = ncondition; 219 } else if (condPattern.equals("-d")) { 220 ResourceCondition ncondition = new ResourceCondition(); 221 ncondition.type = 0; 222 condition = ncondition; 223 } else if (condPattern.equals("-f")) { 224 ResourceCondition ncondition = new ResourceCondition(); 225 ncondition.type = 1; 226 condition = ncondition; 227 } else if (condPattern.equals("-s")) { 228 ResourceCondition ncondition = new ResourceCondition(); 229 ncondition.type = 2; 230 condition = ncondition; 231 } else { 232 PatternCondition ncondition = new PatternCondition(); 233 int flags = 0; 234 if (isNocase()) { 235 flags |= Pattern.CASE_INSENSITIVE; 236 } 237 ncondition.pattern = Pattern.compile(condPattern, flags); 238 condition = ncondition; 239 } 240 this.condition.set(condition); 241 } 242 if (positive) { 243 return condition.evaluate(value, resolver); 244 } else { 245 return !condition.evaluate(value, resolver); 246 } 247 } 248 249 public boolean isNocase() { 250 return nocase; 251 } 252 253 public void setNocase(boolean nocase) { 254 this.nocase = nocase; 255 } 256 257 public boolean isOrnext() { 258 return ornext; 259 } 260 261 public void setOrnext(boolean ornext) { 262 this.ornext = ornext; 263 } 264 265 public boolean isPositive() { 266 return positive; 267 } 268 269 public void setPositive(boolean positive) { 270 this.positive = positive; 271 } 272 273 } 274 | Popular Tags |