Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB xjc: How to generate code for Strings that returns empty if the value is null?

Tags:

jaxb

Given the following example xsd snippet:

< xs:attribute name="SEGMENT"  default="" use="optional" type="xs:string"/ >

when xjc generates the class containing the SEGMENT bean attribute, the following getter is auto-generated:

public String getSEGMENT() {
    if (segment == null) {
        return "";
    } else {
        return segment;
    }
}

My question is how do you get it do the same for xs:element objects? In other words, given the following xsd snippet:

< xs:element name="NAME" default="" type="xs:string"/ >

I want to know if I can get xjc to generate the following:

public String getNAME() {
    if (name == null) {
        return "";
    } else {
        return name;
    }
}

How can this be done?

like image 273
java luva Avatar asked Dec 11 '12 17:12

java luva


1 Answers

JAXB doesn't generate the same code for an element with default value as it does for an attribute with default value because the XML schema differentiates between element and attribute defaults:

Default values of both attributes and elements are declared using the default attribute, although this attribute has a slightly different consequence in each case. When an attribute is declared with a default value, the value of the attribute is whatever value appears as the attribute's value in an instance document; if the attribute does not appear in the instance document, the schema processor provides the attribute with a value equal to that of the default attribute. Note that default values for attributes only make sense if the attributes themselves are optional, and so it is an error to specify both a default value and anything other than a value of optional for use.

The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element's content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.

You can always count on the default value for a missing attribute (from here the special getter) but there is a catch with a missing element value.

Nonetheless, when you unmarshall an instance, the unmarshaller knows how to handle the default value. See here for details:

  • Element default values and marshalling
  • Element default values and unmarshalling

XJC won't add the getter code or initialize the fields with the default value, so if you need the "null safe check" you can either add it yourself manually after the code is generated by XJC or try to use some plugin to do it automatically:

  • JAXB 2 Default Value Plugin
  • CXF XJC Default Value Plugin
like image 186
Bogdan Avatar answered Nov 05 '22 11:11

Bogdan