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);
}
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);
}
}
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);
}
Aggregations