I personally think unions are an especially ugly kludge in schema, but understand that since schema defines them people need to work with them. The best way I can suggest for handling them is by creating a class that has members matching the components of the union along with some form of mode field to say which type of component is used by this particular instance, then use a custom serializer/deserializer for converting to and from instances of this class. This gets ugly - the deserializer code first has to look at the data provided to determine the actual deserializer to be used, and then call that type-specific deserializer and store the result in the appropriate member field, along with setting the mode field value to match.

So for this very simple union:

<simpleType name="volumeControl">
 <union>
   <simpleType>
     <restriction base="int">
       <minInclusive value="0"/>
       <maxInclusive value="10"/>
     </restriction>
   </simpleType>
   <simpleType>
     <restriction base="token">
       <enumeration value="quiet"/>
       <enumeration value="loud"/>
       <enumeration value="really loud"/>
     </restriction>
   </simpleType>
 </union>
</simpleType>

you'd need a structure like:

public class VolumeControlType
{
private int m_type; // 0 for int, 1 for token
private int m_intValue;
private String m_tokenValue;
public static VolumeControlType deserialize(String value) {
if (value.size() == 0 || !Character.isDigit(value.charAt(0))) {
m_type = 1;
m_tokenValue = value; // except really should check the actual values
} else {
m_type = 0;
m_intValue = ...deserializerInt(value);
}
}
// plus getter/setters, etc.
}


Generating this would be a mess, especially if you try to do it with the existing Xsd2Jibx code. My current plans with Xsd2Jibx are to release the current CVS version as beta 1 along with JiBX RC0 next week, then ignore it. It'd be great if someone could come up with a better alternative; I've discussed some options with Cameron, the original author of the current code, and have also suggested on this list the possibility of taking Castor's code generation and modifying it to leave out the Castor-specific stuff while also generating JiBX bindings.

 - Dennis

Dave Lucek wrote:

Hi,

Does anyone know where in the Xsd2Jibx source code where I should look in order to add support for

XML unions?  Or, how can I get this tool to work with XML unions?



Thanks











-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to