Example usage for org.apache.commons.configuration2.ex ConversionException ConversionException

List of usage examples for org.apache.commons.configuration2.ex ConversionException ConversionException

Introduction

In this page you can find the example usage for org.apache.commons.configuration2.ex ConversionException ConversionException.

Prototype

public ConversionException(final Throwable cause) 

Source Link

Document

Constructs a new ConversionException with specified nested Throwable .

Usage

From source file:net.yck.wkrdb.common.shared.PropertyConverter.java

/**
 * Performs a data type conversion from the specified value object to the
 * given target data class. If additional information is required for this
 * conversion, it is obtained from {@code DefaultConversionHandler.INSTANCE}
 * object. If the class is a primitive type (Integer.TYPE, Boolean.TYPE,
 * etc), the value returned will use the wrapper type (Integer.class,
 * Boolean.class, etc).//from   w ww .ja  va 2 s.com
 *
 * @param cls
 *            the target class of the converted value
 * @param value
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             if the value is not compatible with the requested type
 */
public static Object to(Class<?> cls, Object value) throws ConversionException {
    if (cls.isInstance(value)) {
        return value; // no conversion needed
    }

    if (String.class.equals(cls)) {
        return String.valueOf(value);
    }
    if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) {
        return toBoolean(value);
    } else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) {
        return toCharacter(value);
    } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) {
        if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) {
            return toInteger(value);
        } else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) {
            return toLong(value);
        } else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) {
            return toByte(value);
        } else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) {
            return toShort(value);
        } else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) {
            return toFloat(value);
        } else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) {
            return toDouble(value);
        } else if (BigInteger.class.equals(cls)) {
            return toBigInteger(value);
        } else if (BigDecimal.class.equals(cls)) {
            return toBigDecimal(value);
        }
    } else if (Date.class.equals(cls)) {
        return toDate(value, DefaultConversionHandler.INSTANCE.getDateFormat());
    } else if (Calendar.class.equals(cls)) {
        return toCalendar(value, DefaultConversionHandler.INSTANCE.getDateFormat());
    } else if (URL.class.equals(cls)) {
        return toURL(value);
    } else if (Locale.class.equals(cls)) {
        return toLocale(value);
    } else if (isEnum(cls)) {
        return convertToEnum(cls, value);
    } else if (Color.class.equals(cls)) {
        return toColor(value);
    } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) {
        return toInternetAddress(value);
    } else if (InetAddress.class.isAssignableFrom(cls)) {
        return toInetAddress(value);
    }

    throw new ConversionException("The value '" + value + "' (" + value.getClass() + ")"
            + " can't be converted to a " + cls.getName() + " object");
}

From source file:net.yck.wkrdb.common.shared.PropertyConverter.java

/**
 * Convert the specified object into a Boolean. Internally the
 * {@code org.apache.commons.lang.BooleanUtils} class from the
 * <a href="http://commons.apache.org/lang/">Commons Lang</a> project is
 * used to perform this conversion. This class accepts some more tokens for
 * the boolean value of <b>true</b>, e.g. {@code yes} and {@code on}. Please
 * refer to the documentation of this class for more details.
 *
 * @param value//from  w  ww. j  a va2  s . c om
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to a boolean
 */
public static Boolean toBoolean(Object value) throws ConversionException {
    if (value instanceof Boolean) {
        return (Boolean) value;
    } else if (value instanceof String) {
        Boolean b = BooleanUtils.toBooleanObject((String) value);
        if (b == null) {
            throw new ConversionException("The value " + value + " can't be converted to a Boolean object");
        }
        return b;
    } else {
        throw new ConversionException("The value " + value + " can't be converted to a Boolean object");
    }
}

From source file:net.yck.wkrdb.common.shared.PropertyConverter.java

/**
 * Converts the specified value object to a {@code Character}. This method
 * converts the passed in object to a string. If the string has exactly one
 * character, this character is returned as result. Otherwise, conversion
 * fails./*from w ww . ja va2s .  co m*/
 *
 * @param value
 *            the value to be converted
 * @return the resulting {@code Character} object
 * @throws ConversionException
 *             if the conversion is not possible
 */
public static Character toCharacter(Object value) throws ConversionException {
    String strValue = String.valueOf(value);
    if (strValue.length() == 1) {
        return Character.valueOf(strValue.charAt(0));
    } else {
        throw new ConversionException(
                String.format("The value '%s' cannot be converted to a Character object!", strValue));
    }
}

