KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > rewrite > RewriteCond


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23
24 package org.jboss.web.rewrite;
25
26 import java.util.Map JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30 public class RewriteCond {
31
32     public abstract class Condition {
33         public abstract boolean evaluate(String JavaDoc value, Resolver resolver);
34     }
35     
36     public class PatternCondition extends Condition {
37         public Pattern JavaDoc pattern;
38         public Matcher JavaDoc matcher = null;
39         public boolean evaluate(String JavaDoc value, Resolver resolver) {
40             Matcher JavaDoc 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         /**
52          * -1: <
53          * 0: =
54          * 1: >
55          */

56         public int type = 0;
57         public String JavaDoc condition;
58         public boolean evaluate(String JavaDoc 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         /**
76          * 0: -d (is directory ?)
77          * 1: -f (is regular file ?)
78          * 2: -s (is regular file with size ?)
79          */

80         public int type = 0;
81         public boolean evaluate(String JavaDoc 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 JavaDoc testString = null;
97     protected String JavaDoc condPattern = null;
98     
99     public String JavaDoc getCondPattern() {
100         return condPattern;
101     }
102
103     public void setCondPattern(String JavaDoc condPattern) {
104         this.condPattern = condPattern;
105     }
106
107     public String JavaDoc getTestString() {
108         return testString;
109     }
110
111     public void setTestString(String JavaDoc testString) {
112         this.testString = testString;
113     }
114
115     public void parse(Map JavaDoc 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 JavaDoc getMatcher() {
155         Object JavaDoc condition = this.condition.get();
156         if (condition instanceof PatternCondition) {
157             return ((PatternCondition) condition).matcher;
158         }
159         return null;
160     }
161     
162     /**
163      * String representation.
164      */

165     public String JavaDoc toString() {
166         // FIXME: Add flags if possible
167
return "RewriteCond " + testString + " " + condPattern;
168     }
169     
170     
171     protected boolean positive = true;
172     
173     protected Substitution test = null;
174
175     protected ThreadLocal JavaDoc condition = new ThreadLocal JavaDoc();
176     
177     /**
178      * This makes the test case-insensitive, i.e., there is no difference between
179      * 'A-Z' and 'a-z' both in the expanded TestString and the CondPattern. This
180      * flag is effective only for comparisons between TestString and CondPattern.
181      * It has no effect on filesystem and subrequest checks.
182      */

183     public boolean nocase = false;
184     
185     /**
186      * Use this to combine rule conditions with a local OR instead of the implicit AND.
187      */

188     public boolean ornext = false;
189
190     /**
191      * Evaluate the condition based on the context
192      *
193      * @param rule corresponding matched rule
194      * @param cond last matched condition
195      * @return
196      */

197     public boolean evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
198         String JavaDoc 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