0

I have a CSV file with ; delimiter, but in one of the fields it represents objects with | delimiter:

field1;field2;field3;"field-4.1;field-4.2|field-4.1;field-4.2";field5;field6;

So the problem is with field4 which represents multiples objects separated by pipe.

Actually my template is like this:

           <record name="fields" class="br.com.beanio.MyPojo">
                <field name="field1"/>
                <field name="field2"/>
                <field name="field3"/>
                <segment name="fields4" minOccurs="0" collection="list" class="br.com.beanio.MySubPojo">
                    <field name="field4-1" />
                    <field name="field4-2" />
                </segment>
                <field name="field5"/>
                <field name="field6"/>
            </record>

I tried with different properties to identify when field4 represents a new object, but didn't work.

How could I read field 4 using beanIO templating?

Thanks in advance

Guilherme Bernardi
  • 490
  • 1
  • 6
  • 18
  • I don't know of something out of the box that could do that. It would be nice perhaps if you could specify a different delimiter for the segment, but that is not possible AFAIK. You could of course just do the parsing of `field4` in a setter method to populate the `br.com.beanio.MySubPojo` or if it is a big object, why not create a separate mapping file and then parse `field4` only with BeanIO? It may not be worth the extra effort. – nicoschl Jan 18 '21 at 20:36
  • I'm trying now to use typeHandler. Do you know if its possible to don't split the "field4"? For example, ignore only in this field? – Guilherme Bernardi Jan 18 '21 at 22:08
  • You should get back `"field-4.1;field-4.2|field-4.1;field-4.2"` for field4 by default. A `TypeHandler` is a good alternative to use. – nicoschl Jan 19 '21 at 08:21

1 Answers1

0

My solution was creating a typeHandler for the field:

public class MyCustomTypeHandler implements TypeHandler {

In my template I use the typeHandler:

<field name="fieldWithList" typeHandler="MyCustomTypeHandler"/>

So inside parse method in the typeHandler I could process the field:

        if (nonNull(text)) {
            asList(text.split("\\|")).forEach(str -> {
                String[] fields = str.split(";", -1);
                // process and add items to the list
            });
        }
Guilherme Bernardi
  • 490
  • 1
  • 6
  • 18