KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29
30 public class Substitution {
31
32     public abstract class SubstitutionElement {
33         public abstract String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver);
34     }
35     
36     public class StaticElement extends SubstitutionElement {
37         public String JavaDoc value;
38
39         public String JavaDoc evaluate
40             (Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
41             return value;
42         }
43     
44     }
45     
46     public class RewriteRuleBackReferenceElement extends SubstitutionElement {
47         public int n;
48         public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
49             return rule.group(n);
50         }
51     }
52     
53     public class RewriteCondBackReferenceElement extends SubstitutionElement {
54         public int n;
55         public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
56             return cond.group(n);
57         }
58     }
59     
60     public class ServerVariableElement extends SubstitutionElement {
61         public String JavaDoc key;
62         public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
63             return resolver.resolve(key);
64         }
65     }
66     
67     public class ServerVariableEnvElement extends SubstitutionElement {
68         public String JavaDoc key;
69         public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
70             return resolver.resolveEnv(key);
71         }
72     }
73     
74     public class ServerVariableSslElement extends SubstitutionElement {
75         public String JavaDoc key;
76         public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
77             return resolver.resolveSsl(key);
78         }
79     }
80     
81     public class ServerVariableHttpElement extends SubstitutionElement {
82         public String JavaDoc key;
83         public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
84             return resolver.resolveHttp(key);
85         }
86     }
87     
88     public class MapElement extends SubstitutionElement {
89         public RewriteMap map = null;
90         public String JavaDoc key;
91         public String JavaDoc defaultValue = null;
92         public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
93             String JavaDoc result = map.lookup(key);
94             if (result == null) {
95                 result = defaultValue;
96             }
97             return result;
98         }
99     }
100     
101     protected SubstitutionElement[] elements = null;
102
103     protected String JavaDoc sub = null;
104     public String JavaDoc getSub() { return sub; }
105     public void setSub(String JavaDoc sub) { this.sub = sub; }
106
107     public void parse(Map JavaDoc maps) {
108
109         ArrayList JavaDoc elements = new ArrayList JavaDoc();
110         int pos = 0;
111         int percentPos = 0;
112         int dollarPos = 0;
113         
114         while (pos < sub.length()) {
115             percentPos = sub.indexOf('%', pos);
116             dollarPos = sub.indexOf('$', pos);
117             // FIXME: System.out.println("S: " + sub + " pos: " + pos + " L: " + sub.length() + " %: " + percentPos + " $: " + dollarPos);
118
if (percentPos == -1 && dollarPos == -1) {
119                 // Static text
120
StaticElement newElement = new StaticElement();
121                 newElement.value = sub.substring(pos, sub.length());
122                 pos = sub.length();
123                 elements.add(newElement);
124             } else if (percentPos == -1 || ((dollarPos != -1) && (dollarPos < percentPos))) {
125                 // $: backreference to rule or map lookup
126
if (dollarPos + 1 == sub.length()) {
127                     throw new IllegalArgumentException JavaDoc(sub);
128                 }
129                 if (pos < dollarPos) {
130                     // Static text
131
StaticElement newElement = new StaticElement();
132                     newElement.value = sub.substring(pos, dollarPos);
133                     pos = dollarPos;
134                     elements.add(newElement);
135                 }
136                 if (Character.isDigit(sub.charAt(dollarPos + 1))) {
137                     // $: backreference to rule
138
RewriteRuleBackReferenceElement newElement = new RewriteRuleBackReferenceElement();
139                     newElement.n = Character.digit(sub.charAt(dollarPos + 1), 10);
140                     pos = dollarPos + 2;
141                     elements.add(newElement);
142                 } else {
143                     // $: map lookup as ${mapname:key|default}
144
MapElement newElement = new MapElement();
145                     int open = sub.indexOf('{', dollarPos);
146                     int colon = sub.indexOf(':', dollarPos);
147                     int def = sub.indexOf('|', dollarPos);
148                     int close = sub.indexOf('}', dollarPos);
149                     if (!(-1 < open && open < colon && colon < close)) {
150                         throw new IllegalArgumentException JavaDoc(sub);
151                     }
152                     newElement.map = (RewriteMap) maps.get(sub.substring(open + 1, colon));
153                     if (newElement.map == null) {
154                         throw new IllegalArgumentException JavaDoc(sub + ": No map: " + sub.substring(open + 1, colon));
155                     }
156                     if (def > -1) {
157                         if (!(colon < def && def < close)) {
158                             throw new IllegalArgumentException JavaDoc(sub);
159                         }
160                         newElement.key = sub.substring(colon + 1, def);
161                         newElement.defaultValue = sub.substring(def + 1, close);
162                     } else {
163                         newElement.key = sub.substring(colon + 1, close);
164                     }
165                     pos = close + 1;
166                     elements.add(newElement);
167                 }
168             } else {
169                 // %: backreference to cond or server variable
170
if (percentPos + 1 == sub.length()) {
171                     throw new IllegalArgumentException JavaDoc(sub);
172                 }
173                 if (pos < percentPos) {
174                     // Static text
175
StaticElement newElement = new StaticElement();
176                     newElement.value = sub.substring(pos, percentPos);
177                     pos = percentPos;
178                     elements.add(newElement);
179                 }
180                 if (Character.isDigit(sub.charAt(percentPos + 1))) {
181                     // %: backreference to cond
182
RewriteCondBackReferenceElement newElement = new RewriteCondBackReferenceElement();
183                     newElement.n = Character.digit(sub.charAt(percentPos + 1), 10);
184                     pos = percentPos + 2;
185                     elements.add(newElement);
186                 } else {
187                     // %: server variable as %{variable}
188
SubstitutionElement newElement = null;
189                     int open = sub.indexOf('{', percentPos);
190                     int colon = sub.indexOf(':', percentPos);
191                     int close = sub.indexOf('}', percentPos);
192                     if (!(-1 < open && open < close)) {
193                         throw new IllegalArgumentException JavaDoc(sub);
194                     }
195                     if (colon > -1) {
196                         if (!(open < colon && colon < close)) {
197                             throw new IllegalArgumentException JavaDoc(sub);
198                         }
199                         String JavaDoc type = sub.substring(open + 1, colon);
200                         if (type.equals("ENV")) {
201                             newElement = new ServerVariableEnvElement();
202                             ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close);
203                         } else if (type.equals("SSL")) {
204                             newElement = new ServerVariableSslElement();
205                             ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close);
206                         } else if (type.equals("HTTP")) {
207                             newElement = new ServerVariableHttpElement();
208                             ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close);
209                         } else {
210                             throw new IllegalArgumentException JavaDoc(sub + ": Bad type: " + type);
211                         }
212                     } else {
213                         newElement = new ServerVariableElement();
214                         ((ServerVariableElement) newElement).key = sub.substring(open + 1, close);
215                     }
216                     pos = close + 1;
217                     elements.add(newElement);
218                 }
219             }
220         }
221         
222         this.elements = (SubstitutionElement[]) elements.toArray(new SubstitutionElement[0]);
223         
224     }
225     
226     /**
227      * Create a substitution with the given string.
228      */

