somranjan / beanio

Automatically exported from code.google.com/p/beanio
Apache License 2.0
0 stars 0 forks source link

Zero padded fixed length fields cannot be made optional. #11

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Given a zero padded fixed length field bound to a nullable Java type, such as 
the Integer value defined below, BeanIO will throw a TypeConversionException if 
the field is optional and passed as all spaces, "     ".

     <field name="value" type="java.lang.Integer" length="5" padding="0" justify="right" required="false"/> 

Instead, a field consisting of all spaces should be unmarshalled as null even 
when the padding character is not a space.  To disallow all spaces, required 
should be set to true, rather than relying on the type conversion error.

Until this is fixed in the next release, the following workarounds are 
available:

1.  (Simplest) Set trim="true" on the field.  However this will allow some 
invalid values to pass type conversion, such as "  10 ", which should be 
formatted as "00010".

2.  (Best) Create and use a custom type handler that parses all spaces as null. 
 For example, note the "".equals(text.trim()) check in parse(...) shown below.

public class IntegerTypeHandler implements TypeHandler { 

    public Integer parse(String text) throws TypeConversionException { 
        if (text == null || "".equals(text.trim())) 
            return null; 

        try { 
            return Integer.parseLong(text); 
        } 
        catch (NumberFormatException ex) { 
            throw new TypeConversionException("Invalid long", ex); 
        } 
    } 

    public String format(Object value) { 
        if (value == null) 
            return ""; 
        else 
            return ((Long)value).toString(); 
    } 

    public Class<?> getType() { 
        return Long.class; 
    } 
} 

**Special thanks to Mauro for pointing this out to me in the mailing list.

Original issue reported on code.google.com by kevin.s...@gmail.com on 15 Jul 2011 at 1:45

GoogleCodeExporter commented 8 years ago
Correction of this defect will cause some impact to existing padded field 
behavior.  Please note the following changes (in addition to the change already 
mentioned above):

Regarding padded fields in a fixed length stream format:
1.  During marshalling, null field values will be marshalled as all spaces when 
required="false", regardless of the configured padding character.  If 
required="true", the entire field is filled with the padding character.

Regarding padded fields in any non-fixed length stream format:
1.  During unmarshalling, if required="true" and there is no field text (i.e. 
empty string), a 'required' field validation error is triggered.  (Existing 
behavior).
2.  During unmarshalling, if required="false" and there is no field text (i.e. 
empty string), the field text will pass validation and be subject to type 
conversion.
3.  During unmarshalling, if any field text is provided and the length of the 
field text does not match the configured field length, a 'length' field 
validation error is triggered.
4.  During marshalling, if required="true" and the field value is null, the 
field is filled with the padding character up to the configured length.
5.  During marshalling, if required="false" and the field value is null, the 
value will be marshalled as the empty string.

Original comment by kevin.s...@gmail.com on 16 Jul 2011 at 10:22

GoogleCodeExporter commented 8 years ago
Fixed for release 1.1.1.

Original comment by kevin.s...@gmail.com on 18 Jul 2011 at 12:01