users@jaxb.java.net

model groups with repeating occurrence

From: Mike Bernardo <mbernardo_at_endeca.com>
Date: Thu, 06 Mar 2003 09:19:43 -0500

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.

Is it possible to generate this interface using binding overrides?

Thanks for any help.

Mike Bernardo