From source file:net.yck.wkrdb.common.shared.PropertyConverter.java

/**
 * Convert the specified object into a Locale.
 *
 * @param value// ww w. ja v a  2  s.co m
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to a Locale
 */
public static Locale toLocale(Object value) throws ConversionException {
    if (value instanceof Locale) {
        return (Locale) value;
    } else if (value instanceof String) {
        String[] elements = ((String) value).split("_");
        int size = elements.length;

        if (size >= 1 && ((elements[0]).length() == 2 || (elements[0]).length() == 0)) {
            String language = elements[0];
            String country = (size >= 2) ? elements[1] : "";
            String variant = (size >= 3) ? elements[2] : "";

            return new Locale(language, country, variant);
        } else {
            throw new ConversionException("The value " + value + " can't be converted to a Locale");
        }
    } else {
        throw new ConversionException("The value " + value + " can't be converted to a Locale");
    }
}

From source file:net.yck.wkrdb.common.shared.PropertyConverter.java

/**
 * Convert the specified object into a Color. If the value is a String, the
 * format allowed is (#)?[0-9A-F]{6}([0-9A-F]{2})?. Examples:
 * <ul>/*ww w .  ja  v a2 s.c  om*/
 * <li>FF0000 (red)</li>
 * <li>0000FFA0 (semi transparent blue)</li>
 * <li>#CCCCCC (gray)</li>
 * <li>#00FF00A0 (semi transparent green)</li>
 * </ul>
 *
 * @param value
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to a Color
 */
public static Color toColor(Object value) throws ConversionException {
    if (value instanceof Color) {
        return (Color) value;
    } else if (value instanceof String && !StringUtils.isBlank((String) value)) {
        String color = ((String) value).trim();

        int[] components = new int[3];

        // check the size of the string
        int minlength = components.length * 2;
        if (color.length() < minlength) {
            throw new ConversionException("The value " + value + " can't be converted to a Color");
        }

        // remove the leading #
        if (color.startsWith("#")) {
            color = color.substring(1);
        }

        try {
            // parse the components
            for (int i = 0; i < components.length; i++) {
                components[i] = Integer.parseInt(color.substring(2 * i, 2 * i + 2), HEX_RADIX);
            }

            // parse the transparency
            int alpha;
            if (color.length() >= minlength + 2) {
                alpha = Integer.parseInt(color.substring(minlength, minlength + 2), HEX_RADIX);
            } else {
                alpha = Color.black.getAlpha();
            }

            return new Color(components[0], components[1], components[2], alpha);
        } catch (Exception e) {
            throw new ConversionException("The value " + value + " can't be converted to a Color", e);
        }
    } else {
        throw new ConversionException("The value " + value + " can't be converted to a Color");
    }
}

From source file:net.yck.wkrdb.common.shared.PropertyConverter.java

/**
 * Convert the specified value into a Java 5 enum.
 *
 * @param value/*w w  w .  jav a2 s .  c om*/
 *            the value to convert
 * @param cls
 *            the type of the enumeration
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to an enumeration
 *
 * @since 1.5
 */
static <E extends Enum<E>> E toEnum(Object value, Class<E> cls) throws ConversionException {
    if (value.getClass().equals(cls)) {
        return cls.cast(value);
    } else if (value instanceof String) {
        try {
            return Enum.valueOf(cls, (String) value);
        } catch (Exception e) {
            throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
        }
    } else if (value instanceof Number) {
        try {
            E[] enumConstants = cls.getEnumConstants();
            return enumConstants[((Number) value).intValue()];
        } catch (Exception e) {
            throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
        }
    } else {
        throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
    }
}

From source file:org.demoiselle.internal.implementation.ConfigurationEnumValueExtractor.java

@Override
public Object getValue(String prefix, String key, Field field, Configuration configuration) throws Exception {
    String value = configuration.getString(prefix + key);

    if (value != null && !value.trim().equals("")) {
        Object enums[] = field.getType().getEnumConstants();

        for (int i = 0; i < enums.length; i++) {
            if (((Enum<?>) enums[i]).name().equals(value)) {
                return enums[i];
            }// w  w w .j a v a2  s.c o  m
        }
    } else {
        return null;
    }

    throw new ConversionException(getBundle().getString("configuration-not-conversion", value,
            field.getDeclaringClass().getCanonicalName()));
}