Skip to main content

@XmlEnum with complexType/simpleContent

5 replies [Last post]
ratoo
Offline
Joined: 2007-08-27
Points: 0

Hello!

Could somebody help me with the issue?
It seems that there is no way to do it in JAXB :-(

My schema looks like:

</p>
<p>

But JAXB 2.1.9 just ignores the enumeration.

But when I try starting from JAVA:

<br />
package test.jaxb;</p>
<p>import javax.xml.bind.annotation.XmlValue;<br />
import javax.xml.bind.annotation.XmlAttribute;</p>
<p>public class MyInteger<br />
{<br />
    private String id;<br />
    private int value;</p>
<p>    public MyInteger()<br />
    {<br />
    }</p>
<p>    public MyInteger(int value)<br />
    {<br />
        this.value = value;<br />
    }</p>
<p>    public MyInteger(String value)<br />
    {<br />
        this.value = Integer.parseInt(value);<br />
    }</p>
<p>    @XmlAttribute(name="id")<br />
    public String getId()<br />
    {<br />
        return id == null ? "" : id;<br />
    }</p>
<p>    public void setId(String id)<br />
    {<br />
        this.id = id;<br />
    }</p>
<p>    @XmlValue<br />
    public void setValue(int value)<br />
    {<br />
        this.value = value;<br />
    }</p>
<p>    public int getValue()<br />
    {<br />
        return value;<br />
    }</p>
<p>    @Override<br />
    public String toString()<br />
    {<br />
        return Integer.toString(value);<br />
    }<br />
}<br />

<br />
package test.jaxb;</p>
<p>import javax.xml.bind.annotation.XmlEnum;<br />
import javax.xml.bind.annotation.XmlEnumValue;<br />
import javax.xml.bind.annotation.XmlType;</p>
<p>@XmlType<br />
@XmlEnum(MyInteger.class)</p>
<p>public enum MyEnum<br />
{<br />
    @XmlEnumValue("0") VALUE0,<br />
    @XmlEnumValue("1") VALUE1<br />
}<br />

IT IS OK.
(generated schema)

<br />
<?xml version="1.0" encoding="UTF-8" standalone="yes"?></p>
<p>

Hm... Sorry for stupid questions...

Message was edited by: ratoo

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kristoff
Offline
Joined: 2004-06-04
Points: 0

Read the JAXB spec, appendix D.3.

The problem is, that XJC should explain why it skips the enums that have invalid java identifiers, but it silently goes on it's way.

ratoo
Offline
Joined: 2007-08-27
Points: 0

Hello Kristoff,

Thank you for comment, but I am afraid that the problem is that JAXB doesn't understand enumerations in [b][/b] (even it has a [b][/b])

You can see that when starting from Java it generates [b][/b] that derive from [b][/b].
The funny thing is that it can't later compile it...

To check your theory I changed xs:int to xs:string and enumerations' values to "one" and "two" with the same result.

Another strange thing:

[code]









[/code]

doesn't generate MyEnum, but only ObjectFactory...

I got crazy... Is it possible to have enumerations with custom classes at all?

Cheers,

D.

...

After spending one day more I understand that JAXB doesn't handle [b]enumerations[/b] inside a [b]simpleContext[/b].

When I use schemagen it generates schema, but always makes the Enum a simpleType (restricting a complex one).
JBoss directly rejects my classes and the code that does so is a JAXB one:

[code]
[b]search:[/b]
for (RuntimeClassInfoImpl ci = this; ci != null; ci = ci.getBaseClass()) {
for( RuntimePropertyInfo pi : ci.getProperties() )
if(pi.kind()==PropertyKind.VALUE) {
valuep = (RuntimeValuePropertyInfo)pi;
[b]break search;[/b]
} else [b]if(pi.kind()!=PropertyKind.ATTRIBUTE)[/b] {
// this bean has something other than a value
return null;
}
}
if(valuep==null)
return null;
[b]// if( !valuep.getTarget().isSimpleType() )
// return null; // if there's an error, [/b]recover from it by returning null.

[/code]

After these modifications, I was able to deploy my Service on JBoss, but cannot compile WSDL back:

[code]
[ERROR] cos-st-restricts.1.1: The type 'myEnum' is atomic, so its {base type def
inition}, 'myInteger', must be an atomic simple type definition or a built-in pr
imitive datatype.
[/code]

Should I ask for a new feature?

Thank you very much,

D.

Message was edited by: ratoo

kristoff
Offline
Joined: 2004-06-04
Points: 0

Oh, sorry, I had to re-read your question.

For XJC to properly map a restriction to a java enum, it is required to be an atomic simple type--those which can not be further decomposed.
Since your type, MyInteger has an attribute, it isn't considered an atomic simple type.

And, to your followup XSD snippet, the reason it doesn't generate MyEnum is exactly why I told you in the prior post. It skips it because 0 and 1 are not legal java identifiers for a java enum's members.

I can tell you the following XSD:
[pre]








[/pre]
generates a class that looks like this:
[pre]
@XmlType(name = "MyEnum")
@XmlEnum
public enum MyEnum {

@XmlEnumValue("Zero")
ZERO("Zero"),
@XmlEnumValue("One")
ONE("One");
private final String value;

MyEnum(String v) {
value = v;
}

public String value() {
return value;
}

public static MyEnum fromValue(String v) {
for (MyEnum c: MyEnum.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}

}
[/pre]

Message was edited by: kristoff

ratoo
Offline
Joined: 2007-08-27
Points: 0

Hello,

Thank you for the answer, but I think I don't understand XML spec. well :-(

There question is "can restriction base be a complexType (with a simpleContext) in XML Schema?" Because w3c validator validates such a schema:

[code]
















[/code]

JAXB generates "empty" myEnum (I supposed to have [b]@XmlEnum(MyInteger.class)[/b])

I think this is a JAXB limitation...

Anyway I just replaced MyInteger with xs:int (loosing an attribute)...

Thank you very much.

D.

kristoff
Offline
Joined: 2004-06-04
Points: 0

I clarified my prior response...

I think the problem is that XJC can't intuit how attributes are expected to behave in the generated enum.