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:org.onebusaway.gtfs_transformer.deferred.DeferredValueSupportTest.java

@Test
public void testResolveConverter() {
    Converter converter = _support.resolveConverter(Object.class, "xyz", String.class);
    assertSame(ConvertUtils.lookup(String.class), converter);
}

From source file:org.onebusaway.gtfs_transformer.impl.DeferredValueSupport.java

public Converter resolveConverter(Class<?> parentEntityType, String propertyName, Class<?> expectedValueType) {
    SingleFieldMapping mapping = _schemaCache.getFieldMappingForCsvFieldName(parentEntityType, propertyName);
    if (mapping == null) {
        mapping = _schemaCache.getFieldMappingForObjectFieldName(parentEntityType, propertyName);
    }/*from   w w  w .ja v  a 2  s .co m*/
    if (mapping != null) {
        if (mapping instanceof ConverterFactory) {
            ConverterFactory factory = (ConverterFactory) mapping;
            return factory.create(_reader.getContext());
        }
        if (mapping instanceof Converter) {
            return (Converter) mapping;
        }
    }
    return ConvertUtils.lookup(expectedValueType);
}

From source file:org.onebusaway.gtfs_transformer.match.ObjectEquality.java

public static boolean objectsAreEqual(Object expected, Object actual) {
    boolean nullA = expected == null;
    boolean nullB = actual == null;

    if (nullA && nullB)
        return true;
    if (nullA ^ nullB)
        return false;

    Class<?> expectedType = expected.getClass();
    Class<?> actualType = actual.getClass();

    /**//  w  w w .ja  v a2 s  .co m
     * Implementation note: This conversion theoretically will happen over and
     * over with the same value. Is there some way to cache it?
     */
    if (!actualType.isAssignableFrom(expectedType) && expectedType == String.class) {

        Converter converter = ConvertUtils.lookup(actualType);

        if (converter != null) {
            Object converted = converter.convert(actualType, expected);
            if (converted != null)
                expected = converted;
        }
    }

    return (expected == null && actual == null) || (expected != null && expected.equals(actual));
}

From source file:org.onebusaway.nyc.presentation.impl.NycConfigurationServiceImpl.java

private ConfigurationBean loadSettings() {

    if (_path == null || !_path.exists())
        return new ConfigurationBean();

    try {//from  www  .ja v a 2  s. co  m

        Properties properties = new Properties();
        properties.load(new FileReader(_path));

        ConfigurationBean bean = new ConfigurationBean();
        BeanInfo beanInfo = Introspector.getBeanInfo(ConfigurationBean.class);

        for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
            String name = desc.getName();
            Object value = properties.getProperty(name);
            if (value != null) {
                Converter converter = ConvertUtils.lookup(desc.getPropertyType());
                value = converter.convert(desc.getPropertyType(), value);
                Method m = desc.getWriteMethod();
                m.invoke(bean, value);
            }
        }

        return bean;
    } catch (Exception ex) {
        throw new IllegalStateException("error loading configuration from properties file " + _path, ex);
    }
}

From source file:org.opensingular.form.STypeSimple.java

protected final VALUE convertUsingApache(Object value) {
    if (converter == null) {
        converter = ConvertUtils.lookup(valueClass);
        if (converter == null) {
            throw createConversionError(value);
        }//from  w w w. ja v a 2  s.  c  om
    }
    return valueClass.cast(converter.convert(valueClass, value));
}

From source file:org.openvpms.component.system.common.jxpath.OpenVPMSTypeConverter.java

