users@jaxb.java.net

Enumeration with int values from schema

From: Pierre Buyle <pierre.buyle_at_irislink.com>
Date: Wed, 2 Jan 2008 08:13:35 -0800 (PST)

Hi,

I'm generating typesafe Enum from an xsd:restriction with xsd:int as
baseType. It works when using the jxb:typesafeEnumClass and
jxb:typesafeEnumMember appinfo annotations. However, the value() method for
the generated enum does not return int (or Integer) values but String. This
is fine for most case but it prevents enumeration members to have any
meaningful values.

For instance, with the following schema
<xsd:simpleType>
  <xsd:annotation>
    <xsd:appinfo>
      <jxb:typesafeEnumClass name="priority">
        <jxb:typesafeEnumMember name="VERY_LOW" value="-2" />
        <jxb:typesafeEnumMember name="LOW" value="-1" />
        <jxb:typesafeEnumMember name="NORMAL" value="0" />
        <jxb:typesafeEnumMember name="HIGH" value="1" />
        <jxb:typesafeEnumMember name="VERY_HIGH" value="2" />
      </jxb:typesafeEnumClass>
    </xsd:appinfo>
  </xsd:annotation>
  <xsd:restriction base="xsd:int">
    <xsd:enumeration value="-2" />
    <xsd:enumeration value="-1" />
    <xsd:enumeration value="0" />
    <xsd:enumeration value="1" />
    <xsd:enumeration value="2" />
  </xsd:restriction>
</xsd:simpleType>

The following code is generated
    public enum Priority {
        @XmlEnumValue("-2")
        VERY_LOW("-2"),
        @XmlEnumValue("-1")
        LOW("-1"),
        @XmlEnumValue("0")
        NORMAL("0"),
        @XmlEnumValue("1")
        HIGH("1"),
        @XmlEnumValue("2")
        VERY_HIGH("2");
        private final String value;

        Priority(String v) {
            value = v;
        }

        public String value() {
            return value;
        }

        public static MTaskData.Priority fromValue(String v) {
            for (MTaskData.Priority c: MTaskData.Priority.values()) {
                if (c.value.equals(v)) {
                    return c;
                }
            }
            throw new IllegalArgumentException(v);
        }
    }

Is there a way to have the value() method to return instances of the Java
class for the xsd:restriction baseType ?
-- 
View this message in context: http://www.nabble.com/Enumeration-with-int-values-from-schema-tp14580931p14580931.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.