1 16 17 package org.apache.taglibs.standard.lang.jstl; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 31 32 public class FunctionInvocation 33 extends Expression 34 { 35 40 private String functionName; 41 private List argumentList; 42 public String getFunctionName() { return functionName; } 43 public void setFunctionName(String f) { functionName = f; } 44 public List getArgumentList() { return argumentList; } 45 public void setArgumentList(List l) { argumentList = l; } 46 47 51 public FunctionInvocation (String functionName, List argumentList) 52 { 53 this.functionName = functionName; 54 this.argumentList = argumentList; 55 } 56 57 63 public String getExpressionString () 64 { 65 StringBuffer b = new StringBuffer (); 66 b.append(functionName); 67 b.append("("); 68 Iterator i = argumentList.iterator(); 69 while (i.hasNext()) { 70 b.append(((Expression) i.next()).getExpressionString()); 71 if (i.hasNext()) 72 b.append(", "); 73 } 74 b.append(")"); 75 return b.toString(); 76 } 77 78 79 84 public Object evaluate (Object pContext, 85 VariableResolver pResolver, 86 Map functions, 87 String defaultPrefix, 88 Logger pLogger) 89 throws ELException 90 { 91 92 if (functions == null) 94 pLogger.logError(Constants.UNKNOWN_FUNCTION, functionName); 95 96 String functionName = this.functionName; 98 if (functionName.indexOf(":") == -1) { 99 if (defaultPrefix == null) 100 pLogger.logError(Constants.UNKNOWN_FUNCTION, functionName); 101 functionName = defaultPrefix + ":" + functionName; 102 } 103 104 Method target = (Method ) functions.get(functionName); 106 if (target == null) 107 pLogger.logError(Constants.UNKNOWN_FUNCTION, functionName); 108 109 Class [] params = target.getParameterTypes(); 111 if (params.length != argumentList.size()) 112 pLogger.logError(Constants.INAPPROPRIATE_FUNCTION_ARG_COUNT, 113 new Integer (params.length), 114 new Integer (argumentList.size())); 115 116 Object [] arguments = new Object [argumentList.size()]; 118 for (int i = 0; i < params.length; i++) { 119 arguments[i] = ((Expression) argumentList.get(i)).evaluate(pContext, 121 pResolver, 122 functions, 123 defaultPrefix, 124 pLogger); 125 arguments[i] = Coercions.coerce(arguments[i], params[i], pLogger); 127 } 128 129 try { 131 return (target.invoke(null, arguments)); 132 } catch (InvocationTargetException ex) { 133 pLogger.logError(Constants.FUNCTION_INVOCATION_ERROR, 134 ex.getTargetException(), 135 functionName); 136 return null; 137 } catch (Exception ex) { 138 pLogger.logError(Constants.FUNCTION_INVOCATION_ERROR, ex, functionName); 139 return null; 140 } 141 } 142 143 } 145 | Popular Tags |