Search in sources :

Example 1 with FormatException

use of org.apache.camel.dataformat.bindy.format.FormatException in project camel by apache.

the class BindyFixedLengthFactory method bind.

public void bind(String record, Map<String, Object> model, int line) throws Exception {
    int pos = 1;
    int counterMandatoryFields = 0;
    DataField dataField;
    String token;
    int offset = 1;
    int length;
    String delimiter;
    Field field;
    // Iterate through the list of positions
    // defined in the @DataField
    // and grab the data from the line
    Collection<DataField> c = dataFields.values();
    Iterator<DataField> itr = c.iterator();
    // this iterator is for a link list that was built using items in order
    while (itr.hasNext()) {
        dataField = itr.next();
        length = dataField.length();
        delimiter = dataField.delimiter();
        if (length == 0 && dataField.lengthPos() != 0) {
            Field lengthField = annotatedFields.get(dataField.lengthPos());
            lengthField.setAccessible(true);
            Object modelObj = model.get(lengthField.getDeclaringClass().getName());
            Object lengthObj = lengthField.get(modelObj);
            length = ((Integer) lengthObj).intValue();
        }
        if (length < 1 && delimiter == null && dataField.lengthPos() == 0) {
            throw new IllegalArgumentException("Either length or delimiter must be specified for the field : " + dataField.toString());
        }
        if (offset - 1 <= -1) {
            throw new IllegalArgumentException("Offset/Position of the field " + dataField.toString() + " cannot be negative");
        }
        // skip ahead if the expected position is greater than the offset
        if (dataField.pos() > offset) {
            LOG.debug("skipping ahead [" + (dataField.pos() - offset) + "] chars.");
            offset = dataField.pos();
        }
        if (length > 0) {
            if (record.length() < offset) {
                token = "";
            } else {
                int endIndex = offset + length - 1;
                if (endIndex > record.length()) {
                    endIndex = record.length();
                }
                token = record.substring(offset - 1, endIndex);
            }
            offset += length;
        } else if (!delimiter.equals("")) {
            String tempToken = record.substring(offset - 1, record.length());
            token = tempToken.substring(0, tempToken.indexOf(delimiter));
            // include the delimiter in the offset calculation
            offset += token.length() + 1;
        } else {
            // defined as a zero-length field
            token = "";
        }
        if (dataField.trim()) {
            token = trim(token, dataField, paddingChar);
        //token = token.trim();
        }
        // Check mandatory field
        if (dataField.required()) {
            // Increment counter of mandatory fields
            ++counterMandatoryFields;
            // This is not possible for mandatory fields
            if (token.equals("")) {
                throw new IllegalArgumentException("The mandatory field defined at the position " + pos + " is empty for the line: " + line);
            }
        }
        // Get Field to be set
        field = annotatedFields.get(dataField.pos());
        field.setAccessible(true);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Pos/Offset: {}, Data: {}, Field type: {}", new Object[] { offset, token, field.getType() });
        }
        // Create format object to format the field
        FormattingOptions formattingOptions = ConverterUtils.convert(dataField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
        Format<?> format = formatFactory.getFormat(formattingOptions);
        // field object to be set
        Object modelField = model.get(field.getDeclaringClass().getName());
        // format the data received
        Object value = null;
        if ("".equals(token)) {
            token = dataField.defaultValue();
        }
        if (!"".equals(token)) {
            try {
                value = format.parse(token);
            } catch (FormatException ie) {
                throw new IllegalArgumentException(ie.getMessage() + ", position: " + offset + ", line: " + line, ie);
            } catch (Exception e) {
                throw new IllegalArgumentException("Parsing error detected for field defined at the position/offset: " + offset + ", line: " + line, e);
            }
        } else {
            value = getDefaultValueForPrimitive(field.getType());
        }
        field.set(modelField, value);
        ++pos;
    }
    // check for unmapped non-whitespace data at the end of the line
    if (offset <= record.length() && !(record.substring(offset - 1, record.length())).trim().equals("") && !isIgnoreTrailingChars()) {
        throw new IllegalArgumentException("Unexpected / unmapped characters found at the end of the fixed-length record at line : " + line);
    }
    LOG.debug("Counter mandatory fields: {}", counterMandatoryFields);
    if (pos < totalFields) {
        throw new IllegalArgumentException("Some fields are missing (optional or mandatory), line: " + line);
    }
    if (counterMandatoryFields < numberMandatoryFields) {
        throw new IllegalArgumentException("Some mandatory fields are missing, line: " + line);
    }
}
Also used : BindyConverter(org.apache.camel.dataformat.bindy.annotation.BindyConverter) FormatException(org.apache.camel.dataformat.bindy.format.FormatException) FormatException(org.apache.camel.dataformat.bindy.format.FormatException) Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) DataField(org.apache.camel.dataformat.bindy.annotation.DataField)

