List of usage examples for org.apache.commons.configuration2.ex ConversionException ConversionException
public ConversionException(final String message, final Throwable cause)
From source file:net.yck.wkrdb.common.shared.PropertyConverter.java
/** * Tries to convert the specified object into a number object. This method * is used by the conversion methods for number types. Note that the return * value is not in always of the specified target class, but only if a new * object has to be created./*from w ww. j a v a 2 s. com*/ * * @param value * the value to be converted (must not be <b>null</b>) * @param targetClass * the target class of the conversion (must be derived from * {@code java.lang.Number}) * @return the converted number * @throws ConversionException * if the object cannot be converted */ static Number toNumber(Object value, Class<?> targetClass) throws ConversionException { if (value instanceof Number) { return (Number) value; } else { String str = value.toString(); if (str.startsWith(HEX_PREFIX)) { try { return new BigInteger(str.substring(HEX_PREFIX.length()), HEX_RADIX); } catch (NumberFormatException nex) { throw new ConversionException( "Could not convert " + str + " to " + targetClass.getName() + "! Invalid hex number.", nex); } } if (str.startsWith(BIN_PREFIX)) { try { return new BigInteger(str.substring(BIN_PREFIX.length()), BIN_RADIX); } catch (NumberFormatException nex) { throw new ConversionException("Could not convert " + str + " to " + targetClass.getName() + "! Invalid binary number.", nex); } } try { Constructor<?> constr = targetClass.getConstructor(CONSTR_ARGS); return (Number) constr.newInstance(str); } catch (InvocationTargetException itex) { throw new ConversionException("Could not convert " + str + " to " + targetClass.getName(), itex.getTargetException()); } catch (Exception ex) { // Treat all possible exceptions the same way throw new ConversionException( "Conversion error when trying to convert " + str + " to " + targetClass.getName(), ex); } } }
From source file:net.yck.wkrdb.common.shared.PropertyConverter.java
/** * Convert the specified object into an URL. * * @param value/*www . j a va2s .c o m*/ * the value to convert * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to an URL */ public static URL toURL(Object value) throws ConversionException { if (value instanceof URL) { return (URL) value; } else if (value instanceof String) { try { return new URL((String) value); } catch (MalformedURLException e) { throw new ConversionException("The value " + value + " can't be converted to an URL", e); } } else { throw new ConversionException("The value " + value + " can't be converted to an URL"); } }
From source file:net.yck.wkrdb.common.shared.PropertyConverter.java
/** * Convert the specified value into an internet address. * * @param value//from ww w.j av a 2s . c o m * the value to convert * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to a InetAddress * * @since 1.5 */ static InetAddress toInetAddress(Object value) throws ConversionException { if (value instanceof InetAddress) { return (InetAddress) value; } else if (value instanceof String) { try { return InetAddress.getByName((String) value); } catch (UnknownHostException e) { throw new ConversionException("The value " + value + " can't be converted to a InetAddress", e); } } else { throw new ConversionException("The value " + value + " can't be converted to a InetAddress"); } }
From source file:net.yck.wkrdb.common.shared.PropertyConverter.java
/** * Convert the specified value into an email address. * * @param value/* www. ja va 2 s. c o m*/ * the value to convert * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to an email address * * @since 1.5 */ static Object toInternetAddress(Object value) throws ConversionException { if (value.getClass().getName().equals(INTERNET_ADDRESS_CLASSNAME)) { return value; } else if (value instanceof String) { try { Constructor<?> ctor = Class.forName(INTERNET_ADDRESS_CLASSNAME).getConstructor(String.class); return ctor.newInstance(value); } catch (Exception e) { throw new ConversionException("The value " + value + " can't be converted to a InternetAddress", e); } } else { throw new ConversionException("The value " + value + " can't be converted to a InternetAddress"); } }
From source file:net.yck.wkrdb.common.shared.PropertyConverter.java
/** * Convert the specified object into a Date. * * @param value//w ww.j ava 2 s . c om * the value to convert * @param format * the DateFormat pattern to parse String values * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to a Calendar */ public static Date toDate(Object value, String format) throws ConversionException { if (value instanceof Date) { return (Date) value; } else if (value instanceof Calendar) { return ((Calendar) value).getTime(); } else if (value instanceof String) { try { return new SimpleDateFormat(format).parse((String) value); } catch (ParseException e) { throw new ConversionException("The value " + value + " can't be converted to a Date", e); } } else { throw new ConversionException("The value " + value + " can't be converted to a Date"); } }
From source file:net.yck.wkrdb.common.shared.PropertyConverter.java
/** * Convert the specified object into a Calendar. * * @param value// w w w . j a va2 s .com * the value to convert * @param format * the DateFormat pattern to parse String values * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to a Calendar */ public static Calendar toCalendar(Object value, String format) throws ConversionException { if (value instanceof Calendar) { return (Calendar) value; } else if (value instanceof Date) { Calendar calendar = Calendar.getInstance(); calendar.setTime((Date) value); return calendar; } else if (value instanceof String) { try { Calendar calendar = Calendar.getInstance(); calendar.setTime(new SimpleDateFormat(format).parse((String) value)); return calendar; } catch (ParseException e) { throw new ConversionException("The value " + value + " can't be converted to a Calendar", e); } } else { throw new ConversionException("The value " + value + " can't be converted to a Calendar"); } }