KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > jsp12 > GetPropertyTag


1 /*
2  * Copyright 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 package org.apache.taglibs.jsp12;
17
18 import java.beans.BeanInfo JavaDoc;
19 import java.beans.Introspector JavaDoc;
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.PropertyDescriptor JavaDoc;
22
23 import java.io.IOException JavaDoc;
24
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30 /**
31  * Custom tag that allows JSP 1.1 users to emulate the
32  * behaviour of the JSP 1.2 version of the jsp:getProperty
33  * tag.
34  */

35
36 public class GetPropertyTag extends TagSupport JavaDoc{
37   
38   String JavaDoc _name = null;
39   String JavaDoc _property = null;
40   
41   public void setName(String JavaDoc name) {
42     _name = name;
43   }
44   
45   public void setProperty(String JavaDoc property) {
46     _property = property;
47   }
48   
49   public int doStartTag() {
50     Object JavaDoc o = pageContext.getAttribute(_name);
51     
52     if (o == null) {
53       return EVAL_BODY_INCLUDE;
54     }
55     
56     // perform introspection to access getter method
57
Class JavaDoc cls = o.getClass();
58     BeanInfo JavaDoc beanInfo;
59     try {
60       beanInfo = Introspector.getBeanInfo(cls);
61     } catch (IntrospectionException JavaDoc e) {
62       e.printStackTrace();
63       return EVAL_BODY_INCLUDE;
64     }
65     
66     PropertyDescriptor JavaDoc descriptors[] = beanInfo.getPropertyDescriptors();
67     Method JavaDoc getterMethod = null;
68     for (int i = 0; i < descriptors.length; ++i) {
69       if (descriptors[i].getName().equals(_property)) {
70         getterMethod = descriptors[i].getReadMethod();
71       }
72     }
73     
74     if (getterMethod != null) {
75       try {
76         pageContext.getOut().print(getterMethod.invoke(o, new Object JavaDoc[0]));
77       } catch (IllegalAccessException JavaDoc e) {
78         e.printStackTrace();
79       } catch (InvocationTargetException JavaDoc e) {
80         e.printStackTrace();
81       } catch (IOException JavaDoc e) {
82         e.printStackTrace();
83       }
84     }
85     return EVAL_BODY_INCLUDE;
86   }
87   
88   public void release() {
89     _name = null;
90     _property = null;
91   }
92 }
93
Popular Tags