001: /* Generated By:JJTree: Do not edit this line. AstFunction.java */
002:
003: package org.apache.el.parser;
004:
005: import java.lang.reflect.InvocationTargetException;
006: import java.lang.reflect.Method;
007:
008: import javax.el.ELException;
009: import javax.el.FunctionMapper;
010:
011: import org.apache.el.lang.EvaluationContext;
012: import org.apache.el.util.MessageFactory;
013:
014: /**
015: * @author Jacob Hookom [jacob@hookom.net]
016: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
017: */
018: public final class AstFunction extends SimpleNode {
019:
020: protected String localName = "";
021:
022: protected String prefix = "";
023:
024: public AstFunction(int id) {
025: super (id);
026: }
027:
028: public String getLocalName() {
029: return localName;
030: }
031:
032: public String getOutputName() {
033: if (this .prefix == null) {
034: return this .localName;
035: } else {
036: return this .prefix + ":" + this .localName;
037: }
038: }
039:
040: public String getPrefix() {
041: return prefix;
042: }
043:
044: public Class getType(EvaluationContext ctx) throws ELException {
045:
046: FunctionMapper fnMapper = ctx.getFunctionMapper();
047:
048: // quickly validate again for this request
049: if (fnMapper == null) {
050: throw new ELException(MessageFactory
051: .get("error.fnMapper.null"));
052: }
053: Method m = fnMapper
054: .resolveFunction(this .prefix, this .localName);
055: if (m == null) {
056: throw new ELException(MessageFactory.get(
057: "error.fnMapper.method", this .getOutputName()));
058: }
059: return m.getReturnType();
060: }
061:
062: public Object getValue(EvaluationContext ctx) throws ELException {
063:
064: FunctionMapper fnMapper = ctx.getFunctionMapper();
065:
066: // quickly validate again for this request
067: if (fnMapper == null) {
068: throw new ELException(MessageFactory
069: .get("error.fnMapper.null"));
070: }
071: Method m = fnMapper
072: .resolveFunction(this .prefix, this .localName);
073: if (m == null) {
074: throw new ELException(MessageFactory.get(
075: "error.fnMapper.method", this .getOutputName()));
076: }
077:
078: Class[] paramTypes = m.getParameterTypes();
079: Object[] params = null;
080: Object result = null;
081: int numParams = this .jjtGetNumChildren();
082: if (numParams > 0) {
083: params = new Object[numParams];
084: try {
085: for (int i = 0; i < numParams; i++) {
086: params[i] = this .children[i].getValue(ctx);
087: params[i] = coerceToType(params[i], paramTypes[i]);
088: }
089: } catch (ELException ele) {
090: throw new ELException(MessageFactory.get(
091: "error.function", this .getOutputName()), ele);
092: }
093: }
094: try {
095: result = m.invoke(null, params);
096: } catch (IllegalAccessException iae) {
097: throw new ELException(MessageFactory.get("error.function",
098: this .getOutputName()), iae);
099: } catch (InvocationTargetException ite) {
100: throw new ELException(MessageFactory.get("error.function",
101: this .getOutputName()), ite.getCause());
102: }
103: return result;
104: }
105:
106: public void setLocalName(String localName) {
107: this .localName = localName;
108: }
109:
110: public void setPrefix(String prefix) {
111: this .prefix = prefix;
112: }
113:
114: public String toString() {
115: return ELParserTreeConstants.jjtNodeName[id] + "["
116: + this .getOutputName() + "]";
117: }
118: }
|