The Situation
I'm using MOXy's JAXB implementation to work with a large XML document whose schema has many similiar complex types. Specifically, there are about two dozen types that act as list wrapper elements with the following structure:
<ITEMS attr1="X" attr2="Y">
<ITEM>...</ITEM>
...
<EXTENSION/>
<ITEMS>
For each of these list-wrapper-like elements, the name changes and the list element being contained changes. However, the attributes (which are all optional) and the single EXTENSION element (also optional) are always present. Here's an example of uses of two of the types:
<ROLES visible="false">
<ROLE type="X"/>
<ROLE type="Y"/>
</ROLES>
<PAYMENTS visible="true">
<PAYMENT>
<PAYEENAME>Joe</PAYEENAME>
</PAYMENT>
<EXTENSION>
<SOMETHING>Here</SOMETHING>
</EXTENSION>
</PAYMENTS>
The Question
I would like to avoid code duplication since the only thing changing between these elements is the name and the one-or-more elements it contains. What is the best way to do this?
I can only see two possible solutions.
1
Creating a concrete class using generics to indicate the object type to be used in the collection that changes. Then using MOXy's external OX mappings to indicate how any individual use of the class should be serialized. Something like:
public class GenericContainer<T> {
@XmlAttribute
protected Boolean visibile;
@XmlElement(name = "Extension")
protected Extension extension;
// OX Mappings done in external meta file
protected List<T> items;
...
}
While I like this option, it doesn't appear to be