users@jaxb.java.net

Re: Customization question

From: Noel Rappin <nrappin_at_SOCKEYE.COM>
Date: Mon, 04 Nov 2002 20:52:56 -0500

I understand why you don't let collections bind to Map -- I'm not
looking to replace the existing List, I'm looking to augment it.
 Essentially, what I'd like to do is for JAXB to unmarshal to a class
which implements the Parent interface, but also contains a method like this:

public void addChild(Child c) {
       // do whatever is needed to put c in the list, then...
     this.childMap.put(c.getName(), c);
}

What I don't understand is how to hook into that addChild event when
JAXB unmarshals the data.

Thinking about this a little bit more, I think that my problem here is
in thinking that I need to customize the binding to Parent, when
actually I want to customize the binding to the List for child to be
some kind of custom list that also maintains a map of it's information.

Does that make more sense?

Noel

Joe Fialli wrote:

> Noel Rappin wrote:
>
>> I have an XML document that looks something like this (simplified, of
>> course...):
>>
>> <parent>
>> <child name="fred"/>
>> <child name="barney"/>
>> <child name="wilma"/>
>> </parent>
>>
>> What I get out of JAXB is a Parent interface that has a List property
>> Child. This is fine. However, when the item is marshaled, I'd also
>> like to have the Child objects be inserted into a Map, where the name
>> attribute is the key, and the Child object is the value.
>
>
> Noel,
>
> From the XML document example above, I assume you have a schema
> with an element <child> with "maxOccurs > 1".
>
> The <child> element is mapped to a collection property named "child".
> JAXB does not support customizing a collection property to a
> java.util.Map at this time. One can customize the collection property
> to be an JavaBean indexed bean or an implementation of a java.util.List
> class. The rationale behind this is that ordering within a XML document
> is significant and only collection types that implement java.util.List
> are capable of preserving sequential order between elements of the
> collection.
>
> If you were allowed to specify that the "collectionType" for property
> "child" was an implementation of "java.util.Map" and that one of the
> "child"'s property was the key for the Map, the property for @name in
> your example above, the Java representation of the <child> collection
> property as a Map would not be capable of preserving the original XML
> document order between the <child> elements when marshalling the content
> back to XML. Maps are not capable of supporting arbitrary ordering
> between elements, a sorted map can preserve a natural sorted order
> between keys at best.
>
> In this intial version of data binding, we have tried to be careful to
> not allow customizations that would make it impossible to preserve the
> orignal XML document within the Java representation.
>
> -Joe Fialli, Sun Microsystems
>
>
>>
>> I've been trying to figure out how to get JAXB to bind to a custom class
>> that implements the parent interface and that will do this, but don't
>> think I've quite figured it out. Is there a way to get JAXB to do what
>> I want?
>>
>> Thanks,
>>
>> Noel Rappin
>
>
>
> --