List of usage examples for org.apache.commons.beanutils Converter convert
public Object convert(Class type, Object value);
From source file:org.onebusaway.nyc.presentation.impl.NycConfigurationServiceImpl.java
private ConfigurationBean loadSettings() { if (_path == null || !_path.exists()) return new ConfigurationBean(); try {//www .ja v a2 s. c o 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
@SuppressWarnings("unchecked") @Override//from w w w . j a v a 2s . c o m public <T> T convert(Object value, Class<T> resultClass) { if (value == null) { return null; } else if (resultClass.isAssignableFrom(valueClass)) { return resultClass.cast(convert(value)); } else if (resultClass.isInstance(value)) { return resultClass.cast(value); } else if (resultClass.isAssignableFrom(String.class)) { if (valueClass.isInstance(value)) { return resultClass.cast(toStringPersistence(valueClass.cast(value))); } return resultClass.cast(value.toString()); } else if (Enum.class.isAssignableFrom(resultClass)) { Class<? extends Enum> enumClass = resultClass.asSubclass(Enum.class); return (T) Enum.valueOf(enumClass, value.toString()); } else { Converter apacheConverter = ConvertUtils.lookup(value.getClass(), resultClass); if (apacheConverter != null) { return resultClass.cast(apacheConverter.convert(resultClass, value)); } } throw createConversionError(value, resultClass); }
From source file:org.openspotlight.common.util.Conversion.java
/** * Returns a new converted type based on target type parameter. * /*from www . jav a2 s .c om*/ * @param <E> * @param rawValue * @param targetType * @return a new value from a converted type * @throws SLException */ @SuppressWarnings("unchecked") public static <E> E convert(final Object rawValue, final Class<E> targetType) { Assertions.checkNotNull("targetType", targetType); //$NON-NLS-1$ Assertions.checkCondition("validTargetType:" + targetType.getName(), //$NON-NLS-1$ Conversion.CONVERTERS.containsKey(targetType) || targetType.isEnum()); if (rawValue == null) { return null; } try { if (targetType.isEnum()) { final String rawValueAsString = rawValue.toString(); final Field[] flds = targetType.getDeclaredFields(); for (final Field f : flds) { if (f.isEnumConstant()) { if (f.getName().equals(rawValueAsString)) { final Object value = f.get(null); return (E) value; } } } throw new IllegalStateException(MessageFormat.format("Invalid enum constant:{0} for type {1}", rawValueAsString, targetType)); } final Converter converter = Conversion.CONVERTERS.get(targetType); final E converted = (E) converter.convert(targetType, rawValue); return converted; } catch (final Exception e) { throw Exceptions.logAndReturnNew(e, SLRuntimeException.class); } }
From source file:org.openvpms.component.system.common.jxpath.OpenVPMSTypeConverter.java
@SuppressWarnings("unchecked") @Override/* w w w . j ava 2 s. c o m*/ 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 .ja v a 2 s . co 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.tros.utils.converters.ClassConverterTest.java
/** * Test of convert method, of class ClassConverter. */// w ww . j a va 2s .c om @Test public void testConvert() { LOGGER.info("class convert test"); Converter lookup = UtilsBeanFactory.getConverter(String.class, Class.class); String hex = ClassConverter.class.getName(); Class convert = lookup.convert(Class.class, hex); assertNotNull(convert); assertEquals(hex, convert.getName()); Class fromString = (Class) TypeHandler.fromString(Class.class, hex); assertNotNull(fromString); assertEquals(hex, fromString.getName()); }
From source file:org.tros.utils.converters.ColorConverterTest.java
/** * Test of convert method, of class ColorConverter. *//*from www . j a v a2s . c o m*/ @Test public void testConvert() { LOGGER.info("color convert test"); Converter lookup = UtilsBeanFactory.getConverter(String.class, Color.class); assertEquals("blue", lookup.convert(String.class, "blue")); String hex = "0x0dff00"; Color convert = lookup.convert(Color.class, hex); Assert.assertNotNull(convert); convert = lookup.convert(Color.class, "blue"); Assert.assertNotNull(convert); convert = TypeHandler.fromString(Color.class, "0x0dff00"); Assert.assertNotNull(convert); convert = TypeHandler.fromString(Color.class, "blue"); Assert.assertNotNull(convert); String redHex = TypeHandler.colorToHex(Color.red); Assert.assertNotNull(redHex); Assert.assertEquals("#ff0000", redHex); redHex = TypeHandler.toString(Color.red); Assert.assertNotNull(redHex); Assert.assertEquals("#ff0000", redHex); }
From source file:org.tros.utils.converters.DateConverterTest.java
/** * Test of convert method, of class DateConverter. *//*from www . java2s .c om*/ @Test public void testConvert() { LOGGER.info("date convert test"); Calendar d = Calendar.getInstance(); d.set(Calendar.MILLISECOND, 0); String hex = TypeHandler.dateToString(d); Converter lookup = UtilsBeanFactory.getConverter(Calendar.class, String.class); Calendar convert = lookup.convert(Calendar.class, hex); String hex2 = TypeHandler.dateToString(convert); assertEquals(hex, hex2); assertEquals(d, convert); lookup = UtilsBeanFactory.getConverter(Date.class, String.class); Date convert2 = lookup.convert(Date.class, hex); String hex3 = TypeHandler.dateToString(convert2); lookup = UtilsBeanFactory.getConverter(String.class, Date.class); String convert3 = lookup.convert(String.class, convert2); assertEquals(convert3, hex); lookup = UtilsBeanFactory.getConverter(String.class, Calendar.class); String convert4 = lookup.convert(String.class, convert); assertEquals(convert4, hex2); assertNull(lookup.convert(String.class, null)); assertEquals(lookup.convert(DateConverterTest.class, "demo"), "demo"); assertEquals(hex, hex3); }
From source file:org.tros.utils.TypeHandler.java
/** * Convert an object to a specified type. * * @param <T>//from w ww .j av a 2s. co m * @param to * @param val * @return */ public static <T> T convert(Class<T> to, Object val) { Converter lookup = UtilsBeanFactory.getConverter(val.getClass(), to); if (lookup != null) { return lookup.convert(to, val); } return null; }
From source file:org.webguitoolkit.ui.util.export.PDFTableExport.java
protected void addObjectCell(Font bodyfont, PdfPTable resulttable, DataBag dbag, TableColumn tableColumn, IColumnRenderer renderer, Converter converter) { PdfPCell cell;//from w w w .ja v a 2s . co m Object obj = null; if (renderer != null && renderer instanceof ImageColumnRenderer) { // load image title, if available obj = dbag.get(tableColumn.getProperty() + ".title"); } else if (renderer != null && renderer instanceof CollectionToStringRenderer) { String objString = ""; try { ArrayList<String> listOfStrings = (ArrayList<String>) dbag.get(tableColumn.getProperty()); for (String string : listOfStrings) { objString += string + ", "; } } catch (ClassCastException e) { // ignore } // remove comma and blank from end of the string objString = StringUtils.removeEnd(objString, ", "); obj = objString; } else if (renderer != null && renderer instanceof PlainHtmlColumnRenderer) { obj = dbag.get(tableColumn.getProperty() + ".title"); } if (obj == null) obj = dbag.get(tableColumn.getProperty()); String cellData = ""; if (obj != null) { cellData = obj.toString(); // fetch converter from table and use it to format the date/timestamp value if (converter != null) { if (obj instanceof Timestamp) { // Timestamp time = (Timestamp)obj; cellData = (String) converter.convert(String.class, obj); } else if (obj instanceof Date) { // Date date = (Date) obj; cellData = (String) converter.convert(String.class, obj); } else if (obj instanceof java.sql.Date) { // java.sql.Date date = (java.sql.Date) obj; cellData = (String) converter.convert(String.class, obj); } else if (obj instanceof String) { cellData = obj.toString(); } else { cellData = obj.toString(); } } if (cellData.contains("<br/>") || cellData.contains("<br>")) { cellData = cellData.replace("<br\\/>", "\\/"); cellData = cellData.replace("<br>", "\\/"); } // cell = new PdfPCell(new Paragraph((cellData),headfont)); cell = new PdfPCell(new Paragraph((cellData), bodyfont)); } else { cell = new PdfPCell(new Phrase("")); } resulttable.addCell(cell); }