KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > jsp > PropertyUtils


1 package com.quadcap.http.servlets.jsp;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.beans.BeanInfo JavaDoc;
42 import java.beans.Introspector JavaDoc;
43 import java.beans.IntrospectionException JavaDoc;
44 import java.beans.PropertyDescriptor JavaDoc;
45
46 import java.lang.reflect.InvocationTargetException JavaDoc;
47 import java.lang.reflect.Method JavaDoc;
48
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50
51 import com.quadcap.util.Debug;
52
53 /**
54  * Utility functions to support getProp and setProp.
55  *
56  * @author Stan Bailes
57  */

58 public class PropertyUtils {
59     public static void setPropertiesFromRequest(Object JavaDoc obj,
60                         HttpServletRequest JavaDoc request)
61     throws IntrospectionException JavaDoc, InvocationTargetException JavaDoc,
62            IllegalAccessException JavaDoc
63     {
64     BeanInfo JavaDoc info = Introspector.getBeanInfo(obj.getClass());
65     PropertyDescriptor JavaDoc[] pds = info.getPropertyDescriptors();
66     for (int i = 0; i < pds.length; i++) {
67         PropertyDescriptor JavaDoc pd = pds[i];
68         String JavaDoc name = pd.getName();
69         String JavaDoc[] vals = request.getParameterValues(name);
70         if (vals == null || vals.length == 0 || vals[0].length() == 0) continue;
71         Class JavaDoc pt = pd.getPropertyType();
72         if (pt.isArray()) {
73         setProps(obj, pd, vals);
74         } else {
75         setProp(obj, pd, vals[0]);
76         }
77     }
78     }
79
80     public static void setPropertyFromRequestParameter(Object JavaDoc obj,
81                                HttpServletRequest JavaDoc request,
82                                String JavaDoc name)
83     throws IntrospectionException JavaDoc, InvocationTargetException JavaDoc,
84            IllegalAccessException JavaDoc
85     {
86     BeanInfo JavaDoc info = Introspector.getBeanInfo(obj.getClass());
87     PropertyDescriptor JavaDoc[] pds = info.getPropertyDescriptors();
88     for (int i = 0; i < pds.length; i++) {
89         PropertyDescriptor JavaDoc pd = pds[i];
90         String JavaDoc pname = pd.getName();
91         if (!name.equals(pname)) continue;
92         String JavaDoc[] vals = request.getParameterValues(name);
93         if (vals == null || vals.length == 0 || vals[0].length() == 0) continue;
94         Class JavaDoc pt = pd.getPropertyType();
95         Debug.println("pt.isArray() = " + pt.isArray());
96         if (pt.isArray()) {
97         setProps(obj, pd, vals);
98         } else {
99         setProp(obj, pd, vals[0]);
100         }
101     }
102     }
103
104     public static void setPropertyFromValue(Object JavaDoc obj, String JavaDoc name, String JavaDoc val)
105     throws IntrospectionException JavaDoc, InvocationTargetException JavaDoc,
106            IllegalAccessException JavaDoc
107     {
108     BeanInfo JavaDoc info = Introspector.getBeanInfo(obj.getClass());
109     PropertyDescriptor JavaDoc[] pds = info.getPropertyDescriptors();
110     for (int i = 0; i < pds.length; i++) {
111         PropertyDescriptor JavaDoc pd = pds[i];
112         String JavaDoc pname = pd.getName();
113         if (name.equals(pname)) {
114         setProp(obj, pd, val);
115         }
116     }
117     }
118
119     public static void setProp(Object JavaDoc obj, PropertyDescriptor JavaDoc pd, String JavaDoc val)
120     throws IntrospectionException JavaDoc, InvocationTargetException JavaDoc,
121            IllegalAccessException JavaDoc
122     {
123     Class JavaDoc pt = pd.getPropertyType();
124     Method JavaDoc w = pd.getWriteMethod();
125     Object JavaDoc[] args = new Object JavaDoc[1];
126
127     if (pt.getName().equals("java.lang.Boolean")) {
128         args[0] = Boolean.valueOf(val);
129     } else if (pt.getName().equals("java.lang.Integer")) {
130         args[0] = Integer.valueOf(val);
131     } else if (pt.getName().equals("java.lang.Byte")) {
132         args[0] = Byte.valueOf(val);
133     } else if (pt.getName().equals("java.lang.Short")) {
134         args[0] = Short.valueOf(val);
135     } else if (pt.getName().equals("java.lang.Long")) {
136         args[0] = Long.valueOf(val);
137     } else if (pt.getName().equals("java.lang.Double")) {
138         args[0] = Double.valueOf(val);
139     } else if (pt.getName().equals("java.lang.Float")) {
140         args[0] = Float.valueOf(val);
141     } else if (pt.getName().equals("java.lang.Integer")) {
142         args[0] = Integer.valueOf(val);
143     } else if (pt.getName().equals("java.lang.String")) {
144         args[0] = val;
145     }
146     w.invoke(obj, args);
147     }
148
149     public static void setProps(Object JavaDoc obj, PropertyDescriptor JavaDoc pd, String JavaDoc[] vals)
150     throws IntrospectionException JavaDoc, InvocationTargetException JavaDoc,
151            IllegalAccessException JavaDoc
152     {
153     Class JavaDoc pat = pd.getPropertyType();
154     Class JavaDoc pt = pat.getComponentType();
155     Method JavaDoc w = pd.getWriteMethod();
156     Object JavaDoc[] args = new Object JavaDoc[1];
157
158     if (pt.getName().equals("java.lang.Boolean")) {
159         Boolean JavaDoc[] args1 = new Boolean JavaDoc[vals.length];
160         args[0] = args1;
161         for (int i = 0; i < vals.length; i++) {
162         args1[i] = Boolean.valueOf(vals[i]);
163         }
164     } else if (pt.getName().equals("java.lang.Integer")) {
165         Integer JavaDoc[] args1 = new Integer JavaDoc[vals.length];
166         args[0] = args1;
167         for (int i = 0; i < vals.length; i++) {
168         args1[i] = Integer.valueOf(vals[i]);
169         }
170     } else if (pt.getName().equals("java.lang.Byte")) {
171         Byte JavaDoc[] args1 = new Byte JavaDoc[vals.length];
172         args[0] = args1;
173         for (int i = 0; i < vals.length; i++) {
174         args1[i] = Byte.valueOf(vals[i]);
175         }
176     } else if (pt.getName().equals("java.lang.Short")) {
177         Short JavaDoc[] args1 = new Short JavaDoc[vals.length];
178         args[0] = args1;
179         for (int i = 0; i < vals.length; i++) {
180         args1[i] = Short.valueOf(vals[i]);
181         }
182     } else if (pt.getName().equals("java.lang.Long")) {
183         Long JavaDoc[] args1 = new Long JavaDoc[vals.length];
184         args[0] = args1;
185         for (int i = 0; i < vals.length; i++) {
186         args1[i] = Long.valueOf(vals[i]);
187         }
188     } else if (pt.getName().equals("java.lang.Double")) {
189         Double JavaDoc[] args1 = new Double JavaDoc[vals.length];
190         args[0] = args1;
191         for (int i = 0; i < vals.length; i++) {
192         args1[i] = Double.valueOf(vals[i]);
193         }
194     } else if (pt.getName().equals("java.lang.Float")) {
195         Float JavaDoc[] args1 = new Float JavaDoc[vals.length];
196         args[0] = args1;
197         for (int i = 0; i < vals.length; i++) {
198         args1[i] = Float.valueOf(vals[i]);
199         }
200     } else if (pt.getName().equals("java.lang.String")) {
201         args[0] = vals;
202     }
203     w.invoke(obj, args);
204     }
205
206     public static String JavaDoc getProperty(Object JavaDoc obj, String JavaDoc name)
207     throws IntrospectionException JavaDoc, InvocationTargetException JavaDoc,
208            IllegalAccessException JavaDoc
209     {
210     
211     BeanInfo JavaDoc info = Introspector.getBeanInfo(obj.getClass());
212     PropertyDescriptor JavaDoc[] pds = info.getPropertyDescriptors();
213     for (int i = 0; i < pds.length; i++) {
214         PropertyDescriptor JavaDoc pd = pds[i];
215         String JavaDoc pname = pd.getName();
216         if (name.equals(pname)) {
217         Method JavaDoc r = pd.getReadMethod();
218         Object JavaDoc[] args = new Object JavaDoc[0];
219         Object JavaDoc res = r.invoke(obj, args);
220         return String.valueOf(res);
221         }
222     }
223     return "";
224     }
225 }
226
Popular Tags