Example usage for org.apache.commons.beanutils ConvertUtilsBean ConvertUtilsBean

List of usage examples for org.apache.commons.beanutils ConvertUtilsBean ConvertUtilsBean

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ConvertUtilsBean ConvertUtilsBean.

Prototype

public ConvertUtilsBean() 

Source Link

Document

Construct a bean with standard converters registered

Usage

From source file:org.mule.modules.clarizen.DefaultClarizenClient.java

public DefaultClarizenClient(ClarizenServiceProvider provider) {
    helper = new ClarizenClientHelper();
    serviceProvider = provider;//from w w w  . j av a  2 s . c  o m
    convertUtilsBean = new ConvertUtilsBean();
    clarizenDateConverter = new ClarizenDateConverter();
    clarizenDateConverter.setPattern(DEFAULT_DATE_CONVERTER_PATTERN);
    convertUtilsBean.register(clarizenDateConverter, java.util.Date.class);
    beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
    serviceAddress = provider.getServiceAddress();
}

From source file:org.opencms.workplace.CmsWidgetDialogParameter.java

/**
 * "Commits" (writes) the value of this widget back to the underlying base object.<p> 
 * /*w w  w.j a  v a  2  s.c  om*/
 * @param dialog the widget dialog where the parameter is used on
 * 
 * @throws CmsException in case the String value of the widget is invalid for the base Object
 */
@SuppressWarnings("unchecked")
public void commitValue(CmsWidgetDialog dialog) throws CmsException {

    if (m_baseCollection == null) {
        PropertyUtilsBean bean = new PropertyUtilsBean();
        ConvertUtilsBean converter = new ConvertUtilsBean();
        Object value = null;
        try {
            Class<?> type = bean.getPropertyType(m_baseObject, m_baseObjectProperty);
            value = converter.convert(m_value, type);
            bean.setNestedProperty(m_baseObject, m_baseObjectProperty, value);
            setError(null);
        } catch (InvocationTargetException e) {
            setError(e.getTargetException());
            throw new CmsWidgetException(Messages.get().container(Messages.ERR_PROPERTY_WRITE_3, value,
                    dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()),
                    m_baseObject.getClass().getName()), e.getTargetException(), this);
        } catch (Exception e) {
            setError(e);
            throw new CmsWidgetException(Messages.get().container(Messages.ERR_PROPERTY_WRITE_3, value,
                    dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()),
                    m_baseObject.getClass().getName()), e, this);
        }
    } else if (m_baseCollection instanceof SortedMap) {
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) {
            int pos = m_value.indexOf('=');
            if ((pos > 0) && (pos < (m_value.length() - 1))) {
                String key = m_value.substring(0, pos);
                String value = m_value.substring(pos + 1);
                @SuppressWarnings("rawtypes")
                SortedMap map = (SortedMap) m_baseCollection;
                if (map.containsKey(key)) {
                    Object val = map.get(key);
                    CmsWidgetException error = new CmsWidgetException(
                            Messages.get().container(Messages.ERR_MAP_DUPLICATE_KEY_3,
                                    dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), key, val),
                            this);
                    setError(error);
                    throw error;
                }
                map.put(key, value);
            } else {
                CmsWidgetException error = new CmsWidgetException(
                        Messages.get().container(Messages.ERR_MAP_PARAMETER_FORM_1,
                                dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey())),
                        this);
                setError(error);
                throw error;
            }
        }
    } else if (m_baseCollection instanceof List) {
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) {
            @SuppressWarnings("rawtypes")
            List list = (List) m_baseCollection;
            list.add(m_value);
        }
    }
}

From source file:org.pentaho.platform.util.beans.BeanUtil.java

/**
 * Setup a new bean util for operating on the given bean
 * //ww  w  .  ja  v a2 s .c o m
 * @param targetBean
 *          the bean on which to operate
 */
public BeanUtil(final Object targetBean) {
    this.bean = targetBean;
    //
    // Configure a bean util that throws exceptions during type conversion
    //
    ConvertUtilsBean convertUtil = new ConvertUtilsBean();
    convertUtil.register(true, true, 0);

    typeConvertingBeanUtil = new BeanUtilsBean(convertUtil);
    setDefaultCallback(new EagerFailingCallback());
}

From source file:org.seasar.struts.bean.FixPageBeanUtilsBeanTest.java

