XStream
  1. XStream
  2. XSTR-300

Support for aliasing Enums and Enum types

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.4.5
    • Component/s: None
    • Labels:
      None

      Description

      In my world of J2EE schemas all the enumerations are in camel case such as....

      <xsd:complexType name="trans-attributeType">
      <xsd:simpleContent>
      <xsd:restriction base="javaee:string">
      <xsd:enumeration value="NotSupported"/>
      <xsd:enumeration value="Supports"/>
      <xsd:enumeration value="Required"/>
      <xsd:enumeration value="RequiresNew"/>
      <xsd:enumeration value="Mandatory"/>
      <xsd:enumeration value="Never"/>
      </xsd:restriction>
      </xsd:simpleContent>
      </xsd:complexType>

      ...or are contain illegal java characters like...

      <xsd:complexType name="cmp-versionType">
      <xsd:simpleContent>
      <xsd:restriction base="javaee:string">
      <xsd:enumeration value="1.x"/>
      <xsd:enumeration value="2.x"/>
      </xsd:restriction>
      </xsd:simpleContent>
      </xsd:complexType>

      This patch allows me to alias them into Java standard enum names such as...

      xstream.aliasEnum(TxAttribute.NOT_SUPPORTED, "NotSupported");
      xstream.aliasEnum(TxAttribute.SUPPORTS, "Supports");
      xstream.aliasEnum(TxAttribute.REQUIRED, "Required");
      xstream.aliasEnum(TxAttribute.REQUIRES_NEW, "RequiresNew");
      xstream.aliasEnum(TxAttribute.MANDATORY, "Mandatory");
      xstream.aliasEnum(TxAttribute.NEVER, "Never");
      xstream.aliasEnum(TxType.BEAN, "Bean"):
      xstream.aliasEnum(TxType.CONTAINER, "Container"):
      xstream.aliasEnum(CmpVersion.CMP1, "1.x");
      xstream.aliasEnum(CmpVersion.CMP2, "2.x");

      ... or even better on an enum type basis using an EnumFormat...

      xstream.aliasEnumType(TxAttribute.class, new CamelcaseEnumFormat());
      xstream.aliasEnum(TxType.class, new CamelcaseEnumFormat()):
      xstream.aliasEnum(CmpVersion.CMP1, "1.x");
      xstream.aliasEnum(CmpVersion.CMP2, "2.x");

      ... or better yet, specifying what I want the default to be ...

      xstream.setDefaultEnumFormat(new CamelcaseEnumFormat());
      xstream.aliasEnum(CmpVersion.CMP1, "1.x");
      xstream.aliasEnum(CmpVersion.CMP2, "2.x");

      ... and you can alway implement your own EnumFormat if you just don't feel like maintaining direct aliases ...

      xstream.setDefaultEnumFormat(new CamelcaseEnumFormat());
      xstream.aliasEnumType(CmpVersion.class new MyDottedVersionNumerEnumFormat());

      The one caveat to all this is that the methods aliasEnum, aliasEnumType, setDefaultEnumFormat and the interface EnumFormat all require Java 5 and sneaking around that just won't result in a nice API. I can't imagine people in 1.5 being happy with loosely typed methods like aliasEnum(Object, Object) or people in 1.4 having to learn that those aren't methods they can call. So I have subclassed XStream as XStreamTiger and exposed the Enum aliasing support there. Clearly, this is not desired as great lengths have obviously been taken to support all JDK versions with just the one XStream class, but it may be the lesser of two evils; the other evil being providing api support for enums that is on par with other java-isms like classes, fields, and collections while simultaneously pretending they don't exist by abstracting them out of the main XStream API.

        People

        • Assignee:
          Jörg Schaible
          Reporter:
          David Blevins
        • Votes:
          0 Vote for this issue
          Watchers:
          2 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: