users@jaxb.java.net

Re: attribute inheritance

From: Anders Hessellund <anders.hessellund_at_gmail.com>
Date: Mon, 1 Mar 2010 14:06:42 +0100

Thanks for the reply,

The inheritance model of XML Schema is clearly different from that of Java.
>

Yep


> There is no definite way this situation should be mapped; what xjc does is
> one way. You are asking for attribute a1 to be generated in the subclasses
> X1 and X2: do you mean field int a1 and both getters? Or just the getter so
> that the default can be accomodated?
>

Probably, the getter is sufficient. In our schemas, we often use an
XSD-pattern where an attribute is declared in a supertype and then
restricted to various fixed/default values in subtypes. A generated getter
in a subtype could mirror the XSD.


> I don't know whether the current implementation of handling eponymous
> attribute redefinitions in extensions/restrictions is due to a wilful
> decision, or whether it just "happened".
>

Fair enough, I just wondered if there was some fundamental reason at play.

Consider using the inject-code plugin which lets you add arbitrary code to
> any generated class.
>

Thanks for the tip.

-- Anders


>
> -W
>
>
>
> On Thu, Feb 25, 2010 at 1:04 PM, Anders Hessellund <
> anders.hessellund_at_gmail.com> wrote:
>
>> Hi everyone,
>>
>> I don't mean to be annoying but I never got an answer to the question
>> below about attribute inheritance. I am using version 2.1.12. Has support
>> for overriding default values been added since? And a related question: If I
>> declared an attribute in a supertype and then override it (restrict it) with
>> a fixed value in a subtype, why doesn't the generated code reflect this.
>>
>> If xjc does not support it, is there perhaps a plugin which does?
>>
>> Thanks,
>>
>> Anders
>>
>> On Wed, Sep 23, 2009 at 11:07 AM, Anders Hessellund <
>> anders.hessellund_at_gmail.com> wrote:
>>
>>> Hi,
>>>
>>> Suppose you have the following schema:
>>>
>>> <xs:complexType name="XB">
>>> <xs:attribute name="a1" type="xs:int"/>
>>> <xs:attribute name="a2" type="xs:int" default="2"/>
>>> </xs:complexType>
>>>
>>> <xs:complexType name="X1">
>>> <xs:complexContent>
>>> <xs:restriction base="XB">
>>> <xs:attribute name="a1" type="xs:int" default="11"/>
>>> </xs:restriction>
>>> </xs:complexContent>
>>> </xs:complexType>
>>>
>>> <xs:complexType name="X2">
>>> <xs:complexContent>
>>> <xs:restriction base="XB">
>>> <xs:attribute name="a1" type="xs:int" default="21"/>
>>> </xs:restriction>
>>> </xs:complexContent>
>>> </xs:complexType>
>>>
>>> You get the following generated code
>>>
>>> public class XB {
>>> @XmlAttribute
>>> protected Integer a1;
>>> @XmlAttribute
>>> protected Integer a2;
>>>
>>> public Integer getA1() {
>>> return a1;
>>> }
>>>
>>> ...
>>>
>>> public int getA2() {
>>> if (a2 == null) {
>>> return 2;
>>> } else {
>>> return a2;
>>> }
>>> }
>>> ...
>>> }
>>>
>>> public class X1
>>> extends XB
>>> {
>>> }
>>>
>>> public class X2
>>> extends XB
>>> {
>>> }
>>>
>>> Why are attributes not generated in the subclasses? Is this a bug? It
>>> seems like a fairly trivial thing to do (without knowing anything
>>> about the implementation).
>>>
>>> Cheers,
>>>
>>> Anders
>>>
>>> --
>>> Anders Hessellund
>>> www.itu.dk/people/hessellund/
>>>
>>
>>
>>
>> --
>> Anders Hessellund
>> www.itu.dk/people/hessellund/
>>
>
>


-- 
Anders Hessellund
www.itu.dk/people/hessellund/