001: package com.quadcap.http.servlets.jsp;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.beans.BeanInfo;
042: import java.beans.Introspector;
043: import java.beans.IntrospectionException;
044: import java.beans.PropertyDescriptor;
045:
046: import java.lang.reflect.InvocationTargetException;
047: import java.lang.reflect.Method;
048:
049: import javax.servlet.http.HttpServletRequest;
050:
051: import com.quadcap.util.Debug;
052:
053: /**
054: * Utility functions to support getProp and setProp.
055: *
056: * @author Stan Bailes
057: */
058: public class PropertyUtils {
059: public static void setPropertiesFromRequest(Object obj,
060: HttpServletRequest request) throws IntrospectionException,
061: InvocationTargetException, IllegalAccessException {
062: BeanInfo info = Introspector.getBeanInfo(obj.getClass());
063: PropertyDescriptor[] pds = info.getPropertyDescriptors();
064: for (int i = 0; i < pds.length; i++) {
065: PropertyDescriptor pd = pds[i];
066: String name = pd.getName();
067: String[] vals = request.getParameterValues(name);
068: if (vals == null || vals.length == 0
069: || vals[0].length() == 0)
070: continue;
071: Class pt = pd.getPropertyType();
072: if (pt.isArray()) {
073: setProps(obj, pd, vals);
074: } else {
075: setProp(obj, pd, vals[0]);
076: }
077: }
078: }
079:
080: public static void setPropertyFromRequestParameter(Object obj,
081: HttpServletRequest request, String name)
082: throws IntrospectionException, InvocationTargetException,
083: IllegalAccessException {
084: BeanInfo info = Introspector.getBeanInfo(obj.getClass());
085: PropertyDescriptor[] pds = info.getPropertyDescriptors();
086: for (int i = 0; i < pds.length; i++) {
087: PropertyDescriptor pd = pds[i];
088: String pname = pd.getName();
089: if (!name.equals(pname))
090: continue;
091: String[] vals = request.getParameterValues(name);
092: if (vals == null || vals.length == 0
093: || vals[0].length() == 0)
094: continue;
095: Class pt = pd.getPropertyType();
096: Debug.println("pt.isArray() = " + pt.isArray());
097: if (pt.isArray()) {
098: setProps(obj, pd, vals);
099: } else {
100: setProp(obj, pd, vals[0]);
101: }
102: }
103: }
104:
105: public static void setPropertyFromValue(Object obj, String name,
106: String val) throws IntrospectionException,
107: InvocationTargetException, IllegalAccessException {
108: BeanInfo info = Introspector.getBeanInfo(obj.getClass());
109: PropertyDescriptor[] pds = info.getPropertyDescriptors();
110: for (int i = 0; i < pds.length; i++) {
111: PropertyDescriptor pd = pds[i];
112: String pname = pd.getName();
113: if (name.equals(pname)) {
114: setProp(obj, pd, val);
115: }
116: }
117: }
118:
119: public static void setProp(Object obj, PropertyDescriptor pd,
120: String val) throws IntrospectionException,
121: InvocationTargetException, IllegalAccessException {
122: Class pt = pd.getPropertyType();
123: Method w = pd.getWriteMethod();
124: Object[] args = new Object[1];
125:
126: if (pt.getName().equals("java.lang.Boolean")) {
127: args[0] = Boolean.valueOf(val);
128: } else if (pt.getName().equals("java.lang.Integer")) {
129: args[0] = Integer.valueOf(val);
130: } else if (pt.getName().equals("java.lang.Byte")) {
131: args[0] = Byte.valueOf(val);
132: } else if (pt.getName().equals("java.lang.Short")) {
133: args[0] = Short.valueOf(val);
134: } else if (pt.getName().equals("java.lang.Long")) {
135: args[0] = Long.valueOf(val);
136: } else if (pt.getName().equals("java.lang.Double")) {
137: args[0] = Double.valueOf(val);
138: } else if (pt.getName().equals("java.lang.Float")) {
139: args[0] = Float.valueOf(val);
140: } else if (pt.getName().equals("java.lang.Integer")) {
141: args[0] = Integer.valueOf(val);
142: } else if (pt.getName().equals("java.lang.String")) {
143: args[0] = val;
144: }
145: w.invoke(obj, args);
146: }
147:
148: public static void setProps(Object obj, PropertyDescriptor pd,
149: String[] vals) throws IntrospectionException,
150: InvocationTargetException, IllegalAccessException {
151: Class pat = pd.getPropertyType();
152: Class pt = pat.getComponentType();
153: Method w = pd.getWriteMethod();
154: Object[] args = new Object[1];
155:
156: if (pt.getName().equals("java.lang.Boolean")) {
157: Boolean[] args1 = new Boolean[vals.length];
158: args[0] = args1;
159: for (int i = 0; i < vals.length; i++) {
160: args1[i] = Boolean.valueOf(vals[i]);
161: }
162: } else if (pt.getName().equals("java.lang.Integer")) {
163: Integer[] args1 = new Integer[vals.length];
164: args[0] = args1;
165: for (int i = 0; i < vals.length; i++) {
166: args1[i] = Integer.valueOf(vals[i]);
167: }
168: } else if (pt.getName().equals("java.lang.Byte")) {
169: Byte[] args1 = new Byte[vals.length];
170: args[0] = args1;
171: for (int i = 0; i < vals.length; i++) {
172: args1[i] = Byte.valueOf(vals[i]);
173: }
174: } else if (pt.getName().equals("java.lang.Short")) {
175: Short[] args1 = new Short[vals.length];
176: args[0] = args1;
177: for (int i = 0; i < vals.length; i++) {
178: args1[i] = Short.valueOf(vals[i]);
179: }
180: } else if (pt.getName().equals("java.lang.Long")) {
181: Long[] args1 = new Long[vals.length];
182: args[0] = args1;
183: for (int i = 0; i < vals.length; i++) {
184: args1[i] = Long.valueOf(vals[i]);
185: }
186: } else if (pt.getName().equals("java.lang.Double")) {
187: Double[] args1 = new Double[vals.length];
188: args[0] = args1;
189: for (int i = 0; i < vals.length; i++) {
190: args1[i] = Double.valueOf(vals[i]);
191: }
192: } else if (pt.getName().equals("java.lang.Float")) {
193: Float[] args1 = new Float[vals.length];
194: args[0] = args1;
195: for (int i = 0; i < vals.length; i++) {
196: args1[i] = Float.valueOf(vals[i]);
197: }
198: } else if (pt.getName().equals("java.lang.String")) {
199: args[0] = vals;
200: }
201: w.invoke(obj, args);
202: }
203:
204: public static String getProperty(Object obj, String name)
205: throws IntrospectionException, InvocationTargetException,
206: IllegalAccessException {
207:
208: BeanInfo info = Introspector.getBeanInfo(obj.getClass());
209: PropertyDescriptor[] pds = info.getPropertyDescriptors();
210: for (int i = 0; i < pds.length; i++) {
211: PropertyDescriptor pd = pds[i];
212: String pname = pd.getName();
213: if (name.equals(pname)) {
214: Method r = pd.getReadMethod();
215: Object[] args = new Object[0];
216: Object res = r.invoke(obj, args);
217: return String.valueOf(res);
218: }
219: }
220: return "";
221: }
222: }
|