229     /*
230     public Substitution(String sub, Map maps) {
231         ArrayList elements = new ArrayList();
232         int pos = 0;
233         int percentPos = 0;
234         int dollarPos = 0;
235         
236         while (pos < sub.length()) {
237             percentPos = sub.indexOf('%', pos);
238             dollarPos = sub.indexOf('$', pos);
239             // FIXME: System.out.println("S: " + sub + " pos: " + pos + " L: " + sub.length() + " %: " + percentPos + " $: " + dollarPos);
240             if (percentPos == -1 && dollarPos == -1) {
241                 // Static text
242                 StaticElement newElement = new StaticElement();
243                 newElement.value = sub.substring(pos, sub.length());
244                 pos = sub.length();
245                 elements.add(newElement);
246             } else if (percentPos == -1 || ((dollarPos != -1) && (dollarPos < percentPos))) {
247                 // $: backreference to rule or map lookup
248                 if (dollarPos + 1 == sub.length()) {
249                     throw new IllegalArgumentException(sub);
250                 }
251                 if (pos < dollarPos) {
252                     // Static text
253                     StaticElement newElement = new StaticElement();
254                     newElement.value = sub.substring(pos, dollarPos);
255                     pos = dollarPos;
256                     elements.add(newElement);
257                 }
258                 if (Character.isDigit(sub.charAt(dollarPos + 1))) {
259                     // $: backreference to rule
260                     RewriteRuleBackReferenceElement newElement = new RewriteRuleBackReferenceElement();
261                     newElement.n = Character.digit(sub.charAt(dollarPos + 1), 10);
262                     pos = dollarPos + 2;
263                     elements.add(newElement);
264                 } else {
265                     // $: map lookup as ${mapname:key|default}
266                     MapElement newElement = new MapElement();
267                     int open = sub.indexOf('{', dollarPos);
268                     int colon = sub.indexOf(':', dollarPos);
269                     int def = sub.indexOf('|', dollarPos);
270                     int close = sub.indexOf('}', dollarPos);
271                     if (!(-1 < open && open < colon && colon < close)) {
272                         throw new IllegalArgumentException(sub);
273                     }
274                     newElement.map = (RewriteMap) maps.get(sub.substring(open + 1, colon));
275                     if (newElement.map == null) {
276                         throw new IllegalArgumentException(sub + ": No map: " + sub.substring(open + 1, colon));
277                     }
278                     if (def > -1) {
279                         if (!(colon < def && def < close)) {
280                             throw new IllegalArgumentException(sub);
281                         }
282                         newElement.key = sub.substring(colon + 1, def);
283                         newElement.defaultValue = sub.substring(def + 1, close);
284                     } else {
285                         newElement.key = sub.substring(colon + 1, close);
286                     }
287                     pos = close + 1;
288                     elements.add(newElement);
289                 }
290             } else {
291                 // %: backreference to cond or server variable
292                 if (percentPos + 1 == sub.length()) {
293                     throw new IllegalArgumentException(sub);
294                 }
295                 if (pos < percentPos) {
296                     // Static text
297                     StaticElement newElement = new StaticElement();
298                     newElement.value = sub.substring(pos, percentPos);
299                     pos = percentPos;
300                     elements.add(newElement);
301                 }
302                 if (Character.isDigit(sub.charAt(percentPos + 1))) {
303                     // %: backreference to cond
304                     RewriteCondBackReferenceElement newElement = new RewriteCondBackReferenceElement();
305                     newElement.n = Character.digit(sub.charAt(percentPos + 1), 10);
306                     pos = percentPos + 2;
307                     elements.add(newElement);
308                 } else {
309                     // %: server variable as %{variable}
310                     SubstitutionElement newElement = null;
311                     int open = sub.indexOf('{', percentPos);
312                     int colon = sub.indexOf(':', percentPos);
313                     int close = sub.indexOf('}', percentPos);
314                     if (!(-1 < open && open < close)) {
315                         throw new IllegalArgumentException(sub);
316                     }
317                     if (colon > -1) {
318                         if (!(open < colon && colon < close)) {
319                             throw new IllegalArgumentException(sub);
320                         }
321                         String type = sub.substring(open + 1, colon);
322                         if (type.equals("ENV")) {
323                             newElement = new ServerVariableEnvElement();
324                             ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close);
325                         } else if (type.equals("SSL")) {
326                             newElement = new ServerVariableSslElement();
327                             ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close);
328                         } else if (type.equals("HTTP")) {
329                             newElement = new ServerVariableHttpElement();
330                             ((ServerVariableEnvElement) newElement).key = sub.substring(colon + 1, close);
331                         } else {
332                             throw new IllegalArgumentException(sub + ": Bad type: " + type);
333                         }
334                     } else {
335                         newElement = new ServerVariableElement();
336                         ((ServerVariableElement) newElement).key = sub.substring(open + 1, close);
337                     }
338                     pos = close + 1;
339                     elements.add(newElement);
340                 }
341             }
342         }
343         
344         this.elements = (SubstitutionElement[]) elements.toArray(new SubstitutionElement[0]);
345         
346     }
347     */

348     
349     /**
350      * Evaluate the substituation based on the context
351      *
352      * @param rule corresponding matched rule
353      * @param cond last matched condition
354      * @return
355      */

356     public String JavaDoc evaluate(Matcher JavaDoc rule, Matcher JavaDoc cond, Resolver resolver) {
357         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
358         for (int i = 0; i < elements.length; i++) {
359             buf.append(elements[i].evaluate(rule, cond, resolver));
360         }
361         return buf.toString();
362     }
363
364 }
365
Popular Tags