@SuppressWarnings("unchecked")
@Override//from w  w w .ja va2 s  .  com
public Object convert(Object object, Class toType) {
    if (object == null) {
        if (toType.isPrimitive()) {
            return convertNullToPrimitive(toType);
        }
        return null;
    }

    if (toType == Object.class) {
        if (object instanceof NodeSet) {
            return convert(((NodeSet) object).getValues(), toType);
        } else if (object instanceof Pointer) {
            return convert(((Pointer) object).getValue(), toType);
        }
        return object;
    }

    Class fromType = object.getClass();
    if (fromType.equals(toType) || toType.isAssignableFrom(fromType)) {
        return object;
    }

    if (fromType.isArray()) {
        int length = Array.getLength(object);
        if (toType.isArray()) {
            Class cType = toType.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;
        } else if (Collection.class.isAssignableFrom(toType)) {
            Collection collection = allocateCollection(toType);
            for (int i = 0; i < length; i++) {
                collection.add(Array.get(object, i));
            }
            return unmodifiableCollection(collection);
        } else {
            if (length > 0) {
                Object value = Array.get(object, 0);
                return convert(value, toType);
            } else {
                return convert("", toType);
            }
        }
    } else if (object instanceof Collection) {
        int length = ((Collection) object).size();
        if (toType.isArray()) {
            Class cType = toType.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;
        } else if (Collection.class.isAssignableFrom(toType)) {
            Collection collection = allocateCollection(toType);
            collection.addAll((Collection) object);
            return unmodifiableCollection(collection);
        } else {
            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, toType);
            } else {
                return convert("", toType);
            }
        }
    } else if (object instanceof NodeSet) {
        return convert(((NodeSet) object).getValues(), toType);
    } else if (object instanceof Pointer) {
        return convert(((Pointer) object).getValue(), toType);
    } else if (toType == String.class) {
        return object.toString();
    } else if (object instanceof Boolean) {
        if (toType == boolean.class) {
            return object;
        }
        boolean value = ((Boolean) object).booleanValue();
        return allocateNumber(toType, value ? 1 : 0);
    } else if (object instanceof BigDecimal) {
        BigDecimal value = (BigDecimal) object;
        if (toType == boolean.class || toType == Boolean.class) {
            return value == BigDecimal.ZERO ? Boolean.FALSE : Boolean.TRUE;
        }
        if (toType.isPrimitive() || Number.class.isAssignableFrom(toType)) {
            return allocateNumber(toType, value);
        }
    } else if (object instanceof BigInteger) {
        BigInteger value = (BigInteger) object;
        if (toType == boolean.class || toType == Boolean.class) {
            return value == BigInteger.ZERO ? Boolean.FALSE : Boolean.TRUE;
        }
        if (toType.isPrimitive() || Number.class.isAssignableFrom(toType)) {
            return allocateNumber(toType, value);
        }
    } else if (object instanceof Number) {
        double value = ((Number) object).doubleValue();
        if (toType == boolean.class || toType == Boolean.class) {
            return value == 0.0 ? Boolean.FALSE : Boolean.TRUE;
        }
        if (toType.isPrimitive() || Number.class.isAssignableFrom(toType)) {
            return allocateNumber(toType, value);
        }
    } else if (object instanceof Character) {
        if (toType == char.class) {
            return object;
        }
    } else if (object instanceof String) {
        Object value = convertStringToPrimitive(object, toType);
        if (value != null) {
            return value;
        }
    }

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

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

From source file:org.seasar.mayaa.impl.util.ObjectUtil.java

/**
 * ??????????/*from  w  ww  .j  a  va 2 s.  c o m*/
 * ???????????????
 *
 * @param expectedClass ????
 * @param value ??
 * @return ?? (expectedClass?)
 */
public static Object convert(Class expectedClass, Object value) {
    if (Object.class.equals(expectedClass)
            || (value != null && expectedClass.isAssignableFrom(value.getClass()))) {
        return value;
    }
    if (String.class.equals(expectedClass)) {
        return (value != null) ? value.toString() : null;
    }
    if (Boolean.class.equals(expectedClass) || Boolean.TYPE.equals(expectedClass)) {
        if (value instanceof Boolean) {
            return value;
        }
        return Boolean.valueOf(ObjectUtil.booleanValue(value, false));
    }
    Converter converter = ConvertUtils.lookup(expectedClass);
    if (converter != null) {
        if (value != null) {
            return converter.convert(expectedClass, value);
        }
        if (expectedClass.isPrimitive() || Number.class.isAssignableFrom(expectedClass)) {
            if (BigInteger.class.isAssignableFrom(expectedClass)) {
                return BigInteger.ZERO;
            } else if (BigDecimal.class.isAssignableFrom(expectedClass)) {
                return BigDecimal.valueOf(0);
            }
            return converter.convert(expectedClass, value);
        }
    }
    return value;
}

From source file:org.xchain.framework.lifecycle.Lifecycle.java

/**
 * Sets up the default conversion objects in the bean utils.
 *//*from  w  w w.j  a  va  2s  .  co m*/
@StartStep(localName = "default-conversions")
public static void startDefaultConversions(LifecycleContext lifecycleContext) {
    oldQNameConverter = ConvertUtils.lookup(QName.class);
    ConvertUtils.register(new QNameConverter(), QName.class);
}