for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
DataField datafield = field.getAnnotation(DataField.class);
if (datafield != null) {
if (obj != null) {
// Retrieve the format, pattern and precision associated to
// the type
Class type = field.getType();
String pattern = datafield.pattern();
int precision = datafield.precision();
// Create format
Format format = FormatFactory.getFormat(type, pattern, getLocale(), precision);
// Get field value
Object value = field.get(obj);
result = formatString(format, value);
// trim if enabled
if (datafield.trim()) {
result = result.trim();
}
// Get length of the field, alignment (LEFT or RIGHT), pad
int fieldLength = datafield.length();
String align = datafield.align();
char padCharField = datafield.paddingChar();
char padChar;
if (fieldLength > 0) {
StringBuilder temp = new StringBuilder();
// Check if we must pad
if (result.length() < fieldLength) {
// No padding defined for the field
if (padCharField == 0) {
// We use the padding defined for the Record
padChar = paddingChar;
} else {
padChar = padCharField;
}
if (align.contains("R")) {
temp.append(generatePaddingChars(padChar, fieldLength, result.length()));
temp.append(result);
} else if (align.contains("L")) {
temp.append(result);
temp.append(generatePaddingChars(padChar, fieldLength, result.length()));
} else {
throw new IllegalArgumentException("Alignment for the field: " + field.getName()
+ " must be equal to R for RIGHT or L for LEFT");
}
result = temp.toString();
} else if (result.length() > fieldLength) {
// we are bigger than allowed
// is clipped enabled? if so clip the field
if (datafield.clip()) {
result = result.substring(0, fieldLength);
} else {
throw new IllegalArgumentException("Length for the " + field.getName()
+ " must not be larger than allowed, was: " + result.length() + ", allowed: " + fieldLength);
}
}
} else {
throw new IllegalArgumentException("Length of the field: " + field.getName()
+ " is a mandatory field and cannot be equal to zero or to be negative, was: " + fieldLength);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Value to be formatted: {}, position: {}, and its formatted value: {}", new Object[]{value, datafield.pos(), result});
}
} else {
result = "";
}
Integer key;
key = datafield.pos();
if (!results.containsKey(key)) {
List list = new LinkedList();
list.add(result);
results.put(key, list);