users@jaxb.java.net

Re: Question about primitive types API: determing if an element is set or not?

From: Joe Fialli <Joseph.Fialli_at_sun.com>
Date: Thu, 29 May 2003 16:36:06 -0400

 From JAXB 1.0 specification:

4.5.4 isSet Property Modifier

This optional modifier augments a modifiable property to enable the
manipulation of the property s value as a set value or a defaulted
value. Since
this functionality is above and beyond the typical JavaBean pattern for a
property, the method(s) associated with this modifier are not generated by
default. Chapter 6, Customization describes how to enable this
customization
using the generateIsSetMethod attribute.

The method signatures for the isSet property modifier are the following:
where Id is defined as it was for simple and collection property.

public boolean isSetId();

? The isSet method returns true if the property has been set during
unmarshalling or by invocation of the mutation method setId with a
non-null value. To aid the understanding of what isSet method implies,
note that the
unmarshalling process only unmarshals set values into XML content.

A list property and a simple property with a non-reference base type
require an
additional method to enable you to discard the set value for a property:

? The unset method marks the property as having no set value. A
subsequent call to getId method returns the schema-specified default
if it existed; otherwise, it returns the Java default initial value for
Type.

public void unsetId();

-Joe Fialli, Sun Microsystems
Han Ming Ong wrote:

> Hi all,
>
> Say I have a fragment of a Schema:
>
> <xs:element name="read-only" type="xs:boolean"/>
>
> <xs:element name="stats">
> <xs:complexType>
> <xs:sequence>
> <xs:element minOccurs="0" ref="read-only"/>
> <xs:element minOccurs="0" ref="read-time-out"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> From JAXB, I would get the following APIs for the StatsType class:
>
> ...
> boolean isReadOnly();
> void setReadOnly(boolean value);
> ...
>
> My problem is that my users want me to tell them if in the original
> XML document, the element <read-only> is actually present or not. For
> Java, isReadOnly() will return 'false' if not initialized, hence it
> becomes ambiguous if the actually value is 'false' or just plain
> missing.
>
> This problem occurs in all primitive types mapping. String is OK since
> 'null' is returned if element is missing.
>
> Any cool thoughts? Workaround (note: I prefer not to change the
> Schema) ?
>
> Thanks much, Han Ming