1 package fr.improve.struts.taglib.layout.el; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.WeakHashMap ; 7 8 import javax.servlet.jsp.PageContext ; 9 10 import fr.improve.struts.taglib.layout.skin.Skin; 11 import fr.improve.struts.taglib.layout.util.LayoutUtils; 12 13 18 public class Expression { 19 20 21 22 25 private static WeakHashMap cache = new WeakHashMap (); 26 27 30 public static String evaluate(String in_string, PageContext in_pg) throws EvaluationException { 31 if (in_string==null) { 32 return null; 33 } 34 Skin skin = LayoutUtils.getSkin(in_pg.getSession()); 35 String elDelim = skin.getELCharacter() + "{"; 36 if (in_string.indexOf(elDelim)==-1) { 37 return in_string; 38 } 39 40 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 53 private List blocks = new ArrayList (); 54 55 58 private Expression(String in_string, String in_delim) throws EvaluationException { 59 int lc_beginIndex = -1; 60 int lc_endIndex = -1; 61 String lc_string = in_string; 62 lc_beginIndex = lc_string.indexOf(in_delim); 63 while (lc_beginIndex!=-1) { 64 lc_endIndex = lc_string.indexOf('}', lc_beginIndex); 66 if (lc_endIndex==-1) { 67 throw new EvaluationException("Invalid expression " + in_string); 68 } 69 70 if (lc_beginIndex>0) { 72 blocks.add(new TextBlock(lc_string.substring(0, lc_beginIndex))); 73 } 74 75 int lc_nestedVar = lc_string.indexOf(in_delim, lc_beginIndex+2); 77 if (lc_nestedVar>lc_beginIndex && lc_nestedVar<lc_endIndex) { 78 lc_endIndex = lc_string.indexOf('}', lc_endIndex+1); 80 } 81 82 String 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 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 if (lc_string!=null) { 102 blocks.add(new TextBlock(lc_string)); 103 } 104 } 105 106 private Block parseBlock(String in_name, String in_property) { 107 if (in_name!=null) { 108 int position = in_name.indexOf('+'); 109 if (position!=-1) { 110 String left = in_name.substring(0, position); 111 String 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 evalueExpression(PageContext in_pg) throws EvaluationException { 121 StringBuffer lc_buffer = new StringBuffer (10 * blocks.size()); 122 Iterator 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 toString() { 131 StringBuffer buffer = new StringBuffer (); 132 Iterator 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 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 |