users@jaxb.java.net

Simplify Plugin

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Sat, 1 Oct 2011 18:07:53 +0200

Hi,

I am glad to announce a new plugin:

http://confluence.highsource.org/display/J2B/Simplify+Plugin

Will be released in the next version of JAXB2 Basics (0.6.3).
Currently available from the Highsource snapshot repository:

http://repository.highsource.org/maven2/snapshots/org/jvnet/jaxb2_commons/jaxb2-basics/0.6.3-SNAPSHOT/

This plugin allows simplifying "complex" properties. Thes properties
are often generated from repeatable choices like this one:

       <xs:complexType name="typeWithReferencesProperty">
               <xs:choice maxOccurs="unbounded">
                       <xs:element name="a" type="someType"/>
                       <xs:element name="b" type="someType"/>
               </xs:choice>
       </xs:complexType>
       <xs:complexType name="typeWithElementsProperty">
               <xs:choice maxOccurs="unbounded">
                       <xs:element name="a" type="xs:string"/>
                       <xs:element name="b" type="xs:int"/>
               </xs:choice>
       </xs:complexType>
By default, XJC will complex properties modelling several references
or elements in one.

   @XmlElementRefs({
       @XmlElementRef(name = "a", type = JAXBElement.class),
       @XmlElementRef(name = "b", type = JAXBElement.class)
   })
   protected List<JAXBElement<SomeType>> aOrB;
   @XmlElements({
       @XmlElement(name = "a", type = String.class)
       @XmlElement(name = "b", type = Integer.class),
   })
   protected List<Serializable> aOrB;

These complex properties are required to model complex content of the
XML schema adequately, i.e. to maintain the order of the elements in
the repeatable choice.
Unfortunately, they are not idiomatic as bean properties. These
properties are "heterogeneous" (in a sense that they store different
types), which makes it hard to work with them.

However, if the order of the elements is not significant - that is,
you can live with the fact that it will change afte re-marshalling,
the structures of these properties can be simplified: complex
properties can be split into several simple properties.

The Simplify Plugin implements this task. It allows you to simplify
your complex properties. The plugin will remove the complex property
and insert several simpler properties instead of the original
(complex) property.

So you can get something like:

@XmlElement(name = "a", type = String.class)
protected List<String> a;
@XmlElement(name = "b", type = Integer.class)
protected List<Integer> b;
Or:

@XmlElement(name = "a")
protected List<SomeType> a;
@XmlElement(name = "b")
protected List<SomeType> b;
Or:

@XmlElementRef(name = "a", type = JAXBElement.class)
protected List<JAXBElement<SomeType>> a;
@XmlElementRef(name = "b", type = JAXBElement.class)
protected List<JAXBElement<SomeType>> b;

Depending on the customization.

Bye,
/lexi