users@jaxb.java.net

generation of constants into code by JAXB

From: Mr. Beeba <mr.beeba_at_volny.cz>
Date: Wed, 27 Aug 2008 13:11:53 +0200

Hi all,

how do I make JAXB to generate enumerations and constants refered to in the schema as restrictions when generating classes - see the schema below:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="ontology.observer.aws.istcontract.sf.net"
            elementFormDefault="qualified" xmlns:ns0="ontology.sensor.aws.istcontract.sf.net" xmlns:tns="ontology.observer.aws.istcontract.sf.net">
    <xsd:element name="ActionPerformedSubscribe">
        <xsd:complexType>
            <xsd:choice>
                <xsd:element name="actionId" type="xsd:string"/>
                <xsd:element name="anyAction" type="xsd:string" fixed="ANY_ACTION"/>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

The ActionPerformedSubscribe may contain either <actionId> or <anyAction> and if it is <anyAction> its value should be "ANY_ACTION". But the developer is not provided with a constant in the class ActionPerformedSubscribe to fill in the proper value when instantiating the object and setting its properties, neither a default value (or anything) is generated for the property. I assume marshaller does not do it automatically either, as the schema does not need to be available and the annotations should be enough (though, they are quite poor - see the generated class in attachment). Basically, the developer needs to check the schema to know what the expected content should be which is little clumsy. How could I make JAXB to generate say a constant like

public final static String anyActionFixed="ANY_ACTION"

or better generate a setter like

public void setAnyActionFixed() {
    anyAction="ANY_FIXED";
}

so that the marshalled XML checked against the schema could not be invalid by means of setting improper values in Java representation?

Cheers,
Bebe