nested segment tags got error message

461 views
Skip to first unread message

dav...@gmail.com

unread,
Apr 1, 2013, 11:34:08 PM4/1/13
to bea...@googlegroups.com
Here is my mapping.xml

<beanio xmlns="http://www.beanio.org/2012/03"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

  <stream name="employeeFile" format="fixedlength">
    <record name="employee" class="poc.bean.io.Employee">
      <field name="firstName" length="10"/>
      <field name="lastName" length="10"/>
      <field name="title" length="20"/>
      <field name="salary" length="10"/>
      <field name="hireDate" format="MMddyyyy" length="8"/>
      <segment name="addressList" collection="list" class="poc.bean.io.Address" minOccurs="1" maxOccurs="unbounded">
        <field name="street" length="40"/>
        <field name="city" length="20"/>
        <field name="state" length="2"/>
        <field name="zip" length="5"/>
        <segment name="phoneList" collection="list" class="poc.bean.io.Phone" minOccurs="1" maxOccurs="unbounded">
            <field name="phone" length="10"/>
        </segment>
      </segment>
    </record>
  </stream>
</beanio>

I want to use beanio to write out my Employee into a fixed length string. My writer class is

package poc.bean.io;

import org.beanio.*;
import java.io.*;
import java.util.*;

public class BeanWriterExample {
    public static void main(String[] args) throws Exception {
        // create a StreamFactory
        StreamFactory factory = StreamFactory.newInstance();
        // load the mapping file
        factory.load("test/mapping/mapping.xml");
       
        Employee employee = new Employee();
        employee.setFirstName("Jennifer");
        employee.setLastName("Jones");
        employee.setTitle("Marketing");
        employee.setSalary(60000);
        employee.setHireDate(new Date());
       
        Address address1 = new Address();
        address1.setStreet("100 General Hancock Blvd");
        address1.setCity("North Wales");
        address1.setState("PA");
        address1.setZip("19454");
       
        List<Phone> phoneList=new ArrayList<Phone>();
        phoneList.add(new Phone("1234567890"));
        phoneList.add(new Phone("0000000000"));
        address1.setPhoneList(phoneList);
       
        Address address2 = new Address();
        address2.setStreet("14101 Atwater Dr");
        address2.setCity("Sterling Heights");
        address2.setState("MI");
        address2.setZip("48318");
       
        List<Phone> phoneList2=new ArrayList<Phone>();
        phoneList2.add(new Phone("8888888888"));
        phoneList2.add(new Phone("9999999999"));
        address2.setPhoneList(phoneList2);
       
        List<Address> listAddress = new ArrayList<Address>();
        listAddress.add(address1);
        listAddress.add(address2);
        employee.setAddressList(listAddress);
       
        // use a StreamFactory to create a BeanWriter
        BeanWriter out = factory.createWriter("employeeFile", new File("output/employee.txt"));
        // write an Employee object directly to the BeanWriter
        out.write(employee);
        out.flush();
        out.close();
    }
}

However, I got error output as below. How to fix it? Thanks

Exception in thread "main" org.beanio.BeanIOConfigurationException: Invalid segment 'addressList', in record 'employee', in stream 'employeeFile': A segment of indeterminate size may not follow another component of indeterminate size
    at org.beanio.internal.compiler.ProcessorSupport.process(ProcessorSupport.java:93)
    at org.beanio.internal.compiler.ParserFactorySupport.createStream(ParserFactorySupport.java:83)
    at org.beanio.internal.compiler.StreamCompiler.createStreamDefinitions(StreamCompiler.java:130)
    at org.beanio.internal.compiler.StreamCompiler.loadMapping(StreamCompiler.java:84)
    at org.beanio.internal.DefaultStreamFactory.load(DefaultStreamFactory.java:52)
    at org.beanio.StreamFactory.load(StreamFactory.java:258)
    at org.beanio.StreamFactory.load(StreamFactory.java:234)
    at org.beanio.StreamFactory.load(StreamFactory.java:223)
    at poc.bean.io.BeanWriterExample.main(BeanWriterExample.java:12)

