KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > parser > AstFunction


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  */
/* Generated By:JJTree: Do not edit this line. AstFunction.java */
23
24 package com.sun.el.parser;
25
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 import javax.el.ELException;
30 import javax.el.FunctionMapper;
31
32 import com.sun.el.lang.EvaluationContext;
33 import com.sun.el.util.MessageFactory;
34
35 /**
36  * @author Jacob Hookom [jacob@hookom.net]
37  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
38  */

39 public final class AstFunction extends SimpleNode {
40
41     protected String JavaDoc localName = "";
42
43     protected String JavaDoc prefix = "";
44
45     public AstFunction(int id) {
46         super(id);
47     }
48
49     public String JavaDoc getLocalName() {
50         return localName;
51     }
52
53     public String JavaDoc getOutputName() {
54         if (this.prefix == null) {
55             return this.localName;
56         } else {
57             return this.prefix + ":" + this.localName;
58         }
59     }
60
61     public String JavaDoc getPrefix() {
62         return prefix;
63     }
64
65     public Class JavaDoc getType(EvaluationContext ctx)
66             throws ELException {
67         
68         FunctionMapper fnMapper = ctx.getFunctionMapper();
69         
70         // quickly validate again for this request
71
if (fnMapper == null) {
72             throw new ELException(MessageFactory.get("error.fnMapper.null"));
73         }
74         Method JavaDoc m = fnMapper.resolveFunction(this.prefix, this.localName);
75         if (m == null) {
76             throw new ELException(MessageFactory.get("error.fnMapper.method",
77                     this.getOutputName()));
78         }
79         return m.getReturnType();
80     }
81
82     public Object JavaDoc getValue(EvaluationContext ctx)
83             throws ELException {
84         
85         FunctionMapper fnMapper = ctx.getFunctionMapper();
86         
87         // quickly validate again for this request
88
if (fnMapper == null) {
89             throw new ELException(MessageFactory.get("error.fnMapper.null"));
90         }
91         Method JavaDoc m = fnMapper.resolveFunction(this.prefix, this.localName);
92         if (m == null) {
93             throw new ELException(MessageFactory.get("error.fnMapper.method",
94                     this.getOutputName()));
95         }
96
97         Class JavaDoc[] paramTypes = m.getParameterTypes();
98         Object JavaDoc[] params = null;
99         Object JavaDoc result = null;
100         int numParams = this.jjtGetNumChildren();
101         if (numParams > 0) {
102             params = new Object JavaDoc[numParams];
103             try {
104                 for (int i = 0; i < numParams; i++) {
105                     params[i] = this.children[i].getValue(ctx);
106                     params[i] = coerceToType(params[i], paramTypes[i]);
107                 }
108             } catch (ELException ele) {
109                 throw new ELException(MessageFactory.get("error.function", this
110                         .getOutputName()), ele);
111             }
112         }
113         try {
114             result = m.invoke(null, params);
115         } catch (IllegalAccessException JavaDoc iae) {
116             throw new ELException(MessageFactory.get("error.function", this
117                     .getOutputName()), iae);
118         } catch (InvocationTargetException JavaDoc ite) {
119             throw new ELException(MessageFactory.get("error.function", this
120                     .getOutputName()), ite.getCause());
121         }
122         return result;
123     }
124
125     public void setLocalName(String JavaDoc localName) {
126         this.localName = localName;
127     }
128
129     public void setPrefix(String JavaDoc prefix) {
130         this.prefix = prefix;
131     }
132     
133     
134     public String JavaDoc toString()
135     {
136         return ELParserTreeConstants.jjtNodeName[id] + "[" + this.getOutputName() + "]";
137     }
138 }
139
Popular Tags