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:
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.
The valueOf method returns the actual value (e.g. the int value represented by the enum).
The validate method returns the enum constant that corresponds to the given actual value.
The toString method returns the lexical representation.
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:
We begin with the enumeration value as the name.
If an enumeration identifier consists of whitespace only (for example, enumeration value=""), the special name BLANK is used.
Other special names are used for other single punctuation mark identifiers (for example, '+' = PLUS).
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).
All invalid C#/Java identifier characters are replaced with underscores (_) within the name. (Thus, x_2 for "-2" in the example above).