users@jaxb.java.net

Re: restriction attribute handling

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 28 Sep 2005 16:08:19 -0700

Johnny Tolliver wrote:
> Hi. I am a total newbie with JAXB. I hope someone here an help me with what
> seems like a simple problem.

Welcome!

> It seems that the restriction attribute isn't handled correctly in JAXB. Using
> the generated java classes, I can set the value to anything rather than just
> the restricted list. I found in the list archives an identical complaint from
> 2003 with a reply to change the base type to XSD:NCName.

Correct. There are some schema constraints that we just can't enforce
with every change, and many of datatype facets fall into this category.

Oh, and thank you for searching the archive before you post. We really
appreciate that.


> The XMLBeans approach is to force you to choose one of a subclass of
> org.apache.xmlbeans.StringEnumAbstractBase. This works but is somewhat uglier.

Sounds like your particular restriction is the <enumeration> facet, and
if so, you can manually customize the schema so that the type binds to a
type-safe enum. The following example is taken from the spec:

<xs:simpleType name="USState">
  <xs:annotation><xs:appinfo>
   <jaxb:typesafeEnumClass name="USStateAbbr"/>
  </xs:appinfo></xs:annotation>
  <xs:restrictionbase="xs:NCName">
   <xs:enumeration value="AK">
    <xs:annotation><xs:appinfo>
     <jaxb:typesafeEnumMember name="STATE_AK"/>
    </xs:appinfo></xs:annotation>
   </xs:enumeration>
   <xs:enumeration value="AL">
    <xs:annotation><xs:appinfo>
     <jaxb:typesafeEnumMember name="STATE_AL"/>
    </xs:appinfo></xs:annotation>
   </xs:enumeration>
  </xs:restriction>
</xs:simpleType>

You can also do this by using an external customization without changing
the original schema.


> Is there a modern (2005) solution to this problem? Thanks.

Admittedly it's not a very modern solution either.



-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com