KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > el > Expression


1 package fr.improve.struts.taglib.layout.el;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.WeakHashMap JavaDoc;
7
8 import javax.servlet.jsp.PageContext JavaDoc;
9
10 import fr.improve.struts.taglib.layout.skin.Skin;
11 import fr.improve.struts.taglib.layout.util.LayoutUtils;
12
13 /**
14  * Version très simplifiée des EL de la JSTL.
15  *
16  * @author jer80876
17  */

18 public class Expression {
19     
20     
21     
22     /**
23      * Cache des expressions déjà utilisés.
24      */

25     private static WeakHashMap JavaDoc cache = new WeakHashMap JavaDoc();
26     
27     /**
28      * Méthode public évaluant une expression.
29      */

30     public static String JavaDoc evaluate(String JavaDoc in_string, PageContext JavaDoc in_pg) throws EvaluationException {
31         if (in_string==null) {
32             return null;
33         }
34         Skin skin = LayoutUtils.getSkin(in_pg.getSession());
35         String JavaDoc elDelim = skin.getELCharacter() + "{";
36         if (in_string.indexOf(elDelim)==-1) {
37             return in_string;
38         }
39         
40         // Ok, on a affaire à une expression.
41
Expression lc_expr = (Expression) cache.get(in_string);
42         if (lc_expr==null) {
43             lc_expr = new Expression(in_string, elDelim);
44             cache.put(in_string, lc_expr);
45         }
46         
47         return lc_expr.evalueExpression(in_pg);
48     }
49     
50     /**
51      * Blocs de l'expression.
52      */

53     private List JavaDoc blocks = new ArrayList JavaDoc();
54     
55     /**
56      * Parse une expression.
57      */

58     private Expression(String JavaDoc in_string, String JavaDoc in_delim) throws EvaluationException {
59         int lc_beginIndex = -1;
60         int lc_endIndex = -1;
61         String JavaDoc lc_string = in_string;
62         lc_beginIndex = lc_string.indexOf(in_delim);
63         while (lc_beginIndex!=-1) {
64             // On cherche l'emplacement d'une variable.
65
lc_endIndex = lc_string.indexOf('}', lc_beginIndex);
66             if (lc_endIndex==-1) {
67                 throw new EvaluationException("Invalid expression " + in_string);
68             }
69             
70             // On créé le bloc texte avant la variable.
71
if (lc_beginIndex>0) {
72                 blocks.add(new TextBlock(lc_string.substring(0, lc_beginIndex)));
73             }
74             
75             // Check for nested variable (1 niveau)
76
int lc_nestedVar = lc_string.indexOf(in_delim, lc_beginIndex+2);
77             if (lc_nestedVar>lc_beginIndex && lc_nestedVar<lc_endIndex) {
78                 // Nested variable, fix end location.
79
lc_endIndex = lc_string.indexOf('}', lc_endIndex+1);
80             }
81             
82             // On créé le bloc variable.
83
String JavaDoc lc_variable = lc_string.substring(lc_beginIndex+2, lc_endIndex);
84             int lc_delim = lc_variable.indexOf('.');
85             if (lc_delim==-1) {
86                 blocks.add(parseBlock(lc_variable, null));
87             } else {
88                 blocks.add(parseBlock(lc_variable.substring(0,lc_delim), lc_variable.substring(lc_delim+1)));
89             }
90             
91             
92             // Et on continue.
93
lc_string = lc_string.substring(lc_endIndex+1);
94             lc_beginIndex = lc_string.indexOf(in_delim);
95             lc_endIndex = -1;
96             
97                         
98         }
99         
100         // Plus (ou pas de variable)
101
if (lc_string!=null) {
102             blocks.add(new TextBlock(lc_string));
103         }
104     }
105     
106     private Block parseBlock(String JavaDoc in_name, String JavaDoc in_property) {
107         if (in_name!=null) {
108             int position = in_name.indexOf('+');
109             if (position!=-1) {
110                 String JavaDoc left = in_name.substring(0, position);
111                 String JavaDoc right = in_name.substring(position+1);
112                 VarBlock var1 = new VarBlock(left, null);
113                 TextBlock var2 = new TextBlock(right);
114                 return new AddBlock(var1, var2);
115             }
116         }
117         return new VarBlock(in_name, in_property);
118     }
119     
120     private String JavaDoc evalueExpression(PageContext JavaDoc in_pg) throws EvaluationException {
121         StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc(10 * blocks.size());
122         Iterator JavaDoc lc_it = blocks.iterator();
123         while (lc_it.hasNext()) {
124             Block lc_block = (Block) lc_it.next();
125             lc_buffer.append(lc_block.evaluate(in_pg));
126         }
127         return lc_buffer.toString();
128     }
129     
130     public String JavaDoc toString() {
131         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
132         Iterator JavaDoc it = blocks.iterator();
133         while (it.hasNext()) {
134             Block block = (Block) it.next();
135             buffer.append(block.toString());
136             buffer.append(" ");
137         }
138         return buffer.toString();
139     }
140     
141     public static void main(String JavaDoc argv[]) {
142         Expression lc_expr = new Expression("${personnes[${idPersonne}].qualite}", "${");
143         System.out.println(lc_expr);
144         
145         lc_expr = new Expression("${name}", "${");
146         System.out.println(lc_expr);
147         
148         lc_expr = new Expression("${name.property}", "${");
149         System.out.println(lc_expr);
150         
151         lc_expr = new Expression("abc${name}def", "${");
152         System.out.println(lc_expr);
153         
154         lc_expr = new Expression("${index+1}", "${");
155         System.out.println(lc_expr);
156     }
157 }
158
Popular Tags