users@jaxb.java.net

Re: How do you pick up default values from XSD/JAXB

From: Joe Fialli <Joseph.Fialli_at_sun.com>
Date: Tue, 03 Jun 2003 09:43:38 -0400

Gary Gregory wrote:

>
> My XSD defines default values for a bunch of attributes.
>
> I have some code that reads our apps configuration files with JAXB. In
> the case where the config file is missing, I pass in a JAXB generated
> class that I instantiate with:
>
> FooBarType t = new ObjectFactory().createMyFooBar();
> t.setFoo(xxx);
> t.setBar(yyy);
>
> Obviously, this is lame and must be updated everytime I change the
> default values in the XSD. How can I pick up the values I defined in
> the XSD?
>
It just works if you use XSD default attribute.

Given the following schema fragment:
  <xsd:attribute name="defaultedValue" type="xsd:int" default="10"/>

Calling getDefaultedValue() returns "10" if the attribute was not specified
in the unmarshalled XML document.

To get methods to check if a property has a default value and to be able
to unset a properties value to get the default value, the following
customization generates "boolean isSetDefaultedValue()" and
"unsetDefaultedValue()"
at attribute level. (There is a way to enable these methods to be
generated in jxb:globalBindings
also)

  <xsd:attribute name="defaultedValue" type="xsd:int" default="10">
     <xsd:annotation><xsd:appinfo>
       <jxb:property generateIsSetMethod="true"/>
     </xsd:appinfo></xsd:annotation>
  </xsd:attribute>

The JAXB specification describes the "isSet property modifier" in detail.
These modifiers are not generated by default since JavaBeans do not
typically provide methods to check if a property has been set or not.

-Joe Fialli, Sun Microsystems

> Thanks,
> Gary
>