1 17 18 package org.apache.jasper.compiler; 19 20 import java.util.*; 21 import javax.servlet.jsp.tagext.FunctionInfo ; 22 import org.apache.jasper.JasperException; 23 24 32 33 public class ELFunctionMapper { 34 static private int currFunc = 0; 35 private ErrorDispatcher err; 36 StringBuffer ds; StringBuffer ss; 39 45 public static void map(Compiler compiler, Node.Nodes page) 46 throws JasperException { 47 48 currFunc = 0; 49 ELFunctionMapper map = new ELFunctionMapper(); 50 map.err = compiler.getErrorDispatcher(); 51 map.ds = new StringBuffer (); 52 map.ss = new StringBuffer (); 53 54 page.visit(map.new ELFunctionVisitor()); 55 56 String ds = map.ds.toString(); 58 if (ds.length() > 0) { 59 Node root = page.getRoot(); 60 new Node.Declaration(map.ss.toString(), null, root); 61 new Node.Declaration("static {\n" + ds + "}\n", null, root); 62 } 63 } 64 65 69 class ELFunctionVisitor extends Node.Visitor { 70 71 75 private HashMap gMap = new HashMap(); 76 77 public void visit(Node.ParamAction n) throws JasperException { 78 doMap(n.getValue()); 79 visitBody(n); 80 } 81 82 public void visit(Node.IncludeAction n) throws JasperException { 83 doMap(n.getPage()); 84 visitBody(n); 85 } 86 87 public void visit(Node.ForwardAction n) throws JasperException { 88 doMap(n.getPage()); 89 visitBody(n); 90 } 91 92 public void visit(Node.SetProperty n) throws JasperException { 93 doMap(n.getValue()); 94 visitBody(n); 95 } 96 97 public void visit(Node.UseBean n) throws JasperException { 98 doMap(n.getBeanName()); 99 visitBody(n); 100 } 101 102 public void visit(Node.PlugIn n) throws JasperException { 103 doMap(n.getHeight()); 104 doMap(n.getWidth()); 105 visitBody(n); 106 } 107 108 public void visit(Node.JspElement n) throws JasperException { 109 110 Node.JspAttribute[] attrs = n.getJspAttributes(); 111 for (int i = 0; attrs != null && i < attrs.length; i++) { 112 doMap(attrs[i]); 113 } 114 doMap(n.getNameAttribute()); 115 visitBody(n); 116 } 117 118 public void visit(Node.UninterpretedTag n) throws JasperException { 119 120 Node.JspAttribute[] attrs = n.getJspAttributes(); 121 for (int i = 0; attrs != null && i < attrs.length; i++) { 122 doMap(attrs[i]); 123 } 124 visitBody(n); 125 } 126 127 public void visit(Node.CustomTag n) throws JasperException { 128 Node.JspAttribute[] attrs = n.getJspAttributes(); 129 for (int i = 0; attrs != null && i < attrs.length; i++) { 130 doMap(attrs[i]); 131 } 132 visitBody(n); 133 } 134 135 public void visit(Node.ELExpression n) throws JasperException { 136 doMap(n.getEL()); 137 } 138 139 private void doMap(Node.JspAttribute attr) 140 throws JasperException { 141 if (attr != null) { 142 doMap(attr.getEL()); 143 } 144 } 145 146 149 private void doMap(ELNode.Nodes el) 150 throws JasperException { 151 152 class Fvisitor extends ELNode.Visitor { 154 ArrayList funcs = new ArrayList(); 155 HashMap keyMap = new HashMap(); 156 public void visit(ELNode.Function n) throws JasperException { 157 String key = n.getPrefix() + ":" + n.getName(); 158 if (! keyMap.containsKey(key)) { 159 keyMap.put(key,""); 160 funcs.add(n); 161 } 162 } 163 } 164 165 if (el == null) { 166 return; 167 } 168 169 Fvisitor fv = new Fvisitor(); 171 el.visit(fv); 172 ArrayList functions = fv.funcs; 173 174 if (functions.size() == 0) { 175 return; 176 } 177 178 String decName = matchMap(functions); 180 if (decName != null) { 181 el.setMapName(decName); 182 return; 183 } 184 185 decName = getMapName(); 187 ss.append("static private org.apache.jasper.runtime.ProtectedFunctionMapper " + decName + ";\n"); 188 189 ds.append(" " + decName + "= "); 190 ds.append("org.apache.jasper.runtime.ProtectedFunctionMapper"); 191 192 String funcMethod = null; 194 if (functions.size() == 1) { 195 funcMethod = ".getMapForFunction"; 196 } else { 197 ds.append(".getInstance();\n"); 198 funcMethod = " " + decName + ".mapFunction"; 199 } 200 201 for (int i = 0; i < functions.size(); i++) { 203 ELNode.Function f = (ELNode.Function)functions.get(i); 204 FunctionInfo funcInfo = f.getFunctionInfo(); 205 String key = f.getPrefix()+ ":" + f.getName(); 206 ds.append(funcMethod + "(\"" + key + "\", " + 207 funcInfo.getFunctionClass() + ".class, " + 208 '\"' + f.getMethodName() + "\", " + 209 "new Class[] {"); 210 String params[] = f.getParameters(); 211 for (int k = 0; k < params.length; k++) { 212 if (k != 0) { 213 ds.append(", "); 214 } 215 int iArray = params[k].indexOf('['); 216 if (iArray < 0) { 217 ds.append(params[k] + ".class"); 218 } 219 else { 220 String baseType = params[k].substring(0, iArray); 221 ds.append("java.lang.reflect.Array.newInstance("); 222 ds.append(baseType); 223 ds.append(".class,"); 224 225 int aCount = 0; 227 for (int jj = iArray; jj < params[k].length(); jj++ ) { 228 if (params[k].charAt(jj) == '[') { 229 aCount++; 230 } 231 } 232 if (aCount == 1) { 233 ds.append("0).getClass()"); 234 } else { 235 ds.append("new int[" + aCount + "]).getClass()"); 236 } 237 } 238 } 239 ds.append("});\n"); 240 gMap.put(f.getPrefix() + ':' + f.getName() + ':' + f.getUri(), 242 decName); 243 } 244 el.setMapName(decName); 245 } 246 247 255 private String matchMap(ArrayList functions) { 256 257 String mapName = null; 258 for (int i = 0; i < functions.size(); i++) { 259 ELNode.Function f = (ELNode.Function)functions.get(i); 260 String temName = (String ) gMap.get(f.getPrefix() + ':' + 261 f.getName() + ':' + f.getUri()); 262 if (temName == null) { 263 return null; 264 } 265 if (mapName == null) { 266 mapName = temName; 267 } else if (!temName.equals(mapName)) { 268 return null; 270 } 271 } 272 return mapName; 273 } 274 275 278 private String getMapName() { 279 return "_jspx_fnmap_" + currFunc++; 280 } 281 } 282 } 283 284 | Popular Tags |