BeanIo and Fixed Length + Long/Integer fields

1,876 views
Skip to first unread message

mauro

unread,
Jul 13, 2011, 5:50:26 AM7/13/11
to bea...@googlegroups.com
I have some Long Integer fields in a fixed length file which are leftpadded with zero and justified to the right.
Some of these fields are not mandatory (so they are filled with blank values) and when I read them BeanIo is returning the error Invalid Long Type.
 
 
I created the NumberTypeHandler class and LongTypeHandler class in my component and I replaced the method parse in the following way:
 
if (text == null || "".equals(text.trim())) {
           return null;
       }
 
You can see that I added the trim ........ 

mauro

unread,
Jul 13, 2011, 5:50:49 AM7/13/11
to bea...@googlegroups.com

Kevin

unread,
Jul 13, 2011, 9:42:45 PM7/13/11
to beanio-users
Hi mauro,

Did that not work for you? I tried the same thing and it seemed to
work for me.

First I created a LongTypeHandler:

public class LongTypeHandler implements TypeHandler {
@Override
public Long parse(String text) throws TypeConversionException {
if (text == null || "".equals(text.trim()))
return null;

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

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

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

Then using the following mapping file where I overwrite the default
Long type handler with my own:

<beanio xmlns="http://www.beanio.org/2011/01">
<stream name="stream" format="fixedlength">
<typeHandler type="Long" class="test.LongTypeHandler" />
<record name="record" class="map">
<field name="value1" type="Long" length="10" padding="0"
justify="right"/>
<field name="value2" type="Long" length="10" padding="0"
justify="right"/>
</record>
</stream>
</beanio>

I was able to parse the following file (courier font would be nice
here):

00000000000000000000
0000000099

00000000010000000001

And got the following results for each record:

{value1=0, value2=0}
{value1=null, value2=99}
{value1=null, value2=null}
{value1=1, value2=1}

Is this what you are trying to do?

Thanks,
Kevin

mauro

unread,
Jul 14, 2011, 3:17:32 AM7/14/11
to beanio-users
yes....in this way (with the trim) it works.
I just wanted to suggest you to put directly this instruction in BenIo
NumberTypeHandler class. I think that this situation can happens also
to others and in any case if they want that the field is mandatory
they could always use the property required="true" to raise the error
when the field is missing.
As it is now practically the numeric field (if defined with padding
="0" at least) are always mandatory (also with required="false").


Thanks and ciao,
Mauro
> > You can see that I added the trim ........- Nascondi testo citato
>
> - Mostra testo citato -

Kevin

unread,
Jul 14, 2011, 11:23:00 AM7/14/11
to beanio-users
Hi Mauro,

You almost convinced me :), but then I remembered this is already
supported (although not very obvious). All you need to do is add
trim="true" and field text will be trimmed before passing it to a type
handler.

In my example, the updated mapping file would look like this. No
custom type handler is necessary. And the results are the same.

<stream name="stream" format="fixedlength">
<record name="record" class="map">
<field name="value1" type="Long" length="10" padding="0"
justify="right" trim="true"/>
<field name="value2" type="Long" length="10" padding="0"
justify="right" trim="true"/>
</record>
</stream>

Does that work for you?

Thanks,
Kevin

mauro

unread,
Jul 14, 2011, 12:09:48 PM7/14/11
to beanio-users
I did not have time to test, but I see a problem:

suppose that the field length is 10 as in your structure but the file
is filled with ' 0012345' or '0012345 ' or something mixed with
blank at the beginning and at the end or ' 12345 '.
In these cases normally the program should raise an error because the
field is defined as numeric and I am afraid that if you put
trim="true" the spaces will be removed and no error will be raised.
Im many system in case of fixed length file numeric fields are built
leftpadded with '0' and normally the space value is never accepted
except the case of empty value field ( to distinguish from zero value
field: in this case the field is with as many '0' as its length).




ciao
> > > - Mostra testo citato -- Nascondi testo citato

Kevin

unread,
Jul 14, 2011, 12:20:38 PM7/14/11
to beanio-users
Good point. I will add this to the list of enhancements for the next
release.

Thanks,
Kevin

Kevin

unread,
Jul 21, 2011, 9:26:10 PM7/21/11
to beanio-users
Hi Mauro,

I believe release 1.1.1 will resolve your concerns, regarding optional
zero padded fields in a fixed length stream.

Thanks again for bringing this to my attention.

Kevin
Reply all
Reply to author
Forward
0 new messages