KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > FunctionInvocation


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jstl;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  *
27  * <p>Represents a function call.</p>
28  *
29  * @author Shawn Bayern (in the style of Nathan's other classes)
30  **/

31
32 public class FunctionInvocation
33   extends Expression
34 {
35   //-------------------------------------
36
// Properties
37
//-------------------------------------
38
// property index
39

40   private String JavaDoc functionName;
41   private List JavaDoc argumentList;
42   public String JavaDoc getFunctionName() { return functionName; }
43   public void setFunctionName(String JavaDoc f) { functionName = f; }
44   public List JavaDoc getArgumentList() { return argumentList; }
45   public void setArgumentList(List JavaDoc l) { argumentList = l; }
46
47   //-------------------------------------
48
/**
49    * Constructor
50    **/

51   public FunctionInvocation (String JavaDoc functionName, List JavaDoc argumentList)
52   {
53     this.functionName = functionName;
54     this.argumentList = argumentList;
55   }
56
57   //-------------------------------------
58
// Expression methods
59
//-------------------------------------
60
/**
61    * Returns the expression in the expression language syntax
62    **/

63   public String JavaDoc getExpressionString ()
64   {
65     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
66     b.append(functionName);
67     b.append("(");
68     Iterator JavaDoc 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   //-------------------------------------
80
/**
81    *
82    * Evaluates by looking up the name in the VariableResolver
83    **/

84   public Object JavaDoc evaluate (Object JavaDoc pContext,
85                           VariableResolver pResolver,
86               Map JavaDoc functions,
87               String JavaDoc defaultPrefix,
88                           Logger pLogger)
89     throws ELException
90   {
91
92     // if the Map is null, then the function is invalid
93
if (functions == null)
94       pLogger.logError(Constants.UNKNOWN_FUNCTION, functionName);
95
96     // normalize function name against default prefix
97
String JavaDoc 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     // ensure that the function's name is mapped
105
Method JavaDoc target = (Method JavaDoc) functions.get(functionName);
106     if (target == null)
107       pLogger.logError(Constants.UNKNOWN_FUNCTION, functionName);
108
109     // ensure that the number of arguments matches the number of parameters
110
Class JavaDoc[] params = target.getParameterTypes();
111     if (params.length != argumentList.size())
112       pLogger.logError(Constants.INAPPROPRIATE_FUNCTION_ARG_COUNT,
113                new Integer JavaDoc(params.length),
114                new Integer JavaDoc(argumentList.size()));
115
116     // now, walk through each parameter, evaluating and casting its argument
117
Object JavaDoc[] arguments = new Object JavaDoc[argumentList.size()];
118     for (int i = 0; i < params.length; i++) {
119       // evaluate
120
arguments[i] = ((Expression) argumentList.get(i)).evaluate(pContext,
121                                  pResolver,
122                                  functions,
123                                  defaultPrefix,
124                                  pLogger);
125       // coerce
126
arguments[i] = Coercions.coerce(arguments[i], params[i], pLogger);
127     }
128
129     // finally, invoke the target method, which we know to be static
130
try {
131       return (target.invoke(null, arguments));
132     } catch (InvocationTargetException JavaDoc ex) {
133       pLogger.logError(Constants.FUNCTION_INVOCATION_ERROR,
134             ex.getTargetException(),
135             functionName);
136       return null;
137     } catch (Exception JavaDoc ex) {
138       pLogger.logError(Constants.FUNCTION_INVOCATION_ERROR, ex, functionName);
139       return null;
140     }
141   }
142
143   //-------------------------------------
144
}
145
Popular Tags