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

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

Introduction

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

Prototype

public static Converter lookup(Class clazz) 

Source Link

Document

Look up and return any registered Converter for the specified destination class; if there is no registered Converter, return null.

For more details see ConvertUtilsBean.

Usage

From source file:es.caib.zkib.jxpath.util.BasicTypeConverter.java

/**
 * Converts the supplied object to the specified
 * type. Throws a runtime exception if the conversion is
 * not possible./*from  w  w  w.ja v a  2  s  .  c o  m*/
 * @param object to convert
 * @param toType destination class
 * @return converted object
 */
public Object convert(Object object, final Class toType) {
    if (object == null) {
        return toType.isPrimitive() ? convertNullToPrimitive(toType) : null;
    }

    if (toType == Object.class) {
        if (object instanceof NodeSet) {
            return convert(((NodeSet) object).getValues(), toType);
        }
        if (object instanceof Pointer) {
            return convert(((Pointer) object).getValue(), toType);
        }
        return object;
    }
    final Class useType = TypeUtils.wrapPrimitive(toType);
    Class fromType = object.getClass();

    if (useType.isAssignableFrom(fromType)) {
        return object;
    }

    if (fromType.isArray()) {
        int length = Array.getLength(object);
        if (useType.isArray()) {
            Class cType = useType.getComponentType();

            Object array = Array.newInstance(cType, length);
            for (int i = 0; i < length; i++) {
                Object value = Array.get(object, i);
                Array.set(array, i, convert(value, cType));
            }
            return array;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            Collection collection = allocateCollection(useType);
            for (int i = 0; i < length; i++) {
                collection.add(Array.get(object, i));
            }
            return unmodifiableCollection(collection);
        }
        if (length > 0) {
            Object value = Array.get(object, 0);
            return convert(value, useType);
        }
        return convert("", useType);
    }
    if (object instanceof Collection) {
        int length = ((Collection) object).size();
        if (useType.isArray()) {
            Class cType = useType.getComponentType();
            Object array = Array.newInstance(cType, length);
            Iterator it = ((Collection) object).iterator();
            for (int i = 0; i < length; i++) {
                Object value = it.next();
                Array.set(array, i, convert(value, cType));
            }
            return array;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            Collection collection = allocateCollection(useType);
            collection.addAll((Collection) object);
            return unmodifiableCollection(collection);
        }
        if (length > 0) {
            Object value;
            if (object instanceof List) {
                value = ((List) object).get(0);
            } else {
                Iterator it = ((Collection) object).iterator();
                value = it.next();
            }
            return convert(value, useType);
        }
        return convert("", useType);
    }
    if (object instanceof NodeSet) {
        return convert(((NodeSet) object).getValues(), useType);
    }
    if (object instanceof Pointer) {
        return convert(((Pointer) object).getValue(), useType);
    }
    if (useType == String.class) {
        return object.toString();
    }
    if (object instanceof Boolean) {
        if (Number.class.isAssignableFrom(useType)) {
            return allocateNumber(useType, ((Boolean) object).booleanValue() ? 1 : 0);
        }
        if ("java.util.concurrent.atomic.AtomicBoolean".equals(useType.getName())) {
            try {
                return useType.getConstructor(new Class[] { boolean.class })
                        .newInstance(new Object[] { object });
            } catch (Exception e) {
                throw new JXPathTypeConversionException(useType.getName(), e);
            }
        }
    }
    if (object instanceof Number) {
        double value = ((Number) object).doubleValue();
        if (useType == Boolean.class) {
            return value == 0.0 ? Boolean.FALSE : Boolean.TRUE;
        }
        if (Number.class.isAssignableFrom(useType)) {
            return allocateNumber(useType, value);
        }
    }
    if (object instanceof String) {
        Object value = convertStringToPrimitive(object, useType);
        if (value != null || "".equals(object)) {
            return value;
        }
    }

    Converter converter = ConvertUtils.lookup(useType);
    if (converter != null) {
        return converter.convert(useType, object);
    }

    throw new JXPathTypeConversionException("Cannot convert " + object.getClass() + " to " + useType);
}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static RowMapperFactory getScalarRowMapperFactory(final Type itemType, final String name, boolean req) {
    if (itemType == String.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getString(name);
            }//  www.j av  a  2 s . c om
        };
    }
    if (itemType == Integer.class || itemType == Integer.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getInt(name);
            }
        };
    }
    if (itemType == URL.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getURL(name);
            }
        };
    }
    if (itemType == BigInteger.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                final BigDecimal res = rs.getBigDecimal(name);
                return res == null ? null : res.toBigInteger();
            }
        };
    }
    if (itemType == BigDecimal.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBigDecimal(name);
            }
        };
    }
    if (itemType == InputStream.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBinaryStream(name);
            }
        };
    }
    if (itemType == Boolean.class || itemType == Boolean.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBoolean(name);
            }
        };
    }
    if (itemType == Blob.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBlob(name);
            }
        };
    }
    if (itemType == java.sql.Date.class || itemType == java.util.Date.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getTimestamp(name);
            }
        };
    }
    if (itemType instanceof Class) {
        final Class itemClass = (Class) itemType;
        final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass);
        if (columnMapper != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    return columnMapper.map(rs, name);
                }
            };
        }
        final Converter converter = ConvertUtils.lookup(itemClass);
        if (converter != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(name);
                    if (s == null) {
                        return null;
                    }
                    return converter.convert(itemClass, s);
                }
            };
        }
        if (Enum.class.isAssignableFrom((Class<?>) itemType)) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(name);
                    if (s == null) {
                        return null;
                    }
                    //noinspection unchecked
                    return Enum.valueOf((Class<Enum>) itemType, s);
                }
            };
        }
    }
    if (req) {
        throw new IllegalArgumentException("no mapping defined for " + itemType);
    }
    return null;
}