Kevin

unread,
Apr 2, 2013, 11:26:09 AM4/2/13
to bea...@googlegroups.com
Hello,

That is by design, a segment of indeterminate length may not contain another segment or field of indeterminate length in a fixed length layout.  When reading this layout, how do you know if you are reading another phone number or the next address?

Thanks,
Kevin

dav...@gmail.com

unread,
Apr 2, 2013, 12:36:38 PM4/2/13
to bea...@googlegroups.com
Kevin
 
Thanks for replying me. I understand your point. Actually, I am doing poc of beanio to evaluate whether we can use it to replace the parser we have today in my company.
 
Here is the example of what we have today for more dynamic fixedlength message format. In this example, we uses occurA and occurB for telling the engine how many occurances for the looping, and RepeatEndIndex tells where the looping ends.
 
MsgExample.Output1.Name=Item1
MsgExample.Output1.Length=8
MsgExample.Output2.Name=OccurA
MsgExample.Output2.Length=2
MsgExample.Output2.RepeatEndIndex=5
MsgExample.Output3.Name=Item3
MsgExample.Output3.Length=8
MsgExample.Output4.Name=OccurB
MsgExample.Output4.Length=8
MsgExample.Output4.RepeatEndIndex=5
MsgExample.Output5.Name=Item5
MsgExample.Output5.Length=5
MsgExample.Output6.Name=Item6
MsgExample.Output6.Length=3
 
The example message string can be
Item102Item303Item5Item5Item5Item302Item5Item5END
 
The message layout in properties format can be used to parse it into
 
Item1
02 (repeat 2 times from index 3 to index 5)
-- Item3
-- 03
-- -- Item5
-- -- Item5
-- -- Item5
-- Item3
-- 02
-- -- Item5
-- -- Item5
END
 
We'd like to replace the old parser with one that can take xml message template. The way we can realize it is to tell the parser how many repeated items from some number of occurs fields. I know it's not in beanio in v 2.0.5, can we have it in the next version, or what can I change in beanio source to implement this nested looping examples.
 
Thanks
Lei

Kevin

unread,
Apr 3, 2013, 2:17:19 PM4/3/13
to bea...@googlegroups.com
Hi Lei,

I couldn't quite follow your configuration settings, but basically you're using a field in the same record to indicate the number of occurrences of another segment or field, right?  I've been considering adding this functionality, but haven't looked into how difficult it would be yet.  I'll try to find time to look into over the next week and if it doesn't prove to be too difficult, I can certainly include it in the next release.

Thanks,
Kevin

dav...@gmail.com

unread,
Apr 3, 2013, 7:03:08 PM4/3/13
to bea...@googlegroups.com
Kevin,

Yes, what we have in our message today uses one occurrence field to indicate the number of the iteration of the nested segment. This gives us more flexible to deal with some level of dynamic in our messages. By doing this, the parser will get different number of occurs in different messages while parsing the message instead of knowing all the lengths and occurs before marshalling or unmarshalling.

It'll be great if this feature can be added in the next release. Please keep me updated.

Thanks
Lei

Kevin

unread,
Apr 7, 2013, 10:29:26 PM4/7/13
to bea...@googlegroups.com
Hi Lei,

I opened issue #69 and attached a 2.1.0.M1 snapshot jar that may have the logic you're looking for.  See https://code.google.com/p/beanio/issues/detail?id=69 for details.

Thanks,
Kevin

dav...@gmail.com

unread,
Apr 8, 2013, 12:38:33 AM4/8/13
to bea...@googlegroups.com
Thanks Keven, I will give a try tomorrow. btw, is it also working for fixed length string?

dav...@gmail.com

unread,
Apr 8, 2013, 7:45:14 AM4/8/13
to bea...@googlegroups.com
It works Keven. When will you release the new version, 2.1?

Kevin

unread,
Apr 8, 2013, 3:56:23 PM4/8/13
to bea...@googlegroups.com
I hope to create a 2.1.0 milestone in about a month, but there is no set schedule.
Reply all
Reply to author
Forward
0 new messages