protected void setUp() throws Exception {
    super.setUp();

    beanUtilsBean = new FixPageBeanUtilsBean(new ConvertUtilsBean(), new SuppressPropertyUtilsBean());

    {//  www . ja va 2s .  c o m
        FormBeanConfig config = new FormBeanConfig();
        config.setType(ValidatorForm.class.getName());
        validatorForm = (ValidatorForm) config.createActionForm(new ActionServlet());
    }

    {
        FormBeanConfig config = new FormBeanConfig();
        config.setType(MyForm.class.getName());
        beanValidatorForm = (BeanValidatorForm) config.createActionForm(new ActionServlet());
    }

    {
        FormBeanConfig config = new FormBeanConfig();
        config.setType(DynaValidatorForm.class.getName());
        config.addFormPropertyConfig(new FormPropertyConfig("page", int.class.getName(), "0"));
        dynaValidatorForm = (DynaValidatorForm) config.createActionForm(new ActionServlet());
    }
}

From source file:org.seasar.struts.bean.SuppressPropertyUtilsBeanTest.java

public void testBeanUtilsBean_suppressed_populate() throws Exception {
    List classes = new ArrayList();
    classes.add(FullName.class);
    BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), new SuppressPropertyUtilsBean(classes));
    Person person = new Person();
    Map properties = new HashMap();
    properties.put("fullName.firstName", "aaa");
    properties.put("fullName.lastName", "bbb");
    properties.put("address.street", "ccc");
    properties.put("age", "20");
    beanUtils.populate(person, properties);
    assertNull(person.getFullName().getFirstName());
    assertNull(person.getFullName().getLastName());
    assertEquals("ccc", person.getAddress().getStreet());
    assertEquals("20", person.getAge());
}

From source file:org.seasar.struts.filter.S2StrutsFilter.java

public void init(FilterConfig config) throws ServletException {
    BeanUtilsBean beanUtilsBean = new FixPageBeanUtilsBean(new ConvertUtilsBean(),
            new SuppressPropertyUtilsBean());
    BeanUtilsBean.setInstance(beanUtilsBean);
}

From source file:org.xmlactions.common.reflection.BeanManager.java

/**
 * Sets a property on a bean//from   www.j ava  2  s  .c  o  m
 * @param bean
 * @param propertyName
 * @param value
 */
public void setPropertyValue(Object bean, String propertyName, Object value) {
    Method setMethod = findMatchingSetMethod(bean, propertyName);
    try {
        ConvertUtilsBean cub = new ConvertUtilsBean();
        Object data = cub.convert(value, setMethod.getParameterTypes()[0]);
        setMethod.invoke(bean, data);
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to set property [" + propertyName + "] on bean ["
                + bean.getClass().getCanonicalName() + "] with the value [" + value + "]", e);
    }

}

From source file:org.xmlactions.mapping.xml_to_bean.PopulateClassFromXml.java

private void setProperty(Object object, String propertyName, Object value, String fieldName, XMLObject xo)
        throws InvocationTargetException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException {
    PropertyDescriptor pd = null;
    try {//from w ww . jav  a2s  .c  om
        pd = findPropertyDescriptor(object, fieldName);
        if (pd != null) {
            BeanUtils.setProperty(object, fieldName, value);
        } else {
            Class<?> c = object.getClass();
            Field field = c.getDeclaredField(fieldName);
            ConvertUtilsBean cub = new ConvertUtilsBean();
            Object converted = cub.convert(value, field.getType());
            field.set(object, converted);
        }
    } catch (ConversionException ex) {
        if (ignoreErrorWithEmptyValues == true && (value == null || ("" + value).length() == 0)) {
            // carry on processing, ignore because we have an empty value
        } else {
            throw new ConversionException("Unable to set property [" + propertyName + "] on bean ["
                    + object.getClass().getName() + "] with value of [" + value + "] error:" + ex.getMessage(),
                    ex);
        }
    } catch (NoSuchFieldException ex) {
        if (strict == true) {
            throw ex;
        } else {
            // log.warn(object.getClass().getName() +
            // " has no such field [" + propertyName + "]:" +
            // ex.getMessage());
        }
    } catch (IllegalAccessException ex) {
        throw new IllegalAccessException("Unable to set property [" + propertyName + "] on bean ["
                + object.getClass().getName() + "] with value of [" + value + "] error:" + ex.getMessage());
    } catch (IllegalArgumentException ex) {
        if (pd != null) {
            try {
                // try and match up a populator.
                Class<?> obj = pd.getPropertyType();
                log.debug("obj:" + obj);
                if (obj != null) {
                    if (List.class.getName().equals(obj.getName())) {
                        useAction(PopulatorArrayList.class.getName(), null, object, fieldName, value, xo);
                    }
                }
            } catch (Exception ex_ignore) {
                // ignore this, we'll throw the original exception
                throw ex;
            }
        } else {
            throw ex;
        }
    }

}

From source file:utils.beanUtils.JIMBeanUtilsBean.java

/** 
 * <p>Constructs an instance using new property 
 * and conversion instances.</p>/*  w  w w .  j  av  a2s.co m*/
 */
protected JIMBeanUtilsBean() {
    this(new ConvertUtilsBean(), new JIMPropertyUtilsBean());
}