users@jaxb.java.net

Re: model groups with repeating occurrence

From: Joe Fialli <joseph.fialli_at_sun.com>
Date: Thu, 06 Mar 2003 16:58:49 -0500

Comments inlined below.

Mike Bernardo wrote:
> Has anyone been able to generate a clean interface to a repeating occurrence
> model group that contains multiple items of different types?
>
> To put this question a bit more concretely, consider the following schema:
>
> <xs:element name="list" type="listType"/>
> <xs:complexType name="listType">
> <xs:sequence>
> <xs:choice minOccurs="1" maxOccurs="unbounded">
> <xs:element name="a" type="xs:string"/>
> <xs:element name="b" type="xs:int"/>
> <xs:element name="c" type="xs:boolean"/>
> </xs:choice>
> </xs:sequence>
> </xs:complexType>
>
> This results in the following generated interface for ListType:
>
> public interface ListType {
> java.util.List getAOrBOrC();
> public interface A {
> java.lang.String getValue();
> void setValue(java.lang.String value);
> }
> public interface B {
> int getValue();
> void setValue(int value);
> }
> public interface C {
> boolean isValue();
> void setValue(boolean value);
> }
> }
>
> Now, the problem arises when trying to write clean code to process a
> ListType object. You need to perform a lot of runtime type checking like the
> following example:
>
> java.util.List items = l.getAOrBOrC();
>
> for (Iterator i = items.iterator(); i.hasNext(); ) {
> Object thing = i.next();
>
> if (thing instanceof ListType.A) {
> ListType.A newthing = (ListType.A)thing;
> String value = newthing.getValue();
> System.out.println(value);
> } else if (thing instanceof ListType.B) {
> ListType.B newthing = (ListType.B)thing;
> int value = newthing.getValue();
> System.out.println(value);
> } else if (thing instanceof ListType.C) {
> ListType.C newthing = (ListType.C)thing;
> boolean value = newthing.isValue();
> System.out.println(value);
> }
> }
>
> Now imagine a scenario where ListType has many more sub-elements, (say,
> around 50) and each sub element is a complexType. The situation gets very
> messy quickly.
>
> Is there any way to override the binding to produce a cleaner interface? One
> possibility I can think of would be the following interface:
>
> public interface ListType {
> java.util.List getAOrBOrC();
>
> // the following methods would return homogeneous lists of the
> // individual types
> java.util.List getListofA();
> java.util.List getListofB();
> java.util.List getListofC();
>
> public interface A {
> java.lang.String getValue();
> void setValue(java.lang.String value);
> }
> public interface B {
> int getValue();
> void setValue(int value);
> }
> public interface C {
> boolean isValue();
> void setValue(boolean value);
> }
> }
>
> Now, if you don't care about the ordering of the sub-elements, you could
> cleanly process lists of a single type.

That is the catch. Since XML represents sequential data, cases do exist where
the ordering between elements is very important. If the ordering was not
important between the elements, a schema description does exist to describe
exactly what you desire and JAXB will generate the method signatures you
desire for that XML Schema representation.

Here is that schema description:

<xs:element name="list" type="listType"/>
<xs:complexType name="listType">
     <xs:sequence>
             <xs:element name="a" type="xs:string" maxOccurs="unbounded"/>
             <xs:element name="b" type="xs:int" maxOccurs="unbounded"/>
             <xs:element name="c" type="xs:boolean" maxOccurs="unbounded"/>
         </xs:choice>
     </xs:sequence>
</xs:complexType>

As previously suggested, either a Vistor pattern over the list
or perhaps a FilteredIterator abstraction would provide the
abstraction you are looking for over an unbounded choice.

The FilteredIterator would allow your application to have the
virtual list you desire; however, underneath there would be
one sequential list.
>
> Is it possible to generate this interface using binding overrides?

No. It would be ambiguous what the impact of adding a new instance
into one of the proposed "virtual" list of one type would have on the one
sequential list of A's, B's and C's. Generally speaking, the ordering
between the elements constrained by an unbounded choice is significant
for the Java representation to maintain.

-Joe Fialli, Sun Microsystems


>
> Thanks for any help.
>
> Mike Bernardo


--