users@jaxb.java.net

Re: JAXB Lists and Trees

From: Jukka Uusisalo <jukkauus_at_mail.verkkotieto.com>
Date: Mon, 27 Jan 2003 00:15:56 +0200 (EET)

This message was originally submitted by jukkauus_at_MAIL.VERKKOTIETO.COM to the
JAXB-INTEREST list at JAVA.SUN.COM. If you simply forward it back to the list,
using a mail command that generates "Resent-" fields (ask your local user
support or consult the documentation of your mail program if in doubt), it will
be distributed and the explanations you are now reading will be removed
automatically. If on the other hand you edit the contributions you receive into
a digest, you will have to remove this paragraph manually. Finally, you should
be able to contact the author of this message by using the normal "reply"
function of your mail program.

----------------- Message requiring your approval (96 lines) ------------------


On Sun, 26 Jan 2003, Les Hazlewood wrote:

>
> Given the following partial XML Schema definition example:
>
> <xsd:element name="rubberDucky">
> <xsd:complexType>
> <xsd:attribute name="name" type="xsd:token"/>
> <xsd:attribute name="color" type="xsd:token"/>
> </xsd:complexType>
> </xsd:element>
>
> <xsd:element name="bathtubToys">
> <xsd:complexType>
> <xsd:sequence>
> <xsd:element ref="rubberDucky" minOccurs="1" maxOccurs="unbounded"/>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:element>
>
> We can envision an instance such as:
>
> <bathtubToys>
> <rubberDucky name="Fred" color="yellow"/>
> <rubberDucky name="John" color="blue"/>
> <rubberDucky name="Jack" color="green"/>
> </bathtubToys>
>
>
> The Sun reference implementation would generate the cooresponding BathtubToys, BathtubToysType, RubberDucky, and RubberDuckyType interfaces as needed.
>
> Continuing with this example, the BathtubToysType interface would have a "getter" method with the signature:
>
> public java.util.List getRubberDucky().
>
> This is where I have a large conceptual problem with such a signature. However, I'm not sure if this signature is done this way because of the reference implementation or if its a requirement from the 0.90 version of the spec. I've read most of the spec, but I can't remember, so please inform me either way.
>
> To me, this method should be called getRubberDuckyList().
>
> Conceptually the method signature getRubberDucky() implies there is only one, just like mutator methods for strings. I've noticed this is a conceptual problem for many other programmers, as in my google searching, I've seen the following question asked numerous times:
>
> "How come there is a getRubberDucky() method that returns a List, but no setRubberDucky(List myList) method that allows me to set one? I.E. how do I add a RubberDucky to the list of BathtubToys?"
>
> The common answer is "just do getRubberDucky().add(myRubberDucky)", which of course is a good answer given the implementation. However, *conceptually* this method signature does not make sense given the singualar naming convention for something that really can contain multiple entries.
>
> It would not be difficult at all to change the method signature...and would in fact help clear up future confusion.
>
> Some may think that I'm being picky over small details. However a key factor that makes the OO paradigm so powerful (especially Java) is the detail of addressing human intuition. In my opinion this small change could resolve many questions that are sure to arise in the future as JAXB becomes more widely used.
>
> How difficult would it be to implement such a change? Is there way to submit a feature request to the JAXB standards body?
>

I think custom binding would help this. Something like

       <xsd:element ref="rubberDucky" minOccurs="1" maxOccurs="unbounded">
        <xsd:annotation>
                <xsd:appinfo>
                        <jxb:property name="RubberDuckyList"/>
                </xsd:appinfo>
        </xsd:annotation>
       </xsd:element>

Then you will got signature
List getRubberDuckyList();

Adding RubberDucky objects to BathTubToys, i do not see any problem
working like

BathTubToys toys = ObjectFactory.createBathToys();
RubberDucky ducky = ObjectFactory.createRubberDucky();
ducky.setName("Fred");
ducky.setColor("yellow");
List rubberDuckList = toys.getRubberDuckyList();
rubberDuckList.add(ducky);

Of course, with large schema there is lot work to do even to
create valid instances thru to whole object-structure, specially
if schema is public and can't be modified.

Even schema definition is like class definition, schema defines
xml data structures and class defines (java) objects and they
are not just equals. I think that's why JAXB needs custom bindings.
I hope there will be more custom things next JAXB specification,
current final specification should be available any moment, early Q1
2003 ;). I like to see something like define own create listeners to
JAXB objects or define other observers to JAXB objects.

Sorry, i think this went off-topic and quite far from original post.

- Jukka -