users@jaxb.java.net

Re: Accessing enumerations

From: Ryan Shoemaker - JavaSoft East <Ryan.Shoemaker_at_Sun.COM>
Date: Tue, 18 May 2004 12:35:59 -0400

Ryan Shoemaker - JavaSoft East wrote:

> Jon Richards wrote:
>
>> Hi everyone,
>>
>> In our schemas we quite often use enumerated restrictions with string
>> types. Is there any way of getting to them from the JAXB classes?
>>
>
>
> Unfortunately not. I filed an RFE a long time ago for this kind of
> functionality and the report contains some workarounds that might be
> better than hard coding. Have a look:
>
> http://developer.java.sun.com/developer/bugParade/bugs/4897223.html
>

I probably should have pointed out that the reflection technique requires
that you customize your schema to generate type safe enums. Something
like this:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
             jxb:version="1.0">

        <xsd:element name="StringEnum" type="StringEnumType"/>
        
        <xsd:simpleType name="StringEnumType">
                <xsd:annotation>
                        <xsd:appinfo>
                                <jxb:typesafeEnumClass/>
                        </xsd:appinfo>
                </xsd:annotation>
                <xsd:restriction base="xsd:string">
                        <xsd:enumeration value="Nosferatu"/>
                        <xsd:enumeration value="Godzilla"/>
                </xsd:restriction>
        </xsd:simpleType>
</xsd:schema>

JAXB doesn't generate the type safe enum by default, so you have to
explicitly tell jaxb to generate them. In this case, you would get

public class StringEnumType {

     public final static java.lang.String _NOSFERATU =
        com.sun.xml.bind.DatatypeConverterImpl.installHook("Nosferatu");
     public final static generated.StringEnumType NOSFERATU =
        new generated.StringEnumType(_NOSFERATU);

     public final static java.lang.String _GODZILLA =
        com.sun.xml.bind.DatatypeConverterImpl.installHook("Godzilla");
     public final static generated.StringEnumType GODZILLA =
        new generated.StringEnumType(_GODZILLA);

     ...
}

The enum contains the lexical content from the schema as well as the
generated value type.

Also, the code in the bug report should work with the inline-customize
sample app.

Hope that gives you a little more context.

--Ryan


> --Ryan
>
>> At the moment I'm hard coding static final strings in our code, but of
>> course if the schema changes I'll have to remember to change the code
>> and that doesn't seem right!
>>
>> Thanks in advance,
>>
>> Jon
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net