JAXB bindings - set typesafeEnumBase to empty (con

2019-07-15 08:07发布

问题:

According to the link

If typesafeEnumBase is set to xsd:string, it would be a global way to specify that all simple type definitions deriving directly or indirectly from xsd:string and having enumeration facets should be bound by default to a typesafe enum. If typesafeEnumBase is set to an empty string, "", no simple type definitions would ever be bound to a typesafe enum class by default. The value of typesafeEnumBase can be any atomic simple type definition except xsd:boolean and both binary types.

So I have set to my binding.xjb the following:

<jxb:globalBindings typesafeEnumBase=""/>

and when running the jaxb2-maven-plugin I get the following exception:

lineNumber: 5; columnNumber: 46; cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_typesafeEnumBaseglobalBindings'.
...
lineNumber: 5; columnNumber: 46; cvc-attribute.3: The value '' of attribute 'typesafeEnumBase' on element 'jxb:globalBindings' is not valid with respect to its type, 'null'.

As far as I understand I cannot set the empty string "" to typesafeEnumBase even though the documentation says so. Documentation also mentions that it cannot be xsd:boolean.

All I want is to convert the following to String instead of enum

<xs:simpleType name="phraseID">
    <xs:restriction base="escapedStringUserType">
        <xs:enumeration value="NOT_SPECIFIED"/>
        <xs:enumeration value="X000-9999"/>
        <xs:enumeration value="X000-9998"/>
    </xs:restriction>
</xs:simpleType>

A relevant SO question is here but since I cannot set the empty string "" or set the xsd:boolean value to typesafeEnumBase none of the answers work for me (tried both).

回答1:

Setting the following

<jxb:globalBindings typesafeEnumMaxMembers="0"/>

will not generate any enums for all simple types with enumeration restrictions instead will convert them to strings, however it spits out warnings in the console like the following:

Simple type "xxx-address" was not mapped to Enum due to EnumMemberSizeCap limit. Facets count: 10, current limit: 0. You can use customization attribute "typesafeEnumMaxMembers" to extend the limit.


回答2:

You can specify for a specific simple type with enumeration restrictions that you do not want it to be mapped to a Java enum like in this example:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
    <jaxb:bindings
            schemaLocation="myschema.xsd" 
            node="/xs:schema/xs:simpleType[@name='phraseID']">
        <jaxb:typesafeEnumClass map="false"/>
    </jaxb:bindings>
</jaxb:bindings>

But as far as I know, there is no way to set this globally for all simple types with enum restrictions (you cannot use <jaxb:typesafeEnumClass map="false"/> directly inside a <jaxb:globalBindings>, for example).

(If someone knows how to do this, I'd like to know as well, so please comment or answer).