users@jaxb.java.net

Re: Bind jaxb ENUM class to a java ENUM class

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Mon, 5 Apr 2010 19:01:15 +0200

You should use a schema type and annotation like this:

  <xs:simpleType name="Fruit">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>

  <xs:annotation>
    <xs:documentation>Backed using Fruit.java enum</xs:documentation>
    <xs:appinfo>
      <jaxb:globalBindings>
        <jaxb:javaType name="com.fruits.Fruit"
                       xmlType="Fruit"
                       parseMethod="com.fruits.Fruit.parseObst"
                       printMethod="com.fruits.Fruit.printObst"/>
      </jaxb:globalBindings>
    </xs:appinfo>
  </xs:annotation>


Then, your enum must be extended to contain static methods:

   public static Fruit parseObst( String str ){
       return valueOf( Fruit.class, str );
   }

   public static String printObst( Fruit f ){
       return f.toString();
   }

And, basically, that's all there is to it.

If you want to have "Apple" rather than "APPLE" in your XML, your can create
a Map in your enum class and use this in the parse methods, and print should
return the description property of your enum.

-W




On Mon, Apr 5, 2010 at 3:59 PM, RPierce <rpierce_at_empoweredbenefits.com>wrote:

>
> Suppose I have an existing java enum class (Fruit.java) - is there a way to
> define a jaxb class to be an instance of that java class? I'd like the jaxb
> class to be backed by the java enum, so that if I need to extend the enum I
> only have to modify the java class, not the java class AND the xsd.
>
>
>
> package com.fruits;
>
> public enum Fruit{
>
> APPLE("Apple"),
> PEAR("Pear"),
> BANANA("Banana");
>
> private String description;
>
> private DeductionType(String desc) {
> this.description = desc;
> }
>
> public String getDescription() {
> return description;
> }
> }
>
>
> ...
> <xs:simpleType name="Fruit">
> <xs:annotation>
> <xs:documentation>Backed using Fruit.java enum</xs:documentation>
> <!-- SOMETHING? -->
> </xs:annotation>
> <!-- Don't want to have to maintainrestriction, enumeration elements
> -->
> </xs:simpleType>
> ...
>
>
>
>
>
>
> --
> View this message in context:
> http://old.nabble.com/Bind-jaxb-ENUM-class-to-a-java-ENUM-class-tp28140264p28140264.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>