KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > Identifier


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.jpath.expression;
18
19 import java.beans.BeanInfo JavaDoc;
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26
27 import javax.servlet.jsp.PageContext JavaDoc;
28
29 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
30
31 /**
32  * The Identifier class
33  *
34  *
35  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
36  * @version
37  */

38 public class Identifier extends SimpleNode implements Introspectable {
39
40     protected String JavaDoc val;
41
42     /**
43      * Used to create an instance of the Identifier class
44      *
45      *
46      * @param id
47      *
48      */

49     public Identifier(int id) {
50         super(id);
51     }
52
53     /**
54      * Used to create an instance of the Identifier class
55      *
56      *
57      * @param p
58      * @param id
59      *
60      */

61     public Identifier(Parser p, int id) {
62         super(p, id);
63     }
64
65     /**
66      * Provides a method to print a normalized version of the original
67      * expression. The normalized version has standardized spacing and
68      * parenthesis, and can be used to compare expressions formatted
69      * in different ways to see if they are actually the same expression.
70      *
71      *
72      * @return The normalized version of the original expression
73      *
74      */

75     public String JavaDoc toNormalizedString() {
76         return val;
77     }
78
79     /**
80      * This method evaluates this node of the expression and all child nodes.
81      * It returns the result of the
82      * evaluation as an <tt>Object</tt>. If any problems are encountered
83      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
84      *
85      *
86      * @param pageContext the current JSP PageContext
87      *
88      * @param icontext the Iteration Context of the expression. If there is
89      * no interation context, this should be null.
90      *
91      * @return the result of the expression evaluation as an object
92      *
93      * @throws EvaluationException if a problem is encountered during the
94      * evaluation
95      */

96     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
97             throws EvaluationException {
98
99         Object JavaDoc result;
100
101         //try {
102
//result = Convert.toJSPType(pageContext.findAttribute(val));
103
//} catch (ConversionException ce) {
104
//throw new EvaluationException(this, ce.getMessage());
105
//}
106
result = pageContext.findAttribute(val);
107
108         return result;
109     }
110
111     /**
112      * The evaluate method
113      *
114      *
115      * @param pageContext
116      * @param icontext
117      * @param scope
118      *
119      * @return
120      *
121      * @throws EvaluationException
122      *
123      */

124     public Object JavaDoc evaluate(
125             PageContext JavaDoc pageContext, IterationContext icontext, int scope)
126                 throws EvaluationException {
127
128         Object JavaDoc result;
129
130         //try {
131
//result = Convert.toJSPType(pageContext.getAttribute(val, scope));
132
//} catch (ConversionException ce) {
133
//throw new EvaluationException(this, ce.getMessage());
134
//}
135
result = pageContext.getAttribute(val, scope);
136
137         return result;
138     }
139
140     /**
141      * The evaluate method
142      *
143      *
144      * @param pageContext
145      * @param icontext
146      * @param parent
147      *
148      * @return
149      *
150      * @throws EvaluationException
151      *
152      */

153     public Object JavaDoc evaluate(
154             PageContext JavaDoc pageContext, IterationContext icontext, Object JavaDoc parent)
155                 throws EvaluationException {
156
157         Object JavaDoc result = null;
158
159         //try {
160
if (parent != null) {
161             try {
162                 PropertyDescriptor JavaDoc pd =
163                     getFeatureDescriptor(parent.getClass(), val);
164
165                 if (pd != null) {
166                     result = getAttribute(pd, parent);
167                 }
168             } catch (IntrospectionException JavaDoc ie) {
169                 throw new EvaluationException(this,
170                         "Introspection Exception:" + ie.getMessage());
171             } catch (NoSuchMethodException JavaDoc nsme) {
172                 throw new EvaluationException(this,
173                         "NoSuchMethodException:" + nsme.getMessage());
174             } catch (IllegalAccessException JavaDoc iae) {
175                 throw new EvaluationException(this,
176                         "IllegalAccessException:" + iae.toString());
177             } catch (InvocationTargetException JavaDoc ite) {
178                 throw new EvaluationException(this,
179                         "InvocationTargetException:" + ite.getMessage());
180             }
181         }
182
183         //result = Convert.toJSPType(result);
184
//} catch (ConversionException ce) {
185
//throw new EvaluationException(this, ce.getMessage());
186
//}
187
return result;
188     }
189
190     /**
191      * The getFeatureDescriptor method
192      *
193      *
194      * @param c
195      * @param key
196      *
197      * @return
198      *
199      * @throws IntrospectionException
200      *
201      */

202     private PropertyDescriptor JavaDoc getFeatureDescriptor(Class JavaDoc c, String JavaDoc key)
203             throws IntrospectionException JavaDoc {
204
205         BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(c);
206         PropertyDescriptor JavaDoc[] pda = beanInfo.getPropertyDescriptors();
207
208         for (int i = pda.length - 1; i >= 0; --i) {
209             PropertyDescriptor JavaDoc pd = pda[i];
210
211             if (pd.getName().equals(key)) {
212                 return pd;
213             }
214         }
215
216         return null;
217     }
218
219     /**
220      * The getAttribute method
221      *
222      *
223      * @param pd
224      * @param o
225      *
226      * @return
227      *
228      * @throws IllegalAccessException
229      * @throws InvocationTargetException
230      * @throws NoSuchMethodException
231      *
232      */

233     private Object JavaDoc getAttribute(PropertyDescriptor JavaDoc pd, Object JavaDoc o)
234             throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc,
235                 InvocationTargetException JavaDoc {
236
237         Object JavaDoc result = null;
238         Method JavaDoc m = pd.getReadMethod();
239
240         m = getPublicMethod(m.getDeclaringClass(), m.getName(),
241                 m.getParameterTypes());
242         result = m.invoke(o, null);
243
244         return result;
245     }
246
247     /**
248      * The getPublicMethod method
249      *
250      *
251      * @param c
252      * @param name
253      * @param paramTypes
254      *
255      * @return
256      *
257      * @throws NoSuchMethodException
258      *
259      */

260     private Method JavaDoc getPublicMethod(Class JavaDoc c, String JavaDoc name, Class JavaDoc[] paramTypes)
261             throws NoSuchMethodException JavaDoc {
262
263         Method JavaDoc result = null;
264
265         if ((c.getModifiers() & Modifier.PUBLIC) == 0) {
266             Class JavaDoc sc = c.getSuperclass();
267
268             if (sc != null) {
269                 try {
270                     result = getPublicMethod(sc, name, paramTypes);
271                 } catch (NoSuchMethodException JavaDoc nsme) {
272
273                     //Intentionally ignored and thrown later
274
}
275             }
276
277             if (result == null) {
278                 Class JavaDoc[] interfaces = c.getInterfaces();
279
280                 for (int i = 0; i < interfaces.length; i++) {
281                     try {
282                         result = getPublicMethod(interfaces[i], name,
283                                 paramTypes);
284                     } catch (NoSuchMethodException JavaDoc nsme) {
285
286                         //Intentionally ignored and thrown later
287
}
288                 }
289             }
290         } else {
291
292             //It was public
293
result = c.getMethod(name, paramTypes);
294         }
295
296         return result;
297     }
298 }
299
Popular Tags