0001: /*
0002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
0003: * Distributed under the terms of either:
0004: * - the common development and distribution license (CDDL), v1.0; or
0005: * - the GNU Lesser General Public License, v2.1 or later
0006: * $Id: BeanUtils.java 3716 2007-04-11 06:21:18Z gbevin $
0007: */
0008: package com.uwyn.rife.tools;
0009:
0010: import java.util.*;
0011:
0012: import com.uwyn.rife.config.RifeConfig;
0013: import com.uwyn.rife.datastructures.EnumClass;
0014: import com.uwyn.rife.engine.UploadedFile;
0015: import com.uwyn.rife.site.Constrained;
0016: import com.uwyn.rife.site.ConstrainedProperty;
0017: import com.uwyn.rife.site.ConstrainedUtils;
0018: import com.uwyn.rife.site.Validated;
0019: import com.uwyn.rife.site.ValidationError;
0020: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
0021: import com.uwyn.rife.tools.exceptions.ConversionException;
0022: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
0023: import com.uwyn.rife.tools.exceptions.SerializationUtilsErrorException;
0024: import java.beans.BeanInfo;
0025: import java.beans.IntrospectionException;
0026: import java.beans.Introspector;
0027: import java.beans.PropertyDescriptor;
0028: import java.io.FileInputStream;
0029: import java.io.FileNotFoundException;
0030: import java.io.InputStream;
0031: import java.io.Serializable;
0032: import java.lang.reflect.Array;
0033: import java.lang.reflect.InvocationTargetException;
0034: import java.lang.reflect.Method;
0035: import java.math.BigDecimal;
0036: import java.text.DateFormat;
0037: import java.text.Format;
0038: import java.text.ParseException;
0039: import java.text.SimpleDateFormat;
0040:
0041: public abstract class BeanUtils {
0042: public static final Accessors GETTERS = new Accessors("GETTERS");
0043: public static final Accessors SETTERS = new Accessors("SETTERS");
0044: public static final Accessors GETTERS_SETTERS = new Accessors(
0045: "GETTERS_SETTERS");
0046:
0047: public static DateFormat getConcisePreciseDateFormat() {
0048: SimpleDateFormat sf = new SimpleDateFormat(
0049: "yyyyMMddHHmmssSSSZ", Localization.getLocale());
0050: sf.setTimeZone(RifeConfig.Tools.getDefaultTimeZone());
0051: return sf;
0052: }
0053:
0054: public static BeanInfo getBeanInfo(Class beanClass)
0055: throws BeanUtilsException {
0056: try {
0057: return Introspector.getBeanInfo(beanClass);
0058: } catch (IntrospectionException e) {
0059: throw new BeanUtilsException(
0060: "Couldn't introspect the bean.", beanClass, e);
0061: }
0062: }
0063:
0064: private static String validateProperty(Accessors accessor,
0065: PropertyDescriptor property,
0066: Collection<String> includedProperties,
0067: Collection<String> excludedProperties, String prefix) {
0068: String name = null;
0069:
0070: // only take properties into account that have both read and write accessors
0071: if ((GETTERS == accessor && property.getReadMethod() != null)
0072: || (SETTERS == accessor && property.getWriteMethod() != null)
0073: || (GETTERS_SETTERS == accessor
0074: && property.getReadMethod() != null && property
0075: .getWriteMethod() != null)) {
0076: name = property.getName();
0077:
0078: // don't take the class and the metaClass property into account
0079: if (name.equals("class") || name.equals("metaClass")) {
0080: return null;
0081: }
0082:
0083: // apply the prefix if it was provided
0084: if (prefix != null) {
0085: name = prefix + name;
0086: }
0087:
0088: // check if the property isn't explicitly included or if it should be excluded before adding it
0089: if ((null == includedProperties || includedProperties
0090: .contains(name))
0091: && (null == excludedProperties || !excludedProperties
0092: .contains(name))) {
0093: return name;
0094: }
0095: }
0096: return null;
0097: }
0098:
0099: public static Set<String> getPropertyNames(Class beanClass,
0100: String[] includedProperties, String[] excludedProperties,
0101: String prefix) throws BeanUtilsException {
0102: return getPropertyNames(GETTERS_SETTERS, beanClass,
0103: includedProperties, excludedProperties, prefix);
0104: }
0105:
0106: public static Set<String> getPropertyNames(Accessors accessors,
0107: Class beanClass, String[] includedProperties,
0108: String[] excludedProperties, String prefix)
0109: throws BeanUtilsException {
0110: if (null == beanClass)
0111: return Collections.emptySet();
0112:
0113: final LinkedHashSet<String> property_names = new LinkedHashSet<String>();
0114:
0115: processProperties(accessors, beanClass, includedProperties,
0116: excludedProperties, prefix,
0117: new BeanPropertyProcessor() {
0118: public boolean gotProperty(String name,
0119: PropertyDescriptor descriptor)
0120: throws IllegalAccessException,
0121: IllegalArgumentException,
0122: InvocationTargetException {
0123: property_names.add(name);
0124:
0125: return true;
0126: }
0127: });
0128:
0129: return property_names;
0130: }
0131:
0132: public static void processProperties(Class beanClass,
0133: String[] includedProperties, String[] excludedProperties,
0134: String prefix, BeanPropertyProcessor processor)
0135: throws BeanUtilsException {
0136: processProperties(GETTERS_SETTERS, beanClass,
0137: includedProperties, excludedProperties, prefix,
0138: processor);
0139: }
0140:
0141: public static void processProperties(Accessors accessors,
0142: Class beanClass, String[] includedProperties,
0143: String[] excludedProperties, String prefix,
0144: BeanPropertyProcessor processor) throws BeanUtilsException {
0145: if (null == beanClass)
0146: return;
0147: if (null == processor)
0148: return;
0149:
0150: // obtain the BeanInfo class
0151: BeanInfo bean_info = getBeanInfo(beanClass);
0152:
0153: // process the properties of the bean
0154: PropertyDescriptor[] bean_properties = bean_info
0155: .getPropertyDescriptors();
0156: if (bean_properties.length > 0) {
0157: String property_name = null;
0158: Collection<String> included_properties = null;
0159: Collection<String> excluded_properties = null;
0160: if (null != includedProperties
0161: && includedProperties.length > 0) {
0162: included_properties = new ArrayList<String>(Arrays
0163: .asList(includedProperties));
0164: }
0165: if (null != excludedProperties
0166: && excludedProperties.length > 0) {
0167: excluded_properties = new ArrayList<String>(Arrays
0168: .asList(excludedProperties));
0169: }
0170:
0171: // iterate over the properties of the bean
0172: for (PropertyDescriptor bean_property : bean_properties) {
0173: property_name = validateProperty(accessors,
0174: bean_property, included_properties,
0175: excluded_properties, prefix);
0176:
0177: // process the property if it was valid
0178: if (property_name != null) {
0179: try {
0180: if (!processor.gotProperty(property_name,
0181: bean_property)) {
0182: break;
0183: }
0184: } catch (IllegalAccessException e) {
0185: throw new BeanUtilsException(
0186: "No permission to invoke a method of the property '"
0187: + property_name
0188: + "' of the bean.", beanClass,
0189: e);
0190: } catch (IllegalArgumentException e) {
0191: throw new BeanUtilsException(
0192: "Invalid arguments while invoking a method of the property '"
0193: + property_name
0194: + "' on the bean.", beanClass,
0195: e);
0196: } catch (InvocationTargetException e) {
0197: throw new BeanUtilsException(
0198: "A method of the property '"
0199: + property_name
0200: + "' of the bean has thrown an exception.",
0201: beanClass, e.getTargetException());
0202: }
0203: }
0204: }
0205: }
0206: }
0207:
0208: public static void processPropertyValues(final Object bean,
0209: String[] includedProperties, String[] excludedProperties,
0210: String prefix, final BeanPropertyValueProcessor processor)
0211: throws BeanUtilsException {
0212: processPropertyValues(GETTERS_SETTERS, bean,
0213: includedProperties, excludedProperties, prefix,
0214: processor);
0215: }
0216:
0217: public static void processPropertyValues(Accessors accessors,
0218: final Object bean, String[] includedProperties,
0219: String[] excludedProperties, String prefix,
0220: final BeanPropertyValueProcessor processor)
0221: throws BeanUtilsException {
0222: if (null == bean)
0223: return;
0224: if (bean instanceof Class)
0225: throw new IllegalArgumentException(
0226: "bean should be a bean instance, not a bean class.");
0227:
0228: processProperties(accessors, bean.getClass(),
0229: includedProperties, excludedProperties, prefix,
0230: new BeanPropertyProcessor() {
0231: public boolean gotProperty(String name,
0232: PropertyDescriptor descriptor)
0233: throws IllegalAccessException,
0234: IllegalArgumentException,
0235: InvocationTargetException {
0236: // obtain the value of the property
0237: Method property_read_method = descriptor
0238: .getReadMethod();
0239: if (property_read_method != null) {
0240: // handle the property value
0241: processor.gotProperty(name, descriptor,
0242: property_read_method.invoke(bean,
0243: (Object[]) null));
0244: }
0245:
0246: return true;
0247: }
0248: });
0249: }
0250:
0251: public static int countProperties(Class beanClass,
0252: String[] includedProperties, String[] excludedProperties,
0253: String prefix) throws BeanUtilsException {
0254: return countProperties(GETTERS_SETTERS, beanClass,
0255: includedProperties, excludedProperties, prefix);
0256: }
0257:
0258: public static int countProperties(Accessors accessors,
0259: Class beanClass, String[] includedProperties,
0260: String[] excludedProperties, String prefix)
0261: throws BeanUtilsException {
0262: if (null == beanClass)
0263: return 0;
0264:
0265: final int[] result = new int[] { 0 };
0266:
0267: processProperties(accessors, beanClass, includedProperties,
0268: excludedProperties, prefix,
0269: new BeanPropertyProcessor() {
0270: public boolean gotProperty(String name,
0271: PropertyDescriptor descriptor)
0272: throws IllegalAccessException,
0273: IllegalArgumentException,
0274: InvocationTargetException {
0275: result[0]++;
0276: return true;
0277: }
0278: });
0279:
0280: return result[0];
0281: }
0282:
0283: public static Object getPropertyValue(Object bean, String name)
0284: throws BeanUtilsException {
0285: if (null == bean)
0286: throw new IllegalArgumentException("bean can't be null.");
0287: if (bean instanceof Class)
0288: throw new IllegalArgumentException(
0289: "bean should be a bean instance, not a bean class.");
0290: if (null == name)
0291: throw new IllegalArgumentException("name can't be null.");
0292: if (0 == name.length())
0293: throw new IllegalArgumentException("name can't be empty.");
0294:
0295: // obtain the BeanInfo class
0296: Class bean_class = bean.getClass();
0297: BeanInfo bean_info = getBeanInfo(bean_class);
0298:
0299: // process the properties of the bean
0300: PropertyDescriptor[] bean_properties = bean_info
0301: .getPropertyDescriptors();
0302: if (bean_properties.length > 0) {
0303: String property_name = null;
0304: Method property_read_method = null;
0305:
0306: // iterate over the properties of the bean
0307: for (PropertyDescriptor bean_property : bean_properties) {
0308: property_name = bean_property.getName();
0309:
0310: // process the property if it was valid
0311: if (property_name.equals(name)) {
0312: // obtain the value of the property
0313: property_read_method = bean_property
0314: .getReadMethod();
0315: if (null == property_read_method) {
0316: throw new BeanUtilsException(
0317: "The bean '"
0318: + bean_class
0319: + "' doesn't contain a getter for property '"
0320: + name + "'", bean_class);
0321: }
0322:
0323: try {
0324: return property_read_method.invoke(bean,
0325: (Object[]) null);
0326: } catch (IllegalAccessException e) {
0327: throw new BeanUtilsException(
0328: "No permission to invoke the '"
0329: + property_read_method
0330: .getName()
0331: + "' method on the bean.",
0332: bean_class, e);
0333: } catch (IllegalArgumentException e) {
0334: throw new BeanUtilsException(
0335: "Invalid arguments while invoking the '"
0336: + property_read_method
0337: .getName()
0338: + "' method on the bean.",
0339: bean_class, e);
0340: } catch (InvocationTargetException e) {
0341: throw new BeanUtilsException(
0342: "The '"
0343: + property_read_method
0344: .getName()
0345: + "' method of the bean has thrown an exception.",
0346: bean_class, e.getTargetException());
0347: }
0348: }
0349: }
0350: }
0351:
0352: throw new BeanUtilsException("The bean '" + bean_class
0353: + "' doesn't contain property '" + name + "'",
0354: bean_class);
0355: }
0356:
0357: public static void setPropertyValue(Object bean, String name,
0358: Object value) throws BeanUtilsException {
0359: if (null == bean)
0360: throw new IllegalArgumentException("bean can't be null.");
0361: if (bean instanceof Class)
0362: throw new IllegalArgumentException(
0363: "bean should be a bean instance, not a bean class.");
0364: if (null == name)
0365: throw new IllegalArgumentException("name can't be null.");
0366: if (0 == name.length())
0367: throw new IllegalArgumentException("name can't be empty.");
0368:
0369: // obtain the BeanInfo class
0370: Class bean_class = bean.getClass();
0371: BeanInfo bean_info = getBeanInfo(bean_class);
0372:
0373: // process the properties of the bean
0374: PropertyDescriptor[] bean_properties = bean_info
0375: .getPropertyDescriptors();
0376: if (bean_properties.length > 0) {
0377: String property_name = null;
0378: Method property_write_method = null;
0379:
0380: // iterate over the properties of the bean
0381: for (PropertyDescriptor bean_property : bean_properties) {
0382: property_name = bean_property.getName();
0383:
0384: // process the property if it was valid
0385: if (property_name.equals(name)) {
0386: // obtain the value of the property
0387: property_write_method = bean_property
0388: .getWriteMethod();
0389: try {
0390: property_write_method.invoke(bean, value);
0391: return;
0392: } catch (IllegalAccessException e) {
0393: throw new BeanUtilsException(
0394: "No permission to invoke the '"
0395: + property_write_method
0396: .getName()
0397: + "' method on the bean.",
0398: bean_class, e);
0399: } catch (IllegalArgumentException e) {
0400: throw new BeanUtilsException(
0401: "Invalid arguments while invoking the '"
0402: + property_write_method
0403: .getName()
0404: + "' method on the bean.",
0405: bean_class, e);
0406: } catch (InvocationTargetException e) {
0407: throw new BeanUtilsException(
0408: "The '"
0409: + property_write_method
0410: .getName()
0411: + "' method of the bean has thrown an exception.",
0412: bean_class, e.getTargetException());
0413: }
0414: }
0415: }
0416: }
0417:
0418: throw new BeanUtilsException("The bean '" + bean_class
0419: + "' doesn't contain property '" + name + "'",
0420: bean_class);
0421: }
0422:
0423: public static Class getPropertyType(Class beanClass, String name)
0424: throws BeanUtilsException {
0425: if (null == beanClass)
0426: throw new IllegalArgumentException(
0427: "beanClass can't be null.");
0428: if (null == name)
0429: throw new IllegalArgumentException("name can't be null.");
0430: if (0 == name.length())
0431: throw new IllegalArgumentException("name can't be empty.");
0432:
0433: // obtain the BeanInfo class
0434: BeanInfo bean_info = getBeanInfo(beanClass);
0435:
0436: // process the properties of the bean
0437: PropertyDescriptor[] bean_properties = bean_info
0438: .getPropertyDescriptors();
0439: if (bean_properties.length > 0) {
0440: String property_name = null;
0441: Method property_read_method = null;
0442:
0443: // iterate over the properties of the bean
0444: for (PropertyDescriptor bean_property : bean_properties) {
0445: property_name = bean_property.getName();
0446:
0447: // process the property if it was valid
0448: if (property_name.equals(name)) {
0449: // obtain the value of the property
0450: property_read_method = bean_property
0451: .getReadMethod();
0452: return property_read_method.getReturnType();
0453: }
0454: }
0455: }
0456:
0457: throw new BeanUtilsException("The bean '" + beanClass
0458: + "' doesn't contain property '" + name + "'",
0459: beanClass);
0460: }
0461:
0462: public static Map<String, Object> getPropertyValues(Object bean,
0463: String[] includedProperties, String[] excludedProperties,
0464: String prefix) throws BeanUtilsException {
0465: return getPropertyValues(GETTERS_SETTERS, bean,
0466: includedProperties, excludedProperties, prefix);
0467: }
0468:
0469: public static Map<String, Object> getPropertyValues(
0470: Accessors accessors, Object bean,
0471: String[] includedProperties, String[] excludedProperties,
0472: String prefix) throws BeanUtilsException {
0473: final LinkedHashMap<String, Object> property_values = new LinkedHashMap<String, Object>();
0474:
0475: processPropertyValues(accessors, bean, includedProperties,
0476: excludedProperties, prefix,
0477: new BeanPropertyValueProcessor() {
0478: public void gotProperty(String name,
0479: PropertyDescriptor descriptor, Object value)
0480: throws IllegalAccessException,
0481: IllegalArgumentException,
0482: InvocationTargetException {
0483: // store the property value
0484: property_values.put(name, value);
0485: }
0486: });
0487:
0488: return property_values;
0489: }
0490:
0491: public static String formatPropertyValue(Object propertyValue,
0492: ConstrainedProperty constrainedProperty) {
0493: if (propertyValue instanceof String) {
0494: return (String) propertyValue;
0495: }
0496:
0497: Format format = null;
0498: if (constrainedProperty != null
0499: && constrainedProperty.isFormatted()) {
0500: format = constrainedProperty.getFormat();
0501: } else if (propertyValue instanceof Date) {
0502: format = getConcisePreciseDateFormat();
0503: }
0504:
0505: if (format != null) {
0506: return format.format(propertyValue);
0507: }
0508:
0509: return String.valueOf(propertyValue);
0510: }
0511:
0512: public static Map<String, Class> getPropertyTypes(Class beanClass,
0513: String[] includedProperties, String[] excludedProperties,
0514: String prefix) throws BeanUtilsException {
0515: return getPropertyTypes(GETTERS_SETTERS, beanClass,
0516: includedProperties, excludedProperties, prefix);
0517: }
0518:
0519: public static Map<String, Class> getPropertyTypes(
0520: Accessors accessors, Class beanClass,
0521: String[] includedProperties, String[] excludedProperties,
0522: String prefix) throws BeanUtilsException {
0523: if (null == beanClass)
0524: return Collections.emptyMap();
0525:
0526: final LinkedHashMap<String, Class> property_types = new LinkedHashMap<String, Class>();
0527:
0528: processProperties(accessors, beanClass, includedProperties,
0529: excludedProperties, prefix,
0530: new BeanPropertyProcessor() {
0531: public boolean gotProperty(String name,
0532: PropertyDescriptor descriptor)
0533: throws IllegalAccessException,
0534: IllegalArgumentException,
0535: InvocationTargetException {
0536: Class property_class = null;
0537:
0538: // obtain and store the property type
0539: Method property_read_method = descriptor
0540: .getReadMethod();
0541: if (property_read_method != null) {
0542: property_class = property_read_method
0543: .getReturnType();
0544: } else {
0545: Method property_write_method = descriptor
0546: .getWriteMethod();
0547: property_class = property_write_method
0548: .getParameterTypes()[0];
0549: }
0550:
0551: property_types.put(name, property_class);
0552:
0553: return true;
0554: }
0555: });
0556:
0557: return property_types;
0558: }
0559:
0560: /**
0561: * Retrieves a map of all the properties of a bean and their descriptors.
0562: * <p>The property names will be uppercased and an exception will be thrown
0563: * if two properties are equals case-insensitively.
0564: *
0565: * @param beanClass the class of the bean
0566: * @exception com.uwyn.rife.tools.exceptions.BeanUtilsException when an error
0567: * occurred while obtaining the bean properties
0568: * @return the map of the bean properties
0569: * @see #setUppercasedBeanProperty(String, String[], String, Map, Object, Object)
0570: * @see #setUppercasedBeanProperty(String, UploadedFile, String, Map, Object)
0571: * @since 1.4
0572: */
0573: public static HashMap<String, PropertyDescriptor> getUppercasedBeanProperties(
0574: Class beanClass) throws BeanUtilsException {
0575: if (null == beanClass)
0576: throw new IllegalArgumentException(
0577: "beanClass can't be null.");
0578:
0579: HashMap<String, PropertyDescriptor> bean_properties = new HashMap<String, PropertyDescriptor>();
0580: BeanInfo bean_info = null;
0581: PropertyDescriptor[] bean_properties_array = null;
0582:
0583: try {
0584: bean_info = Introspector.getBeanInfo(beanClass);
0585: } catch (IntrospectionException e) {
0586: throw new BeanUtilsException(
0587: "Couldn't introspect the bean with class '"
0588: + beanClass.getName() + "'.", beanClass, e);
0589: }
0590: bean_properties_array = bean_info.getPropertyDescriptors();
0591: String bean_property_name = null;
0592: for (PropertyDescriptor bean_property : bean_properties_array) {
0593: bean_property_name = bean_property.getName().toUpperCase();
0594: if (bean_properties.containsKey(bean_property_name)) {
0595: throw new BeanUtilsException(
0596: "Duplicate case insensitive bean property '"
0597: + bean_property_name + "' in bean '"
0598: + beanClass.getName() + "'.", beanClass);
0599: }
0600: bean_properties.put(bean_property_name, bean_property);
0601: }
0602:
0603: return bean_properties;
0604: }
0605:
0606: /**
0607: * Parses the textual representation of the date using a custom format, or by
0608: * relying on the standard date formats.
0609: *
0610: * @param date the textual representation of the date
0611: * @param format the custom format that should be used for parsing the string
0612: * representation of the date; or {@code null} if the default formats should
0613: * be used
0614: * @return the parsed date
0615: * @throws ParseException if an error occurred when the date was parsed
0616: * @since 1.6
0617: */
0618: public Object parseDate(String date, Format format)
0619: throws ParseException {
0620: if (null == date) {
0621: return null;
0622: }
0623:
0624: Object result = null;
0625: if (null == format) {
0626: try {
0627: result = BeanUtils.getConcisePreciseDateFormat()
0628: .parseObject(date);
0629: } catch (ParseException e) {
0630: try {
0631: result = RifeConfig.Tools
0632: .getDefaultInputDateFormat().parseObject(
0633: date);
0634: } catch (ParseException e2) {
0635: throw e;
0636: }
0637: }
0638: } else {
0639: result = format.parseObject(date);
0640: }
0641:
0642: return result;
0643: }
0644:
0645: /**
0646: * Set the value of a bean property from an array of strings.
0647: *
0648: * @param propertyName the name of the property
0649: * @param propertyValues the values that will be set, can be <code>null</code>
0650: * @param propertyNamePrefix the prefix that the propertyName parameter
0651: * should have, can be <code>null</code>
0652: * @param beanProperties the map of the uppercased bean property names and
0653: * their descriptors
0654: * @param beanInstance the bean instance whose property should be updated
0655: * @param emptyBean this bean instance will be used to set the value of the
0656: * property in case the propertyValues parameter is empty or null, can be
0657: * <code>null</code>
0658: * @exception com.uwyn.rife.tools.exceptions.BeanUtilsException when an error
0659: * occurred while setting the bean property
0660: * @see #getUppercasedBeanProperties(Class)
0661: * @see #setUppercasedBeanProperty(String, UploadedFile, String, Map, Object)
0662: * @since 1.4
0663: */
0664: public static void setUppercasedBeanProperty(String propertyName,
0665: String[] propertyValues, String propertyNamePrefix,
0666: Map<String, PropertyDescriptor> beanProperties,
0667: Object beanInstance, Object emptyBean)
0668: throws BeanUtilsException {
0669: if (null == propertyName)
0670: throw new IllegalArgumentException(
0671: "propertyName can't be null.");
0672: if (null == beanProperties)
0673: throw new IllegalArgumentException(
0674: "beanProperties can't be null.");
0675: if (null == beanInstance)
0676: throw new IllegalArgumentException(
0677: "beanInstance can't be null.");
0678:
0679: Class bean_class = beanInstance.getClass();
0680:
0681: String name_upper = null;
0682: PropertyDescriptor property = null;
0683: Method write_method = null;
0684: Class property_type = null;
0685:
0686: if (propertyNamePrefix != null) {
0687: if (!propertyName.startsWith(propertyNamePrefix)) {
0688: return;
0689: }
0690:
0691: propertyName = propertyName.substring(propertyNamePrefix
0692: .length());
0693: }
0694: name_upper = propertyName.toUpperCase();
0695:
0696: if (beanProperties.containsKey(name_upper)) {
0697: if (null == emptyBean
0698: && (null == propertyValues || 0 == propertyValues.length)) {
0699: return;
0700: }
0701:
0702: property = beanProperties.get(name_upper);
0703:
0704: write_method = property.getWriteMethod();
0705: if (null == write_method) {
0706: return;
0707: }
0708:
0709: property_type = property.getPropertyType();
0710: if (null == property_type) {
0711: return;
0712: }
0713:
0714: Validated validated = null;
0715: if (beanInstance instanceof Validated) {
0716: validated = (Validated) beanInstance;
0717: }
0718:
0719: Constrained constrained = ConstrainedUtils
0720: .makeConstrainedInstance(beanInstance);
0721: ConstrainedProperty constrained_property = null;
0722: if (constrained != null) {
0723: constrained_property = constrained
0724: .getConstrainedProperty(property.getName());
0725: }
0726:
0727: try {
0728: // handle the assignment of empty values to properties
0729: // in case an empty template bean has been provided
0730: if (emptyBean != null
0731: && (null == propertyValues
0732: || 0 == propertyValues.length
0733: || null == propertyValues[0] || 0 == propertyValues[0]
0734: .length())) {
0735: Method read_method = property.getReadMethod();
0736: Object empty_value = read_method.invoke(emptyBean,
0737: (Object[]) null);
0738: write_method.invoke(beanInstance,
0739: new Object[] { empty_value });
0740: }
0741: // assign the value normally
0742: else {
0743: // process an array property
0744: if (property_type.isArray()) {
0745: Class component_type = property_type
0746: .getComponentType();
0747: if (component_type == String.class) {
0748: write_method.invoke(beanInstance,
0749: new Object[] { propertyValues });
0750: } else if (component_type == int.class) {
0751: int parameter_values_typed[] = new int[propertyValues.length];
0752: for (int i = 0; i < propertyValues.length; i++) {
0753: if (propertyValues[i] != null
0754: && propertyValues[i].length() > 0) {
0755: if (constrained_property != null
0756: && constrained_property
0757: .isFormatted()) {
0758: parameter_values_typed[i] = Convert
0759: .toInt(constrained_property
0760: .getFormat()
0761: .parseObject(
0762: propertyValues[i]));
0763: } else {
0764: parameter_values_typed[i] = Convert
0765: .toInt(propertyValues[i]);
0766: }
0767: }
0768: }
0769: write_method
0770: .invoke(
0771: beanInstance,
0772: new Object[] { parameter_values_typed });
0773: } else if (component_type == Integer.class) {
0774: Integer parameter_values_typed[] = new Integer[propertyValues.length];
0775: for (int i = 0; i < propertyValues.length; i++) {
0776: if (propertyValues[i] != null
0777: && propertyValues[i].length() > 0) {
0778: if (constrained_property != null
0779: && constrained_property
0780: .isFormatted()) {
0781: parameter_values_typed[i] = Convert
0782: .toInt(constrained_property
0783: .getFormat()
0784: .parseObject(
0785: propertyValues[i]));
0786: } else {
0787: parameter_values_typed[i] = Convert
0788: .toInt(propertyValues[i]);
0789: }
0790: }
0791: }
0792: write_method
0793: .invoke(
0794: beanInstance,
0795: new Object[] { parameter_values_typed });
0796: } else if (component_type == char.class) {
0797: char parameter_values_typed[] = new char[propertyValues.length];
0798: for (int i = 0; i < propertyValues.length; i++) {
0799: if (propertyValues[i] != null
0800: && propertyValues[i].length() > 0) {
0801: parameter_values_typed[i] = propertyValues[i]
0802: .charAt(0);
0803: }
0804: }
0805: write_method
0806: .invoke(
0807: beanInstance,
0808: new Object[] { parameter_values_typed });
0809: } else if (component_type == Character.class) {
0810: Character parameter_values_typed[] = new Character[propertyValues.length];
0811: for (int i = 0; i < propertyValues.length; i++) {
0812: if (propertyValues[i] != null
0813: && propertyValues[i].length() > 0) {
0814: parameter_values_typed[i] = new Character(
0815: propertyValues[i].charAt(0));
0816: }
0817: }
0818: write_method
0819: .invoke(
0820: beanInstance,
0821: new Object[] { parameter_values_typed });
0822: } else if (component_type == boolean.class) {
0823: boolean parameter_values_typed[] = new boolean[propertyValues.length];
0824: for (int i = 0; i < propertyValues.length; i++) {
0825: if (propertyValues[i] != null
0826: && propertyValues[i].length() > 0) {
0827: parameter_values_typed[i] = StringUtils
0828: .convertToBoolean(propertyValues[i]);
0829: }
0830: }
0831: write_method
0832: .invoke(
0833: beanInstance,
0834: new Object[] { parameter_values_typed });
0835: } else if (component_type == Boolean.class) {
0836: Boolean parameter_values_typed[] = new Boolean[propertyValues.length];
0837: for (int i = 0; i < propertyValues.length; i++) {
0838: if (propertyValues[i] != null
0839: && propertyValues[i].length() > 0) {
0840: parameter_values_typed[i] = Boolean
0841: .valueOf(StringUtils
0842: .convertToBoolean(propertyValues[i]));
0843: }
0844: }
0845: write_method
0846: .invoke(
0847: beanInstance,
0848: new Object[] { parameter_values_typed });
0849: } else if (component_type == byte.class) {
0850: byte parameter_values_typed[] = new byte[propertyValues.length];
0851: for (int i = 0; i < propertyValues.length; i++) {
0852: if (propertyValues[i] != null
0853: && propertyValues[i].length() > 0) {
0854: if (constrained_property != null
0855: && constrained_property
0856: .isFormatted()) {
0857: parameter_values_typed[i] = Convert
0858: .toByte(constrained_property
0859: .getFormat()
0860: .parseObject(
0861: propertyValues[i]));
0862: } else {
0863: parameter_values_typed[i] = Convert
0864: .toByte(propertyValues[i]);
0865: }
0866: }
0867: }
0868: write_method
0869: .invoke(
0870: beanInstance,
0871: new Object[] { parameter_values_typed });
0872: } else if (component_type == Byte.class) {
0873: Byte parameter_values_typed[] = new Byte[propertyValues.length];
0874: for (int i = 0; i < propertyValues.length; i++) {
0875: if (propertyValues[i] != null
0876: && propertyValues[i].length() > 0) {
0877: if (constrained_property != null
0878: && constrained_property
0879: .isFormatted()) {
0880: parameter_values_typed[i] = Convert
0881: .toByte(constrained_property
0882: .getFormat()
0883: .parseObject(
0884: propertyValues[i]));
0885: } else {
0886: parameter_values_typed[i] = Convert
0887: .toByte(propertyValues[i]);
0888: }
0889: }
0890: }
0891: write_method
0892: .invoke(
0893: beanInstance,
0894: new Object[] { parameter_values_typed });
0895: } else if (component_type == double.class) {
0896: double parameter_values_typed[] = new double[propertyValues.length];
0897: for (int i = 0; i < propertyValues.length; i++) {
0898: if (propertyValues[i] != null
0899: && propertyValues[i].length() > 0) {
0900: if (constrained_property != null
0901: && constrained_property
0902: .isFormatted()) {
0903: parameter_values_typed[i] = Convert
0904: .toDouble(constrained_property
0905: .getFormat()
0906: .parseObject(
0907: propertyValues[i]));
0908: } else {
0909: parameter_values_typed[i] = Convert
0910: .toDouble(propertyValues[i]);
0911: }
0912: }
0913: }
0914: write_method
0915: .invoke(
0916: beanInstance,
0917: new Object[] { parameter_values_typed });
0918: } else if (component_type == Double.class) {
0919: Double parameter_values_typed[] = new Double[propertyValues.length];
0920: for (int i = 0; i < propertyValues.length; i++) {
0921: if (propertyValues[i] != null
0922: && propertyValues[i].length() > 0) {
0923: if (constrained_property != null
0924: && constrained_property
0925: .isFormatted()) {
0926: parameter_values_typed[i] = Convert
0927: .toDouble(constrained_property
0928: .getFormat()
0929: .parseObject(
0930: propertyValues[i]));
0931: } else {
0932: parameter_values_typed[i] = Convert
0933: .toDouble(propertyValues[i]);
0934: }
0935: }
0936: }
0937: write_method
0938: .invoke(
0939: beanInstance,
0940: new Object[] { parameter_values_typed });
0941: } else if (component_type == float.class) {
0942: float parameter_values_typed[] = new float[propertyValues.length];
0943: for (int i = 0; i < propertyValues.length; i++) {
0944: if (propertyValues[i] != null
0945: && propertyValues[i].length() > 0) {
0946: if (constrained_property != null
0947: && constrained_property
0948: .isFormatted()) {
0949: parameter_values_typed[i] = Convert
0950: .toFloat(constrained_property
0951: .getFormat()
0952: .parseObject(
0953: propertyValues[i]));
0954: } else {
0955: parameter_values_typed[i] = Convert
0956: .toFloat(propertyValues[i]);
0957: }
0958: }
0959: }
0960: write_method
0961: .invoke(
0962: beanInstance,
0963: new Object[] { parameter_values_typed });
0964: } else if (component_type == Float.class) {
0965: Float parameter_values_typed[] = new Float[propertyValues.length];
0966: for (int i = 0; i < propertyValues.length; i++) {
0967: if (propertyValues[i] != null
0968: && propertyValues[i].length() > 0) {
0969: if (constrained_property != null
0970: && constrained_property
0971: .isFormatted()) {
0972: parameter_values_typed[i] = Convert
0973: .toFloat(constrained_property
0974: .getFormat()
0975: .parseObject(
0976: propertyValues[i]));
0977: } else {
0978: parameter_values_typed[i] = Convert
0979: .toFloat(propertyValues[i]);
0980: }
0981: }
0982: }
0983: write_method
0984: .invoke(
0985: beanInstance,
0986: new Object[] { parameter_values_typed });
0987: } else if (component_type == long.class) {
0988: long parameter_values_typed[] = new long[propertyValues.length];
0989: for (int i = 0; i < propertyValues.length; i++) {
0990: if (propertyValues[i] != null
0991: && propertyValues[i].length() > 0) {
0992: if (constrained_property != null
0993: && constrained_property
0994: .isFormatted()) {
0995: parameter_values_typed[i] = Convert
0996: .toLong(constrained_property
0997: .getFormat()
0998: .parseObject(
0999: propertyValues[i]));
1000: } else {
1001: parameter_values_typed[i] = Convert
1002: .toLong(propertyValues[i]);
1003: }
1004: }
1005: }
1006: write_method
1007: .invoke(
1008: beanInstance,
1009: new Object[] { parameter_values_typed });
1010: } else if (component_type == Long.class) {
1011: Long parameter_values_typed[] = new Long[propertyValues.length];
1012: for (int i = 0; i < propertyValues.length; i++) {
1013: if (propertyValues[i] != null
1014: && propertyValues[i].length() > 0) {
1015: if (constrained_property != null
1016: && constrained_property
1017: .isFormatted()) {
1018: parameter_values_typed[i] = Convert
1019: .toLong(constrained_property
1020: .getFormat()
1021: .parseObject(
1022: propertyValues[i]));
1023: } else {
1024: parameter_values_typed[i] = Convert
1025: .toLong(propertyValues[i]);
1026: }
1027: }
1028: }
1029: write_method
1030: .invoke(
1031: beanInstance,
1032: new Object[] { parameter_values_typed });
1033: } else if (component_type == short.class) {
1034: short parameter_values_typed[] = new short[propertyValues.length];
1035: for (int i = 0; i < propertyValues.length; i++) {
1036: if (propertyValues[i] != null
1037: && propertyValues[i].length() > 0) {
1038: if (constrained_property != null
1039: && constrained_property
1040: .isFormatted()) {
1041: parameter_values_typed[i] = Convert
1042: .toShort(constrained_property
1043: .getFormat()
1044: .parseObject(
1045: propertyValues[i]));
1046: } else {
1047: parameter_values_typed[i] = Convert
1048: .toShort(propertyValues[i]);
1049: }
1050: }
1051: }
1052: write_method
1053: .invoke(
1054: beanInstance,
1055: new Object[] { parameter_values_typed });
1056: } else if (component_type == Short.class) {
1057: Short parameter_values_typed[] = new Short[propertyValues.length];
1058: for (int i = 0; i < propertyValues.length; i++) {
1059: if (propertyValues[i] != null
1060: && propertyValues[i].length() > 0) {
1061: if (constrained_property != null
1062: && constrained_property
1063: .isFormatted()) {
1064: parameter_values_typed[i] = Convert
1065: .toShort(constrained_property
1066: .getFormat()
1067: .parseObject(
1068: propertyValues[i]));
1069: } else {
1070: parameter_values_typed[i] = Convert
1071: .toShort(propertyValues[i]);
1072: }
1073: }
1074: }
1075: write_method
1076: .invoke(
1077: beanInstance,
1078: new Object[] { parameter_values_typed });
1079: } else if (component_type == BigDecimal.class) {
1080: BigDecimal parameter_values_typed[] = new BigDecimal[propertyValues.length];
1081: for (int i = 0; i < propertyValues.length; i++) {
1082: if (propertyValues[i] != null
1083: && propertyValues[i].length() > 0) {
1084: if (constrained_property != null
1085: && constrained_property
1086: .isFormatted()) {
1087: parameter_values_typed[i] = new BigDecimal(
1088: String
1089: .valueOf(constrained_property
1090: .getFormat()
1091: .parseObject(
1092: propertyValues[i])));
1093: } else {
1094: parameter_values_typed[i] = new BigDecimal(
1095: propertyValues[i]);
1096: }
1097: }
1098: }
1099: write_method
1100: .invoke(
1101: beanInstance,
1102: new Object[] { parameter_values_typed });
1103: } else if (component_type == StringBuffer.class) {
1104: StringBuffer parameter_values_typed[] = new StringBuffer[propertyValues.length];
1105: for (int i = 0; i < propertyValues.length; i++) {
1106: if (propertyValues[i] != null
1107: && propertyValues[i].length() > 0) {
1108: parameter_values_typed[i] = new StringBuffer(
1109: propertyValues[i]);
1110: }
1111: }
1112: write_method
1113: .invoke(
1114: beanInstance,
1115: new Object[] { parameter_values_typed });
1116: } else if (component_type == StringBuilder.class) {
1117: StringBuilder parameter_values_typed[] = new StringBuilder[propertyValues.length];
1118: for (int i = 0; i < propertyValues.length; i++) {
1119: if (propertyValues[i] != null
1120: && propertyValues[i].length() > 0) {
1121: parameter_values_typed[i] = new StringBuilder(
1122: propertyValues[i]);
1123: }
1124: }
1125: write_method
1126: .invoke(
1127: beanInstance,
1128: new Object[] { parameter_values_typed });
1129: } else if (Date.class
1130: .isAssignableFrom(component_type)) {
1131: Format custom_format = null;
1132: if (constrained_property != null
1133: && constrained_property
1134: .isFormatted()) {
1135: custom_format = constrained_property
1136: .getFormat();
1137: }
1138:
1139: try {
1140: Date parameter_values_typed[] = new Date[propertyValues.length];
1141: for (int i = 0; i < propertyValues.length; i++) {
1142: if (propertyValues[i] != null
1143: && propertyValues[i]
1144: .length() > 0) {
1145: Format used_format = null;
1146:
1147: Object parameter_value_typed = null;
1148: if (null == custom_format) {
1149: try {
1150: used_format = BeanUtils
1151: .getConcisePreciseDateFormat();
1152: parameter_value_typed = used_format
1153: .parseObject(propertyValues[i]);
1154: } catch (ParseException e) {
1155: try {
1156: used_format = RifeConfig.Tools
1157: .getDefaultInputDateFormat();
1158: parameter_value_typed = used_format
1159: .parseObject(propertyValues[i]);
1160: } catch (ParseException e2) {
1161: throw e;
1162: }
1163: }
1164: } else {
1165: used_format = custom_format;
1166: parameter_value_typed = used_format
1167: .parseObject(propertyValues[i]);
1168: }
1169:
1170: if (propertyValues[i]
1171: .equals(used_format
1172: .format(parameter_value_typed))) {
1173: parameter_values_typed[i] = (Date) parameter_value_typed;
1174: } else {
1175: if (validated != null) {
1176: validated
1177: .addValidationError(new ValidationError.INVALID(
1178: propertyName)
1179: .erroneousValue(propertyValues[i]));
1180: }
1181: }
1182: }
1183: }
1184: write_method
1185: .invoke(
1186: beanInstance,
1187: new Object[] { parameter_values_typed });
1188: } catch (ParseException e) {
1189: if (validated != null) {
1190: validated
1191: .addValidationError(new ValidationError.INVALID(
1192: propertyName)
1193: .erroneousValue(propertyValues[0]));
1194: }
1195: }
1196: } else if (component_type.isEnum()) {
1197: Object parameter_values_typed = Array
1198: .newInstance(component_type,
1199: propertyValues.length);
1200: for (int i = 0; i < propertyValues.length; i++) {
1201: if (propertyValues[i] != null
1202: && propertyValues[i].length() > 0) {
1203: try {
1204: Array
1205: .set(
1206: parameter_values_typed,
1207: i,
1208: Enum
1209: .valueOf(
1210: component_type,
1211: propertyValues[i]));
1212: } catch (IllegalArgumentException e) {
1213: // don't throw an exception for this since any invalid copy/paste of an URL
1214: // will give a general exception, just set the value to null and it will
1215: // no be set to the property
1216: if (validated != null) {
1217: validated
1218: .addValidationError(new ValidationError.INVALID(
1219: propertyName)
1220: .erroneousValue(propertyValues[i]));
1221: }
1222: }
1223: }
1224: }
1225: write_method
1226: .invoke(
1227: beanInstance,
1228: new Object[] { parameter_values_typed });
1229: } else if (Serializable.class
1230: .isAssignableFrom(component_type)) {
1231: Object parameter_values_typed = Array
1232: .newInstance(component_type,
1233: propertyValues.length);
1234: for (int i = 0; i < propertyValues.length; i++) {
1235: if (propertyValues[i] != null
1236: && propertyValues[i].length() > 0) {
1237: try {
1238: Array
1239: .set(
1240: parameter_values_typed,
1241: i,
1242: SerializationUtils
1243: .deserializeFromString(propertyValues[i]));
1244: } catch (SerializationUtilsErrorException e) {
1245: if (validated != null) {
1246: validated
1247: .addValidationError(new ValidationError.INVALID(
1248: propertyName)
1249: .erroneousValue(propertyValues[i]));
1250: }
1251: }
1252: }
1253: }
1254: write_method
1255: .invoke(
1256: beanInstance,
1257: new Object[] { parameter_values_typed });
1258: }
1259: }
1260: // process an object or a primitive type
1261: else if (propertyValues[0] != null
1262: && propertyValues[0].length() > 0) {
1263: Object parameter_value_typed = null;
1264: if (property_type == String.class) {
1265: parameter_value_typed = propertyValues[0];
1266: } else if (property_type == int.class
1267: || property_type == Integer.class) {
1268: if (constrained_property != null
1269: && constrained_property
1270: .isFormatted()) {
1271: parameter_value_typed = Convert
1272: .toInt(constrained_property
1273: .getFormat()
1274: .parseObject(
1275: propertyValues[0]));
1276: } else {
1277: parameter_value_typed = Convert
1278: .toInt(propertyValues[0]);
1279: }
1280: } else if (property_type == char.class
1281: || property_type == Character.class) {
1282: parameter_value_typed = new Character(
1283: propertyValues[0].charAt(0));
1284: } else if (property_type == boolean.class
1285: || property_type == Boolean.class) {
1286: parameter_value_typed = Convert
1287: .toBoolean(StringUtils
1288: .convertToBoolean(propertyValues[0]));
1289: } else if (property_type == byte.class
1290: || property_type == Byte.class) {
1291: if (constrained_property != null
1292: && constrained_property
1293: .isFormatted()) {
1294: parameter_value_typed = Convert
1295: .toByte(constrained_property
1296: .getFormat()
1297: .parseObject(
1298: propertyValues[0]));
1299: } else {
1300: parameter_value_typed = Convert
1301: .toByte(propertyValues[0]);
1302: }
1303: } else if (property_type == double.class
1304: || property_type == Double.class) {
1305: if (constrained_property != null
1306: && constrained_property
1307: .isFormatted()) {
1308: parameter_value_typed = Convert
1309: .toDouble(constrained_property
1310: .getFormat()
1311: .parseObject(
1312: propertyValues[0]));
1313: } else {
1314: parameter_value_typed = Convert
1315: .toDouble(propertyValues[0]);
1316: }
1317: } else if (property_type == float.class
1318: || property_type == Float.class) {
1319: if (constrained_property != null
1320: && constrained_property
1321: .isFormatted()) {
1322: parameter_value_typed = Convert
1323: .toFloat(constrained_property
1324: .getFormat()
1325: .parseObject(
1326: propertyValues[0]));
1327: } else {
1328: parameter_value_typed = Convert
1329: .toFloat(propertyValues[0]);
1330: }
1331: } else if (property_type == long.class
1332: || property_type == Long.class) {
1333: if (constrained_property != null
1334: && constrained_property
1335: .isFormatted()) {
1336: parameter_value_typed = Convert
1337: .toLong(constrained_property
1338: .getFormat()
1339: .parseObject(
1340: propertyValues[0]));
1341: } else {
1342: parameter_value_typed = Convert
1343: .toLong(propertyValues[0]);
1344: }
1345: } else if (property_type == short.class
1346: || property_type == Short.class) {
1347: if (constrained_property != null
1348: && constrained_property
1349: .isFormatted()) {
1350: parameter_value_typed = Convert
1351: .toShort(constrained_property
1352: .getFormat()
1353: .parseObject(
1354: propertyValues[0]));
1355: } else {
1356: parameter_value_typed = Convert
1357: .toShort(propertyValues[0]);
1358: }
1359: } else if (property_type == BigDecimal.class) {
1360: if (constrained_property != null
1361: && constrained_property
1362: .isFormatted()) {
1363: Number n = (Number) constrained_property
1364: .getFormat().parseObject(
1365: propertyValues[0]);
1366: parameter_value_typed = new BigDecimal(
1367: String.valueOf(n));
1368: } else {
1369: parameter_value_typed = new BigDecimal(
1370: propertyValues[0]);
1371: }
1372: } else if (property_type == StringBuffer.class) {
1373: parameter_value_typed = new StringBuffer(
1374: propertyValues[0]);
1375: } else if (property_type == StringBuilder.class) {
1376: parameter_value_typed = new StringBuilder(
1377: propertyValues[0]);
1378: } else if (Date.class
1379: .isAssignableFrom(property_type)) {
1380: Format custom_format = null;
1381: if (constrained_property != null
1382: && constrained_property
1383: .isFormatted()) {
1384: custom_format = constrained_property
1385: .getFormat();
1386: }
1387:
1388: try {
1389: Format used_format = null;
1390:
1391: if (null == custom_format) {
1392: try {
1393: used_format = BeanUtils
1394: .getConcisePreciseDateFormat();
1395: parameter_value_typed = used_format
1396: .parseObject(propertyValues[0]);
1397: } catch (ParseException e) {
1398: try {
1399: used_format = RifeConfig.Tools
1400: .getDefaultInputDateFormat();
1401: parameter_value_typed = used_format
1402: .parseObject(propertyValues[0]);
1403: } catch (ParseException e2) {
1404: throw e;
1405: }
1406: }
1407: } else {
1408: used_format = custom_format;
1409: parameter_value_typed = used_format
1410: .parseObject(propertyValues[0]);
1411: }
1412:
1413: if (!propertyValues[0]
1414: .equals(used_format
1415: .format(parameter_value_typed))) {
1416: parameter_value_typed = null;
1417:
1418: if (validated != null) {
1419: validated
1420: .addValidationError(new ValidationError.INVALID(
1421: propertyName)
1422: .erroneousValue(propertyValues[0]));
1423: }
1424: }
1425: } catch (ParseException e) {
1426: if (validated != null) {
1427: validated
1428: .addValidationError(new ValidationError.INVALID(
1429: propertyName)
1430: .erroneousValue(propertyValues[0]));
1431: }
1432: }
1433: } else if (property_type.isEnum()) {
1434: try {
1435: parameter_value_typed = Enum.valueOf(
1436: property_type,
1437: propertyValues[0]);
1438: } catch (IllegalArgumentException e) {
1439: parameter_value_typed = null;
1440:
1441: if (validated != null) {
1442: validated
1443: .addValidationError(new ValidationError.INVALID(
1444: propertyName)
1445: .erroneousValue(propertyValues[0]));
1446: }
1447: }
1448: } else if (Serializable.class
1449: .isAssignableFrom(property_type)) {
1450: try {
1451: parameter_value_typed = SerializationUtils
1452: .deserializeFromString(propertyValues[0]);
1453: } catch (SerializationUtilsErrorException e) {
1454: // don't throw an exception for this since any invalid copy/paste of an URL
1455: // will give a general exception, just set the value to null and it will
1456: // no be set to the property
1457: parameter_value_typed = null;
1458:
1459: if (validated != null) {
1460: validated
1461: .addValidationError(new ValidationError.INVALID(
1462: propertyName)
1463: .erroneousValue(propertyValues[0]));
1464: }
1465: }
1466: }
1467:
1468: if (parameter_value_typed != null) {
1469: write_method
1470: .invoke(
1471: beanInstance,
1472: new Object[] { parameter_value_typed });
1473: }
1474: }
1475: }
1476: } catch (ParseException e) {
1477: if (validated != null) {
1478: validated
1479: .addValidationError(new ValidationError.NOTNUMERIC(
1480: propertyName)
1481: .erroneousValue(propertyValues[0]));
1482: } else {
1483: throw new BeanUtilsException(
1484: "The '"
1485: + propertyName
1486: + "' property of the bean with class '"
1487: + bean_class.getName()
1488: + "' couldn't be populated due to a parsing error.",
1489: bean_class, e);
1490: }
1491: } catch (ConversionException e) {
1492: if (validated != null) {
1493: validated
1494: .addValidationError(new ValidationError.NOTNUMERIC(
1495: propertyName)
1496: .erroneousValue(propertyValues[0]));
1497: } else {
1498: throw new BeanUtilsException(
1499: "The '"
1500: + propertyName
1501: + "' property of the bean with class '"
1502: + bean_class.getName()
1503: + "' couldn't be populated due to conversion error.",
1504: bean_class, e);
1505: }
1506: } catch (IllegalAccessException e) {
1507: throw new BeanUtilsException(
1508: "No permission to invoke the '"
1509: + write_method.getName()
1510: + "' method on the bean with class '"
1511: + bean_class.getName() + "'.",
1512: bean_class, e);
1513: } catch (IllegalArgumentException e) {
1514: throw new BeanUtilsException(
1515: "Invalid arguments while invoking the '"
1516: + write_method.getName()
1517: + "' method on the bean with class '"
1518: + bean_class.getName() + "'.",
1519: bean_class, e);
1520: } catch (InvocationTargetException e) {
1521: throw new BeanUtilsException("The '"
1522: + write_method.getName()
1523: + "' method of the bean with class '"
1524: + bean_class.getName()
1525: + "' has thrown an exception.", bean_class, e);
1526: }
1527: }
1528: }
1529:
1530: /**
1531: * Set the value of a bean property from an uploaded file.
1532: *
1533: * @param propertyName the name of the property
1534: * @param propertyFile the file that will be set, can be <code>null</code>
1535: * @param propertyNamePrefix the prefix that the propertyName parameter
1536: * should have, can be <code>null</code>
1537: * @param beanProperties the map of the uppercased bean property names and
1538: * their descriptors
1539: * @param beanInstance the bean instance whose property should be updated
1540: * @exception com.uwyn.rife.tools.exceptions.BeanUtilsException when an error
1541: * occurred while setting the bean property
1542: * @see #getUppercasedBeanProperties(Class)
1543: * @see #setUppercasedBeanProperty(String, String[], String, Map, Object, Object)
1544: * @since 1.4
1545: */
1546: public static void setUppercasedBeanProperty(String propertyName,
1547: UploadedFile propertyFile, String propertyNamePrefix,
1548: Map<String, PropertyDescriptor> beanProperties,
1549: Object beanInstance) throws BeanUtilsException {
1550: if (null == propertyName)
1551: throw new IllegalArgumentException(
1552: "propertyName can't be null.");
1553: if (null == beanProperties)
1554: throw new IllegalArgumentException(
1555: "beanProperties can't be null.");
1556: if (null == beanInstance)
1557: throw new IllegalArgumentException(
1558: "beanInstance can't be null.");
1559:
1560: Class bean_class = beanInstance.getClass();
1561:
1562: String name_upper = null;
1563: PropertyDescriptor property = null;
1564: Method write_method = null;
1565: Class property_type = null;
1566:
1567: if (propertyNamePrefix != null) {
1568: if (!propertyName.startsWith(propertyNamePrefix)) {
1569: return;
1570: }
1571:
1572: propertyName = propertyName.substring(propertyNamePrefix
1573: .length());
1574: }
1575: name_upper = propertyName.toUpperCase();
1576:
1577: if (beanProperties.containsKey(name_upper)) {
1578: if (null == propertyFile) {
1579: return;
1580: }
1581:
1582: property = beanProperties.get(name_upper);
1583:
1584: write_method = property.getWriteMethod();
1585: if (null == write_method) {
1586: return;
1587: }
1588:
1589: property_type = property.getPropertyType();
1590: if (null == property_type) {
1591: return;
1592: }
1593:
1594: Validated validated = null;
1595: if (beanInstance instanceof Validated) {
1596: validated = (Validated) beanInstance;
1597: }
1598:
1599: Constrained constrained = ConstrainedUtils
1600: .makeConstrainedInstance(beanInstance);
1601:
1602: try {
1603: if (propertyFile.wasSizeExceeded()) {
1604: if (validated != null) {
1605: validated
1606: .addValidationError(new ValidationError.WRONGLENGTH(
1607: propertyName));
1608: }
1609: } else {
1610: Object parameter_value_typed = null;
1611: if (property_type == InputStream.class) {
1612: parameter_value_typed = new FileInputStream(
1613: propertyFile.getFile());
1614: } else if (property_type == String.class) {
1615: parameter_value_typed = FileUtils
1616: .readString(propertyFile.getFile());
1617: } else if (property_type == byte[].class) {
1618: parameter_value_typed = FileUtils
1619: .readBytes(propertyFile.getFile());
1620: }
1621:
1622: if (parameter_value_typed != null) {
1623: if (constrained != null) {
1624: // fill in the property name automatically if none has been provided
1625: // in the constraints
1626: ConstrainedProperty constrained_property = constrained
1627: .getConstrainedProperty(propertyName);
1628: if (constrained_property != null
1629: && propertyFile.getName() != null) {
1630: if (null == constrained_property
1631: .getName()) {
1632: // if the filename exceeds the maximum length, reduce it intelligently
1633: String file_name = propertyFile
1634: .getName();
1635: if (file_name.length() > 100) {
1636: int reduction_index;
1637: int min_reduction_index = file_name
1638: .length() - 100;
1639: int slash_index = file_name
1640: .lastIndexOf('/');
1641: if (slash_index > min_reduction_index) {
1642: reduction_index = slash_index + 1;
1643: } else {
1644: int backslash_index = file_name
1645: .lastIndexOf('\\');
1646: if (backslash_index > min_reduction_index) {
1647: reduction_index = backslash_index + 1;
1648: } else {
1649: reduction_index = min_reduction_index;
1650: }
1651: }
1652: file_name = file_name
1653: .substring(reduction_index);
1654: }
1655:
1656: constrained_property
1657: .setName(file_name);
1658: }
1659: }
1660: }
1661:
1662: write_method.invoke(beanInstance,
1663: new Object[] { parameter_value_typed });
1664: }
1665: }
1666: } catch (FileUtilsErrorException e) {
1667: throw new BeanUtilsException(
1668: "The '"
1669: + propertyName
1670: + "' property of the bean with class '"
1671: + bean_class.getName()
1672: + "' couldn't be populated due to an unexpected problem during file reading.",
1673: bean_class, e);
1674: } catch (FileNotFoundException e) {
1675: throw new BeanUtilsException(
1676: "The '"
1677: + propertyName
1678: + "' property of the bean with class '"
1679: + bean_class.getName()
1680: + "' couldn't be populated due to an unexpected problem during file reading.",
1681: bean_class, e);
1682: } catch (IllegalAccessException e) {
1683: throw new BeanUtilsException(
1684: "No permission to invoke the '"
1685: + write_method.getName()
1686: + "' method on the bean with class '"
1687: + bean_class.getName() + "'.",
1688: bean_class, e);
1689: } catch (IllegalArgumentException e) {
1690: throw new BeanUtilsException(
1691: "Invalid arguments while invoking the '"
1692: + write_method.getName()
1693: + "' method on the bean with class '"
1694: + bean_class.getName() + "'.",
1695: bean_class, e);
1696: } catch (InvocationTargetException e) {
1697: throw new BeanUtilsException("The '"
1698: + write_method.getName()
1699: + "' method of the bean with class '"
1700: + bean_class.getName()
1701: + "' has thrown an exception.", bean_class, e);
1702: }
1703: }
1704: }
1705:
1706: static class Accessors extends EnumClass<String> {
1707: Accessors(String identifier) {
1708: super (identifier);
1709: }
1710:
1711: public static Accessors getMethod(String name) {
1712: return getMember(Accessors.class, name);
1713: }
1714: }
1715: }
|