users@jaxb.java.net

Re: Supporting Maps

From: Brian Pontarelli <brian_at_pontarelli.com>
Date: Thu, 17 Feb 2005 09:59:35 -0600

> For example, in J2EE deployment descriptors, you define servlets like
> this:
>
> <servlet>
> <servlet-name>Stapler</servlet-name>
> <servlet-class>org.kohsuke.stapler.Stapler</servlet-class>
> </servlet>
>
> ... where <servlet-name> must be unique. Shouldn't you be able to have
> a map from a servlet name to the following Servlet class?
>
> class Servlet {
> @XmlElement
> String servletName;
> @XmlElement
> Class setvletClass;
> }
>
> That's what I meant by using elements as keys.

Gotcha. You either way would be great. The fundamental limitation is
that unless JAXB implements hashcode and equals for all generated
classes, then the element used as a key must be a simple type. So, the
customization could use XPath to determine the key?

> Boy so many people asks this feature!
>
> My current thinking is to use the plugin mechanism of the JAXB RI to
> allow someone else to write it as a plug-in.
>
> If there's any interested taker, we can discuss about how it can be done.

I can implement it if you need. The only concern I have is that if the
Map support is added and elements as keys is supported, than JAXB needs
to implement this. I can still help with the code however.

> I guess the point I was trying to make is that if you have a need to
> build a in-memory representation very different from the on-the-wire
> format, you can always use ObjectLifecycle or @XmlJavaTypeAdapter.
>
> That's the general-purpose catch-all mechanism. For the "map support"
> to be useful, it has to focus on a particular set of well-used use
> cases, and it has to be easy. If it's not well-used enough, it doesn't
> deserve to have its own mechanism. If it's not easy enough, there's no
> point in having it at all.
>
> I was questioning whether this multimap functionality is well-used
> enough to justify its special support.

I would say it would be, but that's just a guess really. Either way,
support for simple Maps with attributes or elements as keys should
definitely be considered. Or at the least the annotations and the
support from the (un)marshaller for it so that I can write it into my
JAXB generated classes or my custom classes by hand.