Example usage for org.apache.commons.beanutils ConvertUtils convert

List of usage examples for org.apache.commons.beanutils ConvertUtils convert

Introduction

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

Prototype

public static Object convert(String values[], Class clazz) 

Source Link

Document

Convert an array of specified values to an array of objects of the specified class (if possible).

For more details see ConvertUtilsBean.

Usage

From source file:org.projectforge.excel.ExcelImport.java

/**
 * convert the cell-value to the type in the bean.
 * /*from w ww .  j  a  v  a  2 s.co m*/
 * @param cell the cell containing an arbitrary value
 * @param destClazz the target class
 * @return a String, Boolean, Date or BigDecimal
 */
private Object toNativeType(final HSSFCell cell, final Class<?> destClazz) {
    if (cell == null) {
        return null;
    }
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        log.debug("using numeric");
        if (Date.class.isAssignableFrom(destClazz)) {
            return cell.getDateCellValue();
        }
        String strVal = String.valueOf(cell.getNumericCellValue());
        strVal = strVal.replaceAll("\\.0*$", "");
        return ConvertUtils.convert(strVal, destClazz);
    case HSSFCell.CELL_TYPE_BOOLEAN:
        log.debug("using boolean");
        return Boolean.valueOf(cell.getBooleanCellValue());
    case HSSFCell.CELL_TYPE_STRING:
        log.debug("using string");
        strVal = StringUtils.trimToNull(cell.getStringCellValue());
        return ConvertUtils.convert(strVal, destClazz);
    case HSSFCell.CELL_TYPE_BLANK:
        return null;
    case HSSFCell.CELL_TYPE_FORMULA:
        return new Formula(cell.getCellFormula());
    default:
        return StringUtils.trimToNull(cell.getStringCellValue());
    }
}

From source file:org.sylvani.bot.dialogs.Question.java

@SuppressWarnings({ "rawtypes", "unchecked" })
protected T convert(String answerText) {
    Class clazz = ((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
    return (T) ConvertUtils.convert(answerText, clazz);
}

From source file:org.vulpe.model.entity.types.EntityType.java

public Object nullSafeGet(final ResultSet resultSet, final String[] names, final Object owner) {
    try {/* w w w .j  av  a 2s.co  m*/
        final String value = resultSet.getString(names[0]);
        if (value == null) {
            return null;
        }

        final Object identifier = ConvertUtils.convert(value, this.idClass);
        return invokeFind(identifier);
    } catch (Exception e) {
        throw new VulpeSystemException(e);
    }
}

From source file:org.wso2.carbon.mss.internal.router.ParamConvertUtils.java

/**
 * Creates a converter function that converts a path segment into the given result type.
 * Current implementation doesn't follow the {@link javax.ws.rs.PathParam} specification to maintain backward
 * compatibility./*from   w w w .  j a v a 2s  .co  m*/
 *
 * @param resultType Result type
 * @return Function the function
 */
public static Function<String, Object> createPathParamConverter(final Type resultType) {
    if (!(resultType instanceof Class)) {
        throw new IllegalArgumentException("Unsupported @PathParam type " + resultType);
    }
    return new Function<String, Object>() {
        @Override
        public Object apply(String value) {
            return ConvertUtils.convert(value, (Class<?>) resultType);
        }
    };
}

From source file:org.wso2.msf4j.internal.router.ParamConvertUtils.java

/**
 * Creates a converter function that converts a path segment into the given result type.
 * Current implementation doesn't follow the {@link javax.ws.rs.PathParam} specification to maintain backward
 * compatibility./*from   w ww.j a va 2s  .  c om*/
 *
 * @param resultType Result type
 * @return Function the function
 */
public static Function<String, Object> createPathParamConverter(final Type resultType) {
    if (!(resultType instanceof Class)) {
        throw new IllegalArgumentException("Unsupported @PathParam type " + resultType);
    }
    return value -> ConvertUtils.convert(value, (Class<?>) resultType);
}

From source file:org.wso2.msf4j.internal.router.ParamConvertUtils.java

/**
 * Creates a converter function that converts cookie value into an object of the given result type.
 * It follows the supported types of {@link javax.ws.rs.CookieParam} with the following exceptions:
 * <ol>/*  ww  w .  j a v  a2s. co m*/
 * <li>Does not support types registered with {@link javax.ws.rs.ext.ParamConverterProvider}</li>
 * </ol>
 *
 * @param resultType Result type
 * @return Function the function
 */
public static Function<String, Object> createCookieParamConverter(Type resultType) {
    return value -> ConvertUtils.convert(value, (Class<?>) resultType);
}

From source file:org.xmlactions.action.actions.ActionUtils.java

public static String evaluateCalculation(IExecContext execContext, String exp) throws BSFException {
    String script = null;/*  w  ww  . j  a v  a2 s .c  o  m*/
    script = convertExpressionAmps(StrSubstitutor.replace(exp, execContext));
    // log.debug("expression:" + exp + " script:" + script);
    Object result = Scripting.getInstance().evaluate(script);
    return ConvertUtils.convert(result, Integer.class).toString();
}

From source file:org.xmlactions.pager.actions.Param.java

public Object getResolvedValue(IExecContext execContext) {
    Object obj;//  w  w  w . ja va2  s.  c o m

    obj = execContext.get(getValue());
    if (obj == null) {
        obj = StrSubstitutor.replace(getValue(), execContext);
    }
    if (obj == null) {
        obj = getValue();
    }
    if (getType() != null && !TypeOption._String.type.equals(getType())) {
        // need to convert
        TypeOption typeOption = TypeOption._String.getTypeOption(getType());
        if (typeOption == null) {
            throw new IllegalArgumentException("Invalid type [" + getType()
                    + "] for param.   Refer to schema 'param_converter_types' for a list of options.");
        }
        // now double check that the obj class is not the same as the converter class
        if (obj.getClass() != typeOption.getClazz()) {
            // must convert
            obj = ConvertUtils.convert(obj, typeOption.getClazz());
        }
    }
    return obj;
}

From source file:org.xwiki.configuration.internal.MemoryConfigurationSource.java

@SuppressWarnings("unchecked")
@Override/*from  w  ww . j  av a2s.  co m*/
public <T> T getProperty(String key, T defaultValue) {
    T result;

    if (this.properties.containsKey(key)) {
        Object value = this.properties.get(key);
        if (value != null && defaultValue != null && !defaultValue.getClass().isInstance(value)) {
            value = ConvertUtils.convert(value, defaultValue.getClass());
        }
        result = (T) value;
    } else {
        result = defaultValue;
    }

    return result;
}

From source file:org.xwiki.configuration.internal.MemoryConfigurationSource.java

@Override
public <T> T getProperty(String key, Class<T> valueClass) {
    T result;/* ww  w  .  ja v a2 s  .  c o  m*/

    if (this.properties.containsKey(key)) {
        Object value = this.properties.get(key);
        if (value != null && valueClass != null && !valueClass.isInstance(value)) {
            value = ConvertUtils.convert(value, valueClass);
        }
        result = (T) value;
    } else {
        result = getDefault(valueClass);
    }

    return result;
}