Search in sources :

Example 1 with TypeConversionException

use of jodd.typeconverter.TypeConversionException in project jodd by oblac.

the class ByteArrayConverter method convertValueToArray.

/**
	 * Converts non-array value to array. Detects various
	 * types and collections, iterates them to make conversion
	 * and to create target array.
 	 */
protected byte[] convertValueToArray(Object value) {
    if (value instanceof Blob) {
        Blob blob = (Blob) value;
        try {
            long length = blob.length();
            if (length > Integer.MAX_VALUE) {
                throw new TypeConversionException("Blob is too big.");
            }
            return blob.getBytes(1, (int) length);
        } catch (SQLException sex) {
            throw new TypeConversionException(value, sex);
        }
    }
    if (value instanceof File) {
        try {
            return FileUtil.readBytes((File) value);
        } catch (IOException ioex) {
            throw new TypeConversionException(value, ioex);
        }
    }
    if (value instanceof List) {
        List list = (List) value;
        byte[] target = new byte[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Object element = list.get(i);
            target[i] = convertType(element);
        }
        return target;
    }
    if (value instanceof Collection) {
        Collection collection = (Collection) value;
        byte[] target = new byte[collection.size()];
        int i = 0;
        for (Object element : collection) {
            target[i] = convertType(element);
            i++;
        }
        return target;
    }
    if (value instanceof Iterable) {
        Iterable iterable = (Iterable) value;
        ArrayList<Byte> byteArrayList = new ArrayList<>();
        for (Object element : iterable) {
            byte convertedValue = convertType(element);
            byteArrayList.add(Byte.valueOf(convertedValue));
        }
        byte[] array = new byte[byteArrayList.size()];
        for (int i = 0; i < byteArrayList.size(); i++) {
            Byte b = byteArrayList.get(i);
            array[i] = b.byteValue();
        }
        return array;
    }
    if (value instanceof CharSequence) {
        String[] strings = StringUtil.splitc(value.toString(), ArrayConverter.NUMBER_DELIMITERS);
        return convertArrayToArray(strings);
    }
    // everything else:
    return convertToSingleElementArray(value);
}
Also used : Blob(java.sql.Blob) TypeConversionException(jodd.typeconverter.TypeConversionException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with TypeConversionException

use of jodd.typeconverter.TypeConversionException in project jodd by oblac.

the class CalendarConverter method convert.

public Calendar convert(Object value) {
    if (value == null) {
        return null;
    }
    if (value instanceof Calendar) {
        return (Calendar) value;
    }
    if (value instanceof Date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime((Date) value);
        return calendar;
    }
    if (value instanceof JDateTime) {
        return ((JDateTime) value).convertToCalendar();
    }
    if (value instanceof Number) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(((Number) value).longValue());
        return calendar;
    }
    String stringValue = value.toString().trim();
    if (!StringUtil.containsOnlyDigits(stringValue)) {
        // try to parse default string format
        JDateTime jdt = new JDateTime(stringValue, JDateTime.DEFAULT_FORMAT);
        return jdt.convertToCalendar();
    }
    try {
        long milliseconds = Long.parseLong(stringValue);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliseconds);
        return calendar;
    } catch (NumberFormatException nfex) {
        throw new TypeConversionException(value, nfex);
    }
}
Also used : TypeConversionException(jodd.typeconverter.TypeConversionException) Calendar(java.util.Calendar) JDateTime(jodd.datetime.JDateTime) Date(java.util.Date)

Example 3 with TypeConversionException

use of jodd.typeconverter.TypeConversionException in project jodd by oblac.

the class FileConverter method convert.

public File convert(Object value) {
    if (value == null) {
        return null;
    }
    if (value instanceof File) {
        return (File) value;
    }
    if (addonFileConverters != null) {
        for (TypeConverter<File> addonFileConverter : addonFileConverters) {
            File file = addonFileConverter.convert(value);
            if (file != null) {
                return file;
            }
        }
    }
    /*
		if (value instanceof FileUpload) {
			FileUpload fileUpload = (FileUpload) value;

			InputStream in = null;
			try {
				in = fileUpload.getFileInputStream();
				File tempFile = FileUtil.createTempFile();
				FileUtil.writeStream(tempFile, in);
				return tempFile;
			} catch (IOException ioex) {
				throw new TypeConversionException(ioex);
			} finally {
				StreamUtil.close(in);
			}
		}
*/
    Class type = value.getClass();
    if (type == byte[].class) {
        try {
            File tempFile = FileUtil.createTempFile();
            FileUtil.writeBytes(tempFile, (byte[]) value);
            return tempFile;
        } catch (IOException ioex) {
            throw new TypeConversionException(ioex);
        }
    }
    if (type == String.class) {
        try {
            File tempFile = FileUtil.createTempFile();
            FileUtil.writeString(tempFile, value.toString());
            return tempFile;
        } catch (IOException ioex) {
            throw new TypeConversionException(ioex);
        }
    }
    throw new TypeConversionException(value);
}
Also used : TypeConversionException(jodd.typeconverter.TypeConversionException) IOException(java.io.IOException) File(java.io.File)

Aggregations

TypeConversionException (jodd.typeconverter.TypeConversionException)3 File (java.io.File)2 IOException (java.io.IOException)2 Blob (java.sql.Blob)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 JDateTime (jodd.datetime.JDateTime)1