From source file:jp.go.nict.langrid.p2pgridbasis.data.langrid.converter.ConvertUtil.java

public static void decode(DataAttributes from, Object to) throws DataConvertException {
    setLangridConverter();//from   ww w.  j a v a  2  s. c o  m
    logger.debug("##### decode #####");
    try {
        for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(to)) {
            logger.debug(
                    "Name : " + descriptor.getName() + " / PropertyType : " + descriptor.getPropertyType());
            Object value = from.getValue(descriptor.getName());
            if (PropertyUtils.isWriteable(to, descriptor.getName()) && value != null) {
                //Write OK
                try {
                    Converter converter = ConvertUtils.lookup(descriptor.getPropertyType());
                    if (!ignoreProps.contains(descriptor.getName())) {
                        if (converter == null) {
                            logger.error("no converter is registered : " + descriptor.getName() + " "
                                    + descriptor.getPropertyType());
                        } else {
                            Object obj = converter.convert(descriptor.getPropertyType(), value);
                            PropertyUtils.setProperty(to, descriptor.getName(), obj);
                        }
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                    logger.info(e.getMessage());
                } catch (NoSuchMethodException e) {
                    logger.info(e.getMessage());
                }
            } else {
                if (value != null) {
                    //Write NG
                    logger.debug("isWriteable = false");
                } else {
                    //Write NG
                    logger.debug("value = null");
                }
            }
        }
    } catch (IllegalAccessException e) {
        throw new DataConvertException(e);
    } catch (InvocationTargetException e) {
        throw new DataConvertException(e);
    }
}

From source file:jp.go.nict.langrid.p2pgridbasis.data.langrid.converter.ConvertUtil.java

static private void setLangridConverter() {
    if (ConvertUtils.lookup(Calendar.class) == null) {
        logger.debug("### ConvertUtils.register ###");
        ConvertUtils.register(converter, String.class);
        ConvertUtils.register(new CalendarConverter(), Calendar.class);
        ConvertUtils.register(new CalendarConverter(), GregorianCalendar.class);
        ConvertUtils.register(new CountryConverter(), CountryName.class);
        ConvertUtils.register(new InputStreamConverter(), InputStream.class);
        ConvertUtils.register(new BlobConverter(), Blob.class);
        ConvertUtils.register(new InstanceTypeConverter(), InstanceType.class);
        ConvertUtils.register(new LanguageConverter(), Language.class);
        ConvertUtils.register(new LanguagePathArrayConverter(), LanguagePath[].class);
        ConvertUtils.register(new LimitTypeConverter(), LimitType.class);
        //         ConvertUtils.register(new NodeTypeConverter()            , NodeType.class);
        ConvertUtils.register(new PeriodConverter(), Period.class);
        ConvertUtils.register(new ResourceTypeConverter(), OldResourceType.class);
        ConvertUtils.register(new ServiceContainerTypeConverter(), ServiceContainerType.class);
        ConvertUtils.register(new ServiceTypeConverter(), OldServiceType.class);
        ConvertUtils.register(new URIConverter(), URI.class);
        return;/*from   w  ww  .  j a  v  a 2  s .c  o m*/
    }
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Convert internal./*from   w w w.j  a va 2 s.co  m*/
 *
 * @param value the value
 * @param type the type
 * @return the object
 */
protected static Object convertInternal(Object value, Class<?> type) {

    Converter converter = ConvertUtils.lookup(type);
    if (converter != null) {
        return converter.convert(type, value);
    } else {
        return value;
    }
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to boolean, if given value is not null.
 * @param value the value to convert.//from ww w . j  a va  2s  .co  m
 * @return the converted value.
 */
@Nullable
public Boolean toBooleanIfNotNull(@Nullable final String value) {
    Boolean result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Boolean.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Boolean.TYPE, value);

        if (t_Result instanceof Boolean) {
            result = (Boolean) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to int if given value is not null.
 * @param value the value to convert.//from w  w w  . j  av a  2  s .co  m
 * @return the converted value.
 */
@Nullable
public Integer toIntIfNotNull(@Nullable final String value) {
    Integer result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Integer.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Integer.TYPE, value);

        if (t_Result instanceof Integer) {
            result = (Integer) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to long if given value is not null.
 * @param value the value to convert./*from  ww w.j a va 2 s  . c  o  m*/
 * @return the converted value.
 */
@Nullable
public Long toLongIfNotNull(@Nullable final String value) {
    Long result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Long.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Long.TYPE, value);

        if (t_Result instanceof Long) {
            result = (Long) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to float, if given value is not null.
 * @param value the value to convert.//from  w  w w  .j a va 2s . c  o m
 * @return the converted value.
 */
@Nullable
public Float toFloatIfNotNull(@Nullable final String value) {
    Float result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Float.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Float.TYPE, value);

        if (t_Result instanceof Float) {
            result = (Float) t_Result;
        }
    }

    return result;
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to double, if given value is not null.
 * @param value the value to convert./*from w ww  .j a  va  2 s .co  m*/
 * @return the converted value.
 */
@Nullable
public Double toDoubleIfNotNull(@Nullable final String value) {
    Double result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Double.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Double.TYPE, value);

        if (t_Result instanceof Double) {
            result = (Double) t_Result;
        }
    }

    return result;
}