Example 2 with FormatException

use of org.apache.camel.dataformat.bindy.format.FormatException in project camel by apache.

the class BindyCsvFactory method bind.

@Override
public void bind(List<String> tokens, Map<String, Object> model, int line) throws Exception {
    int pos = 1;
    int counterMandatoryFields = 0;
    for (String data : tokens) {
        // Get DataField from model
        DataField dataField = dataFields.get(pos);
        ObjectHelper.notNull(dataField, "No position " + pos + " defined for the field: " + data + ", line: " + line);
        if (dataField.trim()) {
            data = data.trim();
        }
        if (dataField.required()) {
            // Increment counter of mandatory fields
            ++counterMandatoryFields;
            // This is not possible for mandatory fields
            if (data.equals("")) {
                throw new IllegalArgumentException("The mandatory field defined at the position " + pos + " is empty for the line: " + line);
            }
        }
        // Get Field to be setted
        Field field = annotatedFields.get(pos);
        field.setAccessible(true);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Pos: {}, Data: {}, Field type: {}", new Object[] { pos, data, field.getType() });
        }
        // Create format object to format the field
        FormattingOptions formattingOptions = ConverterUtils.convert(dataField, field.getType(), field.getAnnotation(BindyConverter.class), getLocale());
        Format<?> format = formatFactory.getFormat(formattingOptions);
        // field object to be set
        Object modelField = model.get(field.getDeclaringClass().getName());
        // format the data received
        Object value = null;
        if (!data.equals("")) {
            try {
                value = format.parse(data);
            } catch (FormatException ie) {
                throw new IllegalArgumentException(ie.getMessage() + ", position: " + pos + ", line: " + line, ie);
            } catch (Exception e) {
                throw new IllegalArgumentException("Parsing error detected for field defined at the position: " + pos + ", line: " + line, e);
            }
        } else {
            if (!dataField.defaultValue().isEmpty()) {
                value = format.parse(dataField.defaultValue());
            } else {
                value = getDefaultValueForPrimitive(field.getType());
            }
        }
        field.set(modelField, value);
        ++pos;
    }
    LOG.debug("Counter mandatory fields: {}", counterMandatoryFields);
    if (counterMandatoryFields < numberMandatoryFields) {
        throw new IllegalArgumentException("Some mandatory fields are missing, line: " + line);
    }
    if (pos < totalFields) {
        setDefaultValuesForFields(model);
    }
}
Also used : Field(java.lang.reflect.Field) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) BindyConverter(org.apache.camel.dataformat.bindy.annotation.BindyConverter) DataField(org.apache.camel.dataformat.bindy.annotation.DataField) FormatException(org.apache.camel.dataformat.bindy.format.FormatException) FormatException(org.apache.camel.dataformat.bindy.format.FormatException)

Aggregations

Field (java.lang.reflect.Field)2 BindyConverter (org.apache.camel.dataformat.bindy.annotation.BindyConverter)2 DataField (org.apache.camel.dataformat.bindy.annotation.DataField)2 FormatException (org.apache.camel.dataformat.bindy.format.FormatException)2