Paperback Writer
Paperback Writer

Reputation: 2125

Make only enumeration of restriction default value in jaxb

I have something like this in xsd (which I cannot edit):

<xsd:simpleType name="Foo">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="BAR"/>
    </xsd:restriction>
</xsd:simpleType>

JAXB generates this java code in class representing element, which has Foo as child:

    public Foo getValue() {
        return value;
    }

    public void setValue(Foo value) {
        this.value = value;
    }

But there is only one possible value of Foo, so I would prefer to see something like this in my java code:

public Foo getValue() {
    if( value == null )
    {
        return Foo.BAR; //Foo already is defined as enum, so this code is fine
    }
    return value;
}

Is there some easy way to make this happen in JAXB?

Upvotes: 1

Views: 843

Answers (1)

willome
willome

Reputation: 3132

to sum up, you are expecting a default value for Foo ? if so try to add a default value on the Foo type element instead.

see http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints for default value

Upvotes: 1

Related Questions