Enumeration

If a type is restricted using an enumeration, an enum class is generated.

Example XSD:

   <xsd:element name="IntegerElement">
      <xsd:simpleType>
         <xsd:restriction base="xsd:integer">
            <xsd:enumeration value="-2" />
            <xsd:enumeration value="2" />
            <xsd:enumeration value="4" />
            <xsd:enumeration value="6" />
         </xsd:restriction>
      </xsd:simpleType>
   </xsd:element>

Beginning of resulting Java class:

public enum IntegerElement_ELEM
{
   x_2("-2"),
   x2("2"),
   x4("4"),
   x6("6");
   ...

Beginning of resulting C# class:

public class IntegerElement_ELEM
{
   public static IntegerElement_ELEM x_2 = new IntegerElement_ELEM("-2");
   public static IntegerElement_ELEM x2 = new IntegerElement_ELEM("2");
   public static IntegerElement_ELEM x4 = new IntegerElement_ELEM("4");
   public static IntegerElement_ELEM x6 = new IntegerElement_ELEM("6");
   ...

Common code:

      //fields for lexical and actual value
      private String lexicalValue;
      private int actualValue;
   
      //constructor
      private IntegerElement_ELEM(String lexicalValue) {...}
   
      public int valueOf() {...}
   
      public static IntegerElement_ELEM validate(int value) {...}
   
      public String toString() {...}

      public static String encode(IntegerElement_ELEM value, XBContext xbContext)
      {...}

      public static IntegerElement_ELEM decode(String text, XBContext xbContext) 
      {...}
   }

Features to note are:

  1. Each instance of the enum class contains both its lexical and actual value. This means no formatting is required when encoding, and no parsing is necessary when asking for the actual value.

  2. The valueOf method returns the actual value (e.g. the int value represented by the enum).

  3. The validate method returns the enum constant that corresponds to the given actual value.

  4. The toString method returns the lexical representation.

  5. The encode/decode methods are invoked by other generated code, wherever the restricted type is used.

The naming for the enum constants follows the following rules:

  1. We begin with the enumeration value as the name.

  2. If an enumeration identifier consists of whitespace only (for example, enumeration value=""), the special name BLANK is used.

  3. Other special names are used for other single punctuation mark identifiers (for example, '+' = PLUS).

  4. If, after applying these rules, the name still has a non-alphabetic start character, the character 'x' is prepended. (Thus, x2 for "2" in the example above).

  5. All invalid C#/Java identifier characters are replaced with underscores (_) within the name. (Thus, x_2 for "-2" in the example above).