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

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

Introduction

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

Prototype

public Object convert(Class type, Object value);

Source Link

Document

Convert the specified input object into an output object of the specified type.

Usage

From source file:com.comcast.cereal.impl.ReflectionHelper.java

/**
 * Convert the given object to be castable to the given <code>goal</code> type.
 * //  ww  w.ja v a 2  s  . c om
 * @param obj
 *            the object to convert
 * @param goal
 *            the goal type
 * 
 * @return the converted object
 */
public static Object convert(Object obj, Class<?> goal) {
    if (null == obj) {
        return obj;
    } else {
        if (goal.isInstance(obj)) {
            return obj;
        } else if (asPrimitive(goal).equals(asPrimitive(obj.getClass()))) {
            return obj;
        } else {
            Converter converter = ConvertUtils.lookup(goal);
            return (null != converter) ? converter.convert(goal, obj) : obj;
        }
    }
}

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

public static RowMapperFactory getScalarMapper(final Type itemType, final int idx, boolean req) {
    if (itemType == String.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getString(idx);
            }/*from   www  . ja va 2  s .com*/
        };
    }
    if (itemType == Integer.class || itemType == Integer.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getInt(idx);
            }
        };
    }
    if (itemType == URL.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getURL(idx);
            }
        };
    }
    if (itemType == BigInteger.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                final BigDecimal res = rs.getBigDecimal(idx);
                return res == null ? null : res.toBigInteger();
            }
        };
    }
    if (itemType == BigDecimal.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBigDecimal(idx);
            }
        };
    }
    if (itemType == InputStream.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBinaryStream(idx);
            }
        };
    }
    if (itemType == Boolean.class || itemType == Boolean.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBoolean(idx);
            }
        };
    }
    if (itemType == Blob.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBlob(idx);
            }
        };
    }
    if (itemType == java.sql.Date.class || itemType == java.util.Date.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getTimestamp(idx);
            }
        };
    }
    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, idx);
                }
            };
        }
        final Converter converter = ConvertUtils.lookup(itemClass);
        if (converter != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(idx);
                    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(idx);
                    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: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);
            }/*from  w ww  . j a va  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   w w w  .  j av  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:com.subakva.formicid.options.CopyFileSetHandler.java

public void handleOption(Task task, String optionName, Object value) {
    Converter converter = this.container.getConverter(FileSet.class);
    FileSet fileset = (FileSet) converter.convert(FileSet.class, value);
    if (task instanceof Copy) {
        Copy copy = ((Copy) task);//from w  w  w .  ja v  a  2 s .c  o m
        copy.addFileset(fileset);
    } else {
        throw new RuntimeException("Invalid task type: " + task);
    }
}

From source file:com.subakva.formicid.options.JarManifestHandler.java

public void handleOption(Task task, String optionName, Object value) {
    Converter converter = this.container.getConverter(Manifest.class);
    Manifest manifest = (Manifest) converter.convert(Manifest.class, value);
    if (task instanceof Jar) {
        try {//from  www. ja va  2 s.c  o m
            Jar jar = ((Jar) task);
            jar.addConfiguredManifest(manifest);
        } catch (ManifestException e) {
            throw new RuntimeException(e);
        }
    } else {
        throw new RuntimeException("Invalid task type: " + task);
    }
}

From source file:com.subakva.formicid.options.ParameterHandler.java

public void handleOption(Task task, String optionName, Object value) {
    HashMap<String, PropertyDescriptor> properties = getWritableProperties(task.getClass());
    PropertyDescriptor descriptor = properties.get(optionName.toLowerCase());
    if (descriptor == null) {
        throw new RuntimeException("Unknown property for " + task.getTaskType() + " task: " + optionName);
    }/*  w w w  .  j  a  va  2 s  .  c  o  m*/
    Class<?> type = descriptor.getPropertyType();
    Converter converter = container.getConverter(type);
    Object converted = converter.convert(type, value);
    try {
        task.log("converting property: " + descriptor.getName(), Project.MSG_DEBUG);
        task.log("converter: " + converter, Project.MSG_DEBUG);
        task.log("converted: " + converted, Project.MSG_DEBUG);

        descriptor.getWriteMethod().invoke(task, new Object[] { converted });
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.subakva.formicid.converters.FileSetConverter.java

public FileSet convert(Class type, Object value) {
    if (value == null || value.equals(Undefined.instance)) {
        throw new ConversionException("No value specified");
    }/* w ww  .ja  v a 2s  . c  o  m*/
    if (value instanceof FileSet) {
        return (FileSet) value;
    } else if (value instanceof Scriptable) {
        FileSet fileSet = (FileSet) container.getProject().createDataType("fileset");
        Scriptable options = (Scriptable) value;
        Object[] ids = options.getIds();
        for (int i = 0; i < ids.length; i++) {
            String id = (String) ids[i];
            Object val = options.get(id, options);
            try {
                PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(fileSet, id);
                Class<?> propertyType = descriptor.getPropertyType();
                Converter converter = container.getConverter(propertyType);
                Object converted = converter.convert(propertyType, val);
                PropertyUtils.setProperty(fileSet, id, converted);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        return fileSet;
    } else if (value instanceof String) {
        FileSet fileSet = (FileSet) container.getProject().createDataType("fileset");
        fileSet.setDir(new File("."));
        FilenameSelector selector = new FilenameSelector();
        selector.setName((String) value);
        fileSet.add(selector);
        return fileSet;
    } else {
        throw new ConversionException("" + value);
    }
}

From source file:com.opencsv.bean.customconverter.ConvertGermanToBoolean.java

/**
 * Converts German text into a Boolean.//  ww  w. ja v  a  2s .  com
 * The comparisons are case-insensitive. The recognized pairs are
 * wahr/falsch, w/f, ja/nein, j/n, 1/0.
 *
 * @param value String that should represent a Boolean
 * @return Boolean
 * @throws CsvDataTypeMismatchException   If anything other than the
 *                                        explicitly translated pairs is found
 * @throws CsvRequiredFieldEmptyException Is not thrown by this
 *                                        implementation. See {@link com.opencsv.bean.customconverter.ConvertGermanToBooleanRequired}.
 */
@Override
protected Object convert(String value) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
    if (StringUtils.isEmpty(value)) {
        return null;
    }
    String[] trueStrings = { WAHR, "ja", "j", "1", "w" };
    String[] falseStrings = { FALSCH, "nein", "n", "0", "f" };
    Converter bc = new BooleanConverter(trueStrings, falseStrings);
    try {
        return bc.convert(Boolean.class, value.trim());
    } catch (ConversionException e) {
        CsvDataTypeMismatchException csve = new CsvDataTypeMismatchException(value, field.getType(),
                "Eingabe war kein boolischer Wert.");
        csve.initCause(e);
        throw csve;
    }
}

From source file:com.github.dozermapper.core.converters.PrimitiveOrWrapperConverter.java

public Object convert(Object srcFieldValue, Class destFieldClass, DateFormatContainer dateFormatContainer,
        String destFieldName, Object destObj) {
    if (srcFieldValue == null || destFieldClass == null
            || (srcFieldValue.equals("") && !destFieldClass.equals(String.class))) {
        return null;
    }//from w w  w . j a va2s . com
    Converter converter = getPrimitiveOrWrapperConverter(destFieldClass, dateFormatContainer, destFieldName,
            destObj);
    try {
        return converter.convert(destFieldClass, unwrapSrcFieldValue(srcFieldValue));
    } catch (org.apache.commons.beanutils.ConversionException e) {
        throw new com.github.dozermapper.core.converters.ConversionException(e);
    }
}