users@jaxb.java.net

Re: Need a schema representation of a multivalue attribute for JAXB

From: Daniel Einspanjer <daniel-java_at_YIPYIP.COM>
Date: Wed, 09 Jul 2003 09:43:06 -0600

I actually came up with a slightly different approach after studying the Schema and JAXB documentation a bit more. This post just documents what I did for posterities sake. :) I appreciate your input very much Kohsuke, and I will use the tutorial you gave me on custom parsing classes to great advantage in a couple of other areas in my project. :)

----------------------------------------------------------------------------
First, in my schema, I defined a simple type called editPermission:
 <xsd:simpleType name="editPermission">
  <xsd:annotation>
   <xsd:appinfo>
    <jaxb:typesafeEnumClass name="EditPermission"/>
   </xsd:appinfo>
  </xsd:annotation>
  <xsd:restriction base="xsd:NCName">
   <xsd:enumeration value="none">
    <xsd:annotation>
     <xsd:appinfo>
      <jaxb:typesafeEnumMember name="NONE"/>
     </xsd:appinfo>
    </xsd:annotation>
   </xsd:enumeration>
   <xsd:enumeration value="change">
    <xsd:annotation>
     <xsd:appinfo>
      <jaxb:typesafeEnumMember name="CHANGE"/>
     </xsd:appinfo>
    </xsd:annotation>
   </xsd:enumeration>
   <xsd:enumeration value="add">
    <xsd:annotation>
     <xsd:appinfo>
      <jaxb:typesafeEnumMember name="ADD"/>
     </xsd:appinfo>
    </xsd:annotation>
   </xsd:enumeration>
   <xsd:enumeration value="delete">
    <xsd:annotation>
     <xsd:appinfo>
      <jaxb:typesafeEnumMember name="DELETE"/>
     </xsd:appinfo>
    </xsd:annotation>
   </xsd:enumeration>
  </xsd:restriction>
 </xsd:simpleType>
----------------------------------------------------------------------------
Next, in the element that needed this, I declared a list attribute:
  <xsd:attribute name="editPermissions" use="required">
   <xsd:simpleType>
    <xsd:list itemType="editPermission"/>
   </xsd:simpleType>
  </xsd:attribute>
----------------------------------------------------------------------------
Finally, in my Java code, a call to getEditPermissions() returns a list that I can use .contains() on to see what permissions the element has (e.g. "if (foo.getEditPermissions().contains(EditPermission.NONE))) {}").

This works quite nicely. The only problem is, there is nothing in the schema to say that you can't mix the NONE permission with others such as ADD.

I think it might actually be able to be done by eliminating the NONE permission and making the attribute nillable, but I don't know exactly how nillable works as I haven't studied up on it yet. :)

So, hopefully, this will be useful to someone else out there wanting to do something similar.