00001
package com.quadcap.http.servlets.jsp;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.beans.BeanInfo;
00042
import java.beans.Introspector;
00043
import java.beans.IntrospectionException;
00044
import java.beans.PropertyDescriptor;
00045
00046
import java.lang.reflect.InvocationTargetException;
00047
import java.lang.reflect.Method;
00048
00049
import javax.servlet.http.HttpServletRequest;
00050
00051
import com.quadcap.util.Debug;
00052
00053
00054
00055
00056
00057
00058 public class PropertyUtils {
00059 public static void setPropertiesFromRequest(Object obj,
00060 HttpServletRequest request)
00061
throws IntrospectionException, InvocationTargetException,
00062 IllegalAccessException
00063 {
00064 BeanInfo info = Introspector.getBeanInfo(obj.getClass());
00065 PropertyDescriptor[] pds = info.getPropertyDescriptors();
00066
for (
int i = 0; i < pds.length; i++) {
00067 PropertyDescriptor pd = pds[i];
00068 String name = pd.getName();
00069 String[] vals = request.getParameterValues(name);
00070
if (vals == null || vals.length == 0 || vals[0].length() == 0)
continue;
00071 Class pt = pd.getPropertyType();
00072
if (pt.isArray()) {
00073
setProps(obj, pd, vals);
00074 }
else {
00075
setProp(obj, pd, vals[0]);
00076 }
00077 }
00078 }
00079
00080 public static void setPropertyFromRequestParameter(Object obj,
00081 HttpServletRequest request,
00082 String name)
00083
throws IntrospectionException, InvocationTargetException,
00084 IllegalAccessException
00085 {
00086 BeanInfo info = Introspector.getBeanInfo(obj.getClass());
00087 PropertyDescriptor[] pds = info.getPropertyDescriptors();
00088
for (
int i = 0; i < pds.length; i++) {
00089 PropertyDescriptor pd = pds[i];
00090 String pname = pd.getName();
00091
if (!name.equals(pname))
continue;
00092 String[] vals = request.getParameterValues(name);
00093
if (vals == null || vals.length == 0 || vals[0].length() == 0)
continue;
00094 Class pt = pd.getPropertyType();
00095
Debug.println(
"pt.isArray() = " + pt.isArray());
00096
if (pt.isArray()) {
00097
setProps(obj, pd, vals);
00098 }
else {
00099
setProp(obj, pd, vals[0]);
00100 }
00101 }
00102 }
00103
00104 public static void setPropertyFromValue(Object obj, String name, String val)
00105
throws IntrospectionException, InvocationTargetException,
00106 IllegalAccessException
00107 {
00108 BeanInfo info = Introspector.getBeanInfo(obj.getClass());
00109 PropertyDescriptor[] pds = info.getPropertyDescriptors();
00110
for (
int i = 0; i < pds.length; i++) {
00111 PropertyDescriptor pd = pds[i];
00112 String pname = pd.getName();
00113
if (name.equals(pname)) {
00114
setProp(obj, pd, val);
00115 }
00116 }
00117 }
00118
00119 public static void setProp(Object obj, PropertyDescriptor pd, String val)
00120
throws IntrospectionException, InvocationTargetException,
00121 IllegalAccessException
00122 {
00123 Class pt = pd.getPropertyType();
00124 Method w = pd.getWriteMethod();
00125 Object[] args =
new Object[1];
00126
00127
if (pt.getName().equals(
"java.lang.Boolean")) {
00128 args[0] = Boolean.valueOf(val);
00129 }
else if (pt.getName().equals(
"java.lang.Integer")) {
00130 args[0] = Integer.valueOf(val);
00131 }
else if (pt.getName().equals(
"java.lang.Byte")) {
00132 args[0] = Byte.valueOf(val);
00133 }
else if (pt.getName().equals(
"java.lang.Short")) {
00134 args[0] = Short.valueOf(val);
00135 }
else if (pt.getName().equals(
"java.lang.Long")) {
00136 args[0] = Long.valueOf(val);
00137 }
else if (pt.getName().equals(
"java.lang.Double")) {
00138 args[0] = Double.valueOf(val);
00139 }
else if (pt.getName().equals(
"java.lang.Float")) {
00140 args[0] = Float.valueOf(val);
00141 }
else if (pt.getName().equals(
"java.lang.Integer")) {
00142 args[0] = Integer.valueOf(val);
00143 }
else if (pt.getName().equals(
"java.lang.String")) {
00144 args[0] = val;
00145 }
00146 w.invoke(obj, args);
00147 }
00148
00149 public static void setProps(Object obj, PropertyDescriptor pd, String[] vals)
00150
throws IntrospectionException, InvocationTargetException,
00151 IllegalAccessException
00152 {
00153 Class pat = pd.getPropertyType();
00154 Class pt = pat.getComponentType();
00155 Method w = pd.getWriteMethod();
00156 Object[] args =
new Object[1];
00157
00158
if (pt.getName().equals(
"java.lang.Boolean")) {
00159 Boolean[] args1 =
new Boolean[vals.length];
00160 args[0] = args1;
00161
for (
int i = 0; i < vals.length; i++) {
00162 args1[i] = Boolean.valueOf(vals[i]);
00163 }
00164 }
else if (pt.getName().equals(
"java.lang.Integer")) {
00165 Integer[] args1 =
new Integer[vals.length];
00166 args[0] = args1;
00167
for (
int i = 0; i < vals.length; i++) {
00168 args1[i] = Integer.valueOf(vals[i]);
00169 }
00170 }
else if (pt.getName().equals(
"java.lang.Byte")) {
00171 Byte[] args1 =
new Byte[vals.length];
00172 args[0] = args1;
00173
for (
int i = 0; i < vals.length; i++) {
00174 args1[i] = Byte.valueOf(vals[i]);
00175 }
00176 }
else if (pt.getName().equals(
"java.lang.Short")) {
00177 Short[] args1 =
new Short[vals.length];
00178 args[0] = args1;
00179
for (
int i = 0; i < vals.length; i++) {
00180 args1[i] = Short.valueOf(vals[i]);
00181 }
00182 }
else if (pt.getName().equals(
"java.lang.Long")) {
00183 Long[] args1 =
new Long[vals.length];
00184 args[0] = args1;
00185
for (
int i = 0; i < vals.length; i++) {
00186 args1[i] = Long.valueOf(vals[i]);
00187 }
00188 }
else if (pt.getName().equals(
"java.lang.Double")) {
00189 Double[] args1 =
new Double[vals.length];
00190 args[0] = args1;
00191
for (
int i = 0; i < vals.length; i++) {
00192 args1[i] = Double.valueOf(vals[i]);
00193 }
00194 }
else if (pt.getName().equals(
"java.lang.Float")) {
00195 Float[] args1 =
new Float[vals.length];
00196 args[0] = args1;
00197
for (
int i = 0; i < vals.length; i++) {
00198 args1[i] = Float.valueOf(vals[i]);
00199 }
00200 }
else if (pt.getName().equals(
"java.lang.String")) {
00201 args[0] = vals;
00202 }
00203 w.invoke(obj, args);
00204 }
00205
00206 public static String
getProperty(Object obj, String name)
00207
throws IntrospectionException, InvocationTargetException,
00208 IllegalAccessException
00209 {
00210
00211 BeanInfo info = Introspector.getBeanInfo(obj.getClass());
00212 PropertyDescriptor[] pds = info.getPropertyDescriptors();
00213
for (
int i = 0; i < pds.length; i++) {
00214 PropertyDescriptor pd = pds[i];
00215 String pname = pd.getName();
00216
if (name.equals(pname)) {
00217 Method r = pd.getReadMethod();
00218 Object[] args =
new Object[0];
00219 Object res = r.invoke(obj, args);
00220
return String.valueOf(res);
00221 }
00222 }
00223
return "";
00224 